blib/lib/Test2/Compare.pm

filter: Statementstmt 99.0% 100 / 101 Branchbran 91.6% 55 / 60 Conditioncond 100% 24 / 24 MC/DCmcdc 100% 14 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 97.3% 218 / 224 CC 39 SCAR 25.5
CC
39
Cov
97%
_convert
24.0
convert
8.2
pop_build
6.0
CRAP 12.8
« blib/lib/Test2/V0.pm ↑ Summary lib/Test/Tester.pm »
1 package Test2::Compare;
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 our $VERSION = '1.302226';
6  
7 176 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 176 ▶use Test2::Util qw/try/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 176 ▶use Test2::Util::Ref qw/rtype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 176 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 176 our @EXPORT_OK = qw{
14 compare
15 get_build push_build pop_build build
16 strict_convert relaxed_convert convert
17 };
18 176 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1832 ▶sub compare {
Subroutine compare: called CC=1 SCAR=0.0
Pod documented
21 1832 my ($got, $check, $convert) = @_;
22  
23 1832 $check = $convert->($check);
24  
25 1832 return $check->run(
26 id => undef,
27 got => $got,
28 exists => 1,
29 convert => $convert,
30 seen => {},
31 );
32 }
33  
34 176 my @BUILD;
35  
36 6782 ▶sub get_build { @BUILD ? $BUILD[-1] : undef }
Subroutine get_build: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@BUILD ? : 6773 9 6782
37 1 ▶sub push_build { push @BUILD => $_[0] }
Subroutine push_build: called CC=1 SCAR=0.0
Pod documented
38  
39 4 ▶sub pop_build {
Subroutine pop_build: called CC=6 SCAR=17.9
Pod documented
40 4 ▶ return pop @BUILD if @BUILD && $_[0] && $BUILD[-1] == $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @BUILD and $_[0] and $BUILD[-1] == $_[0] 1 3 4
Condition100%6/6
@BUILD and $_[0] and $BUILD[-1] == $_[0]
!ll&&!rl&&r
211
@BUILD and $_[0]
!ll&&!rl&&r
112
MC/DC100%3/3
@BUILD and $_[0] and $BUILD[-1] == $_[0]
@BUILD $_[0] $BUILD[-1] == $_[0]
Truth table100%4/4
A and B and C
A= @BUILDB= $_[0]C= $BUILD[-1] == $_[0]
ABCresult
0XX0
10X0
1100
1111
41 3 ▶ my $have = @BUILD ? "$BUILD[-1]" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
@BUILD ? : 2 1 3
42 3 ▶ my $want = $_[0] ? "$_[0]" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 2 1 3
43 3 croak "INTERNAL ERROR: Attempted to pop incorrect build, have $have, tried to pop $want";
44 }
45  
46 2154 ▶sub build {
Subroutine build: called CC=3 SCAR=11.0
Pod documented
47 2154 my ($class, $code) = @_;
48  
49 2154 my @caller = caller(1);
50  
51 2154 ▶ die "'$caller[3]\()' should not be called in void context in $caller[1] line $caller[2]\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 2153 2154
52 unless defined(wantarray);
53  
54 2153 my $build = $class->new(builder => $code, called => \@caller);
55  
56 2153 push @BUILD => $build;
57 2153 ▶ my ($ok, $err) = try { $code->($build); 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2153 pop @BUILD;
59 2153 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 14 2139 2153
60  
61 2139 $build->verify_build;
62  
63 2129 return $build;
64 }
65  
66 9634 ▶sub strict_convert { convert($_[0], { implicit_end => 1, use_regex => 0, use_code => 0 }) }
Subroutine strict_convert: called CC=1 SCAR=0.0
Pod documented
67 3177 ▶sub relaxed_convert { convert($_[0], { implicit_end => 0, use_regex => 1, use_code => 1 }) }
Subroutine relaxed_convert: called CC=1 SCAR=0.0
Pod documented
68  
69 176 my $CONVERT_LOADED = 0;
70 176 my %ALLOWED_KEYS = ( implicit_end => 1, use_regex => 1, use_code => 1 );
71 12812 ▶sub convert {
Subroutine convert: called CC=8 SCAR=21.1
Pod documented
72 12812 my ($thing, $config) = @_;
73  
74 12812 ▶ unless($CONVERT_LOADED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CONVERT_LOADED) 125 12687 12812
75 125 require Test2::Compare::Array;
76 125 require Test2::Compare::Base;
77 125 require Test2::Compare::Custom;
78 125 require Test2::Compare::DeepRef;
79 125 require Test2::Compare::Hash;
80 125 require Test2::Compare::Pattern;
81 125 require Test2::Compare::Ref;
82 125 require Test2::Compare::Regex;
83 125 require Test2::Compare::Scalar;
84 125 require Test2::Compare::String;
85 125 require Test2::Compare::Undef;
86 125 require Test2::Compare::Wildcard;
87 125 $CONVERT_LOADED = 1;
88 }
89  
90 12812 ▶ if (ref($config)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config) { } 12811 1 12812
91 12811 my $bad = join ', ' => grep { !$ALLOWED_KEYS{$_} } keys %$config;
92 12811 ▶ croak "The following config options are not understood by convert(): $bad" if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 0 12811 12811
93 12811 ▶ $config->{implicit_end} = 1 unless defined $config->{implicit_end};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"implicit_end"} 0 12811 12811
94 12811 ▶ $config->{use_regex} = 1 unless defined $config->{use_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"use_regex"} 0 12811 12811
95 12811 ▶ $config->{use_code} = 0 unless defined $config->{use_code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"use_code"} 0 12811 12811
96 }
97 else { # Legacy...
98 1 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) { } 0 1 1
99 0 $config = {
100 implicit_end => 1,
101 use_regex => 0,
102 use_code => 0,
103 };
104 }
105 else {
106 1 $config = {
107 implicit_end => 0,
108 use_regex => 1,
109 use_code => 1,
110 };
111 }
112 }
113  
114 12812 return _convert($thing, $config);
115 }
116  
117 16483 ▶sub _convert {
Subroutine _convert: called CC=24 SCAR=31.8
118 16483 my ($thing, $config) = @_;
119  
120 16483 ▶ return Test2::Compare::Undef->new()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 203 16280 16483
121 unless defined $thing;
122  
123 16280 ▶ if (blessed($thing) && $thing->isa('Test2::Compare::Base')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and $thing->isa("Test2::Compare::Base")) 7989 8291 16280
Condition100%3/3
builtin::blessed($thing) and $thing->isa("Test2::Compare::Base")
!ll&&!rl&&r
79723197989
MC/DC100%2/2
builtin::blessed($thing) and $thing->isa("Test2::Compare::Base")
builtin::blessed($thing) $thing->isa("Test2::Compare::Base")
Truth table100%3/3
A and B
A= builtin::blessed($thing)B= $thing->isa("Test2::Compare::Base")
ABresult
0X0
100
111
124 7989 ▶ if ($config->{implicit_end} && $thing->can('set_ending') && !defined $thing->ending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"implicit_end"} and $thing->can("set_ending") and not defined $thing->ending) 159 7830 7989
Condition100%6/6
$config->{"implicit_end"} and $thing->can("set_ending") and not defined $thing->ending
!ll&&!rl&&r
54532377159
$config->{"implicit_end"} and $thing->can("set_ending")
!ll&&!rl&&r
243930142536
MC/DC100%3/3
$config->{"implicit_end"} and $thing->can("set_ending") and not defined $thing->ending
$config->{"implicit_end"} $thing->can("set_ending") not defined $thing->ending
Truth table100%4/4
A and B and not(C)
A= $config->{"implicit_end"}B= $thing->can("set_ending")C= not defined $thing->ending
ABCresult
0XX0
10X0
1100
1111
125 159 my $clone = $thing->clone;
126 159 $clone->set_ending('implicit');
127 159 return $clone;
128 }
129  
130 7830 ▶ return $thing unless $thing->isa('Test2::Compare::Wildcard');
Branch: 1 branch
BranchTrueFalseTotal
unless $thing->isa("Test2::Compare::Wildcard") 4172 3658 7830
131 3658 my $newthing = _convert($thing->expect, $config);
132 3658 ▶ $newthing->set_builder($thing->builder) unless $newthing->builder;
Branch: 1 branch
BranchTrueFalseTotal
unless $newthing->builder 2382 1276 3658
133 3658 ▶ $newthing->set_file($thing->_file) unless $newthing->_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $newthing->_file 3450 208 3658
134 3658 ▶ $newthing->set_lines($thing->_lines) unless $newthing->_lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $newthing->_lines 3450 208 3658
135 3658 return $newthing;
136 }
137  
138 8291 my $type = rtype($thing);
139  
140 8291 ▶ return Test2::Compare::Array->new(inref => $thing, $config->{implicit_end} ? (ending => 1) : ())
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "ARRAY" 439 7852 8291
$config->{'implicit_end'} ? : 347 92 439
141 if $type eq 'ARRAY';
142  
143 7852 ▶ return Test2::Compare::Hash->new(inref => $thing, $config->{implicit_end} ? (ending => 1) : ())
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "HASH" 338 7514 7852
$config->{'implicit_end'} ? : 258 80 338
144 if $type eq 'HASH';
145  
146 return Test2::Compare::Pattern->new(
147 pattern => $thing,
148 stringify_got => 1,
149 7514 ▶ ) if $config->{use_regex} && $type eq 'REGEXP';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"use_regex"} and $type eq "REGEXP" 245 7269 7514
Condition100%3/3
$config->{"use_regex"} and $type eq "REGEXP"
!ll&&!rl&&r
56451624245
MC/DC100%2/2
$config->{"use_regex"} and $type eq "REGEXP"
$config->{"use_regex"} $type eq "REGEXP"
Truth table100%3/3
A and B
A= $config->{"use_regex"}B= $type eq "REGEXP"
ABresult
0X0
100
111
150  
151 return Test2::Compare::Custom->new(code => $thing)
152 7269 ▶ if $config->{use_code} && $type eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"use_code"} and $type eq "CODE" 2 7267 7269
Condition100%3/3
$config->{"use_code"} and $type eq "CODE"
!ll&&!rl&&r
564516222
MC/DC100%2/2
$config->{"use_code"} and $type eq "CODE"
$config->{"use_code"} $type eq "CODE"
Truth table100%3/3
A and B
A= $config->{"use_code"}B= $type eq "CODE"
ABresult
0X0
100
111
153  
154 7267 ▶ return Test2::Compare::Regex->new(input => $thing)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "REGEXP" 4 7263 7267
155 if $type eq 'REGEXP';
156  
157 7263 ▶ if ($type eq 'SCALAR' || $type eq 'VSTRING') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" or $type eq "VSTRING") 13 7250 7263
Condition100%3/3
$type eq "SCALAR" or $type eq "VSTRING"
l!l&&r!l&&!r
947250
MC/DC100%2/2
$type eq "SCALAR" or $type eq "VSTRING"
$type eq "SCALAR" $type eq "VSTRING"
Truth table100%3/3
A or B
A= $type eq "SCALAR"B= $type eq "VSTRING"
ABresult
1X1
011
000
158 13 my $nested = _convert($$thing, $config);
159 13 return Test2::Compare::Scalar->new(item => $nested);
160 }
161  
162 7250 ▶ return Test2::Compare::DeepRef->new(input => $thing)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "REF" 4 7246 7250
163 if $type eq 'REF';
164  
165 7246 ▶ return Test2::Compare::Ref->new(input => $thing)
Branch: 1 branch
BranchTrueFalseTotal
if $type 32 7214 7246
166 if $type;
167  
168 # is() will assume string and use 'eq'
169 7214 return Test2::Compare::String->new(input => $thing);
170 }
171  
172 176 1;
173  
174 __END__
« blib/lib/Test2/V0.pm ↑ Summary lib/Test/Tester.pm »