blib/lib/Test2/Compare/Array.pm

filter: Statementstmt 100% 127 / 127 Branchbran 95.4% 63 / 66 Conditioncond 93.9% 31 / 33 MC/DCmcdc 87.5% 14 / 16 Subroutinesub 100% 17 / 17 Podpod 63.6% 7 / 11 totaltotal 95.9% 259 / 270 CC 48 SCAR 22.1
CC
48
Cov
98%
deltas
17.0
init
13.0
add_item
6.0
CRAP 9.1
« blib/lib/Test2/Event.pm ↑ Summary blib/lib/Test2/Compare/Number.pm »
1 package Test2::Compare::Array;
2 176 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 176 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 176 ▶use base 'Test2::Compare::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 176 our $VERSION = '1.302226';
8  
9 176 ▶use Test2::Util::HashBase qw/inref meta ending items order for_each/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 176 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 176 ▶use Scalar::Util qw/reftype looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1073 ▶sub init {
Subroutine init: called CC=13 SCAR=25.7
Pod undocumented
15 1073 my $self = shift;
16  
17 1073 ▶ if( defined( my $ref = $self->{+INREF}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ref = $self->{'inref'})) { } 447 626 1073
18 447 ▶ croak "Cannot specify both 'inref' and 'items'" if $self->{+ITEMS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"items"} 1 446 447
19 446 ▶ croak "Cannot specify both 'inref' and 'order'" if $self->{+ORDER};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"order"} 1 445 446
20 445 ▶ croak "'inref' must be an array reference, got '$ref'" unless reftype($ref) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($ref) eq "ARRAY" 1 444 445
21 444 my $order = $self->{+ORDER} = [];
22 444 my $items = $self->{+ITEMS} = {};
23 444 ▶ for (my $i = 0; $i < @$ref; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ref) 1231 444 1675
24 1231 push @$order => $i;
25 1231 $items->{$i} = $ref->[$i];
26 }
27 }
28 else {
29 626 ▶ $self->{+ITEMS} ||= {};
Condition100%2/2
$self->{"items"} ||= {}
l!l
4622
MC/DC100%1/1
$self->{"items"} ||= {}
$self->{"items"}
Truth table100%2/2
A
A= $self->{"items"}
Aresult
11
00
30 croak "All indexes listed in the 'items' hashref must be numeric"
31 626 ▶ if grep { !looks_like_number($_) } keys %{$self->{+ITEMS}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not &looks_like_number($_);} keys %{$$self{"items"};} 1 625 626
32  
33 625 ▶ $self->{+ORDER} ||= [sort { $a <=> $b } keys %{$self->{+ITEMS}}];
Condition100%2/2
$self->{"order"} ||= [(sort {$a <=> $b;} keys %{$$self{"items"};})]
l!l
2623
MC/DC100%1/1
$self->{"order"} ||= [(sort {$a <=> $b;} keys %{$$self{"items"};})]
$self->{"order"}
Truth table100%2/2
A
A= $self->{"order"}
Aresult
11
00
34 croak "All indexes listed in the 'order' arrayref must be numeric"
35 625 ▶ if grep { !(looks_like_number($_) || (ref($_) && reftype($_) eq 'CODE')) } @{$self->{+ORDER}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not &looks_like_number($_) || ref $_ && builtin::reftype($_) eq "CODE";} @{$$self{"order"};} 1 624 625
Condition83%5/6
&looks_like_number($_) || ref $_ && builtin::reftype($_) eq 'CODE'
l!l&&r!l&&!r
511
ref $_ and builtin::reftype($_) eq "CODE"
!ll&&!rl&&r
101
MC/DC66%2/3
&looks_like_number($_) || ref $_ && builtin::reftype($_) eq 'CODE'
&looks_like_number($_) ref $_ builtin::reftype($_) eq "CODE"
Truth table75%3/4
A || B and C
A= &looks_like_number($_)B= ref $_C= builtin::reftype($_) eq "CODE"
ABCresult
1XX1
0111
00X0
0100
36 }
37  
38 1068 ▶ $self->{+FOR_EACH} ||= [];
Condition50%1/2
$self->{"for_each"} ||= []
l!l
01068
MC/DC0%0/1
$self->{"for_each"} ||= []
$self->{"for_each"}
Truth table50%1/2
A
A= $self->{"for_each"}
Aresult
11
00
39  
40 1068 $self->SUPER::init();
41 }
42  
43 21 ▶sub name { '<ARRAY>' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
44  
45 3 ▶sub meta_class { 'Test2::Compare::Meta' }
Subroutine meta_class: called CC=1 SCAR=0.0
Pod undocumented
46  
47 1059 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.1
Pod documented
48 1059 my $self = shift;
49 1059 my %params = @_;
50  
51 1059 ▶ return 0 unless $params{exists};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"exists"} 3 1056 1059
52 1056 my $got = $params{got};
53 1056 ▶ return 0 unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 2 1054 1056
54 1054 ▶ return 0 unless ref($got);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $got 5 1049 1054
55 1049 ▶ return 0 unless reftype($got) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($got) eq "ARRAY" 8 1041 1049
56 1041 return 1;
57 }
58  
59 4 ▶sub add_prop {
Subroutine add_prop: called CC=2 SCAR=6.9
Pod undocumented
60 4 my $self = shift;
61 4 ▶ $self->{+META} = $self->meta_class->new unless defined $self->{+META};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"meta"} 3 1 4
62 4 $self->{+META}->add_prop(@_);
63 }
64  
65 1988 ▶sub top_index {
Subroutine top_index: called CC=3 SCAR=11.0
Pod documented
66 1988 my $self = shift;
67 1988 my @order = @{$self->{+ORDER}};
68  
69 1988 ▶ while(@order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) 1409 596 2005
70 1409 my $idx = pop @order;
71 1409 ▶ next if ref $idx;
Branch: 1 branch
BranchTrueFalseTotal
if ref $idx 17 1392 1409
72 1392 return $idx;
73 }
74  
75 596 return undef; # No indexes
76 }
77  
78 1983 ▶sub add_item {
Subroutine add_item: called CC=6 SCAR=17.9
Pod documented
79 1983 my $self = shift;
80 1983 my $check = pop;
81 1983 my ($idx) = @_;
82  
83 1983 my $top = $self->top_index;
84  
85 1983 ▶ croak "elements must be added in order!"
Branch: 1 branch
BranchTrueFalseTotal
if $top and $idx and $idx <= $top 1 1982 1983
Condition100%6/6
$top and $idx and $idx <= $top
!ll&&!rl&&r
1972101
$top and $idx
!ll&&!rl&&r
876109611
MC/DC100%3/3
$top and $idx and $idx <= $top
$top $idx $idx <= $top
Truth table100%4/4
A and B and C
A= $topB= $idxC= $idx <= $top
ABCresult
0XX0
10X0
1100
1111
86 if $top && $idx && $idx <= $top;
87  
88 1982 ▶ $idx = defined($top) ? $top + 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
unless defined $idx 1946 36 1982
defined $top ? : 1367 579 1946
89 unless defined($idx);
90  
91 1982 push @{$self->{+ORDER}} => $idx;
92 1982 $self->{+ITEMS}->{$idx} = $check;
93 }
94  
95 23 ▶sub add_filter {
Subroutine add_filter: called CC=5 SCAR=16.1
Pod documented
96 23 my $self = shift;
97 23 my ($code) = @_;
98 23 ▶ croak "A single coderef is required"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and $code and ref $code and builtin::reftype($code) eq "CODE" 5 18 23
Condition100%9/9
@_ == 1 and $code and ref $code and builtin::reftype($code) eq "CODE"
!ll&&!rl&&r
4118
@_ == 1 and $code and ref $code
!ll&&!rl&&r
3119
@_ == 1 and $code
!ll&&!rl&&r
2120
MC/DC100%4/4
@_ == 1 and $code and ref $code and builtin::reftype($code) eq "CODE"
@_ == 1 $code ref $code builtin::reftype($code) eq "CODE"
Truth table100%5/5
A and B and C and D
A= @_ == 1B= $codeC= ref $codeD= builtin::reftype($code) eq "CODE"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
99 unless @_ == 1 && $code && ref $code && reftype($code) eq 'CODE';
100  
101 18 push @{$self->{+ORDER}} => $code;
102 }
103  
104 18 ▶sub add_for_each {
Subroutine add_for_each: called CC=1 SCAR=0.0
Pod undocumented
105 18 my $self = shift;
106 18 push @{$self->{+FOR_EACH}} => @_;
107 }
108  
109 606 ▶sub verify_build {
Subroutine verify_build: called CC=4 SCAR=13.9
Pod documented
110 606 my $self = shift;
111  
112 606 ▶ return unless @{$self->{+FOR_EACH}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"for_each"};} 589 17 606
113 17 ▶ return unless $self->{+ENDING};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ending"} 7 10 17
114 10 ▶ return if keys %{$self->{+ITEMS}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"items"};} 5 5 10
115  
116 5 $self->throw_build_error("'end' with no items specified requires an empty array, which discards the 'all_items' checks; use 'etc' instead of 'end' to check every item without bounding the array");
117 }
118  
119 1047 ▶sub deltas {
Subroutine deltas: called CC=17 SCAR=28.3
Pod documented
120 1047 my $self = shift;
121 1047 my %params = @_;
122 1047 my ($got, $convert, $seen) = @params{qw/got convert seen/};
123  
124 1047 my @deltas;
125 1047 my $state = 0;
126 1047 my @order = @{$self->{+ORDER}};
127 1047 my $items = $self->{+ITEMS};
128 1047 my $for_each = $self->{+FOR_EACH};
129  
130 1047 my $meta = $self->{+META};
131 1047 ▶ push @deltas => $meta->deltas(%params) if defined $meta;
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta 2 1045 1047
132  
133 # Make a copy that we can munge as needed.
134 1047 my @list = @$got;
135  
136 1047 ▶ while (@order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) 3247 1047 4294
137 3247 my $idx = shift @order;
138 3247 my $overflow = 0;
139 3247 my $val;
140  
141 # We have a filter, not an index
142 3247 ▶ if (ref($idx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $idx) 18 3229 3247
143 18 @list = $idx->(@list);
144 18 next;
145 }
146  
147 3229 ▶ confess "Internal Error: Stacks are out of sync (state > idx)"
Branch: 1 branch
BranchTrueFalseTotal
if $state > $idx + 1 0 3229 3229
148 if $state > $idx + 1;
149  
150 3229 ▶ while ($state <= $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state <= $idx) 3234 3229 6463
151 3234 $overflow = !@list;
152 3234 $val = shift @list;
153  
154 # check-all goes here so we hit each item, even unspecified ones.
155 3234 for my $check (@$for_each) {
156 16 ▶ last if $overflow; # avoid doing 'for each' checks beyond array bounds
Branch: 1 branch
BranchTrueFalseTotal
if $overflow 1 15 16
157 15 $check = $convert->($check);
158 15 ▶ push @deltas => $check->run(
Branch: 1 branch
BranchTrueFalseTotal
$overflow ? : 0 15 15
159 id => [ARRAY => $state],
160 convert => $convert,
161 seen => $seen,
162 exists => !$overflow,
163 $overflow ? () : (got => $val),
164 );
165 }
166  
167 3234 $state++;
168 }
169  
170 3229 ▶ confess "Internal Error: Stacks are out of sync (state != idx + 1)"
Branch: 1 branch
BranchTrueFalseTotal
unless $state == $idx + 1 0 3229 3229
171 unless $state == $idx + 1;
172  
173 3229 my $check = $convert->($items->{$idx});
174  
175 3229 ▶ push @deltas => $check->run(
Branch: 1 branch
BranchTrueFalseTotal
$overflow ? : 13 3216 3229
176 id => [ARRAY => $idx],
177 convert => $convert,
178 seen => $seen,
179 exists => !$overflow,
180 $overflow ? () : (got => $val),
181 );
182 }
183  
184 1047 ▶ while (@list && (@$for_each || $self->{+ENDING})) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list and @$for_each || $self->{"ending"}) 17 1047 1064
Condition100%6/6
@list and @$for_each || $self->{"ending"}
!ll&&!rl&&r
10202717
@$for_each or $self->{"ending"}
l!l&&r!l&&!r
11627
MC/DC100%3/3
@list and @$for_each || $self->{"ending"}
@list @$for_each $self->{"ending"}
Truth table100%4/4
A and B or C
A= @listB= @$for_eachC= $self->{"ending"}
ABCresult
0XX0
1000
11X1
1011
185 17 my $item = shift @list;
186  
187 17 for my $check (@$for_each) {
188 11 $check = $convert->($check);
189 11 push @deltas => $check->run(
190 id => [ARRAY => $state],
191 convert => $convert,
192 seen => $seen,
193 got => $item,
194 exists => 1,
195 );
196 }
197  
198 # if items are left over, and ending is true, we have a problem!
199 17 ▶ if ($self->{+ENDING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ending"}) 11 6 17
200 push @deltas => $self->delta_class->new(
201 dne => 'check',
202 verified => undef,
203 id => [ARRAY => $state],
204 got => $item,
205 check => undef,
206  
207 11 ▶ $self->{+ENDING} eq 'implicit' ? (note => 'implicit end') : (),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ending'} eq 'implicit' ? : 4 7 11
208 );
209 }
210  
211 17 $state++;
212 }
213  
214 1047 return @deltas;
215 }
216  
217 176 1;
218  
219 __END__
« blib/lib/Test2/Event.pm ↑ Summary blib/lib/Test2/Compare/Number.pm »