Proto/Role/ArrayRef.pm

filter: Statementstmt 96.0% 557 / 580 Branchbran 92.5% 187 / 202 Conditioncond 79.6% 43 / 54 MC/DCmcdc 71.4% 25 / 35 Subroutinesub 98.7% 80 / 81 Podpod 100% 38 / 38 totaltotal 93.9% 930 / 990 CC 211 SCAR 19.1
CC
211
Cov
94%
__ANON__
19.0
__ANON__
13.0
__ANON__
12.7
CRAP 6.7
« Proto/Common.pm ↑ Summary Proto/Formatter/TestBuilder.pm »
1 package Test::Proto::Role::ArrayRef;
2 11 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Test::Proto::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Scalar::Util qw'blessed weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Test::Proto::Role::ArrayRef - Role containing test case methods for array refs.
12  
13 =head1 SYNOPSIS
14  
15 package MyProtoClass;
16 use Moo;
17 with 'Test::Proto::Role::ArrayRef';
18  
19 This Moo Role provides methods to Test::Proto::ArrayRef for test case methods that apply to arrayrefs such as C<map>. It can also be used for objects which use overload or otherwise respond to arrayref syntax.
20  
21 =head1 METHODS
22  
23 =head3 map
24  
25 pArray->map(sub { uc shift }, ['A','B'])->ok(['a','b']);
26  
27 Applies the first argument (a coderef) onto each member of the array. The resulting array is compared to the second argument.
28  
29 =cut
30  
31 3 ▶sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
32 3 my ( $self, $code, $expected, $reason ) = @_;
33 3 $self->add_test(
34 'map',
35 {
36 code => $code,
37 expected => $expected
38 },
39 $reason
40 );
41 }
42  
43 3 ▶define_test 'map' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 3 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
45 3 my $subject = [ map { $data->{code}->($_) } @{ $self->subject } ];
46 3 return upgrade( $data->{expected} )->validate( $subject, $self );
47 11 };
48  
49 =head3 grep
50  
51 pArray->grep(sub { $_[0] eq uc $_[0] }, ['A'])->ok(['A','b']); # passes
52 pArray->grep(sub { $_[0] eq uc $_[0] }, [])->ok(['a','b']); # passes
53 pArray->grep(sub { $_[0] eq uc $_[0] })->ok(['a','b']); # fails - 'boolean' grep behaves like array_any
54  
55 Applies the first argument (a prototype) onto each member of the array; if it returns true, the member is added to the resulting array. The resulting array is compared to the second argument.
56  
57 =cut
58  
59 7 ▶sub grep {
Subroutine grep: called CC=3 SCAR=11.0
Pod documented
60 7 my ( $self, $code, $expected, $reason ) = @_;
61 7 ▶ if ( defined $expected and CORE::ref $expected ) { #~ CORE::ref used because boolean grep might have a reason
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected and ref $expected) { } 3 4 7
Condition100%3/3
defined $expected and ref $expected
!ll&&!rl&&r
223
MC/DC100%2/2
defined $expected and ref $expected
defined $expected ref $expected
Truth table100%3/3
A and B
A= defined $expectedB= ref $expected
ABresult
0X0
100
111
62 3 $self->add_test(
63 'grep',
64 {
65 match => $code,
66 expected => $expected
67 },
68 $reason
69 );
70 }
71 else {
72 4 $reason = $expected;
73 4 $self->add_test( 'array_any', { match => $code }, $reason );
74 }
75 }
76  
77 3 ▶define_test 'grep' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _TEST_grep: called CC=1 SCAR=0.0
78 3 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
79 3 my $subject = [ grep { upgrade( $data->{match} )->validate($_) } @{ $self->subject } ];
80 3 return upgrade( $data->{expected} )->validate( $subject, $self );
81 11 };
82  
83 =head3 array_any
84  
85 pArray->array_any(sub { $_[0] eq uc $_[0] })->ok(['A','b']); # passes
86 pArray->array_any(sub { $_[0] eq uc $_[0] })->ok(['a','b']); # fails
87  
88 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the test case succeeds.
89  
90 =cut
91  
92 38 ▶sub array_any {
Subroutine array_any: called CC=1 SCAR=0.0
Pod documented
93 38 my ( $self, $expected, $reason ) = @_;
94 38 $self->add_test( 'array_any', { match => $expected }, $reason );
95 }
96  
97 42 ▶define_test 'array_any' => sub {
Subroutine _TEST_array_any: called CC=3 SCAR=11.0
98 42 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
99 42 my $i = 0;
100 42 foreach my $single_subject ( @{ $self->subject } ) {
101 47 ▶ return $self->pass("Item $i matched") if upgrade( $data->{match} )->validate($single_subject);
Branch: 1 branch
BranchTrueFalseTotal
if upgrade($data->{"match"})->validate($single_subject) 19 28 47
102 28 $i++;
103 }
104 23 return $self->fail('None matched');
105 11 };
106  
107 =head3 array_none
108  
109 pArray->array_none(sub { $_[0] eq uc $_[0] })->ok(['a','b']); # passes
110 pArray->array_none(sub { $_[0] eq uc $_[0] })->ok(['A','b']); # fails
111  
112 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the test case fails.
113  
114 =cut
115  
116 2 ▶sub array_none {
Subroutine array_none: called CC=1 SCAR=0.0
Pod documented
117 2 my ( $self, $code, $reason ) = @_;
118 2 $self->add_test( 'array_none', { code => $code }, $reason );
119 }
120  
121 0 ▶define_test 'array_none' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
122 0 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
123 0 my $i = 0;
124 0 foreach my $single_subject ( @{ $self->subject } ) {
125 0 ▶ return $self->fail("Item $i matched") if upgrade( $data->{code} )->validate($single_subject);
Branch: 1 branch
BranchTrueFalseTotal
if upgrade($data->{"code"})->validate($single_subject) 0 0 0
126 0 $i++;
127 }
128 0 return $self->pass('None matched');
129 11 };
130  
131 =head3 array_all
132  
133 pArray->array_all(sub { $_[0] eq uc $_[0] })->ok(['A','B']); # passes
134 pArray->array_all(sub { $_[0] eq uc $_[0] })->ok(['A','b']); # fails
135  
136 Applies the first argument (a prototype) onto each member of the array; if any member returns false, the test case fails.
137  
138 =cut
139  
140 2 ▶sub array_all {
Subroutine array_all: called CC=1 SCAR=0.0
Pod documented
141 2 my ( $self, $code, $reason ) = @_;
142 2 $self->add_test( 'array_all', { code => $code }, $reason );
143 }
144  
145 2 ▶define_test 'array_all' => sub {
Subroutine _TEST_array_all: called CC=3 SCAR=11.0
146 2 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
147 2 my $i = 0;
148 2 foreach my $single_subject ( @{ $self->subject } ) {
149 4 ▶ return $self->fail("Item $i did not match") unless upgrade( $data->{code} )->validate($single_subject);
Branch: 1 branch
BranchTrueFalseTotal
unless upgrade($data->{"code"})->validate($single_subject) 1 3 4
150 3 $i++;
151 }
152 1 return $self->pass('All matched');
153 11 };
154  
155 =head3 reduce
156  
157 pArray->reduce(sub { $_[0] + $_[1] }, 6 )->ok([1,2,3]);
158  
159 Applies the first argument (a coderef) onto the first two elements of the array, and thereafter the next element and the return value of the previous calculation. Similar to List::Util::reduce.
160  
161 =cut
162  
163 3 ▶sub reduce {
Subroutine reduce: called CC=1 SCAR=0.0
Pod documented
164 3 my ( $self, $code, $expected, $reason ) = @_;
165 3 $self->add_test(
166 'reduce',
167 {
168 code => $code,
169 expected => $expected
170 },
171 $reason
172 );
173 }
174  
175 3 ▶define_test 'reduce' => sub {
Subroutine _TEST_reduce: called CC=3 SCAR=11.0
176 3 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
177 3 my $length = $#{ $self->subject };
178 3 ▶ return $self->exception( 'Cannot use reduce unless the subject has at least two elements; only ' . ( $length + 1 ) . ' found' ) unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 1 2 3
179 2 my $left = ${ $self->subject }[0];
180 2 my $right;
181 2 my $i = 1;
182 2 ▶ while ( $i <= $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $length) 4 2 6
183 4 $right = ${ $self->subject }[$i];
184 4 $left = $data->{code}->( $left, $right );
185 4 $i++;
186 }
187 2 return upgrade( $data->{expected} )->validate( $left, $self );
188 11 };
189  
190 =head3 nth
191  
192 pArray->nth(1,'b')->ok(['a','b']);
193  
194 Finds the nth item (where n is the first argument) and compares the result to the prototype provided in the second argument.
195  
196 =cut
197  
198 195 ▶sub nth {
Subroutine nth: called CC=1 SCAR=0.0
Pod documented
199 195 my ( $self, $index, $expected, $reason ) = @_;
200 195 $self->add_test(
201 'nth',
202 {
203 'index' => $index,
204 expected => $expected
205 },
206 $reason
207 );
208 }
209  
210 195 ▶define_test nth => sub {
Subroutine _TEST_nth: called CC=2 SCAR=7.1
211 195 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
212 195 ▶ if ( exists $self->subject->[ $data->{'index'} ] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->subject->[$data->{'index'}]) { } 194 1 195
if (exists $self->subject->[$data->{'index'}]) { } 1 2 3
213 0 my $subject = $self->subject->[ $data->{'index'} ];
214 0 return upgrade( $data->{expected} )->validate( $subject, $self );
215 }
216 else {
217 0 return $self->fail( 'The index ' . $data->{'index'} . ' does not exist.' );
218 }
219 11 };
220  
221 =head3 count_items
222  
223 pArray->count_items(2)->ok(['a','b']);
224  
225 Finds the length of the array (i.e. the number of items) and compares the result to the prototype provided in the argument.
226  
227 =cut
228  
229 115 ▶sub count_items {
Subroutine count_items: called CC=1 SCAR=0.0
Pod documented
230 115 my ( $self, $expected, $reason ) = @_;
231 115 $self->add_test( 'count_items', { expected => $expected }, $reason );
232 }
233  
234 115 ▶define_test count_items => sub {
Subroutine _TEST_count_items: called CC=1 SCAR=0.0
235 115 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
236 115 my $subject = scalar @{ $self->subject };
237 115 return upgrade( $data->{expected} )->validate( $subject, $self );
238 11 };
239  
240 =head3 enumerated
241  
242 pArray->enumerated($tests_enumerated)->ok(['a','b']);
243  
244 Produces the indices and values of the subject as an array reference, and tests them against the prototype provided in the argument.
245  
246 In the above example, the prototype C<$tests_enumerated> should return a pass for C<[[0,'a'],[1,'b']]>.
247  
248 =cut
249  
250 4 ▶sub enumerated {
Subroutine enumerated: called CC=1 SCAR=0.0
Pod documented
251 4 my ( $self, $expected, $reason ) = @_;
252 4 $self->add_test( 'enumerated', { expected => $expected }, $reason );
253 }
254  
255 4 ▶define_test 'enumerated' => sub {
Subroutine _TEST_enumerated: called CC=2 SCAR=6.9
256 4 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
257 4 my $subject = [];
258 4 push @$subject, [ $_, $self->subject->[$_] ] foreach ( 0 .. $#{ $self->subject } );
259 4 return upgrade( $data->{expected} )->validate( $subject, $self );
260 11 };
261  
262 =head3 in_groups
263  
264 pArray->in_groups(2,[['a','b'],['c','d'],['e']])->ok(['a','b','c','d','e']);
265  
266 Bundles the contents in groups of n (where n is the first argument), puts each group in an arrayref, and compares the resulting arrayref to the prototype provided in the second argument.
267  
268 =cut
269  
270 7 ▶sub in_groups {
Subroutine in_groups: called CC=1 SCAR=0.0
Pod documented
271 7 my ( $self, $groups, $expected, $reason ) = @_;
272 7 $self->add_test(
273 'in_groups',
274 {
275 'groups' => $groups,
276 expected => $expected
277 },
278 $reason
279 );
280 }
281  
282 7 ▶define_test in_groups => sub {
Subroutine __ANON__: called CC=6 SCAR=19.0
283 0 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
284 0 ▶ return $self->exception('in_groups needs groups of 1 or more') if $data->{'groups'} < 1;
Branch: 2 branches
BranchTrueFalseTotal
if $data->{"groups"} < 1 1 6 7
if $data->{"groups"} < 1 0 0 0
285 0 my $newArray = [];
286 0 my $i = 0;
287 0 my $currentGroup = [];
288 0 foreach my $item ( @{ $self->subject } ) {
289 22 ▶ if ( 0 == ( $i % $data->{'groups'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $i % $data->{"groups"}) 14 8 22
290 14 ▶ push @$newArray, $currentGroup if @$currentGroup;
Branch: 1 branch
BranchTrueFalseTotal
if @$currentGroup 9 5 14
291 14 $currentGroup = [];
292 }
293 22 push @$currentGroup, $item;
294 22 $i++;
295 }
296 6 ▶ push @$newArray, $currentGroup if @$currentGroup;
Branch: 1 branch
BranchTrueFalseTotal
if @$currentGroup 5 1 6
297 6 return upgrade( $data->{expected} )->validate( $newArray, $self );
298 11 };
299  
300 =head3 group_when
301  
302 pArray->group_when(sub {$_[eq uc $_[0]}, [['A'],['B','c','d'],['E']])->ok(['A','B','c','d','E']);
303 pArray->group_when(sub {$_[0] eq $_[0]}, [['a','b','c','d','e']])->ok(['a','b','c','d','e']);
304  
305 Bundles the contents of the test subject in groups; a new group is created when the member matches the first argument (a prototype). The resulting arrayref is compared to the second argument.
306  
307 =cut
308  
309 3 ▶sub group_when {
Subroutine group_when: called CC=1 SCAR=0.0
Pod documented
310 3 my ( $self, $condition, $expected, $reason ) = @_;
311 3 $self->add_test(
312 'group_when',
313 {
314 'condition' => $condition,
315 expected => $expected,
316 must_match => 'value'
317 },
318 $reason
319 );
320 }
321  
322 =head3 group_when_index
323  
324 pArray->group_when_index(p(0)|p(1)|p(4), [['A'],['B','c','d'],['E']])->ok(['A','B','c','d','E']);
325 pArray->group_when_index(p->num_gt(2), [['a','b','c','d','e']])->ok(['a','b','c','d','e']);
326  
327 Bundles the contents of the test subject in groups; a new group is created when the index matches the first argument (a prototype). The resulting arrayref is compared to the second argument.
328  
329 =cut
330  
331 3 ▶sub group_when_index {
Subroutine group_when_index: called CC=1 SCAR=0.0
Pod documented
332 3 my ( $self, $condition, $expected, $reason ) = @_;
333 3 $self->add_test(
334 'group_when',
335 {
336 'condition' => $condition,
337 expected => $expected,
338 must_match => 'index'
339 },
340 $reason
341 );
342 }
343  
344 6 ▶define_test group_when => sub {
Subroutine _TEST_group_when: called CC=8 SCAR=21.0
345 6 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
346 6 my $newArray = [];
347 6 my $currentGroup = [];
348 6 my $condition = upgrade( $data->{condition} );
349 6 my $i = 0;
350 6 foreach my $item ( @{ $self->subject } ) {
351 30 my $got = $item;
352 30 ▶ $got = $i if $data->{must_match} =~ /index/;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"must_match"} =~ /index/ 15 15 30
353 30 ▶ if ( $condition->validate($got) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition->validate($got)) 7 23 30
354 7 ▶ push @$newArray, $currentGroup if defined $currentGroup and @$currentGroup;
Branch: 1 branch
BranchTrueFalseTotal
if defined $currentGroup and @$currentGroup 6 1 7
Condition66%2/3
defined $currentGroup and @$currentGroup
!ll&&!rl&&r
016
MC/DC50%1/2
defined $currentGroup and @$currentGroup
defined $currentGroup @$currentGroup
Truth table66%2/3
A and B
A= defined $currentGroupB= @$currentGroup
ABresult
0X0
100
111
355 7 $currentGroup = [];
356 }
357 30 push @$currentGroup, $item;
358 30 $i++;
359 }
360 6 ▶ push @$newArray, $currentGroup if defined $currentGroup and @$currentGroup;
Branch: 1 branch
BranchTrueFalseTotal
if defined $currentGroup and @$currentGroup 6 0 6
Condition33%1/3
defined $currentGroup and @$currentGroup
!ll&&!rl&&r
006
MC/DC0%0/2
defined $currentGroup and @$currentGroup
defined $currentGroup @$currentGroup
Truth table33%1/3
A and B
A= defined $currentGroupB= @$currentGroup
ABresult
0X0
100
111
361 6 return upgrade( $data->{expected} )->validate( $newArray, $self );
362 11 };
363  
364 =head3 indexes_of
365  
366 pArray->indexes_of('a', [0,2])->ok(['a','b','a']);
367  
368 Finds the indexes which match the first argument, and compares that list as an arrayref with the second list.
369  
370 =cut
371  
372 3 ▶sub indexes_of {
Subroutine indexes_of: called CC=1 SCAR=0.0
Pod documented
373 3 my ( $self, $match, $expected, $reason ) = @_;
374 3 $self->add_test(
375 'indexes_of',
376 {
377 match => $match,
378 expected => $expected
379 },
380 $reason
381 );
382 }
383  
384 3 ▶define_test indexes_of => sub {
Subroutine _TEST_indexes_of: called CC=4 SCAR=13.9
385 3 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
386 3 my $indexes = [];
387 3 for my $i ( 0 .. $#{ $self->subject } ) {
388 7 ▶ push @$indexes, $i if upgrade( $data->{match} )->validate( $self->subject->[$i], $self->subtest( status_message => "Testing index $i" ) );
Branch: 1 branch
BranchTrueFalseTotal
if upgrade($data->{"match"})->validate($self->subject->[$i], $self->subtest("status_message", "Testing index $i")) 3 4 7
389 }
390 3 my $result = upgrade( $data->{expected} )->validate( $indexes, $self->subtest( status_message => 'Checking indexes against expected list' ) );
391 3 ▶ return $self->pass if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 2 1 3
392 1 return $self->fail;
393 11 };
394  
395 =head3 array_eq
396  
397 pArray->array_eq(['a','b'])->ok(['a','b']);
398  
399 Compares the elements of the test subject with the elements of the first argument, using the C<upgrade> feature.
400  
401 =cut
402  
403 114 ▶sub array_eq {
Subroutine array_eq: called CC=1 SCAR=0.0
Pod documented
404 114 my ( $self, $expected, $reason ) = @_;
405 114 $self->add_test( 'array_eq', { expected => $expected }, $reason );
406 }
407  
408 111 ▶define_test array_eq => sub {
Subroutine _TEST_array_eq: called CC=3 SCAR=11.0
409 111 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
410 111 my $length = scalar @{ $data->{expected} };
411 111 my $length_result = Test::Proto::ArrayRef->new()->count_items($length)->validate( $self->subject, $self->subtest );
412 111 ▶ if ($length_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length_result) 99 12 111
413 99 foreach my $i ( 0 .. ( $length - 1 ) ) {
414  
415 #upgrade($data->{expected}->[$i])->validate($self->subject->[$i], $self);
416 192 Test::Proto::ArrayRef->new()->nth( $i, $data->{expected}->[$i] )->validate( $self->subject, $self->subtest );
417 }
418 }
419 111 $self->done;
420 11 };
421  
422 =head3 range
423  
424 pArray->range('1,3..4',[9,7,6,5])->ok([10..1]);
425  
426 Finds the range specified in the first element, and compares them to the second element.
427  
428 =cut
429  
430 16 ▶sub range {
Subroutine range: called CC=1 SCAR=0.0
Pod documented
431 16 my ( $self, $range, $expected, $reason ) = @_;
432 16 $self->add_test(
433 'range',
434 {
435 range => $range,
436 expected => $expected
437 },
438 $reason
439 );
440 }
441  
442 16 ▶define_test range => sub {
Subroutine _TEST_range: called CC=4 SCAR=13.9
443 16 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
444 16 my $range = $data->{range};
445 16 my $result = [];
446 16 my $length = scalar @{ $self->subject };
447 16 $range =~ s/-(\d+)/$length - $1/ge;
448 0 $range =~ s/\.\.$/'..' . ($length - 1)/e;
449 16 $range =~ s/^\.\./0../;
450 16 ▶ return $self->exception('Invalid range specified') unless $range =~ m/^(?:\d+|\d+..\d+)(?:,(\d+|\d+..\d+))*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $range =~ /^(?:\d+|\d+..\d+)(?:,(\d+|\d+..\d+))*$/ 2 14 16
451 14 my @range = eval("($range)"); # surely there is a better way?
452  
453 14 foreach my $i (@range) {
454 40 ▶ return $self->fail("Element $i does not exist") unless exists $self->subject->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->subject->[$i] 1 39 40
455 39 push( @$result, $self->subject->[$i] );
456 }
457 13 return upgrade( $data->{expected} )->validate( $result, $self );
458 11 };
459  
460 =head3 reverse
461  
462 pArray->reverse([10..1])->ok([1..10]);
463  
464 Reverses the order of elements and compares the result to the prototype given.
465  
466 =cut
467  
468 2 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
469 2 my ( $self, $expected, $reason ) = @_;
470 2 $self->add_test( 'reverse', { expected => $expected }, $reason );
471 }
472  
473 2 ▶define_test reverse => sub {
Subroutine _TEST_reverse: called CC=1 SCAR=0.0
474 2 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
475 2 my $reversed = [ CORE::reverse @{ $self->subject } ];
476 2 return upgrade( $data->{expected} )->validate( $reversed, $self );
477 11 };
478  
479 =head3 array_before
480  
481 pArray->array_before('b',['a'])->ok(['a','b']); # passes
482  
483 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the second argument is validated against a new arrayref containing all the preceding members of the array.
484  
485 =cut
486  
487 4 ▶sub array_before {
Subroutine array_before: called CC=1 SCAR=0.0
Pod documented
488 4 my ( $self, $match, $expected, $reason ) = @_;
489 4 $self->add_test(
490 'array_before',
491 {
492 match => $match,
493 expected => $expected
494 },
495 $reason
496 );
497 }
498  
499 =head3 array_before_inclusive
500  
501 pArray->array_before_inclusive('b',['a', 'b'])->ok(['a','b', 'c']); # passes
502  
503 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the second argument is validated against a new arrayref containing all the preceding members of the array, plus the element matched.
504  
505 =cut
506  
507 4 ▶sub array_before_inclusive {
Subroutine array_before_inclusive: called CC=1 SCAR=0.0
Pod documented
508 4 my ( $self, $match, $expected, $reason ) = @_;
509 4 $self->add_test(
510 'array_before',
511 {
512 match => $match,
513 expected => $expected,
514 include_self => 1
515 },
516 $reason
517 );
518 }
519  
520 8 ▶define_test 'array_before' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
521 8 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
522 8 my $i = 0;
523 8 foreach my $single_subject ( @{ $self->subject } ) {
524 22 ▶ if ( upgrade( $data->{match} )->validate($single_subject) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (upgrade($data->{"match"})->validate($single_subject)) 6 16 22
525  
526 # $self->add_info("Item $i matched")
527 6 my $before = [ @{ $self->subject }[ 0 .. $i ] ];
528 6 ▶ pop @$before unless $data->{include_self};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"include_self"} 3 3 6
529 6 return upgrade( $data->{expected} )->validate( $before, $self );
530 }
531 16 $i++;
532 }
533 2 return $self->fail('None matched');
534 11 };
535  
536 =head3 array_after
537  
538 pArray->array_after('a',['b'])->ok(['a','b']); # passes
539  
540 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the second argument is validated against a new arrayref containing all the following members of the array.
541  
542 =cut
543  
544 4 ▶sub array_after {
Subroutine array_after: called CC=1 SCAR=0.0
Pod documented
545 4 my ( $self, $match, $expected, $reason ) = @_;
546 4 $self->add_test(
547 'array_after',
548 {
549 match => $match,
550 expected => $expected
551 },
552 $reason
553 );
554 }
555  
556 =head3 array_after_inclusive
557  
558 pArray->array_after_inclusive('b',['b','c'])->ok(['a','b','c']); # passes
559  
560 Applies the first argument (a prototype) onto each member of the array; if any member returns true, the second argument is validated against a new arrayref containing the element matched, plus all the following members of the array.
561  
562 =cut
563  
564 4 ▶sub array_after_inclusive {
Subroutine array_after_inclusive: called CC=1 SCAR=0.0
Pod documented
565 4 my ( $self, $match, $expected, $reason ) = @_;
566 4 $self->add_test(
567 'array_after',
568 {
569 match => $match,
570 expected => $expected,
571 include_self => 1
572 },
573 $reason
574 );
575 }
576  
577 8 ▶define_test 'array_after' => sub {
Subroutine _TEST_array_after: called CC=4 SCAR=13.9
578 8 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
579 8 my $i = 0;
580 8 foreach my $single_subject ( @{ $self->subject } ) {
581 26 ▶ if ( upgrade( $data->{match} )->validate($single_subject) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (upgrade($data->{"match"})->validate($single_subject)) 6 20 26
582  
583 # $self->add_info("Item $i matched")
584 6 my $last_index = $#{ $self->subject };
585 6 my $after = [ @{ $self->subject }[ $i .. $last_index ] ];
586 6 ▶ shift @$after unless $data->{include_self};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"include_self"} 3 3 6
587 6 return upgrade( $data->{expected} )->validate( $after, $self );
588 }
589 20 $i++;
590 }
591 2 return $self->fail('None matched');
592 11 };
593  
594 =head3 sorted
595  
596 pArray->sorted(['a','c','e'])->ok(['a','e','c']); # passes
597 pArray->sorted([2,10,11], cNumeric)->ok([11,2,10]); # passes
598  
599 This will sort the subject and compare the result against the protoype.
600  
601 =cut
602  
603 6 ▶sub sorted {
Subroutine sorted: called CC=1 SCAR=0.0
Pod documented
604 6 my ( $self, $expected, $compare, $reason ) = @_;
605 6 $self->add_test(
606 'sorted',
607 {
608 compare => $compare,
609 expected => $expected
610 },
611 $reason
612 );
613 }
614  
615 6 ▶define_test 'sorted' => sub {
Subroutine _TEST_sorted: called CC=1 SCAR=0.0
616 6 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
617 6 my $compare = upgrade_comparison( $data->{compare} );
618  
619 #my $got = [sort { $compare->($a, $b) } @{$self->subject}];
620 6 my $got = [ sort { $compare->compare( $a, $b ) } @{ $self->subject } ];
621  
622 6 return upgrade( $data->{expected} )->validate( $got, $self );
623 11 };
624  
625 =head3 ascending
626  
627 pArray->ascending->ok(['a','c','e']); # passes
628 pArray->ascending->ok(['a','c','c','e']); # passes
629 pArray->ascending(cNumeric)->ok([2,10,11]); # passes
630  
631 This will return true if the elements are already in ascending order. Elements which compare as equal as the previous element are permitted.
632  
633 =cut
634  
635 4 ▶sub ascending {
Subroutine ascending: called CC=1 SCAR=0.0
Pod documented
636 4 my ( $self, $compare, $reason ) = @_;
637 4 $self->add_test(
638 'in_order',
639 {
640 compare => $compare,
641 dir => 'ascending'
642 },
643 $reason
644 );
645 }
646  
647 =head3 descending
648  
649 pArray->descending->ok(['e','c','a']); # passes
650 pArray->descending->ok(['e','c','c','a']); # passes
651 pArray->descending(cNumeric)->ok([11,10,2]); # passes
652  
653 This will return true if the elements are already in descending order. Elements which compare as equal as the previous element are permitted.
654  
655 =cut
656  
657 5 ▶sub descending {
Subroutine descending: called CC=1 SCAR=0.0
Pod documented
658 5 my ( $self, $compare, $reason ) = @_;
659 5 $self->add_test(
660 'in_order',
661 {
662 compare => $compare,
663 dir => 'descending'
664 },
665 $reason
666 );
667 }
668  
669 9 ▶define_test 'in_order' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
670 9 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
671 9 ▶ return $self->pass('Empty array is ascending by definition') if $#{ $self->subject } == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == -1 4 5 9
672 5 ▶ return $self->pass('Single-item array is ascending by definition') if $#{ $self->subject } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == 0 0 5 5
673 5 ▶ my $dir = defined $data->{dir} ? $data->{dir} : 'ascending';
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'dir'} ? : 5 0 5
674 5 my $compare = upgrade_comparison( $data->{compare} );
675 5 my @range = 0 .. $#{ $self->subject };
676 5 ▶ @range = CORE::reverse(@range) if $dir eq 'descending';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "descending" 3 2 5
677 5 my $prev = shift @range;
678  
679 5 for my $i (@range) {
680 9 $self->subtest->diag("Comparing items $prev and $i");
681 9 my $result = $compare->le( $self->subject->[$prev], $self->subject->[$i] );
682 9 ▶ return $self->fail("Item $prev > item $i") unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 2 7 9
683 7 $prev = $i;
684 }
685 3 return $self->pass;
686 11 };
687  
688 =head3 array_max
689  
690 pArray->array_max('e')->ok(['a','e','c']); # passes
691 pArray->array_max(p->num_gt(10), cNumeric)->ok(['2','11','10']); # passes
692  
693 This will find the maximum value using the optional comparator in the second argument, and check it against the first argument.
694  
695 =cut
696  
697 11 ▶sub array_max {
Subroutine array_max: called CC=1 SCAR=0.0
Pod documented
698 11 my ( $self, $expected, $compare, $reason ) = @_;
699 11 $self->add_test(
700 'array_best',
701 {
702 expected => $expected,
703 must_match => 'any',
704 compare => $compare,
705 dir => 'max'
706 },
707 $reason
708 );
709 }
710  
711 =head3 array_min
712  
713 pArray->array_min('a')->ok(['a','e','c']); # passes
714 pArray->array_min(p->num_lt(10), cNumeric)->ok(['2','11','10']); # passes
715  
716 This will find the minimum value using the optional comparator in the second argument, and check it against the first argument.
717  
718 =cut
719  
720 11 ▶sub array_min {
Subroutine array_min: called CC=1 SCAR=0.0
Pod documented
721 11 my ( $self, $expected, $compare, $reason ) = @_;
722 11 $self->add_test(
723 'array_best',
724 {
725 expected => $expected,
726 must_match => 'any',
727 compare => $compare,
728 dir => 'min'
729 },
730 $reason
731 );
732 }
733  
734 =head3 array_index_of_max
735  
736 pArray->array_index_of_max(1)->ok(['a','e','c']); # passes
737 pArray->array_index_of_max(1, cNumeric)->ok(['2','11','10']); # passes
738  
739 This will find the index of the maximum value using the optional comparator in the second argument, and check it against the first argument.
740  
741 =cut
742  
743 11 ▶sub array_index_of_max {
Subroutine array_index_of_max: called CC=1 SCAR=0.0
Pod documented
744 11 my ( $self, $expected, $compare, $reason ) = @_;
745 11 $self->add_test(
746 'array_best',
747 {
748 expected => $expected,
749 must_match => 'any index',
750 compare => $compare,
751 dir => 'max'
752 },
753 $reason
754 );
755 }
756  
757 =head3 array_index_of_min
758  
759 pArray->array_index_of_min(0)->ok(['a','e','c']); # passes
760 pArray->array_index_of_min(0, cNumeric)->ok(['2','11','10']); # passes
761  
762 This will find the index of the minimum value using the optional comparator in the second argument, and check it against the first argument.
763  
764 =cut
765  
766 11 ▶sub array_index_of_min {
Subroutine array_index_of_min: called CC=1 SCAR=0.0
Pod documented
767 11 my ( $self, $expected, $compare, $reason ) = @_;
768 11 $self->add_test(
769 'array_best',
770 {
771 expected => $expected,
772 must_match => 'any index',
773 compare => $compare,
774 dir => 'min'
775 },
776 $reason
777 );
778 }
779  
780 44 ▶define_test 'array_best' => sub {
Subroutine _TEST_array_best: called CC=10 SCAR=23.1
781 44 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
782 44 my $i = 0;
783 44 ▶ return $self->fail('Empty array has no max by definition') if $#{ $self->subject } == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == -1 8 36 44
784 36 my $compare = upgrade_comparison( $data->{compare} );
785 36 ▶ my $better = ( defined $data->{dir} and $data->{dir} eq 'min' ? sub { shift() > 0 } : sub { shift() < 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$data->{'dir'} eq 'min' ? : 18 18 36
Condition33%1/3
defined $data->{'dir'} && ($data->{'dir'} eq 'min' ? sub { shift() > 0; } : sub { shift() < 0; } )
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $data->{'dir'} && ($data->{'dir'} eq 'min' ? sub { shift() > 0; } : sub { shift() < 0; } )
defined $data->{'dir'} ($data->{'dir'} eq 'min' ? sub { shift() > 0; } : sub { shift() < 0; } )
Truth table33%1/3
A && B
A= defined $data->{'dir'}B= ($data->{'dir'} eq 'min' ? sub { shift() > 0; } : sub { shift() < 0; } )
ABresult
0X0
100
111
786 36 my $best = [ $self->subject->[0] ];
787 36 my $best_idx = [0];
788 36 foreach my $single_subject ( @{ $self->subject } ) {
789  
790 120 ▶ if ( $i != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 84 36 120
791 84 my $cmp_result = $compare->compare( $best->[0], $single_subject );
792 84 ▶ if ( $better->($cmp_result) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$better($cmp_result)) { } 48 36 84
elsif ($cmp_result == 0) { } 12 24 36
793 48 $best = [$single_subject];
794 48 $best_idx = [$i];
795 }
796 elsif ( $cmp_result == 0 ) {
797 12 push @$best, $single_subject;
798 12 push @$best_idx, $i;
799 }
800 }
801 120 $i++;
802 }
803 36 my $got = $best;
804 36 ▶ $got = $best_idx if $data->{must_match} =~ 'index';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"must_match"} =~ /index/ 18 18 36
805 36 ▶ if ( $data->{must_match} =~ 'any' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'must_match'} =~ /any/) { } 36 0 36
806 36 return Test::Proto::ArrayRef->new()->array_any( $data->{expected} )->validate( $got, $self );
807 }
808 else {
809 0 return upgrade( $data->{expected} )->validate( $got, $self );
810 }
811 11 };
812  
813 =head3 array_all_unique
814  
815 pArray->array_all_unique->ok(['a','b','c']); # passes
816 pArray->array_all_unique(cNumeric)->ok(['0','0e0','0.0']); # fails
817  
818 This will pass if all of the members of the array are unique, using the comparison provided (or cmp).
819  
820 =cut
821  
822 4 ▶sub array_all_unique {
Subroutine array_all_unique: called CC=1 SCAR=0.0
Pod documented
823 4 my ( $self, $compare, $reason ) = @_;
824 4 $self->add_test( 'array_all_unique', { compare => $compare }, $reason );
825 }
826  
827 4 ▶define_test 'array_all_unique' => sub {
Subroutine _TEST_array_all_unique: called CC=6 SCAR=17.9
828 4 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
829 4 my $i = 0;
830 4 my $compare = upgrade_comparison( $data->{compare} );
831 4 ▶ return $self->pass('Empty array unique by definition') if $#{ $self->subject } == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == -1 1 3 4
832 3 ▶ return $self->pass('Array with one element unique by definition') if $#{ $self->subject } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == 0 1 2 3
833 2 foreach my $single_subject ( @{ $self->subject } ) {
834 6 ▶ if ( $i != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 4 2 6
835 4 ▶ return $self->fail("Item $i matches item 0") if $compare->eq( $self->subject->[0], $single_subject );
Branch: 1 branch
BranchTrueFalseTotal
if $compare->eq($self->subject->[0], $single_subject) 1 3 4
836 }
837 5 $i++;
838 }
839 1 return $self->pass('All unique');
840 11 };
841  
842 =head3 array_all_same
843  
844 pArray->array_all_same->ok(['a','a']); # passes
845 pArray->array_all_same(cNumeric)->ok(['0','0e0','0.0']); # passes
846 pArray->array_all_same->ok(['0','0e0','0.0']); # fails
847  
848 This will pass if all of the members of the array are the same, using the comparison provided (or cmp).
849  
850 =cut
851  
852 4 ▶sub array_all_same {
Subroutine array_all_same: called CC=1 SCAR=0.0
Pod documented
853 4 my ( $self, $compare, $reason ) = @_;
854 4 $self->add_test( 'array_all_same', { compare => $compare }, $reason );
855 }
856  
857 4 ▶define_test 'array_all_same' => sub {
Subroutine _TEST_array_all_same: called CC=6 SCAR=17.9
858 4 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
859 4 my $i = 0;
860 4 my $compare = upgrade_comparison( $data->{compare} );
861 4 ▶ return $self->pass('Empty array all same by definition') if $#{ $self->subject } == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == -1 1 3 4
862 3 ▶ return $self->pass('Array with one element all same by definition') if $#{ $self->subject } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$self->subject;} == 0 1 2 3
863 2 foreach my $single_subject ( @{ $self->subject } ) {
864 6 ▶ if ( $i != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 4 2 6
865 4 ▶ return $self->fail("Item $i does not match item 0") if $compare->ne( $self->subject->[0], $single_subject );
Branch: 1 branch
BranchTrueFalseTotal
if $compare->ne($self->subject->[0], $single_subject) 1 3 4
866 }
867 5 $i++;
868 }
869 1 return $self->pass('All the same');
870 11 };
871  
872 =head2 Unordered Comparisons
873  
874 These methods are useful for when you know what the array should contain but do not know what order the elements are in, for example when testing the keys of a hash.
875  
876 The principle is similar to the C<set> and C<bag> tests documented L<List::Util>, but does not use the same implementation and does not suffer from the known bug documented there.
877  
878 =cut
879  
880 =head3 set_of
881  
882 pArray->set_of(['a','b','c'])->ok(['a','c','a','b']); # passes
883  
884 Checks that all of the elements in the test subject match at least one element in the first argument, and vice versa. Members of the test subject may be 'reused'.
885  
886 =cut
887  
888 8 ▶sub set_of {
Subroutine set_of: called CC=1 SCAR=0.0
Pod documented
889 8 my ( $self, $expected, $reason ) = @_;
890 8 $self->add_test(
891 'unordered_comparison',
892 {
893 expected => $expected,
894 method => 'set'
895 },
896 $reason
897 );
898 }
899  
900 =head3 bag_of
901  
902 pArray->bag_of(['a','b','c'])->ok(['c','a','b']); # passes
903  
904 Checks that all of the elements in the test subject match at least one element in the first argument, and vice versa. Members may B<not> be 'reused'.
905  
906 =cut
907  
908 11 ▶sub bag_of {
Subroutine bag_of: called CC=1 SCAR=0.0
Pod documented
909 11 my ( $self, $expected, $reason ) = @_;
910 11 $self->add_test(
911 'unordered_comparison',
912 {
913 expected => $expected,
914 method => 'bag'
915 },
916 $reason
917 );
918 }
919  
920 =head3 subset_of
921  
922 pArray->subset_of(['a','b','c'])->ok(['a','a','b']); # passes
923  
924 Checks that all of the elements in the test subject match at least one element in the first argument. Members of the test subject may be 'reused'.
925  
926 =cut
927  
928 8 ▶sub subset_of {
Subroutine subset_of: called CC=1 SCAR=0.0
Pod documented
929 8 my ( $self, $expected, $reason ) = @_;
930 8 $self->add_test(
931 'unordered_comparison',
932 {
933 expected => $expected,
934 method => 'subset'
935 },
936 $reason
937 );
938 }
939  
940 =head3 superset_of
941  
942 pArray->superset_of(['a','b','a'])->ok(['a','b','c']); # passes
943  
944 Checks that all of the elements in the first argument can validate at least one element in the test subject. Members of the test subject may be 'reused'.
945  
946 =cut
947  
948 8 ▶sub superset_of {
Subroutine superset_of: called CC=1 SCAR=0.0
Pod documented
949 8 my ( $self, $expected, $reason ) = @_;
950 8 $self->add_test(
951 'unordered_comparison',
952 {
953 expected => $expected,
954 method => 'superset'
955 },
956 $reason
957 );
958 }
959  
960 =head3 subbag_of
961  
962 pArray->subbag_of(['a','b','c'])->ok(['a','b']); # passes
963  
964 Checks that all of the elements in the test subject match at least one element in the first argument. Members of the test subject may B<not> be 'reused'.
965  
966 =cut
967  
968 8 ▶sub subbag_of {
Subroutine subbag_of: called CC=1 SCAR=0.0
Pod documented
969 8 my ( $self, $expected, $reason ) = @_;
970 8 $self->add_test(
971 'unordered_comparison',
972 {
973 expected => $expected,
974 method => 'subbag'
975 },
976 $reason
977 );
978 }
979  
980 =head3 superbag_of
981  
982 pArray->superbag_of(['a','b'])->ok(['a','b','c']); # passes
983  
984 Checks that all of the elements in the first argument can validate at least one element in the test subject. Members of the test subject may B<not> be 'reused'.
985  
986 =cut
987  
988 8 ▶sub superbag_of {
Subroutine superbag_of: called CC=1 SCAR=0.0
Pod documented
989 8 my ( $self, $expected, $reason ) = @_;
990 8 $self->add_test(
991 'unordered_comparison',
992 {
993 expected => $expected,
994 method => 'superbag'
995 },
996 $reason
997 );
998 }
999  
1000 11 my $machine;
1001 51 ▶define_test 'unordered_comparison' => sub {
Subroutine _TEST_unordered_comparison: called CC=1 SCAR=0.0
1002 51 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
1003 51 return $machine->( $self, $data->{method}, $self->subject, $data->{expected} );
1004 11 };
1005  
1006 11 my ( $allocate_l, $allocate_r );
1007 813 ▶$allocate_l = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1008 813 my ( $matrix, $pairs, $bag ) = @_;
1009 813 my $best = $pairs;
1010 813 LEFT: foreach my $l ( 0 .. $#{$matrix} ) {
1011 2280 ▶ next LEFT if grep { $_->[0] == $l } @$pairs; # skip if already allocated
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->[0] == $l;} @$pairs 1674 606 2280
1012 606 RIGHT: foreach my $r ( 0 .. $#{ $matrix->[$l] } ) {
1013 1590 ▶ next RIGHT if $bag and grep { $_->[1] == $r } @$pairs; # skip if already allocated and bag logic
Branch: 1 branch
BranchTrueFalseTotal
if $bag and grep {$_->[1] == $r;} @$pairs 318 1272 1590
Condition100%3/3
$bag and grep {$_->[1] == $r;} @$pairs
!ll&&!rl&&r
843429318
MC/DC100%2/2
$bag and grep {$_->[1] == $r;} @$pairs
$bag grep {$_->[1] == $r;} @$pairs
Truth table100%3/3
A and B
A= $bagB= grep {$_->[1] == $r;} @$pairs
ABresult
0X0
100
111
1014 1272 ▶ if ( $matrix->[$l]->[$r] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->[$l][$r]) 762 510 1272
1015 762 my $result = $allocate_l->( $matrix, [ @$pairs, [ $l, $r ] ], $bag );
1016 762 ▶ $best = $result if ( @$result > @$best );
Branch: 1 branch
BranchTrueFalseTotal
if @$result > @$best 399 363 762
1017  
1018 # short circuit if length of Best == length of matrix ?
1019 }
1020 }
1021 }
1022 813 return $best;
1023 11 };
1024 747 ▶$allocate_r = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1025 747 my ( $matrix, $pairs, $bag ) = @_;
1026 747 my $best = $pairs;
1027 747 RIGHT: foreach my $r ( 0 .. $#{ $matrix->[0] } ) {
1028 2070 ▶ next RIGHT if grep { $_->[1] == $r } @$pairs; # skip if already allocated
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->[1] == $r;} @$pairs 1488 582 2070
1029 582 LEFT: foreach my $l ( 0 .. $#{$matrix} ) {
1030 1560 ▶ next LEFT if $bag and grep { $_->[0] == $l } @$pairs; # skip if already allocated and bag logic
Branch: 1 branch
BranchTrueFalseTotal
if $bag and grep {$_->[0] == $l;} @$pairs 312 1248 1560
Condition100%3/3
$bag and grep {$_->[0] == $l;} @$pairs
!ll&&!rl&&r
819429312
MC/DC100%2/2
$bag and grep {$_->[0] == $l;} @$pairs
$bag grep {$_->[0] == $l;} @$pairs
Truth table100%3/3
A and B
A= $bagB= grep {$_->[0] == $l;} @$pairs
ABresult
0X0
100
111
1031 1248 ▶ if ( $matrix->[$l]->[$r] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->[$l][$r]) 696 552 1248
1032 696 my $result = $allocate_r->( $matrix, [ @$pairs, [ $l, $r ] ], $bag );
1033 696 ▶ $best = $result if ( @$result > @$best );
Branch: 1 branch
BranchTrueFalseTotal
if @$result > @$best 387 309 696
1034 }
1035 }
1036 }
1037 747 return $best;
1038 11 };
1039 51 ▶$machine = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
1040 51 my ( $runner, $method, $left, $right ) = @_;
1041 51 my $bag = ( $method =~ /bag$/ );
1042 51 my $matrix = [];
1043 51 my $super = ( $method =~ m/^super/ );
1044  
1045 # prepare the results matrix
1046 51 LEFT: foreach my $l ( 0 .. $#{$left} ) {
1047 126 RIGHT: foreach my $r ( 0 .. $#{$right} ) {
1048 300 my $result = upgrade( $right->[$r] )->validate( $left->[$l], ); #$runner->subtest("Comparing subject->[$l] and expected->[$r]"));
1049 300 $matrix->[$l]->[$r] = $result;
1050 }
1051 }
1052 51 my $pairs = [];
1053  
1054 51 my $allocation_l = $allocate_l->( $matrix, $pairs, $bag );
1055 51 my $allocation_r = $allocate_r->( $matrix, $pairs, $bag );
1056  
1057 51 ▶ if ( $method =~ m/^(sub|)(bag|set)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(sub|)(bag|set)$/) 35 16 51
1058 35 foreach my $l ( 0 .. $#{$left} ) {
1059 84 ▶ unless ( grep { $_->[0] == $l } @$allocation_l ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->[0] == $l;} @$allocation_l) 8 76 84
1060 8 ▶ return $runner->fail('Not a superbag') if $bag;
Branch: 1 branch
BranchTrueFalseTotal
if $bag 6 2 8
1061 2 return $runner->fail('Not a superset');
1062 }
1063  
1064 }
1065 }
1066 43 ▶ if ( $method =~ m/^(super|)(bag|set)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(super|)(bag|set)$/) 31 12 43
1067 31 foreach my $r ( 0 .. $#{$right} ) {
1068 73 ▶ unless ( grep { $_->[1] == $r } @$allocation_r ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->[1] == $r;} @$allocation_r) 6 67 73
1069 6 ▶ return $runner->fail('Not a superbag') if $bag;
Branch: 1 branch
BranchTrueFalseTotal
if $bag 4 2 6
1070 2 return $runner->fail('Not a superset');
1071 }
1072 }
1073 }
1074 37 return $runner->pass("Successful");
1075 11 };
1076  
1077 =head2 Series Validation
1078  
1079 Sometimes you need to check an array matches a certain complex 'pattern' including multiple units of variable length, like in a regular expression or an XML DTD or Schema. Using L<Test::Proto::Series>, L<Test::Proto::Repeatable>, and L<Test::Proto::Alternation>, you can describe these units, and the methods below can be used to iterate over such a structure.
1080  
1081 =cut
1082  
1083 #~ Series handling
1084  
1085 =head3 contains_only
1086  
1087 pArray->contains_only(pSeries(pRepeatable(pAlternation('a', 'b'))->max(5)))->ok(['a','a','a']); # passes
1088  
1089 This passes if the series expected matches exactly the test subject, i.e. the series can legally stop at the point where the subject ends.
1090  
1091 =cut
1092  
1093 11 my ( $bt_core, $bt_advance, $bt_eval_step, $bt_backtrack, $bt_backtrack_to );
1094  
1095 28 ▶sub contains_only {
Subroutine contains_only: called CC=1 SCAR=0.0
Pod documented
1096 28 my ( $self, $expected, $reason ) = @_;
1097 28 $self->add_test( 'contains_only', { expected => $expected }, $reason );
1098 }
1099  
1100 28 ▶define_test 'contains_only' => sub {
Subroutine _TEST_contains_only: called CC=1 SCAR=0.0
1101 28 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
1102 28 return $bt_core->( $self, $self->subject, $data->{expected} );
1103 11 };
1104  
1105 =head3 begins_with
1106  
1107 pArray->begins_with(pSeries('a','a',pRepeatable('a')->max(2)))->ok(['a','a','a']); # passes
1108  
1109 This passes if the full value of the series expected matches the test subject with some elements of the test subject optionally left over at the end.
1110  
1111 =cut
1112  
1113 28 ▶sub begins_with {
Subroutine begins_with: called CC=1 SCAR=0.0
Pod documented
1114 28 my ( $self, $expected, $reason ) = @_;
1115 28 $self->add_test( 'begins_with', { expected => $expected }, $reason );
1116 }
1117  
1118 28 ▶define_test 'begins_with' => sub {
Subroutine _TEST_begins_with: called CC=3 SCAR=11.0
1119 28 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
1120 28 for my $i ( 0 .. $#{ $self->subject } ) {
1121 44 my $subset = [ @{ $self->subject }[ 0 .. $i ] ];
1122 44 ▶ return $self->pass("Succeeded with 0..$i") if $bt_core->( $self->subtest( subject => $subset ), $subset, $data->{expected} );
Branch: 1 branch
BranchTrueFalseTotal
if &$bt_core($self->subtest("subject", $subset), $subset, $data->{"expected"}) 21 23 44
1123 }
1124 7 return $self->fail("No subsets passed");
1125 11 };
1126  
1127 =head3 ends_with
1128  
1129 pArray->ends_with(pSeries('b','c')->ok(['a','b','c']); # passes
1130  
1131 This passes if the full value of the series expected matches the final items of the test subject with some elements of the test subject optionally preceding.
1132  
1133 =cut
1134  
1135 28 ▶sub ends_with {
Subroutine ends_with: called CC=1 SCAR=0.0
Pod documented
1136 28 my ( $self, $expected, $reason ) = @_;
1137 28 $self->add_test( 'ends_with', { expected => $expected }, $reason );
1138 }
1139  
1140 28 ▶define_test 'ends_with' => sub {
Subroutine _TEST_ends_with: called CC=3 SCAR=11.0
1141 28 my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype
1142 28 for my $i ( CORE::reverse( 0 .. $#{ $self->subject } ) ) {
1143 44 my $subset = [ @{ $self->subject }[ $i .. $#{ $self->subject } ] ];
1144 44 ▶ return $self->pass( "Succeeded with " . $i . ".." . $#{ $self->subject } ) if $bt_core->( $self->subtest( subject => $subset ), $subset, $data->{expected} );
Branch: 1 branch
BranchTrueFalseTotal
if &$bt_core($self->subtest("subject", $subset), $subset, $data->{"expected"}) 19 25 44
1145 }
1146 9 return $self->fail("No subsets passed");
1147 11 };
1148  
1149 #~ How the backtracker works
1150 #~
1151 #~ 1. To advance a step
1152 #~
1153 #~ Find the most recent incomplete SERIES
1154 #~
1155 #~ Get its next element.
1156 #~
1157 #~ 2. To get the next alternative (backreack)
1158 #~
1159 #~ Find the most recent VARIABLE_UNIT
1160 #~
1161 #~ If a repeatable, decrease it (they begin greedy)
1162 #~
1163 #~ If an alternation, try the next alternative,
1164 #~
1165 #~ If either of those cannot legally be done, it's no longer a variable unit so keep looking
1166 #~
1167 #~ When you run out of history, fail
1168 #~
1169 #~
1170 #~ So the backtracker should do the following:
1171 #~
1172 #~
1173 #~ backtracker (runner r, subject s, expected e, history h)
1174 #~ loop
1175 #~ next_step = advance (r, s, e, h)
1176 #~ if no next_step
1177 #~ return r->pass if index of last h is length of s
1178 #~ push next step onto history
1179 #~ result = evaluate
1180 #~ if result is not ok
1181 #~ next_solution = backtrack (r, s, e, h) # modifies h
1182 #~ if no next_solution
1183 #~ return r->fail
1184 #~ # implicit else continue and redo the loop
1185 #~
1186 #~
1187 116 ▶$bt_core = sub {
Subroutine __ANON__: called CC=10 SCAR=23.3
1188 116 my ( $runner, $subject, $expected, $history, $options ) = @_;
1189 116 ▶ $history = [] unless defined $history; #:5.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $history 116 0 116
1190 116 while (1) { #~ yeah, scary, I know, but better than diving headlong into recursion
1191  
1192 #~ Advance
1193 747 my $next_step = $bt_advance->( $runner, $subject, $expected, $history );
1194  
1195 #~ If we cannot advance, then pass if what we've matched so far meets the criteria
1196 747 ▶ unless ( defined $next_step ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next_step) 73 674 747
1197 return $runner->pass
1198 if (
1199 0 ( !@{$history} and !@{$subject} ) # this oughtn't to happen
1200 73 ▶ or ( $history->[-1]->{index} == $#$subject )
Branch: 1 branch
BranchTrueFalseTotal
if not @{$history;} || @{$subject;} or $history->[-1]{"index"} == $subject->$#* 57 16 73
Condition50%3/6
not @{$history;} || @{$subject;} or $history->[-1]{"index"} == $subject->$#*
l!l&&r!l&&!r
05716
@{$history;} || @{$subject;}
l!l&&r!l&&!r
7300
MC/DC33%1/3
not @{$history;} || @{$subject;} or $history->[-1]{"index"} == $subject->$#*
@{$history;} @{$subject;} $history->[-1]{"index"} == $subject->$#*
Truth table40%2/5
not(A || B) or C
A= @{$history;}B= @{$subject;}C= $history->[-1]{"index"} == $subject->$#*
ABCresult
00X1
1X11
0111
1X00
0100
1201 );
1202  
1203 #return $runner->fail('No next step; index reached: '.$history->[-1]->{index});
1204 16 $runner->subtest()->diag('No next step');
1205 }
1206  
1207 #~ Add the next step to the history
1208 690 ▶ push @$history, $next_step if defined $next_step;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_step 674 16 690
1209  
1210 #~ Determine if the next step can be executed
1211 690 ▶ my $evaluation_result =
Branch: 1 branch
BranchTrueFalseTotal
defined $next_step ? : 674 16 690
1212 defined $next_step
1213 ? $bt_eval_step->( $runner, $subject, $expected, $history )
1214 : undef;
1215 690 ▶ unless ($evaluation_result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($evaluation_result) 154 536 690
1216 154 my $next_solution = $bt_backtrack->( $runner, $subject, $expected, $history );
1217 154 ▶ unless ( defined $next_solution ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next_solution) 59 95 154
1218 59 return $runner->fail('No more alternatve solutions');
1219 }
1220 }
1221 }
1222 11 };
1223  
1224 747 ▶$bt_advance = sub {
Subroutine __ANON__: called CC=19 SCAR=29.4
1225  
1226 #~ the purpose of this to find the latest series or repeatble which has not been exhausted.
1227 #~ This method adds items to the end of the history stack, and never removes them.
1228 747 my ( $runner, $subject, $expected, $history ) = @_;
1229 747 my $l = $#$history;
1230 747 $runner->subtest( test_case => $history )->diag( 'Advance ' . $l . '!' );
1231 747 my $next_step;
1232  
1233 #~ todo: check if l == -1
1234 747 ▶ if ( $l == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == -1) 116 631 747
1235 return {
1236 116 self => $expected,
1237 parent => undef,
1238 index => -1,
1239 };
1240 }
1241 631 for my $i ( CORE::reverse( 0 .. $l ) ) {
1242 1172 my $step = $history->[$i];
1243 1172 my $children;
1244 1172 ▶ if ( ( blessed $step->{self} ) and $step->{self}->isa('Test::Proto::Series') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($step->{'self'}) and $step->{'self'}->isa('Test::Proto::Series')) { } 503 669 1172
elsif (builtin::blessed($step->{'self'}) and $step->{'self'}->isa('Test::Proto::Repeatable')) { } 178 491 669
elsif (builtin::blessed($step->{'self'}) and $step->{'self'}->isa('Test::Proto::Alternation')) { } 204 553 757
Condition100%3/3
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Series")
!ll&&!rl&&r
266403503
MC/DC100%2/2
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Series")
builtin::blessed($step->{"self"}) $step->{"self"}->isa("Test::Proto::Series")
Truth table100%3/3
A and B
A= builtin::blessed($step->{"self"})B= $step->{"self"}->isa("Test::Proto::Series")
ABresult
0X0
100
111
1245 503 $children = $step->{children};
1246 503 ▶ $children = [] unless defined $children; #:5.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children 218 285 503
1247 503 my $contents = $step->{self}->contents;
1248 503 ▶ if ( $#$children < $#$contents ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children->$#* < $contents->$#*) 328 175 503
1249  
1250 #~ we conclude the series is not complete. Add a new step.
1251 328 $next_step = {
1252 self => $contents->[ $#$children + 1 ],
1253 parent => $step,
1254 element => $#$children + 1
1255 };
1256 328 weaken $next_step->{parent};
1257 328 push @{ $step->{children} }, ($next_step);
1258 }
1259 }
1260 ▶ elsif ( ( blessed $step->{self} ) and $step->{self}->isa('Test::Proto::Repeatable') ) {
Condition100%3/3
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Repeatable")
!ll&&!rl&&r
266225178
MC/DC100%2/2
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Repeatable")
builtin::blessed($step->{"self"}) $step->{"self"}->isa("Test::Proto::Repeatable")
Truth table100%3/3
A and B
A= builtin::blessed($step->{"self"})B= $step->{"self"}->isa("Test::Proto::Repeatable")
ABresult
0X0
100
111
1261 178 $children = $step->{children};
1262 178 ▶ $children = [] unless defined $children; #:5.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children 34 144 178
1263 178 my $max = $step->{max}; #~ the maximum set by a backtrack action
1264 178 ▶ $max = $step->{self}->max unless defined $max; # the maximum allowed by the repeatable
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 72 106 178
1265 #~ NB: Repeatables are greedy, so go as far as they can unless a backtrack has caused them to try being less greedy.
1266 178 ▶ unless ( ( defined $max ) and ( $#$children + 1 >= $max ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max and $children->$#* + 1 >= $max) 110 68 178
Condition100%3/3
defined $max and $children->$#* + 1 >= $max
!ll&&!rl&&r
525868
MC/DC100%2/2
defined $max and $children->$#* + 1 >= $max
defined $max $children->$#* + 1 >= $max
Truth table100%3/3
A and B
A= defined $maxB= $children->$#* + 1 >= $max
ABresult
0X0
100
111
1267  
1268 #~ we conclude the repeatable is not exhausted. Add a new step.
1269 $next_step = {
1270 self => $step->{self}->contents,
1271 110 parent => $step,
1272 element => $#$children + 1
1273 };
1274 110 weaken $next_step->{parent};
1275 110 push @{ $step->{children} }, $next_step;
1276 110 $step->{max_tried} = $#{ $step->{children} } + 1;
1277 }
1278 }
1279 ▶ elsif ( ( blessed $step->{self} ) and $step->{self}->isa('Test::Proto::Alternation') ) {
Condition100%3/3
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Alternation")
!ll&&!rl&&r
26621204
MC/DC100%2/2
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Alternation")
builtin::blessed($step->{"self"}) $step->{"self"}->isa("Test::Proto::Alternation")
Truth table100%3/3
A and B
A= builtin::blessed($step->{"self"})B= $step->{"self"}->isa("Test::Proto::Alternation")
ABresult
0X0
100
111
1280  
1281 #~ Pick first alternative
1282 204 ▶ unless ( ( defined $step->{children} ) and @{ $step->{children} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $step->{"children"} and @{$step->{"children"};}) 120 84 204
Condition100%3/3
defined $step->{"children"} and @{$step->{"children"};}
!ll&&!rl&&r
724884
MC/DC100%2/2
defined $step->{"children"} and @{$step->{"children"};}
defined $step->{"children"} @{$step->{"children"};}
Truth table100%3/3
A and B
A= defined $step->{"children"}B= @{$step->{"children"};}
ABresult
0X0
100
111
1283 120 my $alt = 0;
1284 120 ▶ $alt = $step->{alt} if defined $step->{alt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $step->{"alt"} 48 72 120
1285 $next_step = {
1286 120 self => $step->{self}->alternatives->[$alt],
1287 parent => $step,
1288 element => 0
1289 };
1290 120 weaken $next_step->{parent};
1291 120 $step->{alt} = $alt;
1292 120 push @{ $step->{children} }, $next_step;
1293 }
1294 }
1295 1172 ▶ if ( defined $next_step ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_step) 558 614 1172
1296 558 return $next_step;
1297 }
1298  
1299 #~ Otherwise, next $i.
1300 }
1301 73 return undef;
1302 11 };
1303  
1304 674 ▶$bt_eval_step = sub {
Subroutine __ANON__: called CC=12 SCAR=25.4
1305  
1306 #~ The purpose of this function is to determine if the current solution can continue at this point.
1307 #~ Specifically, if the current step (i.e. the last in the history) validates against the next item in the subject.
1308 #~ However, if the current step is a series/repeatable/altenration, then this is not an issue.
1309 674 my ( $runner, $subject, $expected, $history ) = @_;
1310 674 my $current_step = $history->[-1];
1311 674 ▶ my $current_index = ( ( exists $history->[1] ) ? ( defined $history->[-2]->{index} ? $history->[-2]->{index} : -1 ) : -1 ); # current_index is what has been completed
Branch: 2 branches
BranchTrueFalseTotal
exists $history->[1] ? : 558 116 674
defined $history->[-2]{'index'} ? : 558 0 558
1312 674 $current_step->{index} = $current_index; #:jic
1313 674 ▶ if ( exists $subject->[ $current_index + 1 ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$subject[$current_index + 1]) { } 604 70 674
1314  
1315 #~ if a series, repeatable, or alternation, we're always ok, we just need to update the index
1316 #~ if a prototype, evaluate it.
1317 604 ▶ if ( ( ref $current_step->{self} ) and ref( $current_step->{self} ) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current_step->{'self'} and ref($current_step->{'self'}) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/) { } 324 280 604
Condition100%3/3
ref $current_step->{"self"} and ref($current_step->{"self"}) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/
!ll&&!rl&&r
26119324
MC/DC100%2/2
ref $current_step->{"self"} and ref($current_step->{"self"}) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/
ref $current_step->{"self"} ref($current_step->{"self"}) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/
Truth table100%3/3
A and B
A= ref $current_step->{"self"}B= ref($current_step->{"self"}) =~ /^Test::Proto::(?:Series|Repeatable|Alternation)$/
ABresult
0X0
100
111
1318 324 $runner->subtest( test_case => $history )->diag( 'Starting a ' . ( ref $current_step->{self} ) );
1319 324 $current_step->{index} = $current_index;
1320 324 return 1; #~ always ok
1321 }
1322 else {
1323 280 my $p = upgrade( $current_step->{self} );
1324 280 $runner->subtest( test_case => $history )->diag( 'Validating index ' . ( $current_index + 1 ) );
1325 280 my $result = $p->validate( $subject->[ $current_index + 1 ], $runner->subtest() );
1326 280 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 206 74 280
1327 206 $current_step->{index} = $current_index + 1;
1328 }
1329 else {
1330 74 $current_step->{index} = $current_index; # shouldn't read this
1331 }
1332 280 return $result;
1333 }
1334 }
1335 else {
1336 #~...
1337 #~ We are allowed only:
1338 #~ - repeatables with zero minimum
1339 #~ - alternations
1340 #~ i.e. no prototypes or series
1341 #~ Todo: check if we're repeating interminably by seeing if any object is its own ancestor
1342 70 $runner->subtest()->diag('Reached end of subject, allowing only potentially empty patterns');
1343 70 ▶ if ( ref( $current_step->{self} ) eq 'Test::Proto::Alternation' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $current_step->{'self'} eq 'Test::Proto::Alternation') { } 0 70 70
elsif (ref $current_step->{'self'} eq 'Test::Proto::Repeatable' and $current_step->{'self'}->min <= $#{$$current_step{'children'};} + 1) { } 6 64 70
1344 0 $current_step->{index} = $current_index;
1345 0 return 1;
1346 }
1347 6 ▶ elsif ( ( ( ref $current_step->{self} ) eq 'Test::Proto::Repeatable' ) and ( $current_step->{self}->min <= ( $#{ $current_step->{children} } + 1 ) ) ) {
Condition66%2/3
ref $current_step->{"self"} eq "Test::Proto::Repeatable" and $current_step->{"self"}->min <= $#{$$current_step{"children"};} + 1
!ll&&!rl&&r
6406
MC/DC50%1/2
ref $current_step->{"self"} eq "Test::Proto::Repeatable" and $current_step->{"self"}->min <= $#{$$current_step{"children"};} + 1
ref $current_step->{"self"} eq "Test::Proto::Repeatable" $current_step->{"self"}->min <= $#{$$current_step{"children"};} + 1
Truth table66%2/3
A and B
A= ref $current_step->{"self"} eq "Test::Proto::Repeatable"B= $current_step->{"self"}->min <= $#{$$current_step{"children"};} + 1
ABresult
0X0
100
111
1348 6 $current_step->{max} = $#{ $current_step->{children} } + 1
1349 unless defined( $current_step->{max} )
1350 0 ▶ and $current_step->{max} < ( $#{ $current_step->{children} } + 1 ); #~ we need to consider it complete so we don't end up in a loop of adding and removing these.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_step->{"max"} and $current_step->{"max"} < $#{$$current_step{"children"};} + 1 6 0 6
Condition33%1/3
defined $current_step->{"max"} and $current_step->{"max"} < $#{$$current_step{"children"};} + 1
!ll&&!rl&&r
600
MC/DC0%0/2
defined $current_step->{"max"} and $current_step->{"max"} < $#{$$current_step{"children"};} + 1
defined $current_step->{"max"} $current_step->{"max"} < $#{$$current_step{"children"};} + 1
Truth table33%1/3
A and B
A= defined $current_step->{"max"}B= $current_step->{"max"} < $#{$$current_step{"children"};} + 1
ABresult
0X0
100
111
1351 6 $current_step->{index} = $current_index;
1352 6 return 1;
1353 }
1354 else {
1355 64 $current_step->{index} = $current_index;
1356 64 return 0; #~ cause a backtrack
1357 }
1358  
1359 }
1360 11 };
1361  
1362 154 ▶$bt_backtrack = sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
1363 154 my ( $runner, $subject, $expected, $history ) = @_;
1364  
1365 #~ The purpose of this to find the latest repeatable and alternation which has not had all its options exhausted.
1366 #~ This method then removes all items from the history stack after that point and increments a counter on that history item.
1367 #~ No extra steps are added.
1368 #~ Consider taking the removed slice and keeping it in a 'failed branches' slot of the repeatable/alternation.
1369 154 my $l = $#$history;
1370 154 $runner->subtest()->diag( 'Backtracking... (last history item: ' . $l . ')' );
1371  
1372 #~ todo: check if l == -1 ?
1373 154 for my $i ( CORE::reverse( 0 .. $l ) ) {
1374 540 my $step = $history->[$i];
1375 540 ▶ if ( ( blessed $step->{self} ) and $step->{self}->isa('Test::Proto::Repeatable') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($step->{'self'}) and $step->{'self'}->isa('Test::Proto::Repeatable')) { } 60 480 540
elsif (builtin::blessed($step->{'self'}) and $step->{'self'}->isa('Test::Proto::Alternation')) { } 96 592 688
Condition100%3/3
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Repeatable")
!ll&&!rl&&r
20827260
MC/DC100%2/2
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Repeatable")
builtin::blessed($step->{"self"}) $step->{"self"}->isa("Test::Proto::Repeatable")
Truth table100%3/3
A and B
A= builtin::blessed($step->{"self"})B= $step->{"self"}->isa("Test::Proto::Repeatable")
ABresult
0X0
100
111
1376 60 my $children = $step->{children};
1377 60 ▶ $children = [] unless defined $children; #:5.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children 0 60 60
1378 60 my $max = $step->{max}; #~ the maximum set by a backtrack action
1379 60 ▶ $max = $step->{self}->max unless defined $max; # the maximum allowed by the repeatable
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 33 27 60
1380 60 ▶ $max = $step->{max_tried} unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 25 35 60
1381 60 my $new_max = $max - 1;
1382 60 ▶ unless ( $new_max < $step->{self}->min ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_max < $step->{"self"}->min) 47 13 60
1383 47 $runner->subtest( test_case => ($step) )->diag("Selected a new max of $new_max at Repeatable at step $i");
1384 47 $step->{max} = $new_max;
1385 47 ▶ if ( defined $step->{children}->[0] ) { # then the advance worked
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->{"children"}[0]) 47 0 47
1386 47 $bt_backtrack_to->( $runner, $history, $step->{children}->[0] );
1387 47 $#{ $step->{children} } = -1;
1388 47 return 1;
1389 }
1390 }
1391 }
1392 ▶ elsif ( ( blessed $step->{self} ) and $step->{self}->isa('Test::Proto::Alternation') ) {
Condition100%3/3
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Alternation")
!ll&&!rl&&r
20817696
MC/DC100%2/2
builtin::blessed($step->{"self"}) and $step->{"self"}->isa("Test::Proto::Alternation")
builtin::blessed($step->{"self"}) $step->{"self"}->isa("Test::Proto::Alternation")
Truth table100%3/3
A and B
A= builtin::blessed($step->{"self"})B= $step->{"self"}->isa("Test::Proto::Alternation")
ABresult
0X0
100
111
1393 96 ▶ if ( $step->{alt} < $#{ $step->{self}->{alternatives} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->{"alt"} < $#{$$step{"self"}{"alternatives"};}) 48 48 96
1394 48 $runner->subtest( test_case => ($step) )->diag( "Selected branch " . ( $step->{alt} + 1 ) . " at Alternation at step $i" );
1395 48 $step->{alt}++;
1396 48 ▶ if ( defined $step->{children}->[0] ) { # then the advance worked
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->{"children"}[0]) 48 0 48
1397 48 $bt_backtrack_to->( $runner, $history, $step->{children}->[0] );
1398 48 $#{ $step->{children} } = -1;
1399 48 return 1;
1400 }
1401 }
1402 }
1403 }
1404 59 return undef;
1405  
1406 11 };
1407  
1408 95 ▶$bt_backtrack_to = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
1409  
1410 #~ Backtracks to the target step (inclsively, i.e. deletes the step).
1411 95 my ( $runner, $history, $target_step ) = @_;
1412 95 for my $i ( CORE::reverse( 1 .. $#$history ) ) {
1413 264 ▶ if ( $history->[$i] == $target_step ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($history->[$i] == $target_step) 95 169 264
1414 95 $runner->subtest( test_case => ( $history->[$i] ) )->diag("Backtracked to step $i");
1415  
1416 #~ If step $i or any step after it is a child of a parent earlier in the history, it should no longer be a child, because it will shortly no longer exist.
1417 95 my @delenda = $i .. $#$history;
1418 95 foreach my $j ( 0 .. ( $i - 1 ) ) {
1419 274 ▶ if ( defined $history->[$j]->{children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $history->[$j]{"children"}) 262 12 274
1420 262 foreach my $childIndex ( 0 .. $#{ $history->[$j]->{children} } ) {
1421 302 ▶ if ( grep { $history->[$j]->{children}->[$childIndex] == $history->[$_] } @delenda ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$history->[$j]{"children"}[$childIndex] == $history->[$_];} @delenda) 123 179 302
1422 123 $#{ $history->[$j]->{children} } = $childIndex - 1;
1423 123 last;
1424 }
1425 }
1426 }
1427 }
1428 95 $#$history = $i - 1;
1429 95 return;
1430 }
1431 }
1432 0 die; #~ we should never reach this point
1433 11 };
1434 11 1;
1435  
« Proto/Common.pm ↑ Summary Proto/Formatter/TestBuilder.pm »