blib/lib/Test2/Mock.pm

filter: Statementstmt 99.2% 276 / 278 Branchbran 91.3% 95 / 104 Conditioncond 82.1% 60 / 73 MC/DCmcdc 58.9% 23 / 39 Subroutinesub 100% 49 / 49 Podpod 100% 22 / 22 totaltotal 92.9% 525 / 565 CC 85 SCAR 19.9
CC
85
Cov
95%
_parse_inject
18.5
_inject
15.0
new
12.0
CRAP 7.3
« blib/lib/Test2/Workflow/Task/Group.pm ↑ Summary blib/lib/Test2/Plugin/SRand.pm »
1 package Test2::Mock;
2 163 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 163 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 163 our $VERSION = '1.302226';
6  
7 163 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 163 our @CARP_NOT = (__PACKAGE__);
9  
10 163 ▶use Scalar::Util qw/weaken reftype blessed set_prototype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 163 ▶use Test2::Util qw/pkg_to_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 163 ▶use Test2::Util::Stash qw/parse_symbol slot_to_sig get_symbol get_stash purge_symbol/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 163 ▶use Test2::Util::Sub qw/gen_accessor gen_reader gen_writer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new; # Prevent hashbase from giving us 'new';
16 163 ▶use Test2::Util::HashBase qw/class parent child _purge_on_destroy _blocked_load _symbols _track sub_tracking call_tracking/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 70 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
19 70 my $class = shift;
20  
21 70 ▶ croak "Called new() on a blessed instance, did you mean to call \$control->class->new()?"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($class) 1 69 70
22 if blessed($class);
23  
24 69 my $self = bless({}, $class);
25  
26 69 ▶ $self->{+SUB_TRACKING} ||= {};
Condition50%1/2
$self->{"sub_tracking"} ||= {}
l!l
069
MC/DC0%0/1
$self->{"sub_tracking"} ||= {}
$self->{"sub_tracking"}
Truth table50%1/2
A
A= $self->{"sub_tracking"}
Aresult
11
00
27 69 ▶ $self->{+CALL_TRACKING} ||= [];
Condition50%1/2
$self->{"call_tracking"} ||= []
l!l
069
MC/DC0%0/1
$self->{"call_tracking"} ||= []
$self->{"call_tracking"}
Truth table50%1/2
A
A= $self->{"call_tracking"}
Aresult
11
00
28  
29 69 my @sets;
30 69 ▶ while (my $arg = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @_) 131 69 200
31 131 my $val = shift @_;
32  
33 131 ▶ if ($class->can(uc($arg))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can(uc $arg)) 70 61 131
34 70 $self->{$arg} = $val;
35 70 next;
36 }
37  
38 61 push @sets => [$arg, $val];
39 }
40  
41 croak "The 'class' field is required"
42 69 ▶ unless $self->{+CLASS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"class"} 1 68 69
43  
44 68 for my $set (@sets) {
45 61 my ($meth, $val) = @$set;
46 61 my $type = reftype($val);
47  
48 61 ▶ confess "'$meth' is not a valid constructor argument for $class"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 1 60 61
49 unless $self->can($meth);
50  
51 60 ▶ if (!$type) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 22 38 60
elsif ($type eq 'HASH') { } 1 37 38
elsif ($type eq 'ARRAY') { } 36 1 37
52 22 $self->$meth($val);
53 }
54 elsif($type eq 'HASH') {
55 1 $self->$meth(%$val);
56 }
57 elsif($type eq 'ARRAY') {
58 36 $self->$meth(@$val);
59 }
60 else {
61 1 croak "'$val' is not a valid argument for '$meth'"
62 }
63 }
64  
65 63 return $self;
66 }
67  
68 301 ▶sub _check {
Subroutine _check: called CC=2 SCAR=6.9
69 301 ▶ return unless $_[0]->{+CHILD};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"child"} 300 1 301
70 1 croak "There is an active child controller, cannot proceed";
71 }
72  
73 12 ▶sub purge_on_destroy {
Subroutine purge_on_destroy: called CC=2 SCAR=6.9
Pod documented
74 12 my $self = shift;
75 12 ▶ ($self->{+_PURGE_ON_DESTROY}) = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 3 12
76 12 return $self->{+_PURGE_ON_DESTROY};
77 }
78  
79 21 ▶sub stash {
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
80 21 my $self = shift;
81 21 get_stash($self->{+CLASS});
82 }
83  
84 20 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod documented
85 20 my $self = shift;
86 20 my $file = $self->class;
87 20 return pkg_to_file($self->class);
88 }
89  
90 9 ▶sub block_load {
Subroutine block_load: called CC=2 SCAR=6.9
Pod documented
91 9 my $self = shift;
92 9 $self->_check();
93  
94 9 my $file = $self->file;
95  
96 croak "Cannot block the loading of module '" . $self->class . "', already loaded in file $INC{$file}"
97 9 ▶ if $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 1 8 9
98  
99 8 $INC{$file} = __FILE__;
100  
101 8 $self->{+_BLOCKED_LOAD} = 1;
102 }
103  
104 my %NEW = (
105 6 ▶ hash => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 6 my ($class, %params) = @_;
107 6 return bless \%params, $class;
108 },
109 1 ▶ array => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 1 my ($class, @params) = @_;
111 1 return bless \@params, $class;
112 },
113 1 ▶ ref => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 1 my ($class, $params) = @_;
115 1 return bless $params, $class;
116 },
117 2 ▶ ref_copy => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
118 2 my ($class, $params) = @_;
119 2 my $type = reftype($params);
120  
121 2 ▶ return bless {%$params}, $class
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 1 1 2
122 if $type eq 'HASH';
123  
124 1 ▶ return bless [@$params], $class
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 1 0 1
125 if $type eq 'ARRAY';
126  
127 0 croak "Not sure how to construct an '$class' from '$params'";
128 },
129 163 );
130  
131 6 ▶sub override_constructor {
Subroutine override_constructor: called CC=2 SCAR=6.9
Pod documented
132 6 my $self = shift;
133 6 my ($name, $type) = @_;
134 6 $self->_check();
135  
136 6 ▶ my $sub = $NEW{$type}
Condition100%2/2
$NEW{$type} || croak("'${type}' is not a known constructor type")
l!l
42
MC/DC100%1/1
$NEW{$type} || croak("'${type}' is not a known constructor type")
$NEW{$type}
Truth table100%2/2
A
A= $NEW{$type}
Aresult
11
00
137 || croak "'$type' is not a known constructor type";
138  
139 4 $self->override($name => $sub);
140 }
141  
142 9 ▶sub add_constructor {
Subroutine add_constructor: called CC=2 SCAR=6.9
Pod documented
143 9 my $self = shift;
144 9 my ($name, $type) = @_;
145 9 $self->_check();
146  
147 9 ▶ my $sub = $NEW{$type}
Condition100%2/2
$NEW{$type} || croak("'${type}' is not a known constructor type")
l!l
72
MC/DC100%1/1
$NEW{$type} || croak("'${type}' is not a known constructor type")
$NEW{$type}
Truth table100%2/2
A
A= $NEW{$type}
Aresult
11
00
148 || croak "'$type' is not a known constructor type";
149  
150 7 $self->add($name => $sub);
151 }
152  
153 13 ▶sub autoload {
Subroutine autoload: called CC=7 SCAR=19.5
Pod documented
154 13 my $self = shift;
155 13 $self->_check();
156 13 my $class = $self->class;
157 13 my $stash = $self->stash;
158  
159 croak "Class '$class' already has an AUTOLOAD"
160 13 ▶ if $stash->{AUTOLOAD} && *{$stash->{AUTOLOAD}}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"AUTOLOAD"} and *{$stash->{"AUTOLOAD"};}{"CODE"} 2 11 13
Condition100%2/2
$stash->{"AUTOLOAD"} and *{$stash->{"AUTOLOAD"};}{"CODE"}
!ll
94
MC/DC100%1/1
$stash->{"AUTOLOAD"} and *{$stash->{"AUTOLOAD"};}{"CODE"}
$stash->{"AUTOLOAD"}
Truth table100%2/2
A
A= $stash->{"AUTOLOAD"}
Aresult
00
11
161 croak "Class '$class' already has an can"
162 11 ▶ if $stash->{can} && *{$stash->{can}}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"can"} and *{$stash->{"can"};}{"CODE"} 0 11 11
Condition100%2/2
$stash->{"can"} and *{$stash->{"can"};}{"CODE"}
!ll
92
MC/DC100%1/1
$stash->{"can"} and *{$stash->{"can"};}{"CODE"}
$stash->{"can"}
Truth table100%2/2
A
A= $stash->{"can"}
Aresult
00
11
163  
164 # Weaken this reference so that AUTOLOAD does not prevent its own
165 # destruction.
166 11 weaken(my $c = $self);
167  
168 11 my ($file, $line) = (__FILE__, __LINE__ + 3);
169 11 ▶ my $autoload = eval <<EOT || die "Failed generating AUTOLOAD sub: $@";
Condition100%2/2
eval qq[package $class;\n#line $line "$file (Generated AUTOLOAD)"\nour \$AUTOLOAD;\n sub {\n my (\$self) = \@_;\n my (\$pkg, \$name) = (\$AUTOLOAD =~ m/^(.*)::([^:]+)\$/g);\n \$AUTOLOAD = undef;\n\n return if \$name eq 'DESTROY';\n my \$sub = sub {\n my \$self = shift;\n (\$self->{\$name}) = \@_ if \@_;\n return \$self->{\$name};\n };\n\n \$c->add(\$name => \$sub);\n\n if (\$c->{_track}) {\n my \$call = {sub_name => \$name, sub_ref => \$sub, args => [\@_]};\n push \@{\$c->{sub_tracking}->{\$name}} => \$call;\n push \@{\$c->{call_tracking}} => \$call;\n }\n\n goto &\$sub;\n }\n] || die("Failed generating AUTOLOAD sub: $@")
l!l
101
MC/DC100%1/1
eval qq[package $class;\n#line $line "$file (Generated AUTOLOAD)"\nour \$AUTOLOAD;\n sub {\n my (\$self) = \@_;\n my (\$pkg, \$name) = (\$AUTOLOAD =~ m/^(.*)::([^:]+)\$/g);\n \$AUTOLOAD = undef;\n\n return if \$name eq 'DESTROY';\n my \$sub = sub {\n my \$self = shift;\n (\$self->{\$name}) = \@_ if \@_;\n return \$self->{\$name};\n };\n\n \$c->add(\$name => \$sub);\n\n if (\$c->{_track}) {\n my \$call = {sub_name => \$name, sub_ref => \$sub, args => [\@_]};\n push \@{\$c->{sub_tracking}->{\$name}} => \$call;\n push \@{\$c->{call_tracking}} => \$call;\n }\n\n goto &\$sub;\n }\n] || die("Failed generating AUTOLOAD sub: $@")
eval qq[package $class;\n#line $line "$file (Generated AUTOLOAD)"\nour \$AUTOLOAD;\n sub {\n my (\$self) = \@_;\n my (\$pkg, \$name) = (\$AUTOLOAD =~ m/^(.*)::([^:]+)\$/g);\n \$AUTOLOAD = undef;\n\n return if \$name eq 'DESTROY';\n my \$sub = sub {\n my \$self = shift;\n (\$self->{\$name}) = \@_ if \@_;\n return \$self->{\$name};\n };\n\n \$c->add(\$name => \$sub);\n\n if (\$c->{_track}) {\n my \$call = {sub_name => \$name, sub_ref => \$sub, args => [\@_]};\n push \@{\$c->{sub_tracking}->{\$name}} => \$call;\n push \@{\$c->{call_tracking}} => \$call;\n }\n\n goto &\$sub;\n }\n]
Truth table100%2/2
A
A= eval qq[package $class;\n#line $line "$file (Generated AUTOLOAD)"\nour \$AUTOLOAD;\n sub {\n my (\$self) = \@_;\n my (\$pkg, \$name) = (\$AUTOLOAD =~ m/^(.*)::([^:]+)\$/g);\n \$AUTOLOAD = undef;\n\n return if \$name eq 'DESTROY';\n my \$sub = sub {\n my \$self = shift;\n (\$self->{\$name}) = \@_ if \@_;\n return \$self->{\$name};\n };\n\n \$c->add(\$name => \$sub);\n\n if (\$c->{_track}) {\n my \$call = {sub_name => \$name, sub_ref => \$sub, args => [\@_]};\n push \@{\$c->{sub_tracking}->{\$name}} => \$call;\n push \@{\$c->{call_tracking}} => \$call;\n }\n\n goto &\$sub;\n }\n]
Aresult
11
00
170 package $class;
171 #line $line "$file (Generated AUTOLOAD)"
172 our \$AUTOLOAD;
173 sub {
174 my (\$self) = \@_;
175 my (\$pkg, \$name) = (\$AUTOLOAD =~ m/^(.*)::([^:]+)\$/g);
176 \$AUTOLOAD = undef;
177  
178 return if \$name eq 'DESTROY';
179 my \$sub = sub {
180 my \$self = shift;
181 (\$self->{\$name}) = \@_ if \@_;
182 return \$self->{\$name};
183 };
184  
185 \$c->add(\$name => \$sub);
186  
187 if (\$c->{_track}) {
188 my \$call = {sub_name => \$name, sub_ref => \$sub, args => [\@_]};
189 push \@{\$c->{sub_tracking}->{\$name}} => \$call;
190 push \@{\$c->{call_tracking}} => \$call;
191 }
192  
193 goto &\$sub;
194 }
195 EOT
196  
197 10 $line = __LINE__ + 3;
198 10 ▶ my $can = eval <<EOT || die "Failed generating can method: $@";
Condition50%1/2
eval qq[package $class;\n#line $line "$file (Generated can)"\nuse Scalar::Util 'reftype';\n sub {\n my (\$self, \$meth) = \@_;\n if (\$self->SUPER::can(\$meth)) {\n return \$self->SUPER::can(\$meth);\n }\n elsif (ref \$self && reftype \$self eq 'HASH' && exists \$self->{\$meth}) {\n return sub { shift->\$meth(\@_) };\n }\n return undef;\n }\n] || die("Failed generating can method: $@")
l!l
100
MC/DC0%0/1
eval qq[package $class;\n#line $line "$file (Generated can)"\nuse Scalar::Util 'reftype';\n sub {\n my (\$self, \$meth) = \@_;\n if (\$self->SUPER::can(\$meth)) {\n return \$self->SUPER::can(\$meth);\n }\n elsif (ref \$self && reftype \$self eq 'HASH' && exists \$self->{\$meth}) {\n return sub { shift->\$meth(\@_) };\n }\n return undef;\n }\n] || die("Failed generating can method: $@")
eval qq[package $class;\n#line $line "$file (Generated can)"\nuse Scalar::Util 'reftype';\n sub {\n my (\$self, \$meth) = \@_;\n if (\$self->SUPER::can(\$meth)) {\n return \$self->SUPER::can(\$meth);\n }\n elsif (ref \$self && reftype \$self eq 'HASH' && exists \$self->{\$meth}) {\n return sub { shift->\$meth(\@_) };\n }\n return undef;\n }\n]
Truth table50%1/2
A
A= eval qq[package $class;\n#line $line "$file (Generated can)"\nuse Scalar::Util 'reftype';\n sub {\n my (\$self, \$meth) = \@_;\n if (\$self->SUPER::can(\$meth)) {\n return \$self->SUPER::can(\$meth);\n }\n elsif (ref \$self && reftype \$self eq 'HASH' && exists \$self->{\$meth}) {\n return sub { shift->\$meth(\@_) };\n }\n return undef;\n }\n]
Aresult
11
00
199 package $class;
200 #line $line "$file (Generated can)"
201 use Scalar::Util 'reftype';
202 sub {
203 my (\$self, \$meth) = \@_;
204 if (\$self->SUPER::can(\$meth)) {
205 return \$self->SUPER::can(\$meth);
206 }
207 elsif (ref \$self && reftype \$self eq 'HASH' && exists \$self->{\$meth}) {
208 return sub { shift->\$meth(\@_) };
209 }
210 return undef;
211 }
212 EOT
213  
214 {
215 10 local $self->{+_TRACK} = 0;
216 10 $self->add(AUTOLOAD => $autoload);
217 10 $self->add(can => $can);
218 }
219 }
220  
221 3 ▶sub before {
Subroutine before: called CC=1 SCAR=0.0
Pod documented
222 3 my $self = shift;
223 3 my ($name, $sub) = @_;
224 3 $self->_check();
225 3 my $orig = $self->current($name, required => 1);
226 3 ▶ $self->_inject({}, $name => set_prototype(sub { $sub->(@_); $orig->(@_) }, prototype $sub));
Subroutine __ANON__: called CC=1 SCAR=0.0
227 }
228  
229 2 ▶sub after {
Subroutine after: called CC=1 SCAR=0.0
Pod documented
230 2 my $self = shift;
231 2 my ($name, $sub) = @_;
232 2 $self->_check();
233 2 my $orig = $self->current($name, required => 1);
234 $self->_inject(
235 {},
236 $name => set_prototype(
237 4 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
238 4 my @out;
239  
240 4 my $want = wantarray;
241  
242 4 ▶ if ($want) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want) { } 1 3 4
elsif (defined $want) { } 2 1 3
243 1 @out = $orig->(@_);
244 }
245 elsif (defined $want) {
246 2 $out[0] = $orig->(@_);
247 }
248 else {
249 1 $orig->(@_);
250 }
251  
252 4 $sub->(@_);
253  
254 4 ▶ return @out if $want;
Branch: 1 branch
BranchTrueFalseTotal
if $want 1 3 4
255 3 ▶ return $out[0] if defined $want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want 2 1 3
256 1 return;
257 },
258 2 prototype $sub,
259 )
260 );
261 }
262  
263 3 ▶sub around {
Subroutine around: called CC=1 SCAR=0.0
Pod documented
264 3 my $self = shift;
265 3 my ($name, $sub) = @_;
266 3 $self->_check();
267 3 my $orig = $self->current($name, required => 1);
268 2 ▶ $self->_inject({}, $name => set_prototype(sub { $sub->($orig, @_) }, prototype $sub));
Subroutine __ANON__: called CC=1 SCAR=0.0
269 }
270  
271 63 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
272 63 my $self = shift;
273 63 $self->_check();
274 63 $self->_inject({add => 1}, @_);
275 }
276  
277 21 ▶sub override {
Subroutine override: called CC=1 SCAR=0.0
Pod documented
278 21 my $self = shift;
279 21 $self->_check();
280 21 $self->_inject({}, @_);
281 }
282  
283 5 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
284 5 my $self = shift;
285 5 $self->_check();
286 5 $self->_inject({set => 1}, @_);
287 }
288  
289 171 ▶sub current {
Subroutine current: called CC=3 SCAR=11.0
Pod documented
290 171 my $self = shift;
291 171 my ($sym, %params) = @_;
292  
293 171 my $out = get_symbol($sym, $self->{+CLASS});
294 171 ▶ return $out unless $params{required};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"required"} 163 8 171
295 8 ▶ confess "Attempt to modify a sub that does not exist '$self->{+CLASS}\::$sym' (Mock operates on packages, not classes, are you looking for a symbol in a parent class?)"
Branch: 1 branch
BranchTrueFalseTotal
unless $out 1 7 8
296 unless $out;
297 7 return $out;
298 }
299  
300 17 ▶sub orig {
Subroutine orig: called CC=5 SCAR=16.1
Pod documented
301 17 my $self = shift;
302 17 my ($sym) = @_;
303  
304 17 ▶ $sym = "&$sym" unless $sym =~ m/^[&\$\%\@]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sym =~ /^[&\$\%\@]/ 13 4 17
305  
306 17 ▶ my $syms = $self->{+_SYMBOLS}
Branch: 1 branch
BranchTrueFalseTotal
my $syms = $self->{"_symbols"} or croak("No symbols have been mocked yet") 15 2 17
307 or croak "No symbols have been mocked yet";
308  
309 15 my $ref = $syms->{$sym};
310  
311 15 ▶ croak "Symbol '$sym' is not mocked"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and @$ref 2 13 15
Condition66%2/3
$ref and @$ref
!ll&&!rl&&r
2013
MC/DC50%1/2
$ref and @$ref
$ref @$ref
Truth table66%2/3
A and B
A= $refB= @$ref
ABresult
0X0
100
111
312 unless $ref && @$ref;
313  
314 13 my ($orig) = @$ref;
315  
316 13 return $orig;
317 }
318  
319 2 ▶sub track {
Subroutine track: called CC=2 SCAR=7.1
Pod documented
320 2 my $self = shift;
321  
322 2 ▶ ($self->{+_TRACK}) = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
323  
324 2 return $self->{+_TRACK};
325 }
326  
327 1 ▶sub clear_call_tracking { @{shift->{+CALL_TRACKING}} = () }
Subroutine clear_call_tracking: called CC=1 SCAR=0.0
Pod documented
328  
329 2 ▶sub clear_sub_tracking {
Subroutine clear_sub_tracking: called CC=3 SCAR=11.0
Pod documented
330 2 my $self = shift;
331  
332 2 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 1 2
333 1 %{$self->{+SUB_TRACKING}} = ();
334 1 return;
335 }
336  
337 1 for my $item (@_) {
338 2 delete $self->{+SUB_TRACKING}->{$item};
339 }
340  
341 1 return;
342 }
343  
344 132 ▶sub _parse_inject {
Subroutine _parse_inject: called CC=18 SCAR=29.2
345 132 my $self = shift;
346 132 my ($param, $arg) = @_;
347  
348 132 ▶ if ($param =~ m/^-(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^-(.*)$/) 15 117 132
349 15 my $sym = $1;
350 15 my $sig = slot_to_sig(reftype($arg));
351 15 my $ref = $arg;
352 15 return ($sig, $sym, $ref);
353 }
354  
355 117 ▶ return ('&', $param, $arg)
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg and builtin::reftype($arg) eq "CODE" 100 17 117
Condition100%3/3
ref $arg and builtin::reftype($arg) eq "CODE"
!ll&&!rl&&r
116100
MC/DC100%2/2
ref $arg and builtin::reftype($arg) eq "CODE"
ref $arg builtin::reftype($arg) eq "CODE"
Truth table100%3/3
A and B
A= ref $argB= builtin::reftype($arg) eq "CODE"
ABresult
0X0
100
111
356 if ref($arg) && reftype($arg) eq 'CODE';
357  
358 17 my ($is, $field, $val);
359  
360 17 ▶ if(defined($arg) && !ref($arg) && $arg =~ m/^(rw|ro|wo)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $arg and not ref $arg and $arg =~ /^(rw|ro|wo)$/) { } 6 11 17
elsif (not ref $arg) { } 5 6 11
elsif (builtin::reftype($arg) eq 'HASH') { } 6 0 6
Condition100%6/6
defined $arg and not ref $arg and $arg =~ /^(rw|ro|wo)$/
!ll&&!rl&&r
836
defined $arg and not ref $arg
!ll&&!rl&&r
269
MC/DC100%3/3
defined $arg and not ref $arg and $arg =~ /^(rw|ro|wo)$/
defined $arg not ref $arg $arg =~ /^(rw|ro|wo)$/
Truth table100%4/4
A and not(B) and C
A= defined $argB= not ref $argC= $arg =~ /^(rw|ro|wo)$/
ABCresult
0XX0
10X0
1100
1111
361 6 $is = $arg;
362 6 $field = $param;
363 }
364 elsif (!ref($arg)) {
365 5 $val = $arg;
366 5 $is = 'val';
367 }
368 elsif (reftype($arg) eq 'HASH') {
369 6 ▶ $field = delete $arg->{field} || $param;
Condition66%2/3
delete $arg->{'field'} || $param
l!l&&r!l&&!r
240
MC/DC0%0/2
delete $arg->{'field'} || $param
delete $arg->{'field'} $param
Truth table66%2/3
A || B
A= delete $arg->{'field'}B= $param
ABresult
1X1
011
000
370  
371 6 $val = delete $arg->{val};
372 6 $is = delete $arg->{is};
373  
374 6 ▶ croak "Cannot specify 'is' and 'val' together" if $val && $is;
Branch: 1 branch
BranchTrueFalseTotal
if $val and $is 0 6 6
Condition66%2/3
$val and $is
!ll&&!rl&&r
240
MC/DC0%0/2
$val and $is
$val $is
Truth table66%2/3
A and B
A= $valB= $is
ABresult
0X0
100
111
375  
376 6 ▶ $is ||= $val ? 'val' : 'rw';
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 4 0 4
Condition66%2/3
$is ||= $val ? "val" : "rw"
l!l&&r!l&&!r
240
MC/DC0%0/2
$is ||= $val ? "val" : "rw"
$is $val ? "val" : "rw"
Truth table66%2/3
A ||= B
A= $isB= $val ? "val" : "rw"
ABresult
1X1
011
000
377  
378 6 ▶ croak "The following keys are not valid when defining a mocked sub with a hashref: " . join(", " => keys %$arg)
Branch: 1 branch
BranchTrueFalseTotal
if keys %$arg 0 6 6
379 if keys %$arg;
380 }
381 else {
382 0 confess "'$arg' is not a valid argument when defining a mocked sub";
383 }
384  
385 17 my $sub;
386 17 ▶ if ($is eq 'rw') {
Branch: 3 branches
BranchTrueFalseTotal
if ($is eq 'rw') { } 4 13 17
elsif ($is eq 'ro') { } 2 11 13
elsif ($is eq 'wo') { } 2 9 11
387 4 $sub = gen_accessor($field);
388 }
389 elsif ($is eq 'ro') {
390 2 $sub = gen_reader($field);
391 }
392 elsif ($is eq 'wo') {
393 2 $sub = gen_writer($field);
394 }
395 else { # val
396 9 ▶ $sub = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
397 }
398  
399 17 return ('&', $param, $sub);
400 }
401  
402 96 ▶sub _inject {
Subroutine _inject: called CC=15 SCAR=27.1
403 96 my $self = shift;
404 96 my ($params, @pairs) = @_;
405  
406 96 my $add = $params->{add};
407 96 my $set = $params->{set};
408  
409 96 my $class = $self->{+CLASS};
410  
411 96 ▶ $self->{+_SYMBOLS} ||= {};
Condition100%2/2
$self->{"_symbols"} ||= {}
l!l
4848
MC/DC100%1/1
$self->{"_symbols"} ||= {}
$self->{"_symbols"}
Truth table100%2/2
A
A= $self->{"_symbols"}
Aresult
11
00
412 96 my $syms = $self->{+_SYMBOLS};
413  
414 96 ▶ while (my $param = shift @pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param = shift @pairs) 132 91 223
415 132 my $arg = shift @pairs;
416 132 my ($sig, $sym, $ref) = $self->_parse_inject($param, $arg);
417 132 my $orig = $self->current("$sig$sym");
418  
419 132 ▶ croak "Cannot override '$sig$class\::$sym', symbol is not already defined"
Branch: 1 branch
BranchTrueFalseTotal
unless $orig or $add or $set or $sig eq "&" and $class->can($sym) 3 129 132
Condition100%12/12
$orig or $add or $set or $sig eq "&" and $class->can($sym)
l!l&&r!l&&!r
12813
$orig or $add or $set
l!l&&r!l&&!r
12534
$orig or $add
l!l&&r!l&&!r
46797
$sig eq "&" and $class->can($sym)
!ll&&!rl&&r
121
MC/DC100%5/5
$orig or $add or $set or $sig eq "&" and $class->can($sym)
$orig $add $set $sig eq "&" $class->can($sym)
Truth table100%6/6
A or B or C or D and E
A= $origB= $addC= $setD= $sig eq "&"E= $class->can($sym)
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
420 unless $orig || $add || $set || ($sig eq '&' && $class->can($sym));
421  
422 # Cannot be too sure about scalars in globs
423 129 ▶ croak "Cannot add '$sig$class\::$sym', symbol is already defined"
Branch: 1 branch
BranchTrueFalseTotal
if $add and $orig and builtin::reftype($orig) ne "SCALAR" || defined $$orig 2 127 129
Condition77%7/9
$add and $orig and builtin::reftype($orig) ne "SCALAR" || defined $$orig
!ll&&!rl&&r
12702
$add and $orig
!ll&&!rl&&r
48792
builtin::reftype($orig) ne "SCALAR" or defined $$orig
l!l&&r!l&&!r
110
MC/DC50%2/4
$add and $orig and builtin::reftype($orig) ne "SCALAR" || defined $$orig
$add $orig builtin::reftype($orig) ne "SCALAR" defined $$orig
Truth table80%4/5
A and B and C or D
A= $addB= $origC= builtin::reftype($orig) ne "SCALAR"D= defined $$orig
ABCDresult
0XXX0
10XX0
11000
111X1
11011
424 if $add && $orig
425 && (reftype($orig) ne 'SCALAR' || defined($$orig));
426  
427 127 ▶ $syms->{"$sig$sym"} ||= [];
Condition100%2/2
$$syms{"$sig$sym"} ||= []
l!l
9118
MC/DC100%1/1
$$syms{"$sig$sym"} ||= []
$$syms{"$sig$sym"}
Truth table100%2/2
A
A= $$syms{"$sig$sym"}
Aresult
11
00
428 127 push @{$syms->{"$sig$sym"}} => $orig; # Might be undef, that's expected
429  
430 127 ▶ if ($self->{+_TRACK} && $sig eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_track"} and $sig eq "&") 3 124 127
Condition66%2/3
$self->{"_track"} and $sig eq "&"
!ll&&!rl&&r
12403
MC/DC50%1/2
$self->{"_track"} and $sig eq "&"
$self->{"_track"} $sig eq "&"
Truth table66%2/3
A and B
A= $self->{"_track"}B= $sig eq "&"
ABresult
0X0
100
111
431 3 my $sub_tracker = $self->{+SUB_TRACKING};
432 3 my $call_tracker = $self->{+CALL_TRACKING};
433 3 my $sub = $ref;
434 5 ▶ $ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
435 5 my $call = {sub_name => $sym, sub_ref => $sub, args => [@_]};
436 5 push @{$sub_tracker->{$param}} => $call;
437 5 push @$call_tracker => $call;
438 5 goto &$sub;
439 3 };
440 }
441  
442 163 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
443 163 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
444 127 *{"$class\::$sym"} = $ref;
445 }
446  
447 91 return;
448 }
449  
450 116 ▶sub _set_or_unset {
Subroutine _set_or_unset: called CC=2 SCAR=6.9
451 116 my $self = shift;
452 116 my ($symbol, $set) = @_;
453  
454 116 my $class = $self->{+CLASS};
455  
456 116 ▶ return purge_symbol($symbol, $class)
Branch: 1 branch
BranchTrueFalseTotal
unless $set 81 35 116
457 unless $set;
458  
459 35 my $sym = parse_symbol($symbol, $class);
460 163 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
461 163 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
462 35 *{"$class\::$sym->{name}"} = $set;
463 }
464  
465 4 ▶sub restore {
Subroutine restore: called CC=6 SCAR=18.1
Pod documented
466 4 my $self = shift;
467 4 my ($sym) = @_;
468 4 $self->_check();
469  
470 4 ▶ $sym = "&$sym" unless $sym =~ m/^[&\$\%\@]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sym =~ /^[&\$\%\@]/ 4 0 4
471  
472 4 ▶ my $syms = $self->{+_SYMBOLS}
Branch: 1 branch
BranchTrueFalseTotal
my $syms = $self->{"_symbols"} or croak("No symbols are mocked") 3 1 4
473 or croak "No symbols are mocked";
474  
475 3 my $ref = $syms->{$sym};
476  
477 3 ▶ croak "Symbol '$sym' is not mocked"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and @$ref 1 2 3
Condition66%2/3
$ref and @$ref
!ll&&!rl&&r
102
MC/DC50%1/2
$ref and @$ref
$ref @$ref
Truth table66%2/3
A and B
A= $refB= @$ref
ABresult
0X0
100
111
478 unless $ref && @$ref;
479  
480 2 my $old = pop @$ref;
481 2 ▶ delete $syms->{$sym} unless @$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ref 0 2 2
482  
483 2 return $self->_set_or_unset($sym, $old);
484 }
485  
486 116 ▶sub reset {
Subroutine reset: called CC=5 SCAR=16.1
Pod documented
487 116 my $self = shift;
488 116 my ($sym) = @_;
489 116 $self->_check();
490  
491 116 ▶ $sym = "&$sym" unless $sym =~ m/^[&\$\%\@]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sym =~ /^[&\$\%\@]/ 4 112 116
492  
493 116 ▶ my $syms = $self->{+_SYMBOLS}
Branch: 1 branch
BranchTrueFalseTotal
my $syms = $self->{"_symbols"} or croak("No symbols are mocked") 115 1 116
494 or croak "No symbols are mocked";
495  
496 115 my $ref = delete $syms->{$sym};
497  
498 115 ▶ croak "Symbol '$sym' is not mocked"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref and @$ref 1 114 115
Condition66%2/3
$ref and @$ref
!ll&&!rl&&r
10114
MC/DC50%1/2
$ref and @$ref
$ref @$ref
Truth table66%2/3
A and B
A= $refB= @$ref
ABresult
0X0
100
111
499 unless $ref && @$ref;
500  
501 114 my ($old) = @$ref;
502  
503 114 return $self->_set_or_unset($sym, $old);
504 }
505  
506 45 ▶sub reset_all {
Subroutine reset_all: called CC=3 SCAR=11.1
Pod documented
507 45 my $self = shift;
508 45 $self->_check();
509  
510 45 ▶ my $syms = $self->{+_SYMBOLS} || return;
Condition50%1/2
$self->{'_symbols'} || (return)
l!l
450
MC/DC0%0/1
$self->{'_symbols'} || (return)
$self->{'_symbols'}
Truth table50%1/2
A
A= $self->{'_symbols'}
Aresult
11
00
511  
512 45 $self->reset($_) for keys %$syms;
513  
514 45 delete $self->{+_SYMBOLS};
515 }
516  
517 7 ▶sub _purge {
Subroutine _purge: called CC=2 SCAR=6.9
518 7 my $self = shift;
519 7 my $stash = $self->stash;
520 7 delete $stash->{$_} for keys %$stash;
521 }
522  
523 66 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
524 66 my $self = shift;
525  
526 66 delete $self->{+CHILD};
527 66 ▶ $self->reset_all if $self->{+_SYMBOLS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_symbols"} 40 26 66
528  
529 66 ▶ delete $INC{$self->file} if $self->{+_BLOCKED_LOAD};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_blocked_load"} 8 58 66
530  
531 66 ▶ $self->_purge if $self->{+_PURGE_ON_DESTROY};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_purge_on_destroy"} 7 59 66
532 }
533  
534 163 1;
535  
536 __END__
« blib/lib/Test2/Workflow/Task/Group.pm ↑ Summary blib/lib/Test2/Plugin/SRand.pm »