Exercise/Execute.pm

filter: Statementstmt 85.2% 121 / 142 Branchbran 69.2% 54 / 78 Conditioncond 61.9% 13 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 75.0% 207 / 276 CC 51 SCAR 23.6
CC
51
Cov
78%
prepare_tests
22.5
__ANON__
16.6
extract_flags
10.0
CRAP 10.6
↑ Summary Exercise.pm »
1 #######################################################################
2 # housekeeping
3 #######################################################################
4  
5 package Object::Exercise::Execute v3.1.1;
6 12 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use YAML::XS qw( Load );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 ########################################################################
19 # package variables
20 ########################################################################
21  
22 12 our @CARP_NOT = ( __PACKAGE__ );
23  
24 12 my @defaultz =
25 qw
26 (
27 benchmark 0
28 counter 0
29 break 0
30 regex 0
31  
32 debug 0
33 verbose 0
34  
35  
36 continue 1
37 finish 1
38  
39 export exercise
40 );
41  
42 12 my %globalz = @defaultz;
43  
44 ########################################################################
45 # local utility subs
46 ########################################################################
47  
48 ########################################################################
49 # deal with flag settings
50  
51 sub extract_flags
52 163 ▶{
Subroutine extract_flags: called CC=10 SCAR=23.0
Pod undocumented
53 163 my %flagz = ();
54 163 my $count = 0;
55 163 my $found = 0;
56  
57 163 for( @_ )
58 {
59 174 ▶ ref $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and last 149 25 174
60 and last;
61  
62 25 ++$count;
63  
64 25 my ( $negate, $name, $value )
65 = m{ (no)? (\w+) (?:=)?( .+ )? }x;
66  
67 exists $globalz{ $name }
68 or do
69 25 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
exists $globalz{$name} or do { diag("Unknown option: '${_}'"); next } 14 11 25
70 11 diag "Unknown option: '$_'";
71 next
72 11 };
73  
74 14 ++$found;
75  
76 14 ▶ $flagz{ $name }
Branch: 1 branch
BranchTrueFalseTotal
$negate ? : 5 9 14
Condition75%3/4
$value // 1
l!l
05
$value // 1
l!l
18
MC/DC0%0/1
$value // 1
$value
MC/DC100%1/1
$value // 1
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
Truth table100%2/2
A
A= $value
Aresult
11
00
77 = $negate
78 ? ! ( $value // 1 )
79 : ( $value // 1 )
80 ;
81 }
82  
83 $count
84 163 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$count or return 25 138 163
85  
86 25 splice @_, 0, $count;
87  
88 25 ▶ note "Extracted flags:\n", explain \%flagz
Branch: 1 branch
BranchTrueFalseTotal
if $found 14 11 25
89 if $found;
90  
91 wantarray
92 25 ▶ ? %flagz
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 3 25
93 : \%flagz
94 }
95  
96 sub import_flags
97 12 ▶{
Subroutine import_flags: called CC=1 SCAR=0.0
Pod undocumented
98 12 %globalz = ( @defaultz, &extract_flags );
99  
100 # hand back the name to export.
101 # remainder are left in the global settings.
102  
103 delete $globalz{ export }
104 12 }
105  
106 ########################################################################
107 # execute individual tests
108  
109 my $execute
110 = sub
111 68 ▶{
Subroutine __ANON__: called CC=8 SCAR=21.2
112 # note the lack of pass/fail: all this will do is report errors.
113 # any pass/fail handling is dealt with in test_result.
114  
115 68 state $r = [];
116 68 state $t0 = '';
117 68 state $t1 = '';
118 68 state $n = 0;
119  
120 68 my ( $obj, $test ) = @_;
121  
122 68 my ( $method, @argz ) = @$test;
123  
124 # assume that "@$r =" is not significant in the benchmark.
125 # also assume that nothing in gettimeofday will set $@.
126  
127 68 @$r = ();
128  
129 eval
130 68 {
131 68 ▶ $DB::single = 1 if $globalz{ break };
Branch: 1 branch
BranchTrueFalseTotal
if $globalz{"break"} 0 68 68
132  
133 68 $t0 = [ gettimeofday ];
134 68 @$r = $obj->$method( @argz );
135 64 $t1 = [ gettimeofday ];
136 };
137 68 chomp( my $error = $@ );
138  
139 68 ▶ if( $globalz{ counter } )
Branch: 1 branch
BranchTrueFalseTotal
if ($globalz{"counter"}) 0 68 68
140 {
141 0 note 'Instruction: ' . ++$n;
142 }
143  
144 68 ▶ if( $globalz{ benchmark } )
Branch: 1 branch
BranchTrueFalseTotal
if ($globalz{"benchmark"}) 14 54 68
145 {
146 14 state $format = "Benchmark: %0.6f sec\t%s( %s )";
147  
148 14 my $wall = tv_interval $t0, $t1;
149  
150 14 note sprintf $format => $wall, $method, explain @argz;
151 }
152  
153 68 ▶ if( $error )
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 4 64 68
elsif ($globalz{'verbose'}) { } 2 62 64
154 {
155 4 diag "Error: '$method'\n", explain $error;
156 }
157 elsif( $globalz{ verbose } )
158 {
159 2 note "Clean: '$method'\n", explain $r;
160 }
161  
162 68 ▶ if( $error && $globalz{ debug } )
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $globalz{"debug"}) 0 68 68
Condition66%2/3
$error and $globalz{"debug"}
!ll&&!rl&&r
6440
MC/DC0%0/2
$error and $globalz{"debug"}
$error $globalz{"debug"}
Truth table66%2/3
A and B
A= $errorB= $globalz{"debug"}
ABresult
0X0
100
111
163 {
164 # stop here where the error is visible and the
165 # test contents are still in scope.
166  
167 0 $DB::single = 1;
168 0 0
169 }
170  
171 68 ( $error, $r )
172 12 };
173  
174 my $test_result
175 = sub
176 68 ▶{
Subroutine __ANON__: called CC=11 SCAR=28.1
177 68 my ( $obj, $test, $expect, $message ) = @_;
178  
179 68 my $method = $test->[0];
180  
181 68 my ( $error, $found ) = $obj->$execute( $test );
182  
183 68 ▶ $message ||= "$method";
Condition66%2/3
$message ||= "$method"
l!l&&r!l&&!r
14540
MC/DC0%0/2
$message ||= "$method"
$message "$method"
Truth table66%2/3
A ||= B
A= $messageB= "$method"
ABresult
1X1
011
000
184 68 ▶ $message .= " ($error)"
Branch: 1 branch
BranchTrueFalseTotal
if $error 4 64 68
185 if $error;
186  
187 68 for my $type ( reftype $expect )
188 {
189 68 ▶ if( 'CODE' eq $type )
Branch: 3 branches
BranchTrueFalseTotal
if ('CODE' eq $type) { } 0 68 68
elsif ($expect) { } 64 4 68
elsif ($error) { } 4 0 4
190 {
191 0 ok $expect->( $test, $found, $error ), $message;
192 }
193 elsif( $expect )
194 {
195 # this may pass if expect is [ undef ] but will
196 # report the error text either way.
197  
198 64 ▶ if
Branch: 3 branches
BranchTrueFalseTotal
if (not $type and $globalz{'regex'}) { } 0 64 64
elsif ('REGEXP' eq $type) { } 0 64 64
elsif ('ARRAY' eq $type) { } 64 0 64
Condition33%1/3
not $type and $globalz{"regex"}
!ll&&!rl&&r
6400
MC/DC0%0/2
not $type and $globalz{"regex"}
not $type $globalz{"regex"}
Truth table33%1/3
not(A) and B
A= not $typeB= $globalz{"regex"}
ABresult
0X0
100
111
199 (
200 ! $type
201 and
202 $globalz{ regex }
203 )
204 {
205 0 like "@$found", qr{$expect}x, $message;
206 }
207 elsif( 'REGEXP' eq $type )
208 {
209 0 like "@$found", $expect, $message;
210 }
211 elsif( 'ARRAY' eq $type )
212 {
213 64 is_deeply $found, $expect, $message;
214 }
215 else
216 {
217 0 BAIL_OUT
218 "Invalid expect: '$type'\n" .
219 explain( $expect, $found, $message );
220 }
221 }
222 elsif( $error )
223 {
224 # explicit undef expects an error.
225 # zero the string here to avoid rejecting the test
226 # in the caller.
227  
228 4 pass "Expected error: '$error'";
229  
230 4 $error = '';
231 }
232 else
233 {
234 # expected error was not returned, this is a failure.
235  
236 0 fail "Unexpected success: '$method' (no error)";
237 0 diag "Return value:\n", explain $found;
238 }
239 }
240  
241 # mainly useful as a boolean value in the caller.
242  
243 $error
244 68 };
245  
246 my $process
247 = sub
248 68 ▶{
Subroutine __ANON__: called CC=6 SCAR=21.8
249 68 my $obj = shift;
250  
251 # flattened out test entry is left on the stack.
252 # extract any local flags (e.g., verbose for one
253 # test only).
254  
255 68 my $localz = &extract_flags;
256  
257 # trailing flags left nothing further to process.
258  
259 68 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 68 0 68
260 {
261 # sane
262 }
263 else
264 {
265 0 diag "Bogus test: contains only local flags\n", explain $localz;
266 return
267 0 }
268  
269 68 ▶ local @globalz{ keys %$localz } = values %$localz
Branch: 1 branch
BranchTrueFalseTotal
if $localz 3 65 68
270 if $localz;
271  
272 # if there is no expect value then skip the ok check and just
273 # run the method.
274  
275 68 ▶ my $handler
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 68 0 68
276 = @_> 1
277 ? $test_result
278 : $execute
279 ;
280  
281 68 ▶ my $error = $obj->$handler( @_ )
Branch: 1 branch
BranchTrueFalseTotal
my $error = $obj->$handler(@_) or return 0 68 68
282 or return;
283  
284 $globalz{ continue }
285 or
286 0 ▶ die "Error during processing (continue turned off)\n" .
Branch: 1 branch
BranchTrueFalseTotal
$globalz{"continue"} or die "Error during processing (continue turned off)\n" . explain($error) 0 0 0
287 explain $error;
288  
289 return
290 0 };
291  
292 ########################################################################
293 # break up contents of exercise.
294  
295 sub validate_plan
296 10 ▶{
Subroutine validate_plan: called CC=7 SCAR=19.5
Pod undocumented
297 # entrys are anything that gets run, expects have an
298 # tests value and issue a pass/fail for the test.
299  
300 10 my $entrys = 0;
301 10 my $tests = 0;
302  
303 10 for my $fieldz ( @_ )
304 {
305 80 ▶ ref $fieldz or next;
Branch: 1 branch
BranchTrueFalseTotal
ref $fieldz or next 68 12 80
306  
307 68 my $n = $#$fieldz;
308  
309 68 ▶ my $i = first { ref $fieldz->[$_] } ( 0 .. $n )
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
(0 .. $n) // (next)
l!l
680
MC/DC0%0/1
(0 .. $n) // (next)
(0 .. $n)
Truth table50%1/2
A
A= (0 .. $n)
Aresult
11
00
310 // next;
311  
312 68 ++$entrys;
313  
314 68 ▶ $n > $i # i.e., exists $entry->[ 1 + $i ]
Branch: 1 branch
BranchTrueFalseTotal
$n > $i or next 17 51 68
315 or next;
316  
317 17 ++$tests;
318 }
319  
320 $entrys
321 or
322 10 ▶ BAIL_OUT 'Bogus exercise: no executable entry', explain \@_;
Branch: 1 branch
BranchTrueFalseTotal
$entrys or BAIL_OUT("Bogus exercise: no executable entry", explain(\@_)) 10 0 10
323  
324 note "Executing: $entrys entrys ($tests with tests)"
325 10 ▶ if $globalz{ verbose };
Branch: 1 branch
BranchTrueFalseTotal
if $globalz{"verbose"} 1 9 10
326  
327 return
328 10 }
329  
330 sub prepare_tests
331 12 ▶{
Subroutine prepare_tests: called CC=10 SCAR=31.1
Pod undocumented
332 12 ▶ @_ or croak 'Bogus exercise: no tests on the stack';
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("Bogus exercise: no tests on the stack") 12 0 12
333  
334 12 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_ and not ref $_[0]) 2 10 12
Condition66%2/3
1 == @_ and not ref $_[0]
!ll&&!rl&&r
1002
MC/DC50%1/2
1 == @_ and not ref $_[0]
1 == @_ not ref $_[0]
Truth table66%2/3
A and not(B)
A= 1 == @_B= not ref $_[0]
ABresult
0X0
100
111
335 (
336 1 == @_
337 and
338 ! ref $_[0]
339 )
340 {
341 2 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if ('finish' eq $_[0] or 'nofinish' eq $_[0]) { } 2 0 2
Condition66%2/3
"finish" eq $_[0] or "nofinish" eq $_[0]
l!l&&r!l&&!r
110
MC/DC0%0/2
"finish" eq $_[0] or "nofinish" eq $_[0]
"finish" eq $_[0] "nofinish" eq $_[0]
Truth table66%2/3
A or B
A= "finish" eq $_[0]B= "nofinish" eq $_[0]
ABresult
1X1
011
000
342 (
343 'finish' eq $_[0]
344 or
345 'nofinish' eq $_[0]
346 )
347 {
348 2 %globalz = ( %globalz, &extract_flags );
349 return
350 2 }
351 else
352 {
353 # anything else useful requires multiple
354 # entries at this point: the input is YAML.
355  
356 0 my $yaml = shift;
357  
358 note "Non-ref test: assume YAML\n$yaml",
359  
360 0 my $struct = eval { Load $yaml };
361  
362 0 ▶ BAIL_OUT "Invalid YAML: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
363 if $@;
364  
365 0 ▶ @$struct
Branch: 1 branch
BranchTrueFalseTotal
@$struct or BAIL_OUT("Invalid YAML: empty content\n", $yaml) 0 0 0
366 or BAIL_OUT "Invalid YAML: empty content\n", $yaml;
367  
368 0 @_ = @$struct;
369 }
370 }
371
372 # extract any flags floating at the start and
373 # leave the tests in place.
374  
375 10 %globalz = ( %globalz, &extract_flags );
376  
377 # @_ will be empty at this point if there were only flags.
378  
379 10 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 10 0 10
elsif ($globalz{'finish'}) { } 0 0 0
380 {
381 # there is a plan on the stack
382  
383 10 &validate_plan
384 }
385 elsif( $globalz{ finish } )
386 {
387 0 croak
388 "Bogus plan: no tests and finish is true (missing 'nofinish'?)";
389 }
390 else
391 {
392 # no plan, no finish => fine.
393 }
394  
395 # at this point the first 'test' is at the head of the stack.
396  
397 return
398 10 }
399  
400  
401 ########################################################################
402 # interface pushed into caller via Object::Exercise
403 ########################################################################
404  
405 sub exercise
406 12 ▶{
Subroutine exercise: called CC=5 SCAR=16.2
Pod undocumented
407 12 my $obj = shift;
408  
409 # test seqeunce is left on the stack.
410  
411 12 &prepare_tests;
412  
413 # at this point the stack may be empty if the inputs were
414 # flags (e.g., 'finish', or 'nofinish').
415  
416 12 ▶ while( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 71 12 83
417 {
418 71 ▶ if( my @flagz = &extract_flags )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@flagz) = &extract_flags) 3 68 71
419 {
420 3 %globalz = ( %globalz, @flagz );
421  
422 next
423 3 }
424  
425 68 ▶ if( my $entry = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift()) { } 68 0 68
426 {
427 68 $obj->$process( @$entry );
428 }
429 else
430 {
431 0 croak "Bogus test: false entry ($entry).";
432 }
433 }
434  
435 done_testing
436 12 ▶ if $globalz{ finish };
Branch: 1 branch
BranchTrueFalseTotal
if $globalz{"finish"} 9 3 12
437  
438 return
439 12 }
440  
441 # keep require happy
442 12 1
443 __END__
↑ Summary Exercise.pm »