blib/lib/Test2/Tools/Tiny.pm

filter: Statementstmt 99.3% 161 / 162 Branchbran 85.0% 51 / 60 Conditioncond 87.5% 14 / 16 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 29 / 29 Podpod 100% 16 / 16 totaltotal 95.8% 280 / 292 CC 36 SCAR 13.1
CC
36
Cov
95%
is
8.0
isnt
7.0
__ANON__
5.1
CRAP 3.7
« blib/lib/Test2/API/InterceptResult/Squasher.pm ↑ Summary blib/lib/Test2/API/Stack.pm »
1 package Test2::Tools::Tiny;
2 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 102 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 102 ▶use Test2::Util qw/try/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 102 ▶use Test2::API qw/context run_subtest test2_stack/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 102 ▶use Test2::Hub::Interceptor();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 102 ▶use Test2::Hub::Interceptor::Terminator();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 102 our $VERSION = '1.302226';
14  
15 102 ▶BEGIN { require Exporter; our @ISA = qw(Exporter) }
Subroutine BEGIN: called CC=1 SCAR=0.0
16 102 our @EXPORT = qw{
17 ok is isnt like unlike is_deeply diag note skip_all todo plan done_testing
18 warnings exception tests capture
19 };
20  
21 714 ▶sub ok($;$@) {
Subroutine ok: called CC=2 SCAR=6.9
Pod documented
22 714 my ($bool, $name, @diag) = @_;
23 714 my $ctx = context();
24  
25 714 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 701 13 714
26 13 return $ctx->fail_and_release($name, @diag);
27 }
28  
29 674 ▶sub is($$;$@) {
Subroutine is: called CC=8 SCAR=20.8
Pod documented
30 674 my ($got, $want, $name, @diag) = @_;
31 674 my $ctx = context();
32  
33 674 my $bool;
34 674 ▶ if (defined($got) && defined($want)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $got and defined $want) { } 613 61 674
elsif (defined $got xor defined $want) { } 2 59 61
Condition100%3/3
defined $got and defined $want
!ll&&!rl&&r
601613
MC/DC100%2/2
defined $got and defined $want
defined $got defined $want
Truth table100%3/3
A and B
A= defined $gotB= defined $want
ABresult
0X0
100
111
35 613 $bool = "$got" eq "$want";
36 }
37 ▶ elsif (defined($got) xor defined($want)) {
Condition75%3/4
defined $got xor defined $want
l&&rl&&!r!l&&r!l&&!r
01159
MC/DC100%2/2
defined $got xor defined $want
defined $got defined $want
Truth table75%3/4
A xor B
A= defined $gotB= defined $want
ABresult
110
101
011
000
38 2 $bool = 0;
39 }
40 else { # Both are undef
41 59 $bool = 1;
42 }
43  
44 674 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 670 4 674
45  
46 4 ▶ $got = '*NOT DEFINED*' unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 1 3 4
47 4 ▶ $want = '*NOT DEFINED*' unless defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 1 3 4
48 4 unshift @diag => (
49 "GOT: $got",
50 "EXPECTED: $want",
51 );
52  
53 4 return $ctx->fail_and_release($name, @diag);
54 }
55  
56 16 ▶sub isnt($$;$@) {
Subroutine isnt: called CC=7 SCAR=19.5
Pod documented
57 16 my ($got, $want, $name, @diag) = @_;
58 16 my $ctx = context();
59  
60 16 my $bool;
61 16 ▶ if (defined($got) && defined($want)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $got and defined $want) { } 13 3 16
elsif (defined $got xor defined $want) { } 2 1 3
Condition100%3/3
defined $got and defined $want
!ll&&!rl&&r
2113
MC/DC100%2/2
defined $got and defined $want
defined $got defined $want
Truth table100%3/3
A and B
A= defined $gotB= defined $want
ABresult
0X0
100
111
62 13 $bool = "$got" ne "$want";
63 }
64 ▶ elsif (defined($got) xor defined($want)) {
Condition75%3/4
defined $got xor defined $want
l&&rl&&!r!l&&r!l&&!r
0111
MC/DC100%2/2
defined $got xor defined $want
defined $got defined $want
Truth table75%3/4
A xor B
A= defined $gotB= defined $want
ABresult
110
101
011
000
65 2 $bool = 1;
66 }
67 else { # Both are undef
68 1 $bool = 0;
69 }
70  
71 16 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 14 2 16
72  
73 2 ▶ unshift @diag => "Strings are the same (they should not be)"
Branch: 1 branch
BranchTrueFalseTotal
unless $bool 2 0 2
74 unless $bool;
75  
76 2 return $ctx->fail_and_release($name, @diag);
77 }
78  
79 157 ▶sub like($$;$@) {
Subroutine like: called CC=4 SCAR=13.9
Pod documented
80 157 my ($thing, $pattern, $name, @diag) = @_;
81 157 my $ctx = context();
82  
83 157 my $bool;
84 157 ▶ if (defined($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thing) { } 156 1 157
85 156 $bool = "$thing" =~ $pattern;
86 156 ▶ unshift @diag => (
Branch: 1 branch
BranchTrueFalseTotal
unless $bool 1 155 156
87 "Value: $thing",
88 "Does not match: $pattern"
89 ) unless $bool;
90 }
91 else {
92 1 $bool = 0;
93 1 unshift @diag => "Got an undefined value.";
94 }
95  
96 157 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 155 2 157
97 2 return $ctx->fail_and_release($name, @diag);
98 }
99  
100 4 ▶sub unlike($$;$@) {
Subroutine unlike: called CC=4 SCAR=13.9
Pod documented
101 4 my ($thing, $pattern, $name, @diag) = @_;
102 4 my $ctx = context();
103  
104 4 my $bool;
105 4 ▶ if (defined($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thing) { } 3 1 4
106 3 $bool = "$thing" !~ $pattern;
107 3 ▶ unshift @diag => (
Branch: 1 branch
BranchTrueFalseTotal
unless $bool 1 2 3
108 "Unexpected pattern match (it should not match)",
109 "Value: $thing",
110 "Matches: $pattern"
111 ) unless $bool;
112 }
113 else {
114 1 $bool = 0;
115 1 unshift @diag => "Got an undefined value.";
116 }
117  
118 4 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 2 2 4
119 2 return $ctx->fail_and_release($name, @diag);
120 }
121  
122 405 ▶sub is_deeply($$;$@) {
Subroutine is_deeply: called CC=2 SCAR=6.9
Pod documented
123 405 my ($got, $want, $name, @diag) = @_;
124 405 my $ctx = context();
125  
126 102 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 405 require Data::Dumper;
128  
129 # Otherwise numbers might be unquoted
130 405 local $Data::Dumper::Useperl = 1;
131  
132 405 local $Data::Dumper::Sortkeys = 1;
133 405 local $Data::Dumper::Deparse = 1;
134 405 local $Data::Dumper::Freezer = 'XXX';
135 368 ▶ local *UNIVERSAL::XXX = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
136 368 my ($thing) = @_;
137 368 ▶ if (ref($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing) 368 0 368
138 368 ▶ $thing = {%$thing} if "$thing" =~ m/=HASH/;
Branch: 1 branch
BranchTrueFalseTotal
if "$thing" =~ /=HASH/ 299 69 368
139 368 ▶ $thing = [@$thing] if "$thing" =~ m/=ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
if "$thing" =~ /=ARRAY/ 45 323 368
140 368 ▶ $thing = \"$$thing" if "$thing" =~ m/=SCALAR/;
Branch: 1 branch
BranchTrueFalseTotal
if "$thing" =~ /=SCALAR/ 0 368 368
141 }
142 368 $_[0] = $thing;
143 405 };
144  
145 405 my $g = Data::Dumper::Dumper($got);
146 405 my $w = Data::Dumper::Dumper($want);
147  
148 405 my $bool = $g eq $w;
149  
150 405 ▶ return $ctx->pass_and_release($name) if $bool;
Branch: 1 branch
BranchTrueFalseTotal
if $bool 404 1 405
151 1 return $ctx->fail_and_release($name, $g, $w, @diag);
152 }
153  
154 8 ▶sub diag {
Subroutine diag: called CC=1 SCAR=0.0
Pod documented
155 8 my $ctx = context();
156 8 $ctx->diag(join '', @_);
157 8 $ctx->release;
158 }
159  
160 11 ▶sub note {
Subroutine note: called CC=1 SCAR=0.0
Pod documented
161 11 my $ctx = context();
162 11 $ctx->note(join '', @_);
163 11 $ctx->release;
164 }
165  
166 7 ▶sub skip_all {
Subroutine skip_all: called CC=2 SCAR=9.2
Pod documented
167 7 my ($reason) = @_;
168 7 my $ctx = context();
169 7 $ctx->plan(0, SKIP => $reason);
170 0 ▶ $ctx->release if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 0 0 0
171 }
172  
173 1 ▶sub todo {
Subroutine todo: called CC=2 SCAR=6.9
Pod documented
174 1 my ($reason, $sub) = @_;
175 1 my $ctx = context();
176  
177 # This code is mostly copied from Test2::Todo in the Test2-Suite
178 # distribution.
179 1 my $hub = test2_stack->top;
180 my $filter = $hub->pre_filter(
181 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
182 5 my ($active_hub, $event) = @_;
183 5 ▶ if ($active_hub == $hub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_hub == $hub) { } 2 3 5
184 2 ▶ $event->set_todo($reason) if $event->can('set_todo');
Branch: 1 branch
BranchTrueFalseTotal
if $event->can("set_todo") 1 1 2
185 2 $event->add_amnesty({tag => 'TODO', details => $reason});
186 }
187 else {
188 3 $event->add_amnesty({tag => 'TODO', details => $reason, inherited => 1});
189 }
190 5 return $event;
191 },
192 1 inherit => 1,
193 todo => $reason,
194 );
195 1 $sub->();
196 1 $hub->pre_unfilter($filter);
197  
198 1 ▶ $ctx->release if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 1 0 1
199 }
200  
201 7 ▶sub plan {
Subroutine plan: called CC=1 SCAR=0.0
Pod documented
202 7 my ($max) = @_;
203 7 my $ctx = context();
204 7 $ctx->plan($max);
205 7 $ctx->release;
206 }
207  
208 89 ▶sub done_testing {
Subroutine done_testing: called CC=1 SCAR=0.0
Pod documented
209 89 my $ctx = context();
210 89 $ctx->done_testing;
211 89 my $count = $ctx->hub->count;
212 89 $ctx->release;
213 89 return $count;
214 }
215  
216 11 ▶sub warnings(&) {
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
217 11 my $code = shift;
218 11 my @warnings;
219 11 ▶ local $SIG{__WARN__} = sub { push @warnings => @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
220 11 $code->();
221 11 return \@warnings;
222 }
223  
224 84 ▶sub exception(&) {
Subroutine exception: called CC=3 SCAR=11.0
Pod documented
225 84 my $code = shift;
226 84 local ($@, $!, $SIG{__DIE__});
227 84 my $ok = eval { $code->(); 1 };
228 84 ▶ my $error = $@ || 'SQUASHED ERROR';
Condition100%2/2
$@ || 'SQUASHED ERROR'
l!l
768
MC/DC100%1/1
$@ || 'SQUASHED ERROR'
$@
Truth table100%2/2
A
A= $@
Aresult
11
00
229 84 ▶ return $ok ? undef : $error;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 8 76 84
230 }
231  
232 92 ▶sub tests {
Subroutine tests: called CC=2 SCAR=6.9
Pod documented
233 92 my ($name, $code) = @_;
234 92 my $ctx = context();
235  
236 92 my $be = caller->can('before_each');
237  
238 92 ▶ $be->($name) if $be;
Branch: 1 branch
BranchTrueFalseTotal
if $be 4 88 92
239  
240 92 my $bool = run_subtest($name, $code, 1);
241  
242 91 $ctx->release;
243  
244 91 return $bool;
245 }
246  
247 7 ▶sub capture(&) {
Subroutine capture: called CC=2 SCAR=7.0
Pod documented
248 7 my $code = shift;
249  
250 7 my ($err, $out) = ("", "");
251  
252 7 my $handles = test2_stack->top->format->handles;
253 7 my ($ok, $e);
254 {
255 7 my ($out_fh, $err_fh);
256  
257 7 ▶ ($ok, $e) = try {
Subroutine __ANON__: called CC=3 SCAR=11.4
258 # Scalar refs as filehandles were added in 5.8.
259 7 ▶ open($out_fh, '>', \$out) or die "Failed to open a temporary STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $out_fh, ">", \$out or die "Failed to open a temporary STDOUT: $!" 7 0 7
260 7 ▶ open($err_fh, '>', \$err) or die "Failed to open a temporary STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $err_fh, ">", \$err or die "Failed to open a temporary STDERR: $!" 7 0 7
261  
262 7 test2_stack->top->format->set_handles([$out_fh, $err_fh, $out_fh]);
263  
264 7 $code->();
265 7 };
266 }
267 7 test2_stack->top->format->set_handles($handles);
268  
269 7 ▶ die $e unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 7 7
270  
271 7 $err =~ s/ $/_/mg;
272 7 $out =~ s/ $/_/mg;
273  
274 return {
275 7 STDOUT => $out,
276 STDERR => $err,
277 };
278 }
279  
280 102 1;
281  
282 __END__
« blib/lib/Test2/API/InterceptResult/Squasher.pm ↑ Summary blib/lib/Test2/API/Stack.pm »