YAFT.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 127 Podpod 0.0% 0 / 104 totaltotal 0.0% 0 / 458 CC 20 SCAR 27.9
CC
20
Cov
0%
_test_yaft_assumption
72.0
_build_got
20.0
_run_diag
20.0
CRAP 16.3
1  
2 use v5.14;
3 use warnings;
4  
5 use Syntax::Construct qw (package-block package-version);
6  
7 package Test::YAFT v1.0.4 {
8 use parent qw (Exporter::Tiny);
9  
10 use Context::Singleton;
11 use Ref::Util qw ();
12 use Safe::Isa qw ();
13 use Scalar::Util qw ();
14 use Sub::Install qw ();
15 use Sub::Override qw ();
16  
17 use Test::Deep qw ();
18 use Test::Differences qw ();
19 use Test::More v0.970 qw ();
20 use Test::Warnings v0.038 qw (:no_end_test !done_testing);
21  
22 use Test::YAFT::Act;
23 use Test::YAFT::Argument;
24 use Test::YAFT::Argument::Arrange;
25 use Test::YAFT::Argument::Got;
26 use Test::YAFT::Attributes;
27 use Test::YAFT::Cmp;
28 use Test::YAFT::Cmp::Complement;
29 use Test::YAFT::Dumper;
30  
31 # v5.14 forward prototype declaration to prevent warnings from attributes
32 sub act (&;@);
33 sub arrange (&);
34 sub got (&);
35 sub had_no_warnings (;$);
36 sub pass ($);
37  
38 sub act (&;@) :Util;
39 sub arrange (&) :Util(Test::YAFT::Argument::Arrange::);
40 sub assume :Assumption(\&_test_yaft_assumption);
41 sub BAIL_OUT :Util(\&Test::More::BAIL_OUT);
42 sub cmp_details :Foundation(\&Test::Deep::cmp_details);
43 sub deep_diag :Foundation(\&Test::Deep::deep_diag);
44 sub diag :Util(\&Test::More::diag);
45 sub done_testing :Util(\&Test::More::done_testing);
46 sub eq_deeply :Foundation(\&Test::Deep::eq_deeply);
47 sub expect_all :Expectation(\&Test::Deep::all);
48 sub expect_any :Expectation(\&Test::Deep::any);
49 sub expect_array :Expectation(\&Test::Deep::array);
50 sub expect_array_each :Expectation(\&Test::Deep::array_each);
51 sub expect_array_elements_only :Expectation(\&Test::Deep::arrayelementsonly);
52 sub expect_array_length :Expectation(\&Test::Deep::arraylength);
53 sub expect_array_length_only :Expectation(\&Test::Deep::arraylengthonly);
54 sub expect_bag :Expectation(\&Test::Deep::bag);
55 sub expect_blessed :Expectation(\&Test::Deep::blessed);
56 sub expect_bool :Expectation(\&Test::Deep::bool);
57 sub expect_code :Expectation(\&Test::Deep::code);
58 sub expect_compare :Expectation(Test::YAFT::Cmp::Compare);
59 sub expect_complement :Expectation(Test::YAFT::Cmp::Complement);
60 sub expect_false :Expectation(\&Test::Deep::bool, 0);
61 sub expect_hash :Expectation(\&Test::Deep::hash);
62 sub expect_hash_each :Expectation(\&Test::Deep::hash_each);
63 sub expect_hash_keys :Expectation(\&Test::Deep::hashkeys);
64 sub expect_hash_keys_only :Expectation(\&Test::Deep::hashkeysonly);
65 sub expect_isa :Expectation(\&Test::Deep::Isa);
66 sub expect_listmethods :Expectation(\&Test::Deep::listmethods);
67 sub expect_methods :Expectation(\&Test::Deep::methods);
68 sub expect_no_class :Expectation(\&Test::Deep::noclass);
69 sub expect_none :Expectation(\&Test::Deep::none);
70 sub expect_none_of :Expectation(\&Test::Deep::noneof);
71 sub expect_num :Expectation(\&Test::Deep::num);
72 sub expect_obj_isa :Expectation(\&Test::Deep::obj_isa);
73 sub expect_re :Expectation(\&Test::Deep::re);
74 sub expect_ref_type :Expectation(\&Test::Deep::reftype);
75 sub expect_regexp_matches :Expectation(\&Test::Deep::regexpmatches);
76 sub expect_regexp_only :Expectation(\&Test::Deep::regexponly);
77 sub expect_regexpref :Expectation(\&Test::Deep::regexpref);
78 sub expect_regexpref_only :Expectation(\&Test::Deep::regexprefonly);
79 sub expect_scalarref :Expectation(\&Test::Deep::scalref);
80 sub expect_scalarref_only :Expectation(\&Test::Deep::scalarrefonly);
81 sub expect_set :Expectation(\&Test::Deep::set);
82 sub expect_shallow :Expectation(\&Test::Deep::shallow);
83 sub expect_str :Expectation(\&Test::Deep::str);
84 sub expect_subbag :Expectation(\&Test::Deep::subbagof);
85 sub expect_subbag_of :Expectation(\&Test::Deep::subbagof);
86 sub expect_subhash :Expectation(\&Test::Deep::subhashof);
87 sub expect_subhash_of :Expectation(\&Test::Deep::subhashof);
88 sub expect_subset :Expectation(\&Test::Deep::subsetof);
89 sub expect_subset_of :Expectation(\&Test::Deep::subsetof);
90 sub expect_superbag :Expectation(\&Test::Deep::superbagof);
91 sub expect_superbag_of :Expectation(\&Test::Deep::superbagof);
92 sub expect_superhash :Expectation(\&Test::Deep::superhashof);
93 sub expect_superhash_of :Expectation(\&Test::Deep::superhashof);
94 sub expect_superset :Expectation(\&Test::Deep::supersetof);
95 sub expect_superset_of :Expectation(\&Test::Deep::supersetof);
96 sub expect_true :Expectation(\&Test::Deep::bool, 1);
97 sub expect_use_class :Expectation(\&Test::Deep::useclass);
98 sub expect_value :Expectation(Test::YAFT::Cmp);
99 sub explain :Util(\&Test::More::explain);
100 sub fail :Assumption;
101 sub got (&) :Util(Test::YAFT::Argument::Got::);
102 sub had_no_warnings (;$) :Assumption(\&Test::Warnings::had_no_warnings);
103 sub ignore :Expectation(\&Test::Deep::ignore);
104 sub it :Assumption(\&_test_yaft_assumption);
105 sub nok :Assumption;
106 sub note :Util(\&Test::More::note);
107 sub ok :Assumption;
108 sub pass ($) :Assumption(\&Test::More::pass);
109 sub plan :Util(\&Test::More::plan);
110 sub skip :Util(\&Test::More::skip);
111 sub subtest :Util;
112 sub test_deep_cmp :Foundation;
113 sub test_frame (&) :Foundation;
114 sub there :Assumption(\&_test_yaft_assumption);
115 sub todo :Util(\&Test::More::todo);
116 sub todo_skip :Util(\&Test::More::todo_skip);
117  
118 my $SINGLETON_ACT = q (Test::YAFT::act);
119  
120 sub _act_arrange;
121 sub _build_got;
122 sub _run_act;
123 sub _run_coderef;
124 sub _run_diag;
125 sub _test_yaft_assumption_args;
126  
127 sub _act_arrange {
128 my ($args) = @_;
129  
130 proclaim $_->resolve
131 for @{ $args->{arrange} // [] }
132 ;
133 }
134  
135 sub _build_got {
136 my ($args) = @_;
137  
138 return _run_act
139 unless exists $args->{got}
140 ;
141  
142 return _run_coderef ($args->{got}->{code})
143 if $args->{got}->$Safe::Isa::_isa (Test::YAFT::Argument::Got::)
144 ;
145  
146 return _run_coderef ($args->{got})
147 if Ref::Util::is_coderef ($args->{got})
148 ;
149  
150 return +{
151 lives_ok => 1,
152 value => $args->{got},
153 error => undef,
154 };
155 }
156  
157 sub _run_act {
158 my $singleton = deduce ($SINGLETON_ACT)->context;
159  
160 my @missing = $singleton->unresolved;
161  
162 return {
163 lives_ok => 0,
164 value => undef,
165 error => qq (Act dependencies not fulfilled: ${\ join q (, ), sort @missing }),
166 } if @missing;
167  
168 _run_coderef ($singleton->act, $singleton->arguments);
169 }
170  
171 sub _run_coderef {
172 my ($builder, @args) = @_;
173  
174 my $result = { value => undef };
175 $result->{lives_ok} = eval { $result->{value} = $builder->(@args); 1 };
176 $result->{error} = $@;
177  
178 return $result;
179 }
180  
181 sub _run_diag {
182 my ($diag, $stack, $got) = @_;
183  
184 return
185 unless $diag
186 ;
187  
188 return Test::More::diag ($diag->($stack, $got))
189 if Ref::Util::is_coderef ($diag)
190 ;
191  
192 return Test::More::diag (@$diag)
193 if Ref::Util::is_arrayref ($diag)
194 ;
195  
196 return Test::More::diag ($diag);
197 }
198  
199 sub _test_yaft_assumption {
200 my ($title, @args) = @_;
201  
202 my %args = _test_yaft_assumption_args @args;
203  
204 my $guard = Sub::Override::->new (
205 q (Data::Dumper::Dumper) => \ &Test::YAFT::Dumper::Dumper,
206 );
207  
208 my ($ok, $stack, $got, $expect);
209 test_frame {
210 _act_arrange (\ %args);
211 my $result = _build_got (\ %args);
212  
213 my $expected_to_live = ! exists $args{throws};
214  
215 return fail $title, diag => $expected_to_live
216 ? qq (Expected to live but died: $result->{error})
217 : q (Expected to die but lives)
218 if $expected_to_live
219 xor $result->{lives_ok}
220 ;
221  
222 ($got, $expect) = $result->{lives_ok}
223 ? ($result->{value}, $args{expect})
224 : ($result->{error}, $args{throws})
225 ;
226  
227 ($ok, $stack) = Test::Deep::cmp_details ($got, $expect);
228  
229 return Test::More::ok ($ok, $title)
230 if $ok
231 || defined $args{diag}
232 || $expect->$Safe::Isa::_isa (Test::Deep::Boolean::)
233 ;
234  
235 if ($expect->$Safe::Isa::_isa (Test::YAFT::Cmp::Complement::)) {
236 Test::More::ok ($ok, $title);
237 Test::More::diag (Test::Deep::deep_diag ($stack))
238 unless $ok
239 ;
240 return $ok;
241 }
242  
243 Test::Differences::eq_or_diff $got, $expect, $title;
244 Test::More::diag (Test::Deep::deep_diag ($stack))
245 if ref $got
246 || ref $expect
247 ;
248  
249 return;
250 } or _run_diag ($args{diag}, $stack, $got);
251  
252 return $ok;
253 }
254  
255 sub _test_yaft_assumption_args {
256 my %args;
257  
258 while (@_) {
259 my $key = shift;
260  
261 if (Scalar::Util::blessed ($key)) {
262 $key->set_argument (\ %args), next
263 if $key->isa (Test::YAFT::Argument::)
264 ;
265  
266 die qq (Ref ${\ ref $_[0] } not recognized);
267 }
268  
269 my $value = shift;
270  
271 if (my ($property) = $key =~ m (^ with [_] (.*) $)x) {
272 unshift @_, arrange { $property => $value };
273 next;
274 }
275  
276 # TODO: what should be good syntax here ?
277 #push @{ $args{arrange} //= [] }, Test::YAFT::Arrange::->new (sub { $value }) and next
278 # if $key eq q (arrange);
279  
280 $args{$key} = $value;
281 }
282  
283 return %args;
284 }
285  
286 sub act (&;@) {
287 my ($act, @dependencies) = @_;
288  
289 die q (Act already known in current context)
290 if is_deduced $SINGLETON_ACT
291 ;
292  
293 proclaim $SINGLETON_ACT => Test::YAFT::Act::->new ($act, @dependencies);
294 }
295  
296 sub fail {
297 my ($title, %args) = @_;
298  
299 test_frame {
300 _test_yaft_assumption $title,
301 diag => q (),
302 %args,
303 got => 0,
304 expect => expect_true,
305 ;
306 }
307 }
308  
309 sub nok {
310 my ($message, %args) = @_;
311  
312 test_frame {
313 _test_yaft_assumption $message,
314 %args,
315 expect => expect_false,
316 diag => q (),
317 ;
318 }
319 }
320  
321 sub ok {
322 my ($message, %args) = @_;
323  
324 test_frame {
325 _test_yaft_assumption $message,
326 %args,
327 expect => expect_true,
328 diag => q (),
329 ;
330 }
331 }
332  
333 sub subtest {
334 my ($title, $code) = @_;
335  
336 test_frame {
337 Test::More::subtest $title, $code;
338 };
339 }
340  
341 sub test_deep_cmp {
342 my (%methods) = @_;
343  
344 state $serial = 0;
345 my $prefix = q (Test::Deep::Cmp::__ANON__::);
346  
347 my $class = $prefix . ++$serial;
348 my $isa = delete $methods{isa} // q (Test::YAFT::Cmp);
349  
350 {
351 my @isa = Ref::Util::is_arrayref ($isa) ? @$isa : ($isa);
352 eval qq (require $_) for @isa;
353  
354 no strict q (refs);
355 @{ qq ($class\::ISA) } = @isa;
356 }
357  
358 Sub::Install::install_sub ({ into => $class, as => $_, code => $methods{$_} })
359 for keys %methods
360 ;
361  
362 return $class;
363 }
364  
365 sub test_frame (&) {
366 my ($code) = @_;
367  
368 # 1 - caller sub context
369 # 2 - this sub context
370 # 3 - frame
371 # 4 - code arg context
372 local $Test::Builder::Level = $Test::Builder::Level + 4;
373  
374 &frame ($code);
375 }
376  
377 1;
378 };