This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Test-Simple to CPAN version 1.302067
[perl5.git] / cpan / Test-Simple / lib / Test / Builder.pm
1 package Test::Builder;
2
3 use 5.006;
4 use strict;
5 use warnings;
6
7 our $VERSION = '1.302067';
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__
1714
1715 =head1 NAME
1716
1717 Test::Builder - Backend for building test libraries
1718
1719 =head1 SYNOPSIS
1720
1721   package My::Test::Module;
1722   use base 'Test::Builder::Module';
1723
1724   my $CLASS = __PACKAGE__;
1725
1726   sub ok {
1727       my($test, $name) = @_;
1728       my $tb = $CLASS->builder;
1729
1730       $tb->ok($test, $name);
1731   }
1732
1733
1734 =head1 DESCRIPTION
1735
1736 L<Test::Simple> and L<Test::More> have proven to be popular testing modules,
1737 but they're not always flexible enough.  Test::Builder provides a
1738 building block upon which to write your own test libraries I<which can
1739 work together>.
1740
1741 =head2 Construction
1742
1743 =over 4
1744
1745 =item B<new>
1746
1747   my $Test = Test::Builder->new;
1748
1749 Returns a Test::Builder object representing the current state of the
1750 test.
1751
1752 Since you only run one test per program C<new> always returns the same
1753 Test::Builder object.  No matter how many times you call C<new()>, you're
1754 getting the same object.  This is called a singleton.  This is done so that
1755 multiple modules share such global information as the test counter and
1756 where test output is going.
1757
1758 If you want a completely new Test::Builder object different from the
1759 singleton, use C<create>.
1760
1761 =item B<create>
1762
1763   my $Test = Test::Builder->create;
1764
1765 Ok, so there can be more than one Test::Builder object and this is how
1766 you get it.  You might use this instead of C<new()> if you're testing
1767 a Test::Builder based module, but otherwise you probably want C<new>.
1768
1769 B<NOTE>: the implementation is not complete.  C<level>, for example, is still
1770 shared by B<all> Test::Builder objects, even ones created using this method.
1771 Also, the method name may change in the future.
1772
1773 =item B<subtest>
1774
1775     $builder->subtest($name, \&subtests, @args);
1776
1777 See documentation of C<subtest> in Test::More.
1778
1779 C<subtest> also, and optionally, accepts arguments which will be passed to the
1780 subtests reference.
1781
1782 =item B<name>
1783
1784  diag $builder->name;
1785
1786 Returns the name of the current builder.  Top level builders default to C<$0>
1787 (the name of the executable).  Child builders are named via the C<child>
1788 method.  If no name is supplied, will be named "Child of $parent->name".
1789
1790 =item B<reset>
1791
1792   $Test->reset;
1793
1794 Reinitializes the Test::Builder singleton to its original state.
1795 Mostly useful for tests run in persistent environments where the same
1796 test might be run multiple times in the same process.
1797
1798 =back
1799
1800 =head2 Setting up tests
1801
1802 These methods are for setting up tests and declaring how many there
1803 are.  You usually only want to call one of these methods.
1804
1805 =over 4
1806
1807 =item B<plan>
1808
1809   $Test->plan('no_plan');
1810   $Test->plan( skip_all => $reason );
1811   $Test->plan( tests => $num_tests );
1812
1813 A convenient way to set up your tests.  Call this and Test::Builder
1814 will print the appropriate headers and take the appropriate actions.
1815
1816 If you call C<plan()>, don't call any of the other methods below.
1817
1818 =item B<expected_tests>
1819
1820     my $max = $Test->expected_tests;
1821     $Test->expected_tests($max);
1822
1823 Gets/sets the number of tests we expect this test to run and prints out
1824 the appropriate headers.
1825
1826
1827 =item B<no_plan>
1828
1829   $Test->no_plan;
1830
1831 Declares that this test will run an indeterminate number of tests.
1832
1833
1834 =item B<done_testing>
1835
1836   $Test->done_testing();
1837   $Test->done_testing($num_tests);
1838
1839 Declares that you are done testing, no more tests will be run after this point.
1840
1841 If a plan has not yet been output, it will do so.
1842
1843 $num_tests is the number of tests you planned to run.  If a numbered
1844 plan was already declared, and if this contradicts, a failing test
1845 will be run to reflect the planning mistake.  If C<no_plan> was declared,
1846 this will override.
1847
1848 If C<done_testing()> is called twice, the second call will issue a
1849 failing test.
1850
1851 If C<$num_tests> is omitted, the number of tests run will be used, like
1852 no_plan.
1853
1854 C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but
1855 safer. You'd use it like so:
1856
1857     $Test->ok($a == $b);
1858     $Test->done_testing();
1859
1860 Or to plan a variable number of tests:
1861
1862     for my $test (@tests) {
1863         $Test->ok($test);
1864     }
1865     $Test->done_testing(scalar @tests);
1866
1867
1868 =item B<has_plan>
1869
1870   $plan = $Test->has_plan
1871
1872 Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan
1873 has been set), C<no_plan> (indeterminate # of tests) or an integer (the number
1874 of expected tests).
1875
1876 =item B<skip_all>
1877
1878   $Test->skip_all;
1879   $Test->skip_all($reason);
1880
1881 Skips all the tests, using the given C<$reason>.  Exits immediately with 0.
1882
1883 =item B<exported_to>
1884
1885   my $pack = $Test->exported_to;
1886   $Test->exported_to($pack);
1887
1888 Tells Test::Builder what package you exported your functions to.
1889
1890 This method isn't terribly useful since modules which share the same
1891 Test::Builder object might get exported to different packages and only
1892 the last one will be honored.
1893
1894 =back
1895
1896 =head2 Running tests
1897
1898 These actually run the tests, analogous to the functions in Test::More.
1899
1900 They all return true if the test passed, false if the test failed.
1901
1902 C<$name> is always optional.
1903
1904 =over 4
1905
1906 =item B<ok>
1907
1908   $Test->ok($test, $name);
1909
1910 Your basic test.  Pass if C<$test> is true, fail if $test is false.  Just
1911 like Test::Simple's C<ok()>.
1912
1913 =item B<is_eq>
1914
1915   $Test->is_eq($got, $expected, $name);
1916
1917 Like Test::More's C<is()>.  Checks if C<$got eq $expected>.  This is the
1918 string version.
1919
1920 C<undef> only ever matches another C<undef>.
1921
1922 =item B<is_num>
1923
1924   $Test->is_num($got, $expected, $name);
1925
1926 Like Test::More's C<is()>.  Checks if C<$got == $expected>.  This is the
1927 numeric version.
1928
1929 C<undef> only ever matches another C<undef>.
1930
1931 =item B<isnt_eq>
1932
1933   $Test->isnt_eq($got, $dont_expect, $name);
1934
1935 Like L<Test::More>'s C<isnt()>.  Checks if C<$got ne $dont_expect>.  This is
1936 the string version.
1937
1938 =item B<isnt_num>
1939
1940   $Test->isnt_num($got, $dont_expect, $name);
1941
1942 Like L<Test::More>'s C<isnt()>.  Checks if C<$got ne $dont_expect>.  This is
1943 the numeric version.
1944
1945 =item B<like>
1946
1947   $Test->like($thing, qr/$regex/, $name);
1948   $Test->like($thing, '/$regex/', $name);
1949
1950 Like L<Test::More>'s C<like()>.  Checks if $thing matches the given C<$regex>.
1951
1952 =item B<unlike>
1953
1954   $Test->unlike($thing, qr/$regex/, $name);
1955   $Test->unlike($thing, '/$regex/', $name);
1956
1957 Like L<Test::More>'s C<unlike()>.  Checks if $thing B<does not match> the
1958 given C<$regex>.
1959
1960 =item B<cmp_ok>
1961
1962   $Test->cmp_ok($thing, $type, $that, $name);
1963
1964 Works just like L<Test::More>'s C<cmp_ok()>.
1965
1966     $Test->cmp_ok($big_num, '!=', $other_big_num);
1967
1968 =back
1969
1970 =head2 Other Testing Methods
1971
1972 These are methods which are used in the course of writing a test but are not themselves tests.
1973
1974 =over 4
1975
1976 =item B<BAIL_OUT>
1977
1978     $Test->BAIL_OUT($reason);
1979
1980 Indicates to the L<Test::Harness> that things are going so badly all
1981 testing should terminate.  This includes running any additional test
1982 scripts.
1983
1984 It will exit with 255.
1985
1986 =for deprecated
1987 BAIL_OUT() used to be BAILOUT()
1988
1989 =item B<skip>
1990
1991     $Test->skip;
1992     $Test->skip($why);
1993
1994 Skips the current test, reporting C<$why>.
1995
1996 =item B<todo_skip>
1997
1998   $Test->todo_skip;
1999   $Test->todo_skip($why);
2000
2001 Like C<skip()>, only it will declare the test as failing and TODO.  Similar
2002 to
2003
2004     print "not ok $tnum # TODO $why\n";
2005
2006 =begin _unimplemented
2007
2008 =item B<skip_rest>
2009
2010   $Test->skip_rest;
2011   $Test->skip_rest($reason);
2012
2013 Like C<skip()>, only it skips all the rest of the tests you plan to run
2014 and terminates the test.
2015
2016 If you're running under C<no_plan>, it skips once and terminates the
2017 test.
2018
2019 =end _unimplemented
2020
2021 =back
2022
2023
2024 =head2 Test building utility methods
2025
2026 These methods are useful when writing your own test methods.
2027
2028 =over 4
2029
2030 =item B<maybe_regex>
2031
2032   $Test->maybe_regex(qr/$regex/);
2033   $Test->maybe_regex('/$regex/');
2034
2035 This method used to be useful back when Test::Builder worked on Perls
2036 before 5.6 which didn't have qr//.  Now its pretty useless.
2037
2038 Convenience method for building testing functions that take regular
2039 expressions as arguments.
2040
2041 Takes a quoted regular expression produced by C<qr//>, or a string
2042 representing a regular expression.
2043
2044 Returns a Perl value which may be used instead of the corresponding
2045 regular expression, or C<undef> if its argument is not recognized.
2046
2047 For example, a version of C<like()>, sans the useful diagnostic messages,
2048 could be written as:
2049
2050   sub laconic_like {
2051       my ($self, $thing, $regex, $name) = @_;
2052       my $usable_regex = $self->maybe_regex($regex);
2053       die "expecting regex, found '$regex'\n"
2054           unless $usable_regex;
2055       $self->ok($thing =~ m/$usable_regex/, $name);
2056   }
2057
2058
2059 =item B<is_fh>
2060
2061     my $is_fh = $Test->is_fh($thing);
2062
2063 Determines if the given C<$thing> can be used as a filehandle.
2064
2065 =cut
2066
2067
2068 =back
2069
2070
2071 =head2 Test style
2072
2073
2074 =over 4
2075
2076 =item B<level>
2077
2078     $Test->level($how_high);
2079
2080 How far up the call stack should C<$Test> look when reporting where the
2081 test failed.
2082
2083 Defaults to 1.
2084
2085 Setting L<$Test::Builder::Level> overrides.  This is typically useful
2086 localized:
2087
2088     sub my_ok {
2089         my $test = shift;
2090
2091         local $Test::Builder::Level = $Test::Builder::Level + 1;
2092         $TB->ok($test);
2093     }
2094
2095 To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant.
2096
2097 =item B<use_numbers>
2098
2099     $Test->use_numbers($on_or_off);
2100
2101 Whether or not the test should output numbers.  That is, this if true:
2102
2103   ok 1
2104   ok 2
2105   ok 3
2106
2107 or this if false
2108
2109   ok
2110   ok
2111   ok
2112
2113 Most useful when you can't depend on the test output order, such as
2114 when threads or forking is involved.
2115
2116 Defaults to on.
2117
2118 =item B<no_diag>
2119
2120     $Test->no_diag($no_diag);
2121
2122 If set true no diagnostics will be printed.  This includes calls to
2123 C<diag()>.
2124
2125 =item B<no_ending>
2126
2127     $Test->no_ending($no_ending);
2128
2129 Normally, Test::Builder does some extra diagnostics when the test
2130 ends.  It also changes the exit code as described below.
2131
2132 If this is true, none of that will be done.
2133
2134 =item B<no_header>
2135
2136     $Test->no_header($no_header);
2137
2138 If set to true, no "1..N" header will be printed.
2139
2140 =back
2141
2142 =head2 Output
2143
2144 Controlling where the test output goes.
2145
2146 It's ok for your test to change where STDOUT and STDERR point to,
2147 Test::Builder's default output settings will not be affected.
2148
2149 =over 4
2150
2151 =item B<diag>
2152
2153     $Test->diag(@msgs);
2154
2155 Prints out the given C<@msgs>.  Like C<print>, arguments are simply
2156 appended together.
2157
2158 Normally, it uses the C<failure_output()> handle, but if this is for a
2159 TODO test, the C<todo_output()> handle is used.
2160
2161 Output will be indented and marked with a # so as not to interfere
2162 with test output.  A newline will be put on the end if there isn't one
2163 already.
2164
2165 We encourage using this rather than calling print directly.
2166
2167 Returns false.  Why?  Because C<diag()> is often used in conjunction with
2168 a failing test (C<ok() || diag()>) it "passes through" the failure.
2169
2170     return ok(...) || diag(...);
2171
2172 =for blame transfer
2173 Mark Fowler <mark@twoshortplanks.com>
2174
2175 =item B<note>
2176
2177     $Test->note(@msgs);
2178
2179 Like C<diag()>, but it prints to the C<output()> handle so it will not
2180 normally be seen by the user except in verbose mode.
2181
2182 =item B<explain>
2183
2184     my @dump = $Test->explain(@msgs);
2185
2186 Will dump the contents of any references in a human readable format.
2187 Handy for things like...
2188
2189     is_deeply($have, $want) || diag explain $have;
2190
2191 or
2192
2193     is_deeply($have, $want) || note explain $have;
2194
2195 =item B<output>
2196
2197 =item B<failure_output>
2198
2199 =item B<todo_output>
2200
2201     my $filehandle = $Test->output;
2202     $Test->output($filehandle);
2203     $Test->output($filename);
2204     $Test->output(\$scalar);
2205
2206 These methods control where Test::Builder will print its output.
2207 They take either an open C<$filehandle>, a C<$filename> to open and write to
2208 or a C<$scalar> reference to append to.  It will always return a C<$filehandle>.
2209
2210 B<output> is where normal "ok/not ok" test output goes.
2211
2212 Defaults to STDOUT.
2213
2214 B<failure_output> is where diagnostic output on test failures and
2215 C<diag()> goes.  It is normally not read by Test::Harness and instead is
2216 displayed to the user.
2217
2218 Defaults to STDERR.
2219
2220 C<todo_output> is used instead of C<failure_output()> for the
2221 diagnostics of a failing TODO test.  These will not be seen by the
2222 user.
2223
2224 Defaults to STDOUT.
2225
2226 =item reset_outputs
2227
2228   $tb->reset_outputs;
2229
2230 Resets all the output filehandles back to their defaults.
2231
2232 =item carp
2233
2234   $tb->carp(@message);
2235
2236 Warns with C<@message> but the message will appear to come from the
2237 point where the original test function was called (C<< $tb->caller >>).
2238
2239 =item croak
2240
2241   $tb->croak(@message);
2242
2243 Dies with C<@message> but the message will appear to come from the
2244 point where the original test function was called (C<< $tb->caller >>).
2245
2246
2247 =back
2248
2249
2250 =head2 Test Status and Info
2251
2252 =over 4
2253
2254 =item B<current_test>
2255
2256     my $curr_test = $Test->current_test;
2257     $Test->current_test($num);
2258
2259 Gets/sets the current test number we're on.  You usually shouldn't
2260 have to set this.
2261
2262 If set forward, the details of the missing tests are filled in as 'unknown'.
2263 if set backward, the details of the intervening tests are deleted.  You
2264 can erase history if you really want to.
2265
2266
2267 =item B<is_passing>
2268
2269    my $ok = $builder->is_passing;
2270
2271 Indicates if the test suite is currently passing.
2272
2273 More formally, it will be false if anything has happened which makes
2274 it impossible for the test suite to pass.  True otherwise.
2275
2276 For example, if no tests have run C<is_passing()> will be true because
2277 even though a suite with no tests is a failure you can add a passing
2278 test to it and start passing.
2279
2280 Don't think about it too much.
2281
2282
2283 =item B<summary>
2284
2285     my @tests = $Test->summary;
2286
2287 A simple summary of the tests so far.  True for pass, false for fail.
2288 This is a logical pass/fail, so todos are passes.
2289
2290 Of course, test #1 is $tests[0], etc...
2291
2292
2293 =item B<details>
2294
2295     my @tests = $Test->details;
2296
2297 Like C<summary()>, but with a lot more detail.
2298
2299     $tests[$test_num - 1] =
2300             { 'ok'       => is the test considered a pass?
2301               actual_ok  => did it literally say 'ok'?
2302               name       => name of the test (if any)
2303               type       => type of test (if any, see below).
2304               reason     => reason for the above (if any)
2305             };
2306
2307 'ok' is true if Test::Harness will consider the test to be a pass.
2308
2309 'actual_ok' is a reflection of whether or not the test literally
2310 printed 'ok' or 'not ok'.  This is for examining the result of 'todo'
2311 tests.
2312
2313 'name' is the name of the test.
2314
2315 'type' indicates if it was a special test.  Normal tests have a type
2316 of ''.  Type can be one of the following:
2317
2318     skip        see skip()
2319     todo        see todo()
2320     todo_skip   see todo_skip()
2321     unknown     see below
2322
2323 Sometimes the Test::Builder test counter is incremented without it
2324 printing any test output, for example, when C<current_test()> is changed.
2325 In these cases, Test::Builder doesn't know the result of the test, so
2326 its type is 'unknown'.  These details for these tests are filled in.
2327 They are considered ok, but the name and actual_ok is left C<undef>.
2328
2329 For example "not ok 23 - hole count # TODO insufficient donuts" would
2330 result in this structure:
2331
2332     $tests[22] =    # 23 - 1, since arrays start from 0.
2333       { ok        => 1,   # logically, the test passed since its todo
2334         actual_ok => 0,   # in absolute terms, it failed
2335         name      => 'hole count',
2336         type      => 'todo',
2337         reason    => 'insufficient donuts'
2338       };
2339
2340
2341 =item B<todo>
2342
2343     my $todo_reason = $Test->todo;
2344     my $todo_reason = $Test->todo($pack);
2345
2346 If the current tests are considered "TODO" it will return the reason,
2347 if any.  This reason can come from a C<$TODO> variable or the last call
2348 to C<todo_start()>.
2349
2350 Since a TODO test does not need a reason, this function can return an
2351 empty string even when inside a TODO block.  Use C<< $Test->in_todo >>
2352 to determine if you are currently inside a TODO block.
2353
2354 C<todo()> is about finding the right package to look for C<$TODO> in.  It's
2355 pretty good at guessing the right package to look at.  It first looks for
2356 the caller based on C<$Level + 1>, since C<todo()> is usually called inside
2357 a test function.  As a last resort it will use C<exported_to()>.
2358
2359 Sometimes there is some confusion about where C<todo()> should be looking
2360 for the C<$TODO> variable.  If you want to be sure, tell it explicitly
2361 what $pack to use.
2362
2363 =item B<find_TODO>
2364
2365     my $todo_reason = $Test->find_TODO();
2366     my $todo_reason = $Test->find_TODO($pack);
2367
2368 Like C<todo()> but only returns the value of C<$TODO> ignoring
2369 C<todo_start()>.
2370
2371 Can also be used to set C<$TODO> to a new value while returning the
2372 old value:
2373
2374     my $old_reason = $Test->find_TODO($pack, 1, $new_reason);
2375
2376 =item B<in_todo>
2377
2378     my $in_todo = $Test->in_todo;
2379
2380 Returns true if the test is currently inside a TODO block.
2381
2382 =item B<todo_start>
2383
2384     $Test->todo_start();
2385     $Test->todo_start($message);
2386
2387 This method allows you declare all subsequent tests as TODO tests, up until
2388 the C<todo_end> method has been called.
2389
2390 The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out
2391 whether or not we're in a TODO test.  However, often we find that this is not
2392 possible to determine (such as when we want to use C<$TODO> but
2393 the tests are being executed in other packages which can't be inferred
2394 beforehand).
2395
2396 Note that you can use this to nest "todo" tests
2397
2398  $Test->todo_start('working on this');
2399  # lots of code
2400  $Test->todo_start('working on that');
2401  # more code
2402  $Test->todo_end;
2403  $Test->todo_end;
2404
2405 This is generally not recommended, but large testing systems often have weird
2406 internal needs.
2407
2408 We've tried to make this also work with the TODO: syntax, but it's not
2409 guaranteed and its use is also discouraged:
2410
2411  TODO: {
2412      local $TODO = 'We have work to do!';
2413      $Test->todo_start('working on this');
2414      # lots of code
2415      $Test->todo_start('working on that');
2416      # more code
2417      $Test->todo_end;
2418      $Test->todo_end;
2419  }
2420
2421 Pick one style or another of "TODO" to be on the safe side.
2422
2423
2424 =item C<todo_end>
2425
2426  $Test->todo_end;
2427
2428 Stops running tests as "TODO" tests.  This method is fatal if called without a
2429 preceding C<todo_start> method call.
2430
2431 =item B<caller>
2432
2433     my $package = $Test->caller;
2434     my($pack, $file, $line) = $Test->caller;
2435     my($pack, $file, $line) = $Test->caller($height);
2436
2437 Like the normal C<caller()>, except it reports according to your C<level()>.
2438
2439 C<$height> will be added to the C<level()>.
2440
2441 If C<caller()> winds up off the top of the stack it report the highest context.
2442
2443 =back
2444
2445 =head1 EXIT CODES
2446
2447 If all your tests passed, Test::Builder will exit with zero (which is
2448 normal).  If anything failed it will exit with how many failed.  If
2449 you run less (or more) tests than you planned, the missing (or extras)
2450 will be considered failures.  If no tests were ever run Test::Builder
2451 will throw a warning and exit with 255.  If the test died, even after
2452 having successfully completed all its tests, it will still be
2453 considered a failure and will exit with 255.
2454
2455 So the exit codes are...
2456
2457     0                   all tests successful
2458     255                 test died or all passed but wrong # of tests run
2459     any other number    how many failed (including missing or extras)
2460
2461 If you fail more than 254 tests, it will be reported as 254.
2462
2463 =head1 THREADS
2464
2465 In perl 5.8.1 and later, Test::Builder is thread-safe.  The test number is
2466 shared by all threads.  This means if one thread sets the test number using
2467 C<current_test()> they will all be effected.
2468
2469 While versions earlier than 5.8.1 had threads they contain too many
2470 bugs to support.
2471
2472 Test::Builder is only thread-aware if threads.pm is loaded I<before>
2473 Test::Builder.
2474
2475 =head1 MEMORY
2476
2477 An informative hash, accessible via C<details()>, is stored for each
2478 test you perform.  So memory usage will scale linearly with each test
2479 run. Although this is not a problem for most test suites, it can
2480 become an issue if you do large (hundred thousands to million)
2481 combinatorics tests in the same run.
2482
2483 In such cases, you are advised to either split the test file into smaller
2484 ones, or use a reverse approach, doing "normal" (code) compares and
2485 triggering C<fail()> should anything go unexpected.
2486
2487 Future versions of Test::Builder will have a way to turn history off.
2488
2489
2490 =head1 EXAMPLES
2491
2492 CPAN can provide the best examples.  L<Test::Simple>, L<Test::More>,
2493 L<Test::Exception> and L<Test::Differences> all use Test::Builder.
2494
2495 =head1 SEE ALSO
2496
2497 L<Test::Simple>, L<Test::More>, L<Test::Harness>
2498
2499 =head1 AUTHORS
2500
2501 Original code by chromatic, maintained by Michael G Schwern
2502 E<lt>schwern@pobox.comE<gt>
2503
2504 =head1 MAINTAINERS
2505
2506 =over 4
2507
2508 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
2509
2510 =back
2511
2512 =head1 COPYRIGHT
2513
2514 Copyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and
2515                        Michael G Schwern E<lt>schwern@pobox.comE<gt>.
2516
2517 This program is free software; you can redistribute it and/or
2518 modify it under the same terms as Perl itself.
2519
2520 See F<http://www.perl.com/perl/misc/Artistic.html>