Perlito5X/Test2/Tools/Tiny.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 305 CC 26 SCAR 27.7
CC
26
Cov
0%
is
42.0
is_deeply
42.0
isnt
20.0
CRAP 15.9
1 package Test2::Tools::Tiny;
2 use strict;
3 use warnings;
4  
5 use Scalar::Util qw/blessed/;
6  
7 use Test2::Util qw/try/;
8 use Test2::API qw/context run_subtest test2_stack/;
9  
10 use Test2::Hub::Interceptor();
11 use Test2::Hub::Interceptor::Terminator();
12  
13 our $VERSION = '1.302073';
14  
15 BEGIN { require Exporter; our @ISA = qw(Exporter) }
16 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 sub ok($;$@) {
22 my ($bool, $name, @diag) = @_;
23 my $ctx = context();
24 $ctx->ok($bool, $name, \@diag);
25 $ctx->release;
26 return $bool ? 1 : 0;
27 }
28  
29 sub is($$;$@) {
30 my ($got, $want, $name, @diag) = @_;
31 my $ctx = context();
32  
33 my $bool;
34 if (defined($got) && defined($want)) {
35 $bool = "$got" eq "$want";
36 }
37 elsif (defined($got) xor defined($want)) {
38 $bool = 0;
39 }
40 else { # Both are undef
41 $bool = 1;
42 }
43  
44 unless ($bool) {
45 $got = '*NOT DEFINED*' unless defined $got;
46 $want = '*NOT DEFINED*' unless defined $want;
47 unshift @diag => (
48 "GOT: $got",
49 "EXPECTED: $want",
50 );
51 }
52  
53 $ctx->ok($bool, $name, \@diag);
54 $ctx->release;
55 return $bool;
56 }
57  
58 sub isnt($$;$@) {
59 my ($got, $want, $name, @diag) = @_;
60 my $ctx = context();
61  
62 my $bool;
63 if (defined($got) && defined($want)) {
64 $bool = "$got" ne "$want";
65 }
66 elsif (defined($got) xor defined($want)) {
67 $bool = 1;
68 }
69 else { # Both are undef
70 $bool = 0;
71 }
72  
73 unshift @diag => "Strings are the same (they should not be)"
74 unless $bool;
75  
76 $ctx->ok($bool, $name, \@diag);
77 $ctx->release;
78 return $bool;
79 }
80  
81 sub like($$;$@) {
82 my ($thing, $pattern, $name, @diag) = @_;
83 my $ctx = context();
84  
85 my $bool;
86 if (defined($thing)) {
87 $bool = "$thing" =~ $pattern;
88 unshift @diag => (
89 "Value: $thing",
90 "Does not match: $pattern"
91 ) unless $bool;
92 }
93 else {
94 $bool = 0;
95 unshift @diag => "Got an undefined value.";
96 }
97  
98 $ctx->ok($bool, $name, \@diag);
99 $ctx->release;
100 return $bool;
101 }
102  
103 sub unlike($$;$@) {
104 my ($thing, $pattern, $name, @diag) = @_;
105 my $ctx = context();
106  
107 my $bool;
108 if (defined($thing)) {
109 $bool = "$thing" !~ $pattern;
110 unshift @diag => (
111 "Unexpected pattern match (it should not match)",
112 "Value: $thing",
113 "Matches: $pattern"
114 ) unless $bool;
115 }
116 else {
117 $bool = 0;
118 unshift @diag => "Got an undefined value.";
119 }
120  
121 $ctx->ok($bool, $name, \@diag);
122 $ctx->release;
123 return $bool;
124 }
125  
126 sub is_deeply($$;$@) {
127 my ($got, $want, $name, @diag) = @_;
128 my $ctx = context();
129  
130 no warnings 'once';
131 require Data::Dumper;
132 local $Data::Dumper::Sortkeys = 1;
133 local $Data::Dumper::Deparse = 1;
134 local $Data::Dumper::Freezer = 'XXX';
135 local *UNIVERSAL::XXX = sub {
136 my ($thing) = @_;
137 if (ref($thing)) {
138 $thing = {%$thing} if "$thing" =~ m/=HASH/;
139 $thing = [@$thing] if "$thing" =~ m/=ARRAY/;
140 $thing = \"$$thing" if "$thing" =~ m/=SCALAR/;
141 }
142 $_[0] = $thing;
143 };
144  
145 my $g = Data::Dumper::Dumper($got);
146 my $w = Data::Dumper::Dumper($want);
147  
148 my $bool = $g eq $w;
149  
150 my $diff;
151  
152 $ctx->ok($bool, $name, [$diff ? $diff : ($g, $w), @diag]);
153 $ctx->release;
154 return $bool;
155 }
156  
157 sub diag {
158 my $ctx = context();
159 $ctx->diag(join '', @_);
160 $ctx->release;
161 }
162  
163 sub note {
164 my $ctx = context();
165 $ctx->note(join '', @_);
166 $ctx->release;
167 }
168  
169 sub skip_all {
170 my ($reason) = @_;
171 my $ctx = context();
172 $ctx->plan(0, SKIP => $reason);
173 $ctx->release if $ctx;
174 }
175  
176 sub todo {
177 my ($reason, $sub) = @_;
178 my $ctx = context();
179  
180 # This code is mostly copied from Test2::Todo in the Test2-Suite
181 # distribution.
182 my $hub = test2_stack->top;
183 my $filter = $hub->pre_filter(
184 sub {
185 my ($active_hub, $event) = @_;
186  
187 # Turn a diag into a note
188 return Test2::Event::Note->new(%$event) if ref($event) eq 'Test2::Event::Diag';
189  
190 # Set todo on ok's
191 if ($hub == $active_hub && $event->isa('Test2::Event::Ok')) {
192 $event->set_todo($reason);
193 $event->set_effective_pass(1);
194 }
195  
196 return $event;
197 },
198 inherit => 1,
199 todo => $reason,
200 );
201 $sub->();
202 $hub->pre_unfilter($filter);
203  
204 $ctx->release if $ctx;
205 }
206  
207 sub plan {
208 my ($max) = @_;
209 my $ctx = context();
210 $ctx->plan($max);
211 $ctx->release;
212 }
213  
214 sub done_testing {
215 my $ctx = context();
216 $ctx->done_testing;
217 $ctx->release;
218 }
219  
220 sub warnings(&) {
221 my $code = shift;
222 my @warnings;
223 local $SIG{__WARN__} = sub { push @warnings => @_ };
224 $code->();
225 return \@warnings;
226 }
227  
228 sub exception(&) {
229 my $code = shift;
230 local ($@, $!, $SIG{__DIE__});
231 my $ok = eval { $code->(); 1 };
232 my $error = $@ || 'SQUASHED ERROR';
233 return $ok ? undef : $error;
234 }
235  
236 sub tests {
237 my ($name, $code) = @_;
238 my $ctx = context();
239  
240 before_each() if __PACKAGE__->can('before_each');
241  
242 my $bool = run_subtest($name, $code, 1);
243  
244 $ctx->release;
245  
246 return $bool;
247 }
248  
249 sub capture(&) {
250 my $code = shift;
251  
252 my ($err, $out) = ("", "");
253  
254 my $handles = test2_stack->top->format->handles;
255 my ($ok, $e);
256 {
257 my ($out_fh, $err_fh);
258  
259 ($ok, $e) = try {
260 open($out_fh, '>', \$out) or die "Failed to open a temporary STDOUT: $!";
261 open($err_fh, '>', \$err) or die "Failed to open a temporary STDERR: $!";
262  
263 test2_stack->top->format->set_handles([$out_fh, $err_fh, $out_fh]);
264  
265 $code->();
266 };
267 }
268 test2_stack->top->format->set_handles($handles);
269  
270 die $e unless $ok;
271  
272 $err =~ s/ $/_/mg;
273 $out =~ s/ $/_/mg;
274  
275 return {
276 STDOUT => $out,
277 STDERR => $err,
278 };
279 }
280  
281 1;
282  
283 __END__