Events.pm

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