Perlito5X/Test/Builder.pm untested

filter: Statementstmt 0.0% 0 / 937 Branchbran 0.0% 0 / 392 Conditioncond 0.0% 0 / 271 MC/DCmcdc 0.0% 0 / 164 Subroutinesub 0.0% 0 / 107 Podpod 0.0% 0 / 75 totaltotal 0.0% 0 / 1946 CC 196 SCAR 45.4
CC
196
Cov
0%
_ending
702.0
finalize
210.0
subtest
156.0
CRAP 93.6
1 package Test::Builder;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6  
7 our $VERSION = '1.302073';
8  
9 BEGIN {
10 if( $] < 5.008 ) {
11 require Test::Builder::IO::Scalar;
12 }
13 }
14  
15 use Scalar::Util qw/blessed reftype weaken/;
16  
17 use Test2::Util qw/USE_THREADS try get_tid/;
18 use Test2::API qw/context release/;
19 # Make Test::Builder thread-safe for ithreads.
20 BEGIN {
21 warn "Test::Builder was loaded after Test2 initialization, this is not recommended."
22 if Test2::API::test2_init_done() || Test2::API::test2_load_done();
23  
24 if (USE_THREADS) {
25 require Test2::IPC;
26 require Test2::IPC::Driver::Files;
27 Test2::IPC::Driver::Files->import;
28 Test2::API::test2_ipc_enable_polling();
29 Test2::API::test2_no_wait(1);
30 Test2::API::test2_ipc_enable_shm();
31 }
32 }
33  
34 use Test2::Event::Subtest;
35 use Test2::Hub::Subtest;
36  
37 use Test::Builder::Formatter;
38 use Test::Builder::TodoDiag;
39  
40 our $Level = 1;
41 our $Test = $ENV{TB_NO_EARLY_INIT} ? undef : Test::Builder->new;
42  
43 sub _add_ts_hooks {
44 my $self = shift;
45 my $hub = $self->{Stack}->top;
46  
47 # Take a reference to the hash key, we do this to avoid closing over $self
48 # which is the singleton. We use a reference because the value could change
49 # in rare cases.
50 my $epkgr = \$self->{Exported_To};
51  
52 #$hub->add_context_aquire(sub {$_[0]->{level} += $Level - 1});
53  
54 $hub->pre_filter(sub {
55 my ($active_hub, $e) = @_;
56  
57 my $epkg = $$epkgr;
58 my $cpkg = $e->{trace} ? $e->{trace}->{frame}->[0] : undef;
59  
60 no strict 'refs';
61 no warnings 'once';
62 my $todo;
63 $todo = ${"$cpkg\::TODO"} if $cpkg;
64 $todo = ${"$epkg\::TODO"} if $epkg && !$todo;
65  
66 return $e unless $todo;
67  
68 # Turn a diag into a todo diag
69 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
70  
71 # Set todo on ok's
72 if ($e->isa('Test2::Event::Ok')) {
73 $e->set_todo($todo);
74 $e->set_effective_pass(1);
75  
76 if (my $result = $e->get_meta(__PACKAGE__)) {
77 $result->{reason} ||= $todo;
78 $result->{type} ||= 'todo';
79 $result->{ok} = 1;
80 }
81 }
82  
83 return $e;
84 }, inherit => 1);
85 }
86  
87 sub new {
88 my($class) = shift;
89 unless($Test) {
90 my $ctx = context();
91 $Test = $class->create(singleton => 1);
92 $ctx->release;
93  
94 # Non-TB tools normally expect 0 added to the level. $Level is normally 1. So
95 # we only want the level to change if $Level != 1.
96 # TB->ctx compensates for this later.
97 Test2::API::test2_add_callback_context_aquire(sub { $_[0]->{level} += $Level - 1 });
98  
99 Test2::API::test2_add_callback_exit(sub { $Test->_ending(@_) });
100  
101 Test2::API::test2_ipc()->set_no_fatal(1) if USE_THREADS;
102 }
103 return $Test;
104 }
105  
106 sub create {
107 my $class = shift;
108 my %params = @_;
109  
110 my $self = bless {}, $class;
111 if ($params{singleton}) {
112 $self->{Stack} = Test2::API::test2_stack();
113 }
114 else {
115 $self->{Stack} = Test2::API::Stack->new;
116 $self->{Stack}->new_hub(
117 formatter => Test::Builder::Formatter->new,
118 ipc => Test2::API::test2_ipc(),
119 );
120 }
121 $self->reset(%params);
122 $self->_add_ts_hooks;
123  
124 return $self;
125 }
126  
127 sub ctx {
128 my $self = shift;
129 context(
130 # 1 for our frame, another for the -1 off of $Level in our hook at the top.
131 level => 2,
132 fudge => 1,
133 stack => $self->{Stack},
134 hub => $self->{Hub},
135 wrapped => 1,
136 @_
137 );
138 }
139  
140 sub parent {
141 my $self = shift;
142 my $ctx = $self->ctx;
143 my $chub = $self->{Hub} || $ctx->hub;
144 $ctx->release;
145  
146 my $parent = $chub->meta(__PACKAGE__, {})->{parent};
147  
148 return undef unless $parent;
149  
150 return bless {
151 Original_Pid => $$,
152 Stack => $self->{Stack},
153 Hub => $parent,
154 }, blessed($self);
155 }
156  
157 sub child {
158 my( $self, $name ) = @_;
159  
160 $name ||= "Child of " . $self->name;
161 my $ctx = $self->ctx;
162  
163 my $parent = $ctx->hub;
164 my $pmeta = $parent->meta(__PACKAGE__, {});
165 $self->croak("You already have a child named ($pmeta->{child}) running")
166 if $pmeta->{child};
167  
168 $pmeta->{child} = $name;
169  
170 # Clear $TODO for the child.
171 my $orig_TODO = $self->find_TODO(undef, 1, undef);
172  
173 my $subevents = [];
174  
175 my $hub = $ctx->stack->new_hub(
176 class => 'Test2::Hub::Subtest',
177 );
178  
179 $hub->pre_filter(sub {
180 my ($active_hub, $e) = @_;
181  
182 # Turn a diag into a todo diag
183 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
184  
185 return $e;
186 }, inherit => 1) if $orig_TODO;
187  
188 $hub->listen(sub { push @$subevents => $_[1] });
189  
190 $hub->set_nested( $parent->isa('Test2::Hub::Subtest') ? $parent->nested + 1 : 1 );
191  
192 my $meta = $hub->meta(__PACKAGE__, {});
193 $meta->{Name} = $name;
194 $meta->{TODO} = $orig_TODO;
195 $meta->{TODO_PKG} = $ctx->trace->package;
196 $meta->{parent} = $parent;
197 $meta->{Test_Results} = [];
198 $meta->{subevents} = $subevents;
199 $meta->{subtest_id} = $hub->id;
200 $meta->{subtest_buffered} = $parent->format ? 0 : 1;
201  
202 $self->_add_ts_hooks;
203  
204 $ctx->release;
205 return bless { Original_Pid => $$, Stack => $self->{Stack}, Hub => $hub }, blessed($self);
206 }
207  
208 sub finalize {
209 my $self = shift;
210 my $ok = 1;
211 ($ok) = @_ if @_;
212  
213 my $st_ctx = $self->ctx;
214 my $chub = $self->{Hub} || return $st_ctx->release;
215  
216 my $meta = $chub->meta(__PACKAGE__, {});
217 if ($meta->{child}) {
218 $self->croak("Can't call finalize() with child ($meta->{child}) active");
219 }
220  
221 local $? = 0; # don't fail if $subtests happened to set $? nonzero
222  
223 $self->{Stack}->pop($chub);
224  
225 $self->find_TODO($meta->{TODO_PKG}, 1, $meta->{TODO});
226  
227 my $parent = $self->parent;
228 my $ctx = $parent->ctx;
229 my $trace = $ctx->trace;
230 delete $ctx->hub->meta(__PACKAGE__, {})->{child};
231  
232 $chub->finalize($trace, 1)
233 if $ok
234 && $chub->count
235 && !$chub->no_ending
236 && !$chub->ended;
237  
238 my $plan = $chub->plan || 0;
239 my $count = $chub->count;
240 my $failed = $chub->failed;
241 my $passed = $chub->is_passing;
242  
243 my $num_extra = $plan =~ m/\D/ ? 0 : $count - $plan;
244 if ($count && $num_extra != 0) {
245 my $s = $plan == 1 ? '' : 's';
246 $st_ctx->diag(<<"FAIL");
247 Looks like you planned $plan test$s but ran $count.
248 FAIL
249 }
250  
251 if ($failed) {
252 my $s = $failed == 1 ? '' : 's';
253  
254 my $qualifier = $num_extra == 0 ? '' : ' run';
255  
256 $st_ctx->diag(<<"FAIL");
257 Looks like you failed $failed test$s of $count$qualifier.
258 FAIL
259 }
260  
261 if (!$passed && !$failed && $count && !$num_extra) {
262 $st_ctx->diag(<<"FAIL");
263 All assertions inside the subtest passed, but errors were encountered.
264 FAIL
265 }
266  
267 $st_ctx->release;
268  
269 unless ($chub->bailed_out) {
270 my $plan = $chub->plan;
271 if ( $plan && $plan eq 'SKIP' ) {
272 $parent->skip($chub->skip_reason, $meta->{Name});
273 }
274 elsif ( !$chub->count ) {
275 $parent->ok( 0, sprintf q[No tests run for subtest "%s"], $meta->{Name} );
276 }
277 else {
278 $parent->{subevents} = $meta->{subevents};
279 $parent->{subtest_id} = $meta->{subtest_id};
280 $parent->{subtest_buffered} = $meta->{subtest_buffered};
281 $parent->ok( $chub->is_passing, $meta->{Name} );
282 }
283 }
284  
285 $ctx->release;
286 return $chub->is_passing;
287 }
288  
289 sub subtest {
290 my $self = shift;
291 my ($name, $code, @args) = @_;
292 my $ctx = $self->ctx;
293 $ctx->throw("subtest()'s second argument must be a code ref")
294 unless $code && reftype($code) eq 'CODE';
295  
296 $name ||= "Child of " . $self->name;
297  
298 $ctx->note("Subtest: $name");
299  
300 my $child = $self->child($name);
301  
302 my $start_pid = $$;
303 my $st_ctx;
304 my ($ok, $err, $finished, $child_error);
305 T2_SUBTEST_WRAPPER: {
306 my $ctx = $self->ctx;
307 $st_ctx = $ctx->snapshot;
308 $ctx->release;
309 $ok = eval { local $Level = 1; $code->(@args); 1 };
310 ($err, $child_error) = ($@, $?);
311  
312 # They might have done 'BEGIN { skip_all => "whatever" }'
313 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) {
314 $ok = undef;
315 $err = undef;
316 }
317 else {
318 $finished = 1;
319 }
320 }
321  
322 if ($start_pid != $$ && !$INC{'Test/Sync/IPC.pm'}) {
323 warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err;
324 exit 255;
325 }
326  
327 my $trace = $ctx->trace;
328  
329 if (!$finished) {
330 if(my $bailed = $st_ctx->hub->bailed_out) {
331 my $chub = $child->{Hub};
332 $self->{Stack}->pop($chub);
333 $ctx->bail($bailed->reason);
334 }
335 my $code = $st_ctx->hub->exit_code;
336 $ok = !$code;
337 $err = "Subtest ended with exit code $code" if $code;
338 }
339  
340 my $st_hub = $st_ctx->hub;
341 my $plan = $st_hub->plan;
342 my $count = $st_hub->count;
343  
344 if (!$count && (!defined($plan) || "$plan" ne 'SKIP')) {
345 $st_ctx->plan(0) unless defined $plan;
346 $st_ctx->diag('No tests run!');
347 }
348  
349 $child->finalize($st_ctx->trace);
350  
351 $ctx->release;
352  
353 die $err unless $ok;
354  
355 $? = $child_error if defined $child_error;
356  
357 return $st_hub->is_passing;
358 }
359  
360 sub name {
361 my $self = shift;
362 my $ctx = $self->ctx;
363 release $ctx, $ctx->hub->meta(__PACKAGE__, {})->{Name};
364 }
365  
366 sub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
367 my ($self, %params) = @_;
368  
369 Test2::API::test2_set_is_end(0);
370  
371 # We leave this a global because it has to be localized and localizing
372 # hash keys is just asking for pain. Also, it was documented.
373 $Level = 1;
374  
375 $self->{Original_Pid} = $$;
376  
377 my $ctx = $self->ctx;
378 unless ($params{singleton}) {
379 $ctx->hub->reset_state();
380 $ctx->hub->set_pid($$);
381 $ctx->hub->set_tid(get_tid);
382 }
383  
384 my $meta = $ctx->hub->meta(__PACKAGE__, {});
385 %$meta = (
386 Name => $0,
387 Ending => 0,
388 Done_Testing => undef,
389 Skip_All => 0,
390 Test_Results => [],
391 );
392  
393 $self->{Exported_To} = undef;
394  
395 $self->{Orig_Handles} ||= do {
396 my $format = $ctx->hub->format;
397 my $out;
398 if ($format && $format->isa('Test2::Formatter::TAP')) {
399 $out = $format->handles;
400 }
401 $out ? [@$out] : [];
402 };
403  
404 $self->use_numbers(1);
405 $self->no_header(0);
406 $self->no_ending(0);
407 $self->reset_outputs;
408  
409 $ctx->release;
410  
411 return;
412 }
413  
414  
415 my %plan_cmds = (
416 no_plan => \&no_plan,
417 skip_all => \&skip_all,
418 tests => \&_plan_tests,
419 );
420  
421 sub plan {
422 my( $self, $cmd, $arg ) = @_;
423  
424 return unless $cmd;
425  
426 my $ctx = $self->ctx;
427 my $hub = $ctx->hub;
428  
429 $ctx->throw("You tried to plan twice") if $hub->plan;
430  
431 local $Level = $Level + 1;
432  
433 if( my $method = $plan_cmds{$cmd} ) {
434 local $Level = $Level + 1;
435 $self->$method($arg);
436 }
437 else {
438 my @args = grep { defined } ( $cmd, $arg );
439 $ctx->throw("plan() doesn't understand @args");
440 }
441  
442 release $ctx, 1;
443 }
444  
445  
446 sub _plan_tests {
447 my($self, $arg) = @_;
448  
449 my $ctx = $self->ctx;
450  
451 if($arg) {
452 local $Level = $Level + 1;
453 $self->expected_tests($arg);
454 }
455 elsif( !defined $arg ) {
456 $ctx->throw("Got an undefined number of tests");
457 }
458 else {
459 $ctx->throw("You said to run 0 tests");
460 }
461  
462 $ctx->release;
463 }
464  
465  
466 sub expected_tests {
467 my $self = shift;
468 my($max) = @_;
469  
470 my $ctx = $self->ctx;
471  
472 if(@_) {
473 $self->croak("Number of tests must be a positive integer. You gave it '$max'")
474 unless $max =~ /^\+?\d+$/;
475  
476 $ctx->plan($max);
477 }
478  
479 my $hub = $ctx->hub;
480  
481 $ctx->release;
482  
483 my $plan = $hub->plan;
484 return 0 unless $plan;
485 return 0 if $plan =~ m/\D/;
486 return $plan;
487 }
488  
489  
490 sub no_plan {
491 my($self, $arg) = @_;
492  
493 my $ctx = $self->ctx;
494  
495 if (defined $ctx->hub->plan) {
496 warn "Plan already set, no_plan() is a no-op, this will change to a hard failure in the future.";
497 $ctx->release;
498 return;
499 }
500  
501 $ctx->alert("no_plan takes no arguments") if $arg;
502  
503 $ctx->hub->plan('NO PLAN');
504  
505 release $ctx, 1;
506 }
507  
508  
509 sub done_testing {
510 my($self, $num_tests) = @_;
511  
512 my $ctx = $self->ctx;
513  
514 my $meta = $ctx->hub->meta(__PACKAGE__, {});
515  
516 if ($meta->{Done_Testing}) {
517 my ($file, $line) = @{$meta->{Done_Testing}}[1,2];
518 local $ctx->hub->{ended}; # OMG This is awful.
519 $self->ok(0, "done_testing() was already called at $file line $line");
520 $ctx->release;
521 return;
522 }
523 $meta->{Done_Testing} = [$ctx->trace->call];
524  
525 my $plan = $ctx->hub->plan;
526 my $count = $ctx->hub->count;
527  
528 # If done_testing() specified the number of tests, shut off no_plan
529 if( defined $num_tests ) {
530 $ctx->plan($num_tests) if !$plan || $plan eq 'NO PLAN';
531 }
532 elsif ($count && defined $num_tests && $count != $num_tests) {
533 $self->ok(0, "planned to run @{[ $self->expected_tests ]} but done_testing() expects $num_tests");
534 }
535 else {
536 $num_tests = $self->current_test;
537 }
538  
539 if( $self->expected_tests && $num_tests != $self->expected_tests ) {
540 $self->ok(0, "planned to run @{[ $self->expected_tests ]} ".
541 "but done_testing() expects $num_tests");
542 }
543  
544 $ctx->plan($num_tests) if $ctx->hub->plan && $ctx->hub->plan eq 'NO PLAN';
545  
546 $ctx->hub->finalize($ctx->trace, 1);
547  
548 release $ctx, 1;
549 }
550  
551  
552 sub has_plan {
553 my $self = shift;
554  
555 my $ctx = $self->ctx;
556 my $plan = $ctx->hub->plan;
557 $ctx->release;
558  
559 return( $plan ) if $plan && $plan !~ m/\D/;
560 return('no_plan') if $plan && $plan eq 'NO PLAN';
561 return(undef);
562 }
563  
564  
565 sub skip_all {
566 my( $self, $reason ) = @_;
567  
568 my $ctx = $self->ctx;
569  
570 $ctx->hub->meta(__PACKAGE__, {})->{Skip_All} = $reason || 1;
571  
572 # Work around old perl bug
573 if ($] < 5.020000) {
574 my $begin = 0;
575 my $level = 0;
576 while (my @call = caller($level++)) {
577 last unless @call && $call[0];
578 next unless $call[3] =~ m/::BEGIN$/;
579 $begin++;
580 last;
581 }
582 # HACK!
583 die 'Label not found for "last T2_SUBTEST_WRAPPER"' if $begin && $ctx->hub->meta(__PACKAGE__, {})->{parent};
584 }
585  
586 $ctx->plan(0, SKIP => $reason);
587 }
588  
589  
590 sub exported_to {
591 my( $self, $pack ) = @_;
592  
593 if( defined $pack ) {
594 $self->{Exported_To} = $pack;
595 }
596 return $self->{Exported_To};
597 }
598  
599  
600 sub ok {
601 my( $self, $test, $name ) = @_;
602  
603 my $ctx = $self->ctx;
604  
605 # $test might contain an object which we don't want to accidentally
606 # store, so we turn it into a boolean.
607 $test = $test ? 1 : 0;
608  
609 # In case $name is a string overloaded object, force it to stringify.
610 no warnings qw/uninitialized numeric/;
611 $name = "$name" if defined $name;
612  
613 # Profiling showed that the regex here was a huge time waster, doing the
614 # numeric addition first cuts our profile time from ~300ms to ~50ms
615 $self->diag(<<" ERR") if 0 + $name && $name =~ /^[\d\s]+$/;
616 You named your test '$name'. You shouldn't use numbers for your test names.
617 Very confusing.
618 ERR
619 use warnings qw/uninitialized numeric/;
620  
621 my $trace = $ctx->{trace};
622 my $hub = $ctx->{hub};
623  
624 my $result = {
625 ok => $test,
626 actual_ok => $test,
627 reason => '',
628 type => '',
629 (name => defined($name) ? $name : ''),
630 };
631  
632 $hub->{_meta}->{+__PACKAGE__}->{Test_Results}[ $hub->{count} ] = $result;
633  
634 my $orig_name = $name;
635  
636 my @attrs;
637 my $subevents = delete $self->{subevents};
638 my $subtest_id = delete $self->{subtest_id};
639 my $subtest_buffered = delete $self->{subtest_buffered};
640 my $epkg = 'Test2::Event::Ok';
641 if ($subevents) {
642 $epkg = 'Test2::Event::Subtest';
643 push @attrs => (subevents => $subevents, subtest_id => $subtest_id, buffered => $subtest_buffered);
644 }
645  
646 my $e = bless {
647 trace => bless( {%$trace}, 'Test2::Util::Trace'),
648 pass => $test,
649 name => $name,
650 _meta => {'Test::Builder' => $result},
651 effective_pass => $test,
652 @attrs,
653 }, $epkg;
654 $hub->send($e);
655  
656 $self->_ok_debug($trace, $orig_name) unless($test);
657  
658 $ctx->release;
659 return $test;
660 }
661  
662 sub _ok_debug {
663 my $self = shift;
664 my ($trace, $orig_name) = @_;
665  
666 my $is_todo = defined($self->todo);
667  
668 my $msg = $is_todo ? "Failed (TODO)" : "Failed";
669  
670 my $dfh = $self->_diag_fh;
671 print $dfh "\n" if $ENV{HARNESS_ACTIVE} && $dfh;
672  
673 my (undef, $file, $line) = $trace->call;
674 if (defined $orig_name) {
675 $self->diag(qq[ $msg test '$orig_name'\n]);
676 $self->diag(qq[ at $file line $line.\n]);
677 }
678 else {
679 $self->diag(qq[ $msg test at $file line $line.\n]);
680 }
681 }
682  
683 sub _diag_fh {
684 my $self = shift;
685 local $Level = $Level + 1;
686 return $self->in_todo ? $self->todo_output : $self->failure_output;
687 }
688  
689 sub _unoverload {
690 my ($self, $type, $thing) = @_;
691  
692 return unless ref $$thing;
693 return unless blessed($$thing) || scalar $self->_try(sub{ $$thing->isa('UNIVERSAL') });
694 {
695 local ($!, $@);
696 require overload;
697 }
698 my $string_meth = overload::Method( $$thing, $type ) || return;
699 $$thing = $$thing->$string_meth();
700 }
701  
702 sub _unoverload_str {
703 my $self = shift;
704  
705 $self->_unoverload( q[""], $_ ) for @_;
706 }
707  
708 sub _unoverload_num {
709 my $self = shift;
710  
711 $self->_unoverload( '0+', $_ ) for @_;
712  
713 for my $val (@_) {
714 next unless $self->_is_dualvar($$val);
715 $$val = $$val + 0;
716 }
717 }
718  
719 # This is a hack to detect a dualvar such as $!
720 sub _is_dualvar {
721 my( $self, $val ) = @_;
722  
723 # Objects are not dualvars.
724 return 0 if ref $val;
725  
726 no warnings 'numeric';
727 my $numval = $val + 0;
728 return ($numval != 0 and $numval ne $val ? 1 : 0);
729 }
730  
731  
732 sub is_eq {
733 my( $self, $got, $expect, $name ) = @_;
734  
735 my $ctx = $self->ctx;
736  
737 local $Level = $Level + 1;
738  
739 if( !defined $got || !defined $expect ) {
740 # undef only matches undef and nothing else
741 my $test = !defined $got && !defined $expect;
742  
743 $self->ok( $test, $name );
744 $self->_is_diag( $got, 'eq', $expect ) unless $test;
745 $ctx->release;
746 return $test;
747 }
748  
749 release $ctx, $self->cmp_ok( $got, 'eq', $expect, $name );
750 }
751  
752  
753 sub is_num {
754 my( $self, $got, $expect, $name ) = @_;
755 my $ctx = $self->ctx;
756 local $Level = $Level + 1;
757  
758 if( !defined $got || !defined $expect ) {
759 # undef only matches undef and nothing else
760 my $test = !defined $got && !defined $expect;
761  
762 $self->ok( $test, $name );
763 $self->_is_diag( $got, '==', $expect ) unless $test;
764 $ctx->release;
765 return $test;
766 }
767  
768 release $ctx, $self->cmp_ok( $got, '==', $expect, $name );
769 }
770  
771  
772 sub _diag_fmt {
773 my( $self, $type, $val ) = @_;
774  
775 if( defined $$val ) {
776 if( $type eq 'eq' or $type eq 'ne' ) {
777 # quote and force string context
778 $$val = "'$$val'";
779 }
780 else {
781 # force numeric context
782 $self->_unoverload_num($val);
783 }
784 }
785 else {
786 $$val = 'undef';
787 }
788  
789 return;
790 }
791  
792  
793 sub _is_diag {
794 my( $self, $got, $type, $expect ) = @_;
795  
796 $self->_diag_fmt( $type, $_ ) for \$got, \$expect;
797  
798 local $Level = $Level + 1;
799 return $self->diag(<<"DIAGNOSTIC");
800 got: $got
801 expected: $expect
802 DIAGNOSTIC
803  
804 }
805  
806 sub _isnt_diag {
807 my( $self, $got, $type ) = @_;
808  
809 $self->_diag_fmt( $type, \$got );
810  
811 local $Level = $Level + 1;
812 return $self->diag(<<"DIAGNOSTIC");
813 got: $got
814 expected: anything else
815 DIAGNOSTIC
816 }
817  
818  
819 sub isnt_eq {
820 my( $self, $got, $dont_expect, $name ) = @_;
821 my $ctx = $self->ctx;
822 local $Level = $Level + 1;
823  
824 if( !defined $got || !defined $dont_expect ) {
825 # undef only matches undef and nothing else
826 my $test = defined $got || defined $dont_expect;
827  
828 $self->ok( $test, $name );
829 $self->_isnt_diag( $got, 'ne' ) unless $test;
830 $ctx->release;
831 return $test;
832 }
833  
834 release $ctx, $self->cmp_ok( $got, 'ne', $dont_expect, $name );
835 }
836  
837 sub isnt_num {
838 my( $self, $got, $dont_expect, $name ) = @_;
839 my $ctx = $self->ctx;
840 local $Level = $Level + 1;
841  
842 if( !defined $got || !defined $dont_expect ) {
843 # undef only matches undef and nothing else
844 my $test = defined $got || defined $dont_expect;
845  
846 $self->ok( $test, $name );
847 $self->_isnt_diag( $got, '!=' ) unless $test;
848 $ctx->release;
849 return $test;
850 }
851  
852 release $ctx, $self->cmp_ok( $got, '!=', $dont_expect, $name );
853 }
854  
855  
856 sub like {
857 my( $self, $thing, $regex, $name ) = @_;
858 my $ctx = $self->ctx;
859  
860 local $Level = $Level + 1;
861  
862 release $ctx, $self->_regex_ok( $thing, $regex, '=~', $name );
863 }
864  
865 sub unlike {
866 my( $self, $thing, $regex, $name ) = @_;
867 my $ctx = $self->ctx;
868  
869 local $Level = $Level + 1;
870  
871 release $ctx, $self->_regex_ok( $thing, $regex, '!~', $name );
872 }
873  
874  
875 my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );
876  
877 # Bad, these are not comparison operators. Should we include more?
878 my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "...");
879  
880 sub cmp_ok {
881 my( $self, $got, $type, $expect, $name ) = @_;
882 my $ctx = $self->ctx;
883  
884 if ($cmp_ok_bl{$type}) {
885 $ctx->throw("$type is not a valid comparison operator in cmp_ok()");
886 }
887  
888 my ($test, $succ);
889 my $error;
890 {
891 ## no critic (BuiltinFunctions::ProhibitStringyEval)
892  
893 local( $@, $!, $SIG{__DIE__} ); # isolate eval
894  
895 my($pack, $file, $line) = $ctx->trace->call();
896  
897 # This is so that warnings come out at the caller's level
898 $succ = eval qq[
899 #line $line "(eval in cmp_ok) $file"
900 \$test = (\$got $type \$expect);
901 1;
902 ];
903 $error = $@;
904 }
905 local $Level = $Level + 1;
906 my $ok = $self->ok( $test, $name );
907  
908 # Treat overloaded objects as numbers if we're asked to do a
909 # numeric comparison.
910 my $unoverload
911 = $numeric_cmps{$type}
912 ? '_unoverload_num'
913 : '_unoverload_str';
914  
915 $self->diag(<<"END") unless $succ;
916 An error occurred while using $type:
917 ------------------------------------
918 $error
919 ------------------------------------
920 END
921  
922 unless($ok) {
923 $self->$unoverload( \$got, \$expect );
924  
925 if( $type =~ /^(eq|==)$/ ) {
926 $self->_is_diag( $got, $type, $expect );
927 }
928 elsif( $type =~ /^(ne|!=)$/ ) {
929 no warnings;
930 my $eq = ($got eq $expect || $got == $expect)
931 && (
932 (defined($got) xor defined($expect))
933 || (length($got) != length($expect))
934 );
935 use warnings;
936  
937 if ($eq) {
938 $self->_cmp_diag( $got, $type, $expect );
939 }
940 else {
941 $self->_isnt_diag( $got, $type );
942 }
943 }
944 else {
945 $self->_cmp_diag( $got, $type, $expect );
946 }
947 }
948 return release $ctx, $ok;
949 }
950  
951 sub _cmp_diag {
952 my( $self, $got, $type, $expect ) = @_;
953  
954 $got = defined $got ? "'$got'" : 'undef';
955 $expect = defined $expect ? "'$expect'" : 'undef';
956  
957 local $Level = $Level + 1;
958 return $self->diag(<<"DIAGNOSTIC");
959 $got
960 $type
961 $expect
962 DIAGNOSTIC
963 }
964  
965 sub _caller_context {
966 my $self = shift;
967  
968 my( $pack, $file, $line ) = $self->caller(1);
969  
970 my $code = '';
971 $code .= "#line $line $file\n" if defined $file and defined $line;
972  
973 return $code;
974 }
975  
976  
977 sub BAIL_OUT {
978 my( $self, $reason ) = @_;
979  
980 my $ctx = $self->ctx;
981  
982 $self->{Bailed_Out} = 1;
983  
984 $ctx->bail($reason);
985 }
986  
987  
988 {
989 no warnings 'once';
990 *BAILOUT = \&BAIL_OUT;
991 }
992  
993 sub skip {
994 my( $self, $why, $name ) = @_;
995 $why ||= '';
996 $name = '' unless defined $name;
997 $self->_unoverload_str( \$why );
998  
999 my $ctx = $self->ctx;
1000  
1001 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = {
1002 'ok' => 1,
1003 actual_ok => 1,
1004 name => $name,
1005 type => 'skip',
1006 reason => $why,
1007 };
1008  
1009 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
1010 $name =~ s{\n}{\n# }sg;
1011 $why =~ s{\n}{\n# }sg;
1012  
1013 my $tctx = $ctx->snapshot;
1014 $tctx->skip('', $why);
1015  
1016 return release $ctx, 1;
1017 }
1018  
1019  
1020 sub todo_skip {
1021 my( $self, $why ) = @_;
1022 $why ||= '';
1023  
1024 my $ctx = $self->ctx;
1025  
1026 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = {
1027 'ok' => 1,
1028 actual_ok => 0,
1029 name => '',
1030 type => 'todo_skip',
1031 reason => $why,
1032 };
1033  
1034 $why =~ s{\n}{\n# }sg;
1035 my $tctx = $ctx->snapshot;
1036 $tctx->send_event( 'Skip', todo => $why, todo_diag => 1, reason => $why, pass => 0);
1037  
1038 return release $ctx, 1;
1039 }
1040  
1041  
1042 sub maybe_regex {
1043 my( $self, $regex ) = @_;
1044 my $usable_regex = undef;
1045  
1046 return $usable_regex unless defined $regex;
1047  
1048 my( $re, $opts );
1049  
1050 # Check for qr/foo/
1051 if( _is_qr($regex) ) {
1052 $usable_regex = $regex;
1053 }
1054 # Check for '/foo/' or 'm,foo,'
1055 elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
1056 ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
1057 )
1058 {
1059 $usable_regex = length $opts ? "(?$opts)$re" : $re;
1060 }
1061  
1062 return $usable_regex;
1063 }
1064  
1065 sub _is_qr {
1066 my $regex = shift;
1067  
1068 # is_regexp() checks for regexes in a robust manner, say if they're
1069 # blessed.
1070 return re::is_regexp($regex) if defined &re::is_regexp;
1071 return ref $regex eq 'Regexp';
1072 }
1073  
1074 sub _regex_ok {
1075 my( $self, $thing, $regex, $cmp, $name ) = @_;
1076  
1077 my $ok = 0;
1078 my $usable_regex = $self->maybe_regex($regex);
1079 unless( defined $usable_regex ) {
1080 local $Level = $Level + 1;
1081 $ok = $self->ok( 0, $name );
1082 $self->diag(" '$regex' doesn't look much like a regex to me.");
1083 return $ok;
1084 }
1085  
1086 {
1087 my $test;
1088 my $context = $self->_caller_context;
1089  
1090 {
1091 ## no critic (BuiltinFunctions::ProhibitStringyEval)
1092  
1093 local( $@, $!, $SIG{__DIE__} ); # isolate eval
1094  
1095 # No point in issuing an uninit warning, they'll see it in the diagnostics
1096 no warnings 'uninitialized';
1097  
1098 $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0};
1099 }
1100  
1101 $test = !$test if $cmp eq '!~';
1102  
1103 local $Level = $Level + 1;
1104 $ok = $self->ok( $test, $name );
1105 }
1106  
1107 unless($ok) {
1108 $thing = defined $thing ? "'$thing'" : 'undef';
1109 my $match = $cmp eq '=~' ? "doesn't match" : "matches";
1110  
1111 local $Level = $Level + 1;
1112 $self->diag( sprintf <<'DIAGNOSTIC', $thing, $match, $regex );
1113 %s
1114 %13s '%s'
1115 DIAGNOSTIC
1116  
1117 }
1118  
1119 return $ok;
1120 }
1121  
1122  
1123 sub is_fh {
1124 my $self = shift;
1125 my $maybe_fh = shift;
1126 return 0 unless defined $maybe_fh;
1127  
1128 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
1129 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
1130  
1131 return eval { $maybe_fh->isa("IO::Handle") } ||
1132 eval { tied($maybe_fh)->can('TIEHANDLE') };
1133 }
1134  
1135  
1136 sub level {
1137 my( $self, $level ) = @_;
1138  
1139 if( defined $level ) {
1140 $Level = $level;
1141 }
1142 return $Level;
1143 }
1144  
1145  
1146 sub use_numbers {
1147 my( $self, $use_nums ) = @_;
1148  
1149 my $ctx = $self->ctx;
1150 my $format = $ctx->hub->format;
1151 unless ($format && $format->can('no_numbers') && $format->can('set_no_numbers')) {
1152 warn "The current formatter does not support 'use_numbers'" if $format;
1153 return release $ctx, 0;
1154 }
1155  
1156 $format->set_no_numbers(!$use_nums) if defined $use_nums;
1157  
1158 return release $ctx, $format->no_numbers ? 0 : 1;
1159 }
1160  
1161 BEGIN {
1162 for my $method (qw(no_header no_diag)) {
1163 my $set = "set_$method";
1164 my $code = sub {
1165 my( $self, $no ) = @_;
1166  
1167 my $ctx = $self->ctx;
1168 my $format = $ctx->hub->format;
1169 unless ($format && $format->can($set)) {
1170 warn "The current formatter does not support '$method'" if $format;
1171 $ctx->release;
1172 return
1173 }
1174  
1175 $format->$set($no) if defined $no;
1176  
1177 return release $ctx, $format->$method ? 1 : 0;
1178 };
1179  
1180 no strict 'refs'; ## no critic
1181 *$method = $code;
1182 }
1183 }
1184  
1185 sub no_ending {
1186 my( $self, $no ) = @_;
1187  
1188 my $ctx = $self->ctx;
1189  
1190 $ctx->hub->set_no_ending($no) if defined $no;
1191  
1192 return release $ctx, $ctx->hub->no_ending;
1193 }
1194  
1195 sub diag {
1196 my $self = shift;
1197 return unless @_;
1198  
1199 my $ctx = $self->ctx;
1200 $ctx->diag(join '' => map {defined($_) ? $_ : 'undef'} @_);
1201 $ctx->release;
1202 return 0;
1203 }
1204  
1205  
1206 sub note {
1207 my $self = shift;
1208 return unless @_;
1209  
1210 my $ctx = $self->ctx;
1211 $ctx->note(join '' => map {defined($_) ? $_ : 'undef'} @_);
1212 $ctx->release;
1213 return 0;
1214 }
1215  
1216  
1217 sub explain {
1218 my $self = shift;
1219  
1220 local ($@, $!);
1221 require Data::Dumper;
1222  
1223 return map {
1224 ref $_
1225 ? do {
1226 my $dumper = Data::Dumper->new( [$_] );
1227 $dumper->Indent(1)->Terse(1);
1228 $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
1229 $dumper->Dump;
1230 }
1231 : $_
1232 } @_;
1233 }
1234  
1235  
1236 sub output {
1237 my( $self, $fh ) = @_;
1238  
1239 my $ctx = $self->ctx;
1240 my $format = $ctx->hub->format;
1241 $ctx->release;
1242 return unless $format && $format->isa('Test2::Formatter::TAP');
1243  
1244 $format->handles->[Test2::Formatter::TAP::OUT_STD()] = $self->_new_fh($fh)
1245 if defined $fh;
1246  
1247 return $format->handles->[Test2::Formatter::TAP::OUT_STD()];
1248 }
1249  
1250 sub failure_output {
1251 my( $self, $fh ) = @_;
1252  
1253 my $ctx = $self->ctx;
1254 my $format = $ctx->hub->format;
1255 $ctx->release;
1256 return unless $format && $format->isa('Test2::Formatter::TAP');
1257  
1258 $format->handles->[Test2::Formatter::TAP::OUT_ERR()] = $self->_new_fh($fh)
1259 if defined $fh;
1260  
1261 return $format->handles->[Test2::Formatter::TAP::OUT_ERR()];
1262 }
1263  
1264 sub todo_output {
1265 my( $self, $fh ) = @_;
1266  
1267 my $ctx = $self->ctx;
1268 my $format = $ctx->hub->format;
1269 $ctx->release;
1270 return unless $format && $format->isa('Test::Builder::Formatter');
1271  
1272 $format->handles->[Test::Builder::Formatter::OUT_TODO()] = $self->_new_fh($fh)
1273 if defined $fh;
1274  
1275 return $format->handles->[Test::Builder::Formatter::OUT_TODO()];
1276 }
1277  
1278 sub _new_fh {
1279 my $self = shift;
1280 my($file_or_fh) = shift;
1281  
1282 my $fh;
1283 if( $self->is_fh($file_or_fh) ) {
1284 $fh = $file_or_fh;
1285 }
1286 elsif( ref $file_or_fh eq 'SCALAR' ) {
1287 # Scalar refs as filehandles was added in 5.8.
1288 if( $] >= 5.008 ) {
1289 open $fh, ">>", $file_or_fh
1290 or $self->croak("Can't open scalar ref $file_or_fh: $!");
1291 }
1292 # Emulate scalar ref filehandles with a tie.
1293 else {
1294 $fh = Test::Builder::IO::Scalar->new($file_or_fh)
1295 or $self->croak("Can't tie scalar ref $file_or_fh");
1296 }
1297 }
1298 else {
1299 open $fh, ">", $file_or_fh
1300 or $self->croak("Can't open test output log $file_or_fh: $!");
1301 _autoflush($fh);
1302 }
1303  
1304 return $fh;
1305 }
1306  
1307 sub _autoflush {
1308 my($fh) = shift;
1309 my $old_fh = select $fh;
1310 $| = 1;
1311 select $old_fh;
1312  
1313 return;
1314 }
1315  
1316  
1317 sub reset_outputs {
1318 my $self = shift;
1319  
1320 my $ctx = $self->ctx;
1321 my $format = $ctx->hub->format;
1322 $ctx->release;
1323 return unless $format && $format->isa('Test2::Formatter::TAP');
1324 $format->set_handles([@{$self->{Orig_Handles}}]) if $self->{Orig_Handles};
1325  
1326 return;
1327 }
1328  
1329  
1330 sub carp {
1331 my $self = shift;
1332 my $ctx = $self->ctx;
1333 $ctx->alert(join "", @_);
1334 $ctx->release;
1335 }
1336  
1337 sub croak {
1338 my $self = shift;
1339 my $ctx = $self->ctx;
1340 $ctx->throw(join "", @_);
1341 $ctx->release;
1342 }
1343  
1344  
1345 sub current_test {
1346 my( $self, $num ) = @_;
1347  
1348 my $ctx = $self->ctx;
1349 my $hub = $ctx->hub;
1350  
1351 if( defined $num ) {
1352 $hub->set_count($num);
1353  
1354 # If the test counter is being pushed forward fill in the details.
1355 my $test_results = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1356 if( $num > @$test_results ) {
1357 my $start = @$test_results ? @$test_results : 0;
1358 for( $start .. $num - 1 ) {
1359 $test_results->[$_] = {
1360 'ok' => 1,
1361 actual_ok => undef,
1362 reason => 'incrementing test number',
1363 type => 'unknown',
1364 name => undef
1365 };
1366 }
1367 }
1368 # If backward, wipe history. Its their funeral.
1369 elsif( $num < @$test_results ) {
1370 $#{$test_results} = $num - 1;
1371 }
1372 }
1373 return release $ctx, $hub->count;
1374 }
1375  
1376  
1377 sub is_passing {
1378 my $self = shift;
1379  
1380 my $ctx = $self->ctx;
1381 my $hub = $ctx->hub;
1382  
1383 if( @_ ) {
1384 my ($bool) = @_;
1385 $hub->set_failed(0) if $bool;
1386 $hub->is_passing($bool);
1387 }
1388  
1389 return release $ctx, $hub->is_passing;
1390 }
1391  
1392  
1393 sub summary {
1394 my($self) = shift;
1395  
1396 my $ctx = $self->ctx;
1397 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1398 $ctx->release;
1399 return map { $_->{'ok'} } @$data;
1400 }
1401  
1402  
1403 sub details {
1404 my $self = shift;
1405 my $ctx = $self->ctx;
1406 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1407 $ctx->release;
1408 return @$data;
1409 }
1410  
1411  
1412 sub find_TODO {
1413 my( $self, $pack, $set, $new_value ) = @_;
1414  
1415 my $ctx = $self->ctx;
1416  
1417 $pack ||= $ctx->trace->package || $self->exported_to;
1418 $ctx->release;
1419  
1420 return unless $pack;
1421  
1422 no strict 'refs'; ## no critic
1423 no warnings 'once';
1424 my $old_value = ${ $pack . '::TODO' };
1425 $set and ${ $pack . '::TODO' } = $new_value;
1426 return $old_value;
1427 }
1428  
1429 sub todo {
1430 my( $self, $pack ) = @_;
1431  
1432 local $Level = $Level + 1;
1433 my $ctx = $self->ctx;
1434 $ctx->release;
1435  
1436 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo};
1437 return $meta->[-1]->[1] if $meta && @$meta;
1438  
1439 $pack ||= $ctx->trace->package;
1440  
1441 return unless $pack;
1442  
1443 no strict 'refs'; ## no critic
1444 no warnings 'once';
1445 return ${ $pack . '::TODO' };
1446 }
1447  
1448 sub in_todo {
1449 my $self = shift;
1450  
1451 local $Level = $Level + 1;
1452 my $ctx = $self->ctx;
1453 $ctx->release;
1454  
1455 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo};
1456 return 1 if $meta && @$meta;
1457  
1458 my $pack = $ctx->trace->package || return 0;
1459  
1460 no strict 'refs'; ## no critic
1461 no warnings 'once';
1462 my $todo = ${ $pack . '::TODO' };
1463  
1464 return 0 unless defined $todo;
1465 return 0 if "$todo" eq '';
1466 return 1;
1467 }
1468  
1469 sub todo_start {
1470 my $self = shift;
1471 my $message = @_ ? shift : '';
1472  
1473 my $ctx = $self->ctx;
1474  
1475 my $hub = $ctx->hub;
1476 my $filter = $hub->pre_filter(sub {
1477 my ($active_hub, $e) = @_;
1478  
1479 # Turn a diag into a todo diag
1480 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
1481  
1482 # Set todo on ok's
1483 if ($hub == $active_hub && $e->isa('Test2::Event::Ok')) {
1484 $e->set_todo($message);
1485 $e->set_effective_pass(1);
1486  
1487 if (my $result = $e->get_meta(__PACKAGE__)) {
1488 $result->{reason} ||= $message;
1489 $result->{type} ||= 'todo';
1490 $result->{ok} = 1;
1491 }
1492 }
1493  
1494 return $e;
1495 }, inherit => 1);
1496  
1497 push @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}} => [$filter, $message];
1498  
1499 $ctx->release;
1500  
1501 return;
1502 }
1503  
1504 sub todo_end {
1505 my $self = shift;
1506  
1507 my $ctx = $self->ctx;
1508  
1509 my $set = pop @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}};
1510  
1511 $ctx->throw('todo_end() called without todo_start()') unless $set;
1512  
1513 $ctx->hub->pre_unfilter($set->[0]);
1514  
1515 $ctx->release;
1516  
1517 return;
1518 }
1519  
1520  
1521 sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
1522 my( $self ) = @_;
1523  
1524 my $ctx = $self->ctx;
1525  
1526 my $trace = $ctx->trace;
1527 $ctx->release;
1528 return wantarray ? $trace->call : $trace->package;
1529 }
1530  
1531  
1532 sub _try {
1533 my( $self, $code, %opts ) = @_;
1534  
1535 my $error;
1536 my $return;
1537 {
1538 local $!; # eval can mess up $!
1539 local $@; # don't set $@ in the test
1540 local $SIG{__DIE__}; # don't trip an outside DIE handler.
1541 $return = eval { $code->() };
1542 $error = $@;
1543 }
1544  
1545 die $error if $error and $opts{die_on_fail};
1546  
1547 return wantarray ? ( $return, $error ) : $return;
1548 }
1549  
1550 sub _ending {
1551 my $self = shift;
1552 my ($ctx, $real_exit_code, $new) = @_;
1553  
1554 unless ($ctx) {
1555 my $octx = $self->ctx;
1556 $ctx = $octx->snapshot;
1557 $octx->release;
1558 }
1559  
1560 return if $ctx->hub->no_ending;
1561 return if $ctx->hub->meta(__PACKAGE__, {})->{Ending}++;
1562  
1563 # Don't bother with an ending if this is a forked copy. Only the parent
1564 # should do the ending.
1565 return unless $self->{Original_Pid} == $$;
1566  
1567 my $hub = $ctx->hub;
1568 return if $hub->bailed_out;
1569  
1570 my $plan = $hub->plan;
1571 my $count = $hub->count;
1572 my $failed = $hub->failed;
1573 my $passed = $hub->is_passing;
1574 return unless $plan || $count || $failed;
1575  
1576 # Ran tests but never declared a plan or hit done_testing
1577 if( !$hub->plan and $hub->count ) {
1578 $self->diag("Tests were run but no plan was declared and done_testing() was not seen.");
1579  
1580 if($real_exit_code) {
1581 $self->diag(<<"FAIL");
1582 Looks like your test exited with $real_exit_code just after $count.
1583 FAIL
1584 $$new ||= $real_exit_code;
1585 return;
1586 }
1587  
1588 # But if the tests ran, handle exit code.
1589 if($failed > 0) {
1590 my $exit_code = $failed <= 254 ? $failed : 254;
1591 $$new ||= $exit_code;
1592 return;
1593 }
1594  
1595 $$new ||= 254;
1596 return;
1597 }
1598  
1599 if ($real_exit_code && !$count) {
1600 $self->diag("Looks like your test exited with $real_exit_code before it could output anything.");
1601 $$new ||= $real_exit_code;
1602 return;
1603 }
1604  
1605 return if $plan && "$plan" eq 'SKIP';
1606  
1607 if (!$count) {
1608 $self->diag('No tests run!');
1609 $$new ||= 255;
1610 return;
1611 }
1612  
1613 if ($real_exit_code) {
1614 $self->diag(<<"FAIL");
1615 Looks like your test exited with $real_exit_code just after $count.
1616 FAIL
1617 $$new ||= $real_exit_code;
1618 return;
1619 }
1620  
1621 if ($plan eq 'NO PLAN') {
1622 $ctx->plan( $count );
1623 $plan = $hub->plan;
1624 }
1625  
1626 # Figure out if we passed or failed and print helpful messages.
1627 my $num_extra = $count - $plan;
1628  
1629 if ($num_extra != 0) {
1630 my $s = $plan == 1 ? '' : 's';
1631 $self->diag(<<"FAIL");
1632 Looks like you planned $plan test$s but ran $count.
1633 FAIL
1634 }
1635  
1636 if ($failed) {
1637 my $s = $failed == 1 ? '' : 's';
1638  
1639 my $qualifier = $num_extra == 0 ? '' : ' run';
1640  
1641 $self->diag(<<"FAIL");
1642 Looks like you failed $failed test$s of $count$qualifier.
1643 FAIL
1644 }
1645  
1646 if (!$passed && !$failed && $count && !$num_extra) {
1647 $ctx->diag(<<"FAIL");
1648 All assertions passed, but errors were encountered.
1649 FAIL
1650 }
1651  
1652 my $exit_code = 0;
1653 if ($failed) {
1654 $exit_code = $failed <= 254 ? $failed : 254;
1655 }
1656 elsif ($num_extra != 0) {
1657 $exit_code = 255;
1658 }
1659 elsif (!$passed) {
1660 $exit_code = 255;
1661 }
1662  
1663 $$new ||= $exit_code;
1664 return;
1665 }
1666  
1667 # Some things used this even though it was private... I am looking at you
1668 # Test::Builder::Prefix...
1669 sub _print_comment {
1670 my( $self, $fh, @msgs ) = @_;
1671  
1672 return if $self->no_diag;
1673 return unless @msgs;
1674  
1675 # Prevent printing headers when compiling (i.e. -c)
1676 return if $^C;
1677  
1678 # Smash args together like print does.
1679 # Convert undef to 'undef' so its readable.
1680 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1681  
1682 # Escape the beginning, _print will take care of the rest.
1683 $msg =~ s/^/# /;
1684  
1685 local( $\, $", $, ) = ( undef, ' ', '' );
1686 print $fh $msg;
1687  
1688 return 0;
1689 }
1690  
1691 # This is used by Test::SharedFork to turn on IPC after the fact. Not
1692 # documenting because I do not want it used. The method name is borrowed from
1693 # Test::Builder 2
1694 # Once Test2 stuff goes stable this method will be removed and Test::SharedFork
1695 # will be made smarter.
1696 sub coordinate_forks {
1697 my $self = shift;
1698  
1699 {
1700 local ($@, $!);
1701 require Test2::IPC;
1702 }
1703 Test2::IPC->import;
1704 Test2::API::test2_ipc_enable_polling();
1705 my $ipc = Test2::IPC::apply_ipc($self->{Stack});
1706 $ipc->set_no_fatal(1);
1707 Test2::API::test2_no_wait(1);
1708 Test2::API::test2_ipc_enable_shm();
1709 }
1710  
1711 1;
1712  
1713 __END__