| 1 | package Module::Spy; | ||||||||||
| 2 | 3 | ▶ | use 5.008005; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Scalar::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | our $VERSION = "0.07"; | |||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use parent qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | our @EXPORT = qw(spy_on); | |||||||||
| 12 | |||||||||||
| 13 | 20 | ▶ | sub spy_on { | ||||||||
|
Subroutine
spy_on: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 14 | 20 | my ($stuff, $method) = @_; | |||||||||
| 15 | |||||||||||
| 16 | 20 | ▶ | if (Scalar::Util::blessed($stuff)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 6 | Module::Spy::Object->new($stuff, $method); | |||||||||
| 18 | } else { | ||||||||||
| 19 | 14 | Module::Spy::Class->new($stuff, $method); | |||||||||
| 20 | } | ||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | package Module::Spy::Base; | ||||||||||
| 24 | |||||||||||
| 25 | 46 | ▶ | sub stuff { shift->{stuff} } | ||||||||
|
Subroutine
stuff: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 34 | ▶ | sub method { shift->{method} } | ||||||||
|
Subroutine
method: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 5 | ▶ | sub calls_any { @{shift->{spy}->calls_all(@_)} > 0 } | ||||||||
|
Subroutine
calls_any: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 3 | ▶ | sub calls_count { 0+@{shift->{spy}->calls_all(@_)} } | ||||||||
|
Subroutine
calls_count: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 1 | ▶ | sub calls_all { shift->{spy}->calls_all(@_) } | ||||||||
|
Subroutine
calls_all: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | sub calls_most_recent { shift->{spy}->calls_all(@_)->[-1] } | ||||||||
|
Subroutine
calls_most_recent: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 2 | ▶ | sub calls_first { shift->{spy}->calls_all(@_)->[0] } | ||||||||
|
Subroutine
calls_first: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | ▶ | sub calls_reset { shift->{spy}->calls_reset(@_) } | ||||||||
|
Subroutine
calls_reset: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 9 | ▶ | sub called { | ||||||||
|
Subroutine
called: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 9 | my $self = shift; | |||||||||
| 37 | 9 | $self->{spy}->called; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 2 | ▶ | sub and_call_through { | ||||||||
|
Subroutine
and_call_through: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | my $self = shift; | |||||||||
| 42 | 2 | $self->{spy}->call_through; | |||||||||
| 43 | 2 | return $self; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 2 | ▶ | sub and_call_fake { | ||||||||
|
Subroutine
and_call_fake: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 2 | my ($self, $code) = @_; | |||||||||
| 48 | 2 | $self->{spy}->call_fake($code); | |||||||||
| 49 | 2 | return $self; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 2 | ▶ | sub and_returns { | ||||||||
|
Subroutine
and_returns: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | my $self = shift; | |||||||||
| 54 | 2 | $self->{spy}->returns(@_); | |||||||||
| 55 | 2 | return $self; | |||||||||
| 56 | } | ||||||||||
| 57 | 0 | ▶ | sub returns { shift->and_returns(@_) } | ||||||||
|
Subroutine
returns: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | |||||||||||
| 59 | package Module::Spy::Object; | ||||||||||
| 60 | 3 | our @ISA=('Module::Spy::Base'); | |||||||||
| 61 | |||||||||||
| 62 | 3 | my $SINGLETON_ID = 0; | |||||||||
| 63 | |||||||||||
| 64 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
| |||||||||||
| 65 | 6 | my $class = shift; | |||||||||
| 66 | 6 | my ($stuff, $method) = @_; | |||||||||
| 67 | |||||||||||
| 68 | 6 | my $self = bless { stuff => $stuff, method => $method }, $class; | |||||||||
| 69 | |||||||||||
| 70 | 6 | ▶ | my $orig = $self->stuff->can($self->method) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | or die "Missing $method"; | ||||||||||
| 72 | 6 | $self->{orig} = $orig; | |||||||||
| 73 | |||||||||||
| 74 | 6 | my $spy = Module::Spy::Sub->new($orig); | |||||||||
| 75 | 6 | $self->{spy} = $spy; | |||||||||
| 76 | |||||||||||
| 77 | 6 | $self->{orig_class} = ref($stuff); | |||||||||
| 78 | |||||||||||
| 79 | { | ||||||||||
| 80 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | |||||||||||
| 83 | 6 | $SINGLETON_ID++; | |||||||||
| 84 | 6 | my $klass = "Module::Spy::__ANON__::" . $SINGLETON_ID; | |||||||||
| 85 | 6 | $self->{id} = $SINGLETON_ID; | |||||||||
| 86 | 6 | $self->{anon_class} = $klass; | |||||||||
| 87 | 6 | $self->{isa} = do { \@{"${klass}::ISA"} }; | |||||||||
| 88 | 6 | unshift @{$self->{isa}}, ref($stuff); | |||||||||
| 89 | 6 | *{"${klass}::${method}"} = $spy; | |||||||||
| 90 | 6 | bless $stuff, $klass; # rebless | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 6 | return $self; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 6 | ▶ | sub get_stash { | ||||||||
|
Subroutine
get_stash: called CC=3 SCAR=11.0
| |||||||||||
| 97 | 6 | my $klass = shift; | |||||||||
| 98 | |||||||||||
| 99 | 6 | my $pack = *main::; | |||||||||
| 100 | 6 | foreach my $part (split /::/, $klass){ | |||||||||
| 101 | 18 | ▶ | return undef unless $pack = $pack->{$part . '::'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | } | ||||||||||
| 103 | 6 | return *{$pack}{HASH}; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 6 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||
| 107 | 6 | my $self = shift; | |||||||||
| 108 | |||||||||||
| 109 | # Restore the object's type. | ||||||||||
| 110 | 6 | ▶ | if (ref($self->stuff) eq $self->{anon_class}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 6 | bless $self->stuff, $self->{orig_class}; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 6 | @{$self->{isa}} = (); | |||||||||
| 115 | |||||||||||
| 116 | 6 | my $original_stash = get_stash("Module::Spy::__ANON__"); | |||||||||
| 117 | 6 | my $sclass_stashgv = delete $original_stash->{$self->{id} . '::'}; | |||||||||
| 118 | 6 | %{$sclass_stashgv} = (); | |||||||||
| 119 | |||||||||||
| 120 | 6 | undef $self->{spy}; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | package Module::Spy::Class; | ||||||||||
| 124 | 3 | our @ISA=('Module::Spy::Base'); | |||||||||
| 125 | |||||||||||
| 126 | 3 | ▶ | use Class::Load qw(load_class); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 127 | |||||||||||
| 128 | 14 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 129 | 14 | my $class = shift; | |||||||||
| 130 | 14 | my ($stuff, $method) = @_; | |||||||||
| 131 | 14 | load_class($stuff); # $stuff is a class in Module::Spy::Class | |||||||||
| 132 | |||||||||||
| 133 | 14 | my $self = bless { stuff => $stuff, method => $method }, $class; | |||||||||
| 134 | |||||||||||
| 135 | 14 | my $orig = $self->stuff->can($self->method); | |||||||||
| 136 | 14 | $self->{orig} = $orig; | |||||||||
| 137 | |||||||||||
| 138 | 14 | my $spy = Module::Spy::Sub->new($orig); | |||||||||
| 139 | 14 | $self->{spy} = $spy; | |||||||||
| 140 | |||||||||||
| 141 | { | ||||||||||
| 142 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 143 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 144 | 14 | *{$self->stuff . '::' . $self->method} = $spy; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 14 | return $self; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 14 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||
| 151 | 14 | my $self = shift; | |||||||||
| 152 | 14 | my $stuff = $self->{stuff}; | |||||||||
| 153 | 14 | my $method = $self->{method}; | |||||||||
| 154 | 14 | my $orig = $self->{orig}; | |||||||||
| 155 | |||||||||||
| 156 | 14 | ▶ | if (defined $orig) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 158 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 159 | 14 | *{"${stuff}::${method}"} = $orig; | |||||||||
| 160 | } else { | ||||||||||
| 161 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 162 | 0 | delete ${"${stuff}::"}{${method}}; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 14 | undef $self->{spy}; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | package Module::Spy::Sub; | ||||||||||
| 169 | 3 | ▶ | use Scalar::Util qw(refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 170 | |||||||||||
| 171 | # inside-out | ||||||||||
| 172 | 3 | our %COUNTER; | |||||||||
| 173 | 3 | our %RETURNS; | |||||||||
| 174 | 3 | our %CALL_THROUGH; | |||||||||
| 175 | 3 | our %CALL_FAKE; | |||||||||
| 176 | 3 | our %ARGS; | |||||||||
| 177 | |||||||||||
| 178 | 20 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 179 | 20 | my ($class, $orig) = @_; | |||||||||
| 180 | |||||||||||
| 181 | 20 | my $body; | |||||||||
| 182 | 20 | ▶ | my $code = sub { goto $body }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 183 | |||||||||||
| 184 | 20 | my $code_addr = refaddr($code); | |||||||||
| 185 | 19 | ▶ | $body = sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 186 | 19 | $COUNTER{$code_addr}++; | |||||||||
| 187 | 19 | push @{$ARGS{$code_addr}}, [@_]; | |||||||||
| 188 | |||||||||||
| 189 | 19 | ▶ | if (my $fake = $CALL_FAKE{$code_addr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 2 | goto $fake; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 17 | ▶ | if (exists $RETURNS{$code_addr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 2 | ▶ | if (@{$RETURNS{$code_addr}} == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | 1 | return $RETURNS{$code_addr}->[0]; | |||||||||
| 196 | } | ||||||||||
| 197 | 1 | return @{$RETURNS{$code_addr}}; | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | 15 | ▶ | if ($CALL_THROUGH{$code_addr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | 2 | goto $orig; | |||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | 13 | return; | |||||||||
| 205 | 20 | }; | |||||||||
| 206 | 20 | $COUNTER{$code_addr} = 0; | |||||||||
| 207 | 20 | $ARGS{$code_addr} = []; | |||||||||
| 208 | |||||||||||
| 209 | 20 | my $self = bless $code, $class; | |||||||||
| 210 | 20 | return $self; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | 20 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 214 | 20 | my $self = shift; | |||||||||
| 215 | 20 | my $code_addr = refaddr($self); | |||||||||
| 216 | |||||||||||
| 217 | 20 | delete $COUNTER{$code_addr}; | |||||||||
| 218 | 20 | delete $RETURNS{$code_addr}; | |||||||||
| 219 | 20 | delete $CALL_FAKE{$code_addr}; | |||||||||
| 220 | 20 | delete $CALL_THROUGH{$code_addr}; | |||||||||
| 221 | 20 | delete $ARGS{$code_addr}; | |||||||||
| 222 | } | ||||||||||
| 223 | |||||||||||
| 224 | 9 | ▶ | sub called { | ||||||||
|
Subroutine
called: called CC=1 SCAR=0.0
| |||||||||||
| 225 | 9 | my $self = shift; | |||||||||
| 226 | 9 | !!$COUNTER{refaddr($self)}; | |||||||||
| 227 | } | ||||||||||
| 228 | |||||||||||
| 229 | 12 | ▶ | sub calls_all { | ||||||||
|
Subroutine
calls_all: called CC=1 SCAR=0.0
| |||||||||||
| 230 | 12 | my $self = shift; | |||||||||
| 231 | 12 | $ARGS{refaddr($self)}; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | 1 | ▶ | sub calls_reset { | ||||||||
|
Subroutine
calls_reset: called CC=1 SCAR=0.0
| |||||||||||
| 235 | 1 | my $self = shift; | |||||||||
| 236 | 1 | $COUNTER{refaddr($self)} = 0; | |||||||||
| 237 | 1 | $ARGS{refaddr($self)} = []; | |||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | 2 | ▶ | sub returns { | ||||||||
|
Subroutine
returns: called CC=1 SCAR=0.0
| |||||||||||
| 241 | 2 | my $self = shift; | |||||||||
| 242 | 2 | $RETURNS{refaddr($self)} = [@_]; | |||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | 2 | ▶ | sub call_through { | ||||||||
|
Subroutine
call_through: called CC=1 SCAR=0.0
| |||||||||||
| 246 | 2 | my $self = shift; | |||||||||
| 247 | 2 | $CALL_THROUGH{refaddr($self)}++; | |||||||||
| 248 | } | ||||||||||
| 249 | |||||||||||
| 250 | 2 | ▶ | sub call_fake { | ||||||||
|
Subroutine
call_fake: called CC=1 SCAR=0.0
| |||||||||||
| 251 | 2 | my ($self, $code) = @_; | |||||||||
| 252 | 2 | $CALL_FAKE{refaddr($self)} = $code; | |||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | 3 | 1; | |||||||||
| 256 | __END__ | ||||||||||