| 1 | package JIP::Spy::Events; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use version 0.77; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use JIP::Spy::Event; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | our $VERSION = version->declare('v0.0.2'); | |||||||||||||||||||
| 14 | 1 | our $AUTOLOAD; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||
| 17 | 7 | ▶ | my ( $class, %param ) = @ARG; | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 7 | my $state = { | |||||||||||||||||||
| 20 | on_spy_event => {}, | ||||||||||||||||||||
| 21 | events => [], | ||||||||||||||||||||
| 22 | times => {}, | ||||||||||||||||||||
| 23 | skip_methods => {}, | ||||||||||||||||||||
| 24 | want_array => 0, | ||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 7 | ▶ | if ( $param{want_array} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 1 | $state->{want_array} = 1; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 7 | ▶ | if ( my $skip_methods = $param{skip_methods} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 1 | foreach my $method_name ( @{$skip_methods} ) { | |||||||||||||||||||
| 33 | 1 | $state->{skip_methods}->{$method_name} = undef; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 7 | return bless $state, $class; | |||||||||||||||||||
| 38 | } ## end sub new | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub events { | ||||||||||||||||||||
| 41 | 25 | ▶ | my ($self) = @ARG; | ||||||||||||||||||
|
Subroutine
events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 25 | return $self->{events}; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | sub times { | ||||||||||||||||||||
| 47 | 36 | ▶ | my ($self) = @ARG; | ||||||||||||||||||
|
Subroutine
times: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 36 | return $self->{times}; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub want_array { | ||||||||||||||||||||
| 53 | 13 | ▶ | my ($self) = @ARG; | ||||||||||||||||||
|
Subroutine
want_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 13 | return $self->{want_array}; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | sub skip_methods { | ||||||||||||||||||||
| 59 | 13 | ▶ | my ($self) = @ARG; | ||||||||||||||||||
|
Subroutine
skip_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 13 | return $self->{skip_methods}; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | sub clear { | ||||||||||||||||||||
| 65 | 2 | ▶ | my ($self) = @ARG; | ||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | @{ $self->events() } = (); | |||||||||||||||||||
| 68 | 2 | %{ $self->times() } = (); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 2 | return $self; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub on_spy_event { | ||||||||||||||||||||
| 74 | 14 | ▶ | my ( $self, %declarations ) = @ARG; | ||||||||||||||||||
|
Subroutine
on_spy_event: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 14 | ▶ | if (%declarations) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 2 | $self->{on_spy_event} = \%declarations; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 14 | return $self->{on_spy_event}; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | sub AUTOLOAD { | ||||||||||||||||||||
| 84 | 12 | ▶ | my ( $self, @arguments ) = @ARG; | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 12 | ▶ | if ( !blessed $self ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | croak q{Can't call "AUTOLOAD" as a class method}; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 11 | my ($method_name) = $AUTOLOAD =~ m{^ .+ :: ([^:]+) $}x; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 11 | undef $AUTOLOAD; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 11 | return $self->_handle_event( | |||||||||||||||||||
| 95 | method_name => $method_name, | ||||||||||||||||||||
| 96 | arguments => \@arguments, | ||||||||||||||||||||
| 97 | want_array => wantarray, | ||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | sub isa { | ||||||||||||||||||||
| 102 | 1 | ▶ | no warnings 'misc'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 103 | 1 | ▶ | goto &UNIVERSAL::isa; | ||||||||||||||||||
|
Subroutine
isa: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | sub can { | ||||||||||||||||||||
| 107 | 9 | ▶ | my ( $self, $method_name ) = @ARG; | ||||||||||||||||||
|
Subroutine
can: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 9 | ▶ | if ( blessed $self ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 1 | ▶ | no warnings 'misc'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | 9 | goto &UNIVERSAL::can; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||
| 114 | 0 | my $code; | |||||||||||||||||||
| 115 | 1 | ▶ | no warnings 'misc'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 116 | 0 | $code = UNIVERSAL::can( $self, $method_name ); | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | return $code; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | sub DOES { | ||||||||||||||||||||
| 123 | # DOES is equivalent to isa by default | ||||||||||||||||||||
| 124 | 0 | ▶ | goto &isa; | ||||||||||||||||||
|
Subroutine
DOES: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | sub VERSION { | ||||||||||||||||||||
| 128 | 1 | ▶ | no warnings 'misc'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 0 | ▶ | goto &UNIVERSAL::VERSION; | ||||||||||||||||||
|
Subroutine
VERSION: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | ▶ | sub DESTROY { } | |||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | sub _handle_event { | ||||||||||||||||||||
| 135 | 12 | ▶ | my ( $self, %param ) = @ARG; | ||||||||||||||||||
|
Subroutine
_handle_event: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 12 | ▶ | return $self if exists $self->skip_methods()->{ $param{method_name} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | { | ||||||||||||||||||||
| 140 | 11 | my %event = ( | |||||||||||||||||||
| 141 | method => $param{method_name}, | ||||||||||||||||||||
| 142 | arguments => $param{arguments}, | ||||||||||||||||||||
| 143 | 11 | ); | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 11 | ▶ | if ( $self->want_array() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 3 | $event{want_array} = $param{want_array}; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 11 | push @{ $self->events() }, \%event; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 11 | ▶ | my $times = $self->times()->{ $param{method_name} } // 0; | ||||||||||||||||||
|
Condition100%2/2
$self->times->{$param{"method_name"}} // 0
MC/DC100%1/1
$self->times->{$param{"method_name"}} // 0
$self->times->{$param{"method_name"}}
Truth table100%2/2
A
A= $self->times->{$param{"method_name"}}
| |||||||||||||||||||||
| 153 | 11 | $times += 1; | |||||||||||||||||||
| 154 | 11 | $self->times()->{ $param{method_name} } = $times; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 11 | my $on_spy_event = $self->on_spy_event()->{ $param{method_name} }; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 11 | ▶ | return $self if !$on_spy_event; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 2 | ▶ | if ( ( reftype($on_spy_event) || q{} ) ne 'CODE' ) { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($on_spy_event) || ''
MC/DC100%1/1
builtin::reftype($on_spy_event) || ''
builtin::reftype($on_spy_event)
Truth table100%2/2
A
A= builtin::reftype($on_spy_event)
| |||||||||||||||||||||
| 161 | croak sprintf( | ||||||||||||||||||||
| 162 | q{"%s" is not a callback}, | ||||||||||||||||||||
| 163 | $param{method_name}, | ||||||||||||||||||||
| 164 | 1 | ); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | my $event = JIP::Spy::Event->new( | ||||||||||||||||||||
| 168 | method => $param{method_name}, | ||||||||||||||||||||
| 169 | arguments => $param{arguments}, | ||||||||||||||||||||
| 170 | want_array => $param{want_array}, | ||||||||||||||||||||
| 171 | 1 | times => $times, | |||||||||||||||||||
| 172 | ); | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 1 | return $on_spy_event->( $self, $event ); | |||||||||||||||||||
| 175 | } ## end sub _handle_event | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 1 | 1; | |||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||