blib/lib/Test/Lazy/Tester.pm

filter: Statementstmt 85.2% 121 / 142 Branchbran 63.8% 23 / 36 Conditioncond 38.1% 8 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 78.5% 22 / 28 Podpod 100% 6 / 6 totaltotal 73.8% 181 / 245 CC 26 SCAR 23.7
CC
26
Cov
76%
try
33.6
_test
5.2
new
4.1
CRAP 10.7
↑ Summary lib/Test/Lazy/Template.pm »
1 package Test::Lazy::Tester;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Test::Lazy::Tester
9  
10 =head1 SYNOPSIS
11  
12 use Test::Lazy::Tester;
13  
14 $tester = Test::Lazy::Tester->new;
15  
16 # Will evaluate the code and check it:
17 $tester->try('qw/a/' => eq => 'a');
18 $tester->try('qw/a/' => ne => 'b');
19 $tester->try('qw/a/' => is => ['a']);
20  
21 # Don't evaluate, but still compare:
22 $tester->check(1 => is => 1);
23 $tester->check(0 => isnt => 1);
24 $tester->check(a => like => qr/[a-zA-Z]/);
25 $tester->check(0 => unlike => qr/a-zA-Z]/);
26 $tester->check(1 => '>' => 0);
27 $tester->check(0 => '<' => 1);
28  
29 # A failure example:
30  
31 $tester->check([qw/a b/] => is => [qw/a b c/]);
32  
33 # Failed test '['a','b'] is ['a','b','c']'
34 # Compared array length of $data
35 # got : array with 2 element(s)
36 # expect : array with 3 element(s)
37  
38  
39 # Custom test explanation:
40  
41 $tester->try('2 + 2' => '==' => 5, "Math is hard: %?");
42  
43 # Failed test 'Math is hard: 2 + 2 == 5'
44 # got: 4
45 # expected: 5
46  
47 =head1 DESCRIPTION
48  
49 See L<Test::Lazy> for more information.
50  
51 =head1 METHODS
52  
53 =head2 Test::Lazy::Tester->new( cmp_scalar => ?, cmp_structure => ?, render => ? )
54  
55 Create a new Test::Lazy::Tester object, optionally amending the scalar comparison, structure comparison, and render subroutines
56 using the supplied hashes.
57  
58 For now, more information on customization can be gotten by:
59  
60 perldoc -m Test::Lazy::Tester
61  
62 =head2 $tester->check( <got>, <compare>, <expect>, [ <notice> ] )
63  
64 See L<Test::Lazy::check> for details.
65  
66 =head2 $tester->try( <got>, <compare>, <expect>, [ <notice> ] )
67  
68 See L<Test::Lazy::try> for details.
69  
70 =head2 $tester->template()
71  
72 Creates a C<Test::Lazy::Template> using $tester as the basis.
73  
74 See L<Test::Lazy::Template> for more details.
75  
76 Returns a new L<Test::Lazy::Template> object.
77  
78 =head2 $tester->render_value( <value> )
79  
80 Render a gotten or expected value to a form suitable for the test notice/explanation.
81  
82 This method will consult the $tester->render hash to see what if should do based on 'ref <value>'.
83 By default, ARRAY and HASH are handled by Data::Dumper using the following:
84  
85 local $Data::Dumper::Indent = 0;
86 local $Data::Dumper::Varname = 0;
87 local $Data::Dumper::Terse = 1;
88  
89 An undef value is a special case, handled by the $tester->render->{undef} subroutine.
90 By default, the subroutine returns the string "undef"
91  
92 =head2 $tester->render_notice( <left>, <compare>, <right>, <notice> )
93  
94 Render the text explantaion message. You don't need to mess with this.
95  
96 =cut
97  
98 2 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 2 __PACKAGE__->mk_accessors(qw/render cmp_scalar cmp_structure/);
101  
102 2 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 2 ▶use Test::Builder();
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 2 my $deparser;
108 2 eval {
109 2 require B::Deparse;
110 2 $deparser = B::Deparse->new;
111 2 $deparser->ambient_pragmas(strict => 'all', warnings => 'all');
112 };
113 2 ▶undef $deparser if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
114  
115 my %base_cmp_scalar = (
116 30 ▶ ok => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 30 Test::More::ok($_[0], $_[2])
118 },
119  
120 2 ▶ not_ok => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 Test::More::ok(! $_[0], $_[2])
122 },
123  
124 24 ▶ (map { my $mtd = $_; $_ => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 60 Test::More::cmp_ok($_[0] => $mtd => $_[1], $_[2])
126 24 } }
127 qw/< > <= >= lt gt le ge == != eq ne/),
128  
129 2 ▶ (map { my $method = $_; $_ => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
131 20 "Test::More::$method"->($_[0], $_[1], $_[2])
132 8 } }
133 qw/is isnt like unlike/),
134 );
135  
136 my %base_cmp_structure = (
137 0 ▶ ok => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 Test::More::ok($_[0], $_[2])
139 },
140  
141 0 ▶ not_ok => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 Test::More::ok(! $_[0], $_[2])
143 },
144  
145 0 ▶ (map { $_ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 Test::Deep::cmp_bag($_[0], $_[1], $_[2]);
147 6 } }
148 qw/bag same_bag samebag/),
149  
150 0 ▶ (map { $_ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 Test::Deep::cmp_set($_[0], $_[1], $_[2]);
152 6 } }
153 qw/set same_set sameset/),
154  
155 2 ▶ (map { $_ => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
156 2 Test::Deep::cmp_deeply($_[0], $_[1], $_[2]);
157 10 } }
158 qw/same is like eq ==/),
159  
160 2 ▶ (map { $_ => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
161 0 Test::More::ok(!Test::Deep::eq_deeply($_[0], $_[1]), $_[2]);
162 8 } }
163 qw/isnt unlink ne !=/),
164 );
165  
166 my %base_render = (
167 0 ▶ ARRAY => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 local $Data::Dumper::Indent = 0;
169 0 local $Data::Dumper::Varname = 0;
170 0 local $Data::Dumper::Terse = 1;
171 0 my $self = shift;
172 0 my $value = shift;
173 0 return Dumper($value);
174 },
175  
176 2 ▶ HASH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 2 local $Data::Dumper::Indent = 0;
178 2 local $Data::Dumper::Varname = 0;
179 2 local $Data::Dumper::Terse = 1;
180 2 my $self = shift;
181 2 my $value = shift;
182 2 return Dumper($value);
183 },
184  
185 32 ▶ undef => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
186 32 return "undef";
187 },
188 2 );
189  
190 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
191 2 my $self = bless {}, shift;
192 2 local %_ = @_;
193 2 ▶ $self->{cmp_scalar} = { %base_cmp_scalar, %{ $_{cmp_scalar} || {} } };
Condition50%1/2
$_{'cmp_scalar'} || {}
l!l
02
MC/DC0%0/1
$_{'cmp_scalar'} || {}
$_{'cmp_scalar'}
Truth table50%1/2
A
A= $_{'cmp_scalar'}
Aresult
11
00
194 2 ▶ $self->{cmp_structure} = { %base_cmp_structure, %{ $_{cmp_structure} || {} } };
Condition50%1/2
$_{'cmp_structure'} || {}
l!l
02
MC/DC0%0/1
$_{'cmp_structure'} || {}
$_{'cmp_structure'}
Truth table50%1/2
A
A= $_{'cmp_structure'}
Aresult
11
00
195 2 ▶ $self->{render} = { %base_render, %{ $_{base_render} || {} } };
Condition50%1/2
$_{'base_render'} || {}
l!l
02
MC/DC0%0/1
$_{'base_render'} || {}
$_{'base_render'}
Truth table50%1/2
A
A= $_{'base_render'}
Aresult
11
00
196 2 return $self;
197 }
198  
199 114 ▶sub render_notice {
Subroutine render_notice: called CC=3 SCAR=11.1
Pod documented
200 114 my $self = shift;
201 114 my ($left, $compare, $right, $notice, $length) = @_;
202  
203 # my $_notice = $length == 4 ? "$left $compare $right" : "$left $compare";
204 114 my $_notice = "$left $compare $right";
205 114 ▶ if (defined $notice) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $notice) { } 70 44 114
206 70 ▶ if ($notice =~ m/%\?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notice =~ /%\?/) { } 0 70 70
207 0 $notice =~ s/%\?/$_notice/g;
208 }
209 else { # Old version, deprecated.
210 70 $notice =~ s/%(?!%)/%?/g;
211 70 $notice =~ s/%%/%/g;
212 70 $notice =~ s/%\?/$_notice/g;
213 }
214 }
215 else {
216 44 $notice = $_notice;
217 }
218  
219 114 return $notice;
220 }
221  
222 136 ▶sub render_value {
Subroutine render_value: called CC=4 SCAR=13.9
Pod documented
223 136 my $self = shift;
224 136 my $value = shift;
225  
226 136 my $type = ref $value;
227 136 ▶ $type = "undef" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 32 104 136
228  
229 136 ▶ return $value unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 98 38 136
230 38 ▶ return $value unless my $renderer = $self->render->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless my $renderer = $self->render->{$type} 4 34 38
231 34 return $renderer->($self, $value);
232 }
233  
234 114 ▶sub _test {
Subroutine _test: called CC=5 SCAR=16.4
235 114 my $self = shift;
236 114 my ($compare, $got, $expect, $notice) = @_;
237  
238 114 local $Test::Builder::Level = $Test::Builder::Level + 1;
239  
240 114 my $cmp = $compare;
241 114 ▶ if (ref $cmp eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmp eq 'CODE') { } 0 114 114
242 0 Test::More::ok($cmp->($got, $expect), $notice);
243 }
244 else {
245 114 ▶ my $structure = ref $expect eq "ARRAY" || ref $expect eq "HASH";
Condition66%2/3
ref $expect eq 'ARRAY' || ref $expect eq 'HASH'
l!l&&r!l&&!r
02112
MC/DC50%1/2
ref $expect eq 'ARRAY' || ref $expect eq 'HASH'
ref $expect eq 'ARRAY' ref $expect eq 'HASH'
Truth table66%2/3
A || B
A= ref $expect eq 'ARRAY'B= ref $expect eq 'HASH'
ABresult
1X1
011
000
246 114 my $scalar = ! $structure;
247  
248 114 ▶ my $cmp_source = $scalar ? $self->cmp_scalar : $self->cmp_structure;
Branch: 1 branch
BranchTrueFalseTotal
$scalar ? : 112 2 114
249  
250 114 ▶ die "Don't know how to compare via ($compare)" unless $cmp = $cmp_source->{$cmp};
Branch: 1 branch
BranchTrueFalseTotal
unless $cmp = $cmp_source->{$cmp} 0 114 114
251 114 local $Test::Builder::Level = $Test::Builder::Level + 1;
252 114 $cmp->($got, $expect, $notice);
253 }
254 }
255  
256 22 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
257 22 my $self = shift;
258 22 my ($got, $compare, $expect, $notice) = @_;
259 22 my $length = @_;
260  
261 22 my $left = $self->render_value($got);
262 22 my $right = $self->render_value($expect);
263 22 $notice = $self->render_notice($left, $compare, $right, $notice, $length);
264  
265 22 local $Test::Builder::Level = $Test::Builder::Level + 1;
266  
267 22 return $self->_test($compare, $got, $expect, $notice);
268 }
269  
270 92 ▶sub try {
Subroutine try: called CC=14 SCAR=35.2
Pod documented
271 92 my $self = shift;
272 92 my ($statement, $compare, $expect, $notice) = @_;
273 92 my $length = @_;
274  
275 92 ▶ my @got = ref $statement eq "CODE" ? $statement->() : eval $statement;
Branch: 1 branch
BranchTrueFalseTotal
ref $statement eq 'CODE' ? : 0 92 92
276 92 ▶ die "$statement: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 92 92
277 92 my $got;
278 92 ▶ if (@got > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@got > 1) { } 2 90 92
279 2 ▶ if (ref $expect eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expect eq 'ARRAY') { } 0 2 2
elsif (ref $expect eq 'HASH') { } 2 0 2
280 0 $got = \@got;
281 }
282 elsif (ref $expect eq "HASH") {
283 2 $got = { @got };
284 }
285 else {
286 0 $got = scalar @got;
287 }
288 }
289 else {
290 90 ▶ if (ref $expect eq "ARRAY" && (! @got || ref $got[0] ne "ARRAY")) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expect eq 'ARRAY' and !@got || ref $got[0] ne 'ARRAY') { } 0 90 90
elsif (ref $expect eq 'HASH' and not @got) { } 0 90 90
Condition16%1/6
ref $expect eq "ARRAY" and !@got || ref $got[0] ne "ARRAY"
!ll&&!rl&&r
9000
not @got or ref $got[0] ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $expect eq "ARRAY" and !@got || ref $got[0] ne "ARRAY"
ref $expect eq "ARRAY" not @got ref $got[0] ne "ARRAY"
Truth table25%1/4
A and not(B) or C
A= ref $expect eq "ARRAY"B= not @gotC= ref $got[0] ne "ARRAY"
ABCresult
0XX0
1000
11X1
1011
291 0 $got = \@got;
292 }
293 ▶ elsif (ref $expect eq "HASH" && ! @got) {
Condition33%1/3
ref $expect eq "HASH" and not @got
!ll&&!rl&&r
9000
MC/DC0%0/2
ref $expect eq "HASH" and not @got
ref $expect eq "HASH" not @got
Truth table33%1/3
A and not(B)
A= ref $expect eq "HASH"B= not @got
ABresult
0X0
100
111
294 0 $got = { };
295 }
296 else {
297 90 $got = $got[0];
298 }
299 }
300
301 92 my $left;
302 92 ▶ if (ref $statement eq "CODE" && $deparser) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $statement eq 'CODE' and $deparser) { } 0 92 92
Condition33%1/3
ref $statement eq "CODE" and $deparser
!ll&&!rl&&r
9200
MC/DC0%0/2
ref $statement eq "CODE" and $deparser
ref $statement eq "CODE" $deparser
Truth table33%1/3
A and B
A= ref $statement eq "CODE"B= $deparser
ABresult
0X0
100
111
303 0 my $deparse = $deparser->coderef2text($statement);
304 0 my @deparse = split m/\n\s*/, $deparse;
305 0 ▶ $deparse = join ' ', "sub", @deparse if 3 == @deparse;
Branch: 1 branch
BranchTrueFalseTotal
if 3 == @deparse 0 0 0
306 0 $left = $deparse;
307 }
308 else {
309 92 $left = $statement;
310 }
311 92 my $right = $self->render_value($expect);
312 92 $notice = $self->render_notice($left, $compare, $right, $notice, $length);
313  
314 92 local $Test::Builder::Level = $Test::Builder::Level + 1;
315  
316 92 return $self->_test($compare, $got, $expect, $notice);
317 }
318  
319 1 ▶sub template {
Subroutine template: called CC=1 SCAR=0.0
Pod documented
320 1 my $self = shift;
321 1 require Test::Lazy::Template;
322 1 return Test::Lazy::Template->new($self, @_);
323 }
324  
325 2 1;
↑ Summary lib/Test/Lazy/Template.pm »