blib/lib/Test2/Tools/Compare.pm

filter: Statementstmt 97.9% 339 / 346 Branchbran 84.7% 122 / 144 Conditioncond 88.2% 15 / 17 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 98.9% 94 / 95 Podpod 100% 53 / 53 totaltotal 94.8% 631 / 665 CC 79 SCAR 11.9
CC
79
Cov
94%
is
10.6
event
9.0
__ANON__
5.3
CRAP 3.3
« blib/lib/Test2/Compare/Bool.pm ↑ Summary blib/lib/Test2/Util/Grabber.pm »
1 package Test2::Tools::Compare;
2 171 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 171 our $VERSION = '1.302226';
6  
7 171 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 171 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 171 ▶use Test2::API qw/context/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 171 ▶use Test2::Util::Ref qw/rtype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 171 ▶use Test2::Util qw/pkg_to_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 171 ▶use Test2::Compare qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 compare
16 get_build push_build pop_build build
17 strict_convert relaxed_convert
18 171 };
19  
20 171 ▶use Test2::Compare::Array();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 171 ▶use Test2::Compare::Bag();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 171 ▶use Test2::Compare::Bool();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 171 ▶use Test2::Compare::Custom();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 171 ▶use Test2::Compare::Event();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 171 ▶use Test2::Compare::Float();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 171 ▶use Test2::Compare::Hash();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 171 ▶use Test2::Compare::Isa();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 171 ▶use Test2::Compare::Meta();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 171 ▶use Test2::Compare::Number();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 171 ▶use Test2::Compare::Object();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 171 ▶use Test2::Compare::OrderedSubset();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 171 ▶use Test2::Compare::Pattern();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 171 ▶use Test2::Compare::Ref();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 171 ▶use Test2::Compare::DeepRef();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 171 ▶use Test2::Compare::Regex();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 171 ▶use Test2::Compare::Scalar();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 171 ▶use Test2::Compare::Set();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 171 ▶use Test2::Compare::String();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 171 ▶use Test2::Compare::Undef();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 171 ▶use Test2::Compare::Wildcard();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 171 %Carp::Internal = (
43 %Carp::Internal,
44 'Test2::Tools::Compare' => 1,
45 'Test2::Compare::Array' => 1,
46 'Test2::Compare::Bag' => 1,
47 'Test2::Compare::Bool' => 1,
48 'Test2::Compare::Custom' => 1,
49 'Test2::Compare::Event' => 1,
50 'Test2::Compare::Float' => 1,
51 'Test2::Compare::Hash' => 1,
52 'Test2::Compare::Isa' => 1,
53 'Test2::Compare::Meta' => 1,
54 'Test2::Compare::Number' => 1,
55 'Test2::Compare::Object' => 1,
56 'Test2::Compare::Pattern' => 1,
57 'Test2::Compare::Ref' => 1,
58 'Test2::Compare::Regex' => 1,
59 'Test2::Compare::Scalar' => 1,
60 'Test2::Compare::Set' => 1,
61 'Test2::Compare::String' => 1,
62 'Test2::Compare::Undef' => 1,
63 'Test2::Compare::Wildcard' => 1,
64 'Test2::Compare::OrderedSubset' => 1,
65 );
66  
67 171 our @EXPORT = qw/is like/;
68 171 our @EXPORT_OK = qw{
69 is like isnt unlike
70 match mismatch validator
71 hash array bag object meta meta_check number float rounded within string subset bool check_isa
72 number_lt number_le number_ge number_gt
73 in_set not_in_set check_set
74 item field call call_list call_hash prop check all_items all_keys all_vals all_values
75 etc end filter_items
76 T F D DF E DNE FDNE U L
77 event fail_events
78 exact_ref
79 };
80 171 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 231 ▶my $_autodump = sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
83 231 my ($ctx, $got) = @_;
84  
85 231 ▶ my $module = $ENV{'T2_AUTO_DUMP'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $module = $ENV{"T2_AUTO_DUMP"} or return 2 229 231
86 2 ▶ $module = 'Data::Dumper' if $module eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $module eq 1 0 2 2
87  
88 2 my $file = pkg_to_file($module);
89 2 eval { require $file };
90  
91 2 ▶ if (not $module->can('Dump')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module->can("Dump")) 0 2 2
92 0 require Data::Dumper;
93 0 $module = 'Data::Dumper';
94 }
95  
96 2 my $deparse = $Data::Dumper::Deparse;
97 2 ▶ $deparse = !!$ENV{'T2_AUTO_DEPARSE'} if exists $ENV{'T2_AUTO_DEPARSE'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"T2_AUTO_DEPARSE"} 2 0 2
98 2 local $Data::Dumper::Deparse = $deparse;
99  
100 2 $ctx->diag($module->Dump([$got], ['GOT']));
101 171 };
102  
103 1409 ▶sub is($$;$@) {
Subroutine is: called CC=10 SCAR=23.6
Pod documented
104 1409 my ($got, $exp, $name, @diag) = @_;
105 1409 my $ctx = context();
106  
107 1409 my $delta = compare($got, $exp, \&strict_convert);
108  
109 1409 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) { } 219 1190 1409
110 # Temporary thing.
111 219 my $count = 0;
112 219 my $implicit = 0;
113 219 my @deltas = ($delta);
114 219 ▶ while (my $d = shift @deltas) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = shift @deltas) 326 219 545
115 326 my $add = $d->children;
116 326 ▶ push @deltas => @$add if $add && @$add;
Branch: 1 branch
BranchTrueFalseTotal
if $add and @$add 62 264 326
Condition100%3/3
$add and @$add
!ll&&!rl&&r
2424062
MC/DC100%2/2
$add and @$add
$add @$add
Truth table100%3/3
A and B
A= $addB= @$add
ABresult
0X0
100
111
117 326 ▶ next if $d->verified;
Branch: 1 branch
BranchTrueFalseTotal
if $d->verified 62 264 326
118 264 $count++;
119 264 ▶ $implicit++ if $d->note && $d->note eq 'implicit end';
Branch: 1 branch
BranchTrueFalseTotal
if $d->note and $d->note eq "implicit end" 1 263 264
Condition66%2/3
$d->note and $d->note eq "implicit end"
!ll&&!rl&&r
26301
MC/DC50%1/2
$d->note and $d->note eq "implicit end"
$d->note $d->note eq "implicit end"
Truth table66%2/3
A and B
A= $d->noteB= $d->note eq "implicit end"
ABresult
0X0
100
111
120 }
121  
122 219 ▶ if ($implicit == $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($implicit == $count) { } 0 219 219
123 0 $ctx->ok(1, $name);
124 0 ▶ my $meth = $ENV{AUTHOR_TESTING} ? 'throw' : 'alert';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'AUTHOR_TESTING'} ? : 0 0 0
125 0 my $type = $delta->render_check;
126 0 $ctx->$meth(
127 join "\n",
128 "!!! NOTICE OF BEHAVIOR CHANGE !!!",
129 "This test uses at least 1 $type check without using end() or etc().",
130 "The old behavior was to default to etc() when inside is().",
131 "The old behavior was a bug.",
132 "The new behavior is to default to end().",
133 "This test will soon start to fail with the following diagnostics:",
134 $delta->diag->as_string,
135 "",
136 );
137 }
138 else {
139 219 $ctx->fail($name, $delta->diag, @diag);
140 219 $ctx->$_autodump($got);
141 }
142 }
143 else {
144 1190 $ctx->ok(1, $name);
145 }
146  
147 1409 $ctx->release;
148 1409 return !$delta;
149 }
150  
151 18 ▶sub isnt($$;$@) {
Subroutine isnt: called CC=3 SCAR=11.0
Pod documented
152 18 my ($got, $exp, $name, @diag) = @_;
153 18 my $ctx = context();
154  
155 18 my $delta = compare($got, $exp, \&strict_convert);
156  
157 18 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) { } 14 4 18
158 14 $ctx->ok(1, $name);
159 }
160 else {
161 4 $ctx->ok(0, $name, ["Comparison matched (it should not).", @diag]);
162 4 $ctx->$_autodump($got);
163 }
164  
165 18 $ctx->release;
166 18 ▶ return $delta ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delta ? : 14 4 18
167 }
168  
169 365 ▶sub like($$;$@) {
Subroutine like: called CC=2 SCAR=6.9
Pod documented
170 365 my ($got, $exp, $name, @diag) = @_;
171 365 my $ctx = context();
172  
173 365 my $delta = compare($got, $exp, \&relaxed_convert);
174  
175 365 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) { } 3 362 365
176 3 $ctx->fail($name, $delta->diag, @diag);
177 3 $ctx->$_autodump($got);
178 }
179 else {
180 362 $ctx->ok(1, $name);
181 }
182  
183 365 $ctx->release;
184 365 return !$delta;
185 }
186  
187 9 ▶sub unlike($$;$@) {
Subroutine unlike: called CC=3 SCAR=11.0
Pod documented
188 9 my ($got, $exp, $name, @diag) = @_;
189 9 my $ctx = context();
190  
191 9 my $delta = compare($got, $exp, \&relaxed_convert);
192  
193 9 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) { } 4 5 9
194 4 $ctx->ok(1, $name);
195 }
196 else {
197 5 $ctx->ok(0, $name, ["Comparison matched (it should not).", @diag]);
198 5 $ctx->$_autodump($got);
199 }
200  
201 9 $ctx->release;
202 9 ▶ return $delta ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delta ? : 4 5 9
203 }
204  
205 7 ▶sub meta(&) { build('Test2::Compare::Meta', @_) }
Subroutine meta: called CC=1 SCAR=0.0
Pod documented
206 0 ▶sub meta_check(&) { build('Test2::Compare::Meta', @_) }
Subroutine meta_check: not called CC=1 SCAR=6.9
Pod documented
207 559 ▶sub hash(&) { build('Test2::Compare::Hash', @_) }
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
208 609 ▶sub array(&) { build('Test2::Compare::Array', @_) }
Subroutine array: called CC=1 SCAR=0.0
Pod documented
209 436 ▶sub bag(&) { build('Test2::Compare::Bag', @_) }
Subroutine bag: called CC=1 SCAR=0.0
Pod documented
210 11 ▶sub object(&) { build('Test2::Compare::Object', @_) }
Subroutine object: called CC=1 SCAR=0.0
Pod documented
211 31 ▶sub subset(&) { build('Test2::Compare::OrderedSubset', @_) }
Subroutine subset: called CC=1 SCAR=0.0
Pod documented
212  
213 2 ▶sub U() {
Subroutine U: called CC=1 SCAR=0.0
Pod documented
214 2 my @caller = caller;
215 Test2::Compare::Custom->new(
216 2 ▶ code => sub { defined $_ ? 0 : 1 }, name => 'UNDEFINED', operator => '!DEFINED()',
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
217 file => $caller[1],
218 lines => [$caller[2]],
219 );
220 }
221  
222 6 ▶sub D() {
Subroutine D: called CC=1 SCAR=0.0
Pod documented
223 6 my @caller = caller;
224 Test2::Compare::Custom->new(
225 6 ▶ code => sub { defined $_ ? 1 : 0 }, name => 'DEFINED', operator => 'DEFINED()',
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 5 1 6
226 file => $caller[1],
227 lines => [$caller[2]],
228 );
229 }
230  
231 6 ▶sub DF() {
Subroutine DF: called CC=1 SCAR=0.0
Pod documented
232 6 my @caller = caller;
233 Test2::Compare::Custom->new(
234 6 ▶ code => sub { defined $_ && ( ! ref $_ && ! $_ ) ? 1 : 0 }, name => 'DEFINED BUT FALSE', operator => 'DEFINED() && FALSE()',
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !(ref $_ || $_) ? : 2 4 6
Condition83%5/6
defined $_ and not ref $_ || $_
!ll&&!rl&&r
132
ref $_ || $_
l!l&&r!l&&!r
032
MC/DC66%2/3
defined $_ and not ref $_ || $_
defined $_ ref $_ $_
Truth table75%3/4
A and not(B || C)
A= defined $_B= ref $_C= $_
ABCresult
0XX0
11X0
1010
1001
235 file => $caller[1],
236 lines => [$caller[2]],
237 );
238 }
239  
240 27 ▶sub DNE() {
Subroutine DNE: called CC=1 SCAR=0.0
Pod documented
241 27 my @caller = caller;
242 Test2::Compare::Custom->new(
243 27 ▶ code => sub { my %p = @_; $p{exists} ? 0 : 1 }, name => '<DOES NOT EXIST>', operator => '!exists',
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$p{'exists'} ? : 4 27 31
244 file => $caller[1],
245 lines => [$caller[2]],
246 );
247 }
248  
249 2 ▶sub E() {
Subroutine E: called CC=1 SCAR=0.0
Pod documented
250 2 my @caller = caller;
251 Test2::Compare::Custom->new(
252 2 ▶ code => sub { my %p = @_; $p{exists} ? 1 : 0 }, name => '<DOES EXIST>', operator => '!exists',
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$p{'exists'} ? : 1 1 2
253 file => $caller[1],
254 lines => [$caller[2]],
255 );
256 }
257  
258 32 ▶sub F() {
Subroutine F: called CC=1 SCAR=0.0
Pod documented
259 32 my @caller = caller;
260 Test2::Compare::Custom->new(
261 32 ▶ code => sub { my %p = @_; $p{got} ? 0 : $p{exists} }, name => 'FALSE', operator => 'FALSE()',
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$p{'got'} ? : 4 26 30
262 file => $caller[1],
263 lines => [$caller[2]],
264 );
265 }
266  
267 6 ▶sub FDNE() {
Subroutine FDNE: called CC=1 SCAR=0.0
Pod documented
268 6 my @caller = caller;
269 Test2::Compare::Custom->new(
270 6 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
271 6 my %p = @_;
272 6 ▶ return 1 unless $p{exists};
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"exists"} 2 4 6
273 4 ▶ return $p{got} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$p{'got'} ? : 2 2 4
274 },
275 6 name => 'FALSE', operator => 'FALSE() || !exists',
276 file => $caller[1],
277 lines => [$caller[2]],
278 );
279 }
280  
281 164 ▶sub T() {
Subroutine T: called CC=1 SCAR=0.0
Pod documented
282 164 my @caller = caller;
283 Test2::Compare::Custom->new(
284 163 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
285 163 my %p = @_;
286 163 ▶ return 0 unless $p{exists};
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"exists"} 0 163 163
287 163 ▶ return $p{got} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$p{'got'} ? : 159 4 163
288 },
289 164 name => 'TRUE', operator => 'TRUE()',
290 file => $caller[1],
291 lines => [$caller[2]],
292 );
293 }
294  
295 5 ▶sub L() {
Subroutine L: called CC=1 SCAR=0.0
Pod documented
296 5 my @caller = caller;
297 Test2::Compare::Custom->new(
298 5 ▶ code => sub { defined $_ && length $_ ? 1 : 0 }, name => 'LENGTH', operator => 'DEFINED() && LENGTH()',
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && length $_ ? : 3 2 5
Condition100%3/3
defined $_ and length $_
!ll&&!rl&&r
113
MC/DC100%2/2
defined $_ and length $_
defined $_ length $_
Truth table100%3/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
299 file => $caller[1],
300 lines => [$caller[2]],
301 );
302 }
303  
304 30 ▶sub exact_ref($) {
Subroutine exact_ref: called CC=1 SCAR=0.0
Pod documented
305 30 my @caller = caller;
306 30 return Test2::Compare::Ref->new(
307 file => $caller[1],
308 lines => [$caller[2]],
309 input => $_[0],
310 );
311 }
312  
313 203 ▶sub match($) {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
314 203 my @caller = caller;
315 203 return Test2::Compare::Pattern->new(
316 file => $caller[1],
317 lines => [$caller[2]],
318 pattern => $_[0],
319 );
320 }
321  
322 4 ▶sub mismatch($) {
Subroutine mismatch: called CC=1 SCAR=0.0
Pod documented
323 4 my @caller = caller;
324 4 return Test2::Compare::Pattern->new(
325 file => $caller[1],
326 lines => [$caller[2]],
327 negate => 1,
328 pattern => $_[0],
329 );
330 }
331  
332 6 ▶sub validator {
Subroutine validator: called CC=1 SCAR=0.0
Pod documented
333 6 my $code = pop;
334 6 my $cname = pop;
335 6 my $op = pop;
336  
337 6 my @caller = caller;
338 6 return Test2::Compare::Custom->new(
339 file => $caller[1],
340 lines => [$caller[2]],
341 code => $code,
342 name => $cname,
343 operator => $op,
344 );
345 }
346  
347 3 ▶sub number($;@) {
Subroutine number: called CC=1 SCAR=0.0
Pod documented
348 3 my ($num, @args) = @_;
349 3 my @caller = caller;
350 3 return Test2::Compare::Number->new(
351 file => $caller[1],
352 lines => [$caller[2]],
353 input => $num,
354 @args,
355 );
356 }
357  
358 1 ▶sub number_lt($;@) {
Subroutine number_lt: called CC=1 SCAR=0.0
Pod documented
359 1 my ($num, @args) = @_;
360 1 my @caller = caller;
361 1 return Test2::Compare::Number->new(
362 file => $caller[1],
363 lines => [$caller[2]],
364 input => $num,
365 mode => '<',
366 @args,
367 );
368 }
369  
370 1 ▶sub number_le($;@) {
Subroutine number_le: called CC=1 SCAR=0.0
Pod documented
371 1 my ($num, @args) = @_;
372 1 my @caller = caller;
373 1 return Test2::Compare::Number->new(
374 file => $caller[1],
375 lines => [$caller[2]],
376 input => $num,
377 mode => '<=',
378 @args,
379 );
380 }
381  
382 1 ▶sub number_ge($;@) {
Subroutine number_ge: called CC=1 SCAR=0.0
Pod documented
383 1 my ($num, @args) = @_;
384 1 my @caller = caller;
385 1 return Test2::Compare::Number->new(
386 file => $caller[1],
387 lines => [$caller[2]],
388 input => $num,
389 mode => '>=',
390 @args,
391 );
392 }
393  
394 1 ▶sub number_gt($;@) {
Subroutine number_gt: called CC=1 SCAR=0.0
Pod documented
395 1 my ($num, @args) = @_;
396 1 my @caller = caller;
397 1 return Test2::Compare::Number->new(
398 file => $caller[1],
399 lines => [$caller[2]],
400 input => $num,
401 mode => '>',
402 @args,
403 );
404 }
405  
406 6 ▶sub float($;@) {
Subroutine float: called CC=1 SCAR=0.0
Pod documented
407 6 my ($num, @args) = @_;
408 6 my @caller = caller;
409 6 return Test2::Compare::Float->new(
410 file => $caller[1],
411 lines => [$caller[2]],
412 input => $num,
413 @args,
414 );
415 }
416  
417 1 ▶sub rounded($$) {
Subroutine rounded: called CC=1 SCAR=0.0
Pod documented
418 1 my ($num, $precision) = @_;
419 1 my @caller = caller;
420 1 return Test2::Compare::Float->new(
421 file => $caller[1],
422 lines => [$caller[2]],
423 input => $num,
424 precision => $precision,
425 );
426 }
427  
428 2 ▶sub within($;$) {
Subroutine within: called CC=2 SCAR=6.9
Pod documented
429 2 my ($num, $tolerance) = @_;
430 2 my @caller = caller;
431 2 ▶ return Test2::Compare::Float->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $tolerance ? : 1 1 2
432 file => $caller[1],
433 lines => [$caller[2]],
434 input => $num,
435 defined $tolerance ? ( tolerance => $tolerance ) : (),
436 );
437 }
438  
439 203 ▶sub bool($;@) {
Subroutine bool: called CC=1 SCAR=0.0
Pod documented
440 203 my ($bool, @args) = @_;
441 203 my @caller = caller;
442 203 return Test2::Compare::Bool->new(
443 file => $caller[1],
444 lines => [$caller[2]],
445 input => $bool,
446 @args,
447 );
448 }
449  
450 4 ▶sub string($;@) {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
451 4 my ($str, @args) = @_;
452 4 my @caller = caller;
453 4 return Test2::Compare::String->new(
454 file => $caller[1],
455 lines => [$caller[2]],
456 input => $str,
457 @args,
458 );
459 }
460  
461 3 ▶sub check_isa($;@) {
Subroutine check_isa: called CC=1 SCAR=0.0
Pod documented
462 3 my ($class_name, @args) = @_;
463 3 my @caller = caller;
464 3 return Test2::Compare::Isa->new(
465 file => $caller[1],
466 lines => [$caller[2]],
467 input => $class_name,
468 @args,
469 );
470 }
471  
472 19 ▶sub filter_items(&) {
Subroutine filter_items: called CC=4 SCAR=13.9
Pod documented
473 19 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 18 1 19
474  
475 18 ▶ croak "'$build' does not support filters"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_filter") 1 17 18
476 unless $build->can('add_filter');
477  
478 17 ▶ croak "'filter_items' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 16 17
479 if defined wantarray;
480  
481 16 $build->add_filter(@_);
482 }
483  
484 28 ▶sub all_items {
Subroutine all_items: called CC=4 SCAR=14.9
Pod documented
485 28 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 28 0 28
486  
487 28 ▶ croak "'$build' does not support all-items"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_for_each") 0 28 28
488 unless $build->can('add_for_each');
489  
490 28 ▶ croak "'all_items' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 28 28
491 if defined wantarray;
492  
493 28 $build->add_for_each(@_);
494 }
495  
496 6 ▶sub all_keys {
Subroutine all_keys: called CC=4 SCAR=14.9
Pod documented
497 6 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 6 0 6
498  
499 6 ▶ croak "'$build' does not support all-keys"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_for_each_key") 0 6 6
500 unless $build->can('add_for_each_key');
501  
502 6 ▶ croak "'all_keys' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 6 6
503 if defined wantarray;
504  
505 6 $build->add_for_each_key(@_);
506 }
507  
508 171 *all_vals = *all_values;
509 9 ▶sub all_values {
Subroutine all_values: called CC=4 SCAR=14.9
Pod documented
510 9 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 9 0 9
511  
512 9 ▶ croak "'$build' does not support all-values"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_for_each_val") 0 9 9
513 unless $build->can('add_for_each_val');
514  
515 9 ▶ croak "'all_values' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 9 9
516 if defined wantarray;
517  
518 9 $build->add_for_each_val(@_);
519 }
520  
521  
522 295 ▶sub end() {
Subroutine end: called CC=4 SCAR=13.9
Pod documented
523 295 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 294 1 295
524  
525 294 ▶ croak "'$build' does not support 'ending'"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("ending") 1 293 294
526 unless $build->can('ending');
527  
528 293 ▶ croak "'end' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 292 293
529 if defined wantarray;
530  
531 292 $build->set_ending(1);
532 }
533  
534 1115 ▶sub etc() {
Subroutine etc: called CC=4 SCAR=14.9
Pod documented
535 1115 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 1115 0 1115
536  
537 1115 ▶ croak "'$build' does not support 'ending'"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("ending") 0 1115 1115
538 unless $build->can('ending');
539  
540 1115 ▶ croak "'etc' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 1115 1115
541 if defined wantarray;
542  
543 1115 $build->set_ending(0);
544 }
545  
546 818 ▶my $_call = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
547 818 my ($name, $expect, $context, $func_name) = @_;
548 818 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 817 1 818
549  
550 817 ▶ croak "'$build' does not support method calls"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_call") 1 816 817
551 unless $build->can('add_call');
552  
553 816 ▶ croak "'$func_name' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 815 816
554 if defined wantarray;
555  
556 815 my @caller = caller;
557 815 $build->add_call(
558 $name,
559 Test2::Compare::Wildcard->new(
560 expect => $expect,
561 file => $caller[1],
562 lines => [$caller[2]],
563 ),
564 undef,
565 $context,
566 );
567 171 };
568  
569 804 ▶sub call($$) { $_call->(@_,'scalar','call') }
Subroutine call: called CC=1 SCAR=0.0
Pod documented
570 7 ▶sub call_list($$) { $_call->(@_,'list','call_list') }
Subroutine call_list: called CC=1 SCAR=0.0
Pod documented
571 7 ▶sub call_hash($$) { $_call->(@_,'hash','call_hash') }
Subroutine call_hash: called CC=1 SCAR=0.0
Pod documented
572  
573 112 ▶sub prop($$) {
Subroutine prop: called CC=4 SCAR=13.9
Pod documented
574 112 my ($name, $expect) = @_;
575 112 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 111 1 112
576  
577 111 ▶ croak "'$build' does not support meta-checks"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_prop") 0 111 111
578 unless $build->can('add_prop');
579  
580 111 ▶ croak "'prop' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 110 111
581 if defined wantarray;
582  
583 110 my @caller = caller;
584 110 $build->add_prop(
585 $name,
586 Test2::Compare::Wildcard->new(
587 expect => $expect,
588 file => $caller[1],
589 lines => [$caller[2]],
590 ),
591 );
592 }
593  
594 1533 ▶sub item($;$) {
Subroutine item: called CC=4 SCAR=13.9
Pod documented
595 1533 my @args = @_;
596 1533 my $expect = pop @args;
597  
598 1533 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 1532 1 1533
599  
600 1532 ▶ croak "'$build' does not support array item checks"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_item") 1 1531 1532
601 unless $build->can('add_item');
602  
603 1531 ▶ croak "'item' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 1530 1531
604 if defined wantarray;
605  
606 1530 my @caller = caller;
607 1530 push @args => Test2::Compare::Wildcard->new(
608 expect => $expect,
609 file => $caller[1],
610 lines => [$caller[2]],
611 );
612  
613 1530 $build->add_item(@args);
614 }
615  
616 1028 ▶sub field($$) {
Subroutine field: called CC=4 SCAR=13.9
Pod documented
617 1028 my ($name, $expect) = @_;
618  
619 1028 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 1027 1 1028
620  
621 1027 ▶ croak "'$build' does not support hash field checks"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_field") 1 1026 1027
622 unless $build->can('add_field');
623  
624 1026 ▶ croak "'field' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 1025 1026
625 if defined wantarray;
626  
627 1025 my @caller = caller;
628 1025 $build->add_field(
629 $name,
630 Test2::Compare::Wildcard->new(
631 expect => $expect,
632 file => $caller[1],
633 lines => [$caller[2]],
634 ),
635 );
636 }
637  
638 18 ▶sub check($) {
Subroutine check: called CC=4 SCAR=13.9
Pod documented
639 18 my ($check) = @_;
640  
641 18 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 17 1 18
642  
643 17 ▶ croak "'$build' is not a check-set"
Branch: 1 branch
BranchTrueFalseTotal
unless $build->can("add_check") 1 16 17
644 unless $build->can('add_check');
645  
646 16 ▶ croak "'check' should only ever be called in void context"
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 15 16
647 if defined wantarray;
648  
649 15 my @caller = caller;
650 15 my $wc = Test2::Compare::Wildcard->new(
651 expect => $check,
652 file => $caller[1],
653 lines => [$caller[2]],
654 );
655  
656 15 $build->add_check($wc);
657 }
658  
659 4 ▶sub check_set { return _build_set('all' => @_) }
Subroutine check_set: called CC=1 SCAR=0.0
Pod documented
660 9 ▶sub in_set { return _build_set('any' => @_) }
Subroutine in_set: called CC=1 SCAR=0.0
Pod documented
661 5 ▶sub not_in_set { return _build_set('none' => @_) }
Subroutine not_in_set: called CC=1 SCAR=0.0
Pod documented
662  
663 18 ▶sub _build_set {
Subroutine _build_set: called CC=3 SCAR=11.0
664 18 my $redux = shift;
665 18 my ($builder) = @_;
666 18 ▶ my $btype = reftype($builder) || '';
Condition100%2/2
builtin::reftype($builder) || ''
l!l
99
MC/DC100%1/1
builtin::reftype($builder) || ''
builtin::reftype($builder)
Truth table100%2/2
A
A= builtin::reftype($builder)
Aresult
11
00
667  
668 18 my $set;
669 18 ▶ if ($btype eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($btype eq 'CODE') { } 7 11 18
670 7 $set = build('Test2::Compare::Set', $builder);
671 6 $set->set_builder($builder);
672 }
673 else {
674 11 $set = Test2::Compare::Set->new(checks => [@_]);
675 }
676  
677 17 $set->set_reduction($redux);
678 17 return $set;
679 }
680  
681 37 ▶sub fail_events($;$) {
Subroutine fail_events: called CC=3 SCAR=11.2
Pod documented
682 37 my $event = &event(@_);
683  
684 37 my $diag = event('Diag');
685  
686 37 ▶ return ($event, $diag) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 37 37
687  
688 37 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 37 0 37
689 37 $build->add_item($event);
690 37 $build->add_item($diag);
691 }
692  
693 1847 ▶sub event($;$) {
Subroutine event: called CC=9 SCAR=22.0
Pod documented
694 1847 my ($intype, $spec) = @_;
695  
696 1847 my @caller = caller;
697  
698 1847 ▶ croak "type is required" unless $intype;
Branch: 1 branch
BranchTrueFalseTotal
unless $intype 1 1846 1847
699  
700 1846 my $type;
701 1846 ▶ if ($intype =~ m/^\+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intype =~ /^\+(.*)$/) { } 99 1747 1846
702 99 $type = $1;
703 }
704 else {
705 1747 $type = "Test2::Event::$intype";
706 }
707  
708 1846 my $event;
709 1846 ▶ if (!$spec) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $spec) { } 134 1712 1846
elsif (not ref $spec) { } 1 1711 1712
elsif (builtin::reftype($spec) eq 'CODE') { } 489 1222 1711
710 134 $event = Test2::Compare::Event->new(
711 etype => $intype,
712 file => $caller[1],
713 lines => [$caller[2]],
714 ending => 0,
715 );
716 }
717 elsif (!ref $spec) {
718 1 croak "'$spec' is not a valid event specification";
719 }
720 elsif (reftype($spec) eq 'CODE') {
721 489 $event = build('Test2::Compare::Event', $spec);
722 489 $event->set_etype($intype);
723 489 $event->set_builder($spec);
724 489 ▶ $event->set_ending(0) unless defined $event->ending;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event->ending 486 3 489
725 }
726 else {
727 1222 my $refcheck = Test2::Compare::Hash->new(
728 inref => $spec,
729 file => $caller[1],
730 lines => [$caller[2]],
731 );
732 1222 $event = Test2::Compare::Event->new(
733 refcheck => $refcheck,
734 file => $caller[1],
735 lines => [$caller[2]],
736 etype => $intype,
737 ending => 0,
738 );
739 }
740  
741 1845 $event->add_prop('blessed' => $type);
742  
743 1845 ▶ return $event if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 85 1760 1845
744  
745 1760 ▶ defined( my $build = get_build() ) or croak "No current build!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $build = get_build()) or croak("No current build!") 1759 1 1760
746 1759 $build->add_item($event);
747 }
748  
749 171 1;
750  
751 __END__
« blib/lib/Test2/Compare/Bool.pm ↑ Summary blib/lib/Test2/Util/Grabber.pm »