xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test/Builder.pm (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1package Test::Builder;
2
3use 5.006;
4use strict;
5use warnings;
6
7our $VERSION = '1.001014';
8$VERSION = eval $VERSION;    ## no critic (BuiltinFunctions::ProhibitStringyEval)
9
10BEGIN {
11    if( $] < 5.008 ) {
12        require Test::Builder::IO::Scalar;
13    }
14}
15
16
17# Make Test::Builder thread-safe for ithreads.
18BEGIN {
19    use Config;
20    # Load threads::shared when threads are turned on.
21    # 5.8.0's threads are so busted we no longer support them.
22    if( $] >= 5.008001 && $Config{useithreads} && $INC{'threads.pm'} ) {
23        require threads::shared;
24
25        # Hack around YET ANOTHER threads::shared bug.  It would
26        # occasionally forget the contents of the variable when sharing it.
27        # So we first copy the data, then share, then put our copy back.
28        *share = sub (\[$@%]) {
29            my $type = ref $_[0];
30            my $data;
31
32            if( $type eq 'HASH' ) {
33                %$data = %{ $_[0] };
34            }
35            elsif( $type eq 'ARRAY' ) {
36                @$data = @{ $_[0] };
37            }
38            elsif( $type eq 'SCALAR' ) {
39                $$data = ${ $_[0] };
40            }
41            else {
42                die( "Unknown type: " . $type );
43            }
44
45            $_[0] = &threads::shared::share( $_[0] );
46
47            if( $type eq 'HASH' ) {
48                %{ $_[0] } = %$data;
49            }
50            elsif( $type eq 'ARRAY' ) {
51                @{ $_[0] } = @$data;
52            }
53            elsif( $type eq 'SCALAR' ) {
54                ${ $_[0] } = $$data;
55            }
56            else {
57                die( "Unknown type: " . $type );
58            }
59
60            return $_[0];
61        };
62    }
63    # 5.8.0's threads::shared is busted when threads are off
64    # and earlier Perls just don't have that module at all.
65    else {
66        *share = sub { return $_[0] };
67        *lock  = sub { 0 };
68    }
69}
70
71=head1 NAME
72
73Test::Builder - Backend for building test libraries
74
75=head1 SYNOPSIS
76
77  package My::Test::Module;
78  use base 'Test::Builder::Module';
79
80  my $CLASS = __PACKAGE__;
81
82  sub ok {
83      my($test, $name) = @_;
84      my $tb = $CLASS->builder;
85
86      $tb->ok($test, $name);
87  }
88
89
90=head1 DESCRIPTION
91
92L<Test::Simple> and L<Test::More> have proven to be popular testing modules,
93but they're not always flexible enough.  Test::Builder provides a
94building block upon which to write your own test libraries I<which can
95work together>.
96
97=head2 Construction
98
99=over 4
100
101=item B<new>
102
103  my $Test = Test::Builder->new;
104
105Returns a Test::Builder object representing the current state of the
106test.
107
108Since you only run one test per program C<new> always returns the same
109Test::Builder object.  No matter how many times you call C<new()>, you're
110getting the same object.  This is called a singleton.  This is done so that
111multiple modules share such global information as the test counter and
112where test output is going.
113
114If you want a completely new Test::Builder object different from the
115singleton, use C<create>.
116
117=cut
118
119our $Test = Test::Builder->new;
120
121sub new {
122    my($class) = shift;
123    $Test ||= $class->create;
124    return $Test;
125}
126
127=item B<create>
128
129  my $Test = Test::Builder->create;
130
131Ok, so there can be more than one Test::Builder object and this is how
132you get it.  You might use this instead of C<new()> if you're testing
133a Test::Builder based module, but otherwise you probably want C<new>.
134
135B<NOTE>: the implementation is not complete.  C<level>, for example, is
136still shared amongst B<all> Test::Builder objects, even ones created using
137this method.  Also, the method name may change in the future.
138
139=cut
140
141sub create {
142    my $class = shift;
143
144    my $self = bless {}, $class;
145    $self->reset;
146
147    return $self;
148}
149
150
151# Copy an object, currently a shallow.
152# This does *not* bless the destination.  This keeps the destructor from
153# firing when we're just storing a copy of the object to restore later.
154sub _copy {
155    my($src, $dest) = @_;
156
157    %$dest = %$src;
158    _share_keys($dest);
159
160    return;
161}
162
163
164=item B<child>
165
166  my $child = $builder->child($name_of_child);
167  $child->plan( tests => 4 );
168  $child->ok(some_code());
169  ...
170  $child->finalize;
171
172Returns a new instance of C<Test::Builder>.  Any output from this child will
173be indented four spaces more than the parent's indentation.  When done, the
174C<finalize> method I<must> be called explicitly.
175
176Trying to create a new child with a previous child still active (i.e.,
177C<finalize> not called) will C<croak>.
178
179Trying to run a test when you have an open child will also C<croak> and cause
180the test suite to fail.
181
182=cut
183
184sub child {
185    my( $self, $name ) = @_;
186
187    if( $self->{Child_Name} ) {
188        $self->croak("You already have a child named ($self->{Child_Name}) running");
189    }
190
191    my $parent_in_todo = $self->in_todo;
192
193    # Clear $TODO for the child.
194    my $orig_TODO = $self->find_TODO(undef, 1, undef);
195
196    my $class = ref $self;
197    my $child = $class->create;
198
199    # Add to our indentation
200    $child->_indent( $self->_indent . '    ' );
201
202    # Make the child use the same outputs as the parent
203    for my $method (qw(output failure_output todo_output)) {
204        $child->$method( $self->$method );
205    }
206
207    # Ensure the child understands if they're inside a TODO
208    if( $parent_in_todo ) {
209        $child->failure_output( $self->todo_output );
210    }
211
212    # This will be reset in finalize. We do this here lest one child failure
213    # cause all children to fail.
214    $child->{Child_Error} = $?;
215    $?                    = 0;
216    $child->{Parent}      = $self;
217    $child->{Parent_TODO} = $orig_TODO;
218    $child->{Name}        = $name || "Child of " . $self->name;
219    $self->{Child_Name}   = $child->name;
220    return $child;
221}
222
223
224=item B<subtest>
225
226    $builder->subtest($name, \&subtests, @args);
227
228See documentation of C<subtest> in Test::More.
229
230C<subtest> also, and optionally, accepts arguments which will be passed to the
231subtests reference.
232
233=cut
234
235sub subtest {
236    my $self = shift;
237    my($name, $subtests, @args) = @_;
238
239    if ('CODE' ne ref $subtests) {
240        $self->croak("subtest()'s second argument must be a code ref");
241    }
242
243    # Turn the child into the parent so anyone who has stored a copy of
244    # the Test::Builder singleton will get the child.
245    my $error;
246    my $child;
247    my $parent = {};
248    {
249        # child() calls reset() which sets $Level to 1, so we localize
250        # $Level first to limit the scope of the reset to the subtest.
251        local $Test::Builder::Level = $Test::Builder::Level + 1;
252
253        # Store the guts of $self as $parent and turn $child into $self.
254        $child  = $self->child($name);
255        _copy($self,  $parent);
256        _copy($child, $self);
257
258        my $run_the_subtests = sub {
259            # Add subtest name for clarification of starting point
260            $self->note("Subtest: $name");
261            $subtests->(@args);
262            $self->done_testing unless $self->_plan_handled;
263            1;
264        };
265
266        if( !eval { $run_the_subtests->() } ) {
267            $error = $@;
268        }
269    }
270
271    # Restore the parent and the copied child.
272    _copy($self,   $child);
273    _copy($parent, $self);
274
275    # Restore the parent's $TODO
276    $self->find_TODO(undef, 1, $child->{Parent_TODO});
277
278    # Die *after* we restore the parent.
279    die $error if $error and !eval { $error->isa('Test::Builder::Exception') };
280
281    local $Test::Builder::Level = $Test::Builder::Level + 1;
282    my $finalize = $child->finalize;
283
284    $self->BAIL_OUT($child->{Bailed_Out_Reason}) if $child->{Bailed_Out};
285
286    return $finalize;
287}
288
289=begin _private
290
291=item B<_plan_handled>
292
293    if ( $Test->_plan_handled ) { ... }
294
295Returns true if the developer has explicitly handled the plan via:
296
297=over 4
298
299=item * Explicitly setting the number of tests
300
301=item * Setting 'no_plan'
302
303=item * Set 'skip_all'.
304
305=back
306
307This is currently used in subtests when we implicitly call C<< $Test->done_testing >>
308if the developer has not set a plan.
309
310=end _private
311
312=cut
313
314sub _plan_handled {
315    my $self = shift;
316    return $self->{Have_Plan} || $self->{No_Plan} || $self->{Skip_All};
317}
318
319
320=item B<finalize>
321
322  my $ok = $child->finalize;
323
324When your child is done running tests, you must call C<finalize> to clean up
325and tell the parent your pass/fail status.
326
327Calling C<finalize> on a child with open children will C<croak>.
328
329If the child falls out of scope before C<finalize> is called, a failure
330diagnostic will be issued and the child is considered to have failed.
331
332No attempt to call methods on a child after C<finalize> is called is
333guaranteed to succeed.
334
335Calling this on the root builder is a no-op.
336
337=cut
338
339sub finalize {
340    my $self = shift;
341
342    return unless $self->parent;
343    if( $self->{Child_Name} ) {
344        $self->croak("Can't call finalize() with child ($self->{Child_Name}) active");
345    }
346
347    local $? = 0;     # don't fail if $subtests happened to set $? nonzero
348    $self->_ending;
349
350    # XXX This will only be necessary for TAP envelopes (we think)
351    #$self->_print( $self->is_passing ? "PASS\n" : "FAIL\n" );
352
353    local $Test::Builder::Level = $Test::Builder::Level + 1;
354    my $ok = 1;
355    $self->parent->{Child_Name} = undef;
356    unless ($self->{Bailed_Out}) {
357        if ( $self->{Skip_All} ) {
358            $self->parent->skip($self->{Skip_All}, $self->name);
359        }
360        elsif ( not @{ $self->{Test_Results} } ) {
361            $self->parent->ok( 0, sprintf q[No tests run for subtest "%s"], $self->name );
362        }
363        else {
364            $self->parent->ok( $self->is_passing, $self->name );
365        }
366    }
367    $? = $self->{Child_Error};
368    delete $self->{Parent};
369
370    return $self->is_passing;
371}
372
373sub _indent      {
374    my $self = shift;
375
376    if( @_ ) {
377        $self->{Indent} = shift;
378    }
379
380    return $self->{Indent};
381}
382
383=item B<parent>
384
385 if ( my $parent = $builder->parent ) {
386     ...
387 }
388
389Returns the parent C<Test::Builder> instance, if any.  Only used with child
390builders for nested TAP.
391
392=cut
393
394sub parent { shift->{Parent} }
395
396=item B<name>
397
398 diag $builder->name;
399
400Returns the name of the current builder.  Top level builders default to C<$0>
401(the name of the executable).  Child builders are named via the C<child>
402method.  If no name is supplied, will be named "Child of $parent->name".
403
404=cut
405
406sub name { shift->{Name} }
407
408sub DESTROY {
409    my $self = shift;
410    if ( $self->parent and $$ == $self->{Original_Pid} ) {
411        my $name = $self->name;
412        $self->diag(<<"FAIL");
413Child ($name) exited without calling finalize()
414FAIL
415        $self->parent->{In_Destroy} = 1;
416        $self->parent->ok(0, $name);
417    }
418}
419
420=item B<reset>
421
422  $Test->reset;
423
424Reinitializes the Test::Builder singleton to its original state.
425Mostly useful for tests run in persistent environments where the same
426test might be run multiple times in the same process.
427
428=cut
429
430our $Level;
431
432sub reset {    ## no critic (Subroutines::ProhibitBuiltinHomonyms)
433    my($self) = @_;
434
435    # We leave this a global because it has to be localized and localizing
436    # hash keys is just asking for pain.  Also, it was documented.
437    $Level = 1;
438
439    $self->{Name}         = $0;
440    $self->is_passing(1);
441    $self->{Ending}       = 0;
442    $self->{Have_Plan}    = 0;
443    $self->{No_Plan}      = 0;
444    $self->{Have_Output_Plan} = 0;
445    $self->{Done_Testing} = 0;
446
447    $self->{Original_Pid} = $$;
448    $self->{Child_Name}   = undef;
449    $self->{Indent}     ||= '';
450
451    $self->{Curr_Test} = 0;
452    $self->{Test_Results} = &share( [] );
453
454    $self->{Exported_To}    = undef;
455    $self->{Expected_Tests} = 0;
456
457    $self->{Skip_All} = 0;
458
459    $self->{Use_Nums} = 1;
460
461    $self->{No_Header} = 0;
462    $self->{No_Ending} = 0;
463
464    $self->{Todo}       = undef;
465    $self->{Todo_Stack} = [];
466    $self->{Start_Todo} = 0;
467    $self->{Opened_Testhandles} = 0;
468
469    $self->_share_keys;
470    $self->_dup_stdhandles;
471
472    return;
473}
474
475
476# Shared scalar values are lost when a hash is copied, so we have
477# a separate method to restore them.
478# Shared references are retained across copies.
479sub _share_keys {
480    my $self = shift;
481
482    share( $self->{Curr_Test} );
483
484    return;
485}
486
487
488=back
489
490=head2 Setting up tests
491
492These methods are for setting up tests and declaring how many there
493are.  You usually only want to call one of these methods.
494
495=over 4
496
497=item B<plan>
498
499  $Test->plan('no_plan');
500  $Test->plan( skip_all => $reason );
501  $Test->plan( tests => $num_tests );
502
503A convenient way to set up your tests.  Call this and Test::Builder
504will print the appropriate headers and take the appropriate actions.
505
506If you call C<plan()>, don't call any of the other methods below.
507
508If a child calls "skip_all" in the plan, a C<Test::Builder::Exception> is
509thrown.  Trap this error, call C<finalize()> and don't run any more tests on
510the child.
511
512 my $child = $Test->child('some child');
513 eval { $child->plan( $condition ? ( skip_all => $reason ) : ( tests => 3 )  ) };
514 if ( eval { $@->isa('Test::Builder::Exception') } ) {
515    $child->finalize;
516    return;
517 }
518 # run your tests
519
520=cut
521
522my %plan_cmds = (
523    no_plan     => \&no_plan,
524    skip_all    => \&skip_all,
525    tests       => \&_plan_tests,
526);
527
528sub plan {
529    my( $self, $cmd, $arg ) = @_;
530
531    return unless $cmd;
532
533    local $Level = $Level + 1;
534
535    $self->croak("You tried to plan twice") if $self->{Have_Plan};
536
537    if( my $method = $plan_cmds{$cmd} ) {
538        local $Level = $Level + 1;
539        $self->$method($arg);
540    }
541    else {
542        my @args = grep { defined } ( $cmd, $arg );
543        $self->croak("plan() doesn't understand @args");
544    }
545
546    return 1;
547}
548
549
550sub _plan_tests {
551    my($self, $arg) = @_;
552
553    if($arg) {
554        local $Level = $Level + 1;
555        return $self->expected_tests($arg);
556    }
557    elsif( !defined $arg ) {
558        $self->croak("Got an undefined number of tests");
559    }
560    else {
561        $self->croak("You said to run 0 tests");
562    }
563
564    return;
565}
566
567=item B<expected_tests>
568
569    my $max = $Test->expected_tests;
570    $Test->expected_tests($max);
571
572Gets/sets the number of tests we expect this test to run and prints out
573the appropriate headers.
574
575=cut
576
577sub expected_tests {
578    my $self = shift;
579    my($max) = @_;
580
581    if(@_) {
582        $self->croak("Number of tests must be a positive integer.  You gave it '$max'")
583          unless $max =~ /^\+?\d+$/;
584
585        $self->{Expected_Tests} = $max;
586        $self->{Have_Plan}      = 1;
587
588        $self->_output_plan($max) unless $self->no_header;
589    }
590    return $self->{Expected_Tests};
591}
592
593=item B<no_plan>
594
595  $Test->no_plan;
596
597Declares that this test will run an indeterminate number of tests.
598
599=cut
600
601sub no_plan {
602    my($self, $arg) = @_;
603
604    $self->carp("no_plan takes no arguments") if $arg;
605
606    $self->{No_Plan}   = 1;
607    $self->{Have_Plan} = 1;
608
609    return 1;
610}
611
612=begin private
613
614=item B<_output_plan>
615
616  $tb->_output_plan($max);
617  $tb->_output_plan($max, $directive);
618  $tb->_output_plan($max, $directive => $reason);
619
620Handles displaying the test plan.
621
622If a C<$directive> and/or C<$reason> are given they will be output with the
623plan.  So here's what skipping all tests looks like:
624
625    $tb->_output_plan(0, "SKIP", "Because I said so");
626
627It sets C<< $tb->{Have_Output_Plan} >> and will croak if the plan was already
628output.
629
630=end private
631
632=cut
633
634sub _output_plan {
635    my($self, $max, $directive, $reason) = @_;
636
637    $self->carp("The plan was already output") if $self->{Have_Output_Plan};
638
639    my $plan = "1..$max";
640    $plan .= " # $directive" if defined $directive;
641    $plan .= " $reason"      if defined $reason;
642
643    $self->_print("$plan\n");
644
645    $self->{Have_Output_Plan} = 1;
646
647    return;
648}
649
650
651=item B<done_testing>
652
653  $Test->done_testing();
654  $Test->done_testing($num_tests);
655
656Declares that you are done testing, no more tests will be run after this point.
657
658If a plan has not yet been output, it will do so.
659
660$num_tests is the number of tests you planned to run.  If a numbered
661plan was already declared, and if this contradicts, a failing test
662will be run to reflect the planning mistake.  If C<no_plan> was declared,
663this will override.
664
665If C<done_testing()> is called twice, the second call will issue a
666failing test.
667
668If C<$num_tests> is omitted, the number of tests run will be used, like
669no_plan.
670
671C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but
672safer. You'd use it like so:
673
674    $Test->ok($a == $b);
675    $Test->done_testing();
676
677Or to plan a variable number of tests:
678
679    for my $test (@tests) {
680        $Test->ok($test);
681    }
682    $Test->done_testing(scalar @tests);
683
684=cut
685
686sub done_testing {
687    my($self, $num_tests) = @_;
688
689    # If done_testing() specified the number of tests, shut off no_plan.
690    if( defined $num_tests ) {
691        $self->{No_Plan} = 0;
692    }
693    else {
694        $num_tests = $self->current_test;
695    }
696
697    if( $self->{Done_Testing} ) {
698        my($file, $line) = @{$self->{Done_Testing}}[1,2];
699        $self->ok(0, "done_testing() was already called at $file line $line");
700        return;
701    }
702
703    $self->{Done_Testing} = [caller];
704
705    if( $self->expected_tests && $num_tests != $self->expected_tests ) {
706        $self->ok(0, "planned to run @{[ $self->expected_tests ]} ".
707                     "but done_testing() expects $num_tests");
708    }
709    else {
710        $self->{Expected_Tests} = $num_tests;
711    }
712
713    $self->_output_plan($num_tests) unless $self->{Have_Output_Plan};
714
715    $self->{Have_Plan} = 1;
716
717    # The wrong number of tests were run
718    $self->is_passing(0) if $self->{Expected_Tests} != $self->{Curr_Test};
719
720    # No tests were run
721    $self->is_passing(0) if $self->{Curr_Test} == 0;
722
723    return 1;
724}
725
726
727=item B<has_plan>
728
729  $plan = $Test->has_plan
730
731Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan
732has been set), C<no_plan> (indeterminate # of tests) or an integer (the number
733of expected tests).
734
735=cut
736
737sub has_plan {
738    my $self = shift;
739
740    return( $self->{Expected_Tests} ) if $self->{Expected_Tests};
741    return('no_plan') if $self->{No_Plan};
742    return(undef);
743}
744
745=item B<skip_all>
746
747  $Test->skip_all;
748  $Test->skip_all($reason);
749
750Skips all the tests, using the given C<$reason>.  Exits immediately with 0.
751
752=cut
753
754sub skip_all {
755    my( $self, $reason ) = @_;
756
757    $self->{Skip_All} = $self->parent ? $reason : 1;
758
759    $self->_output_plan(0, "SKIP", $reason) unless $self->no_header;
760    if ( $self->parent ) {
761        die bless {} => 'Test::Builder::Exception';
762    }
763    exit(0);
764}
765
766=item B<exported_to>
767
768  my $pack = $Test->exported_to;
769  $Test->exported_to($pack);
770
771Tells Test::Builder what package you exported your functions to.
772
773This method isn't terribly useful since modules which share the same
774Test::Builder object might get exported to different packages and only
775the last one will be honored.
776
777=cut
778
779sub exported_to {
780    my( $self, $pack ) = @_;
781
782    if( defined $pack ) {
783        $self->{Exported_To} = $pack;
784    }
785    return $self->{Exported_To};
786}
787
788=back
789
790=head2 Running tests
791
792These actually run the tests, analogous to the functions in Test::More.
793
794They all return true if the test passed, false if the test failed.
795
796C<$name> is always optional.
797
798=over 4
799
800=item B<ok>
801
802  $Test->ok($test, $name);
803
804Your basic test.  Pass if C<$test> is true, fail if $test is false.  Just
805like Test::Simple's C<ok()>.
806
807=cut
808
809sub ok {
810    my( $self, $test, $name ) = @_;
811
812    if ( $self->{Child_Name} and not $self->{In_Destroy} ) {
813        $name = 'unnamed test' unless defined $name;
814        $self->is_passing(0);
815        $self->croak("Cannot run test ($name) with active children");
816    }
817    # $test might contain an object which we don't want to accidentally
818    # store, so we turn it into a boolean.
819    $test = $test ? 1 : 0;
820
821    lock $self->{Curr_Test};
822    $self->{Curr_Test}++;
823
824    # In case $name is a string overloaded object, force it to stringify.
825    $self->_unoverload_str( \$name );
826
827    $self->diag(<<"ERR") if defined $name and $name =~ /^[\d\s]+$/;
828    You named your test '$name'.  You shouldn't use numbers for your test names.
829    Very confusing.
830ERR
831
832    # Capture the value of $TODO for the rest of this ok() call
833    # so it can more easily be found by other routines.
834    my $todo    = $self->todo();
835    my $in_todo = $self->in_todo;
836    local $self->{Todo} = $todo if $in_todo;
837
838    $self->_unoverload_str( \$todo );
839
840    my $out;
841    my $result = &share( {} );
842
843    unless($test) {
844        $out .= "not ";
845        @$result{ 'ok', 'actual_ok' } = ( ( $self->in_todo ? 1 : 0 ), 0 );
846    }
847    else {
848        @$result{ 'ok', 'actual_ok' } = ( 1, $test );
849    }
850
851    $out .= "ok";
852    $out .= " $self->{Curr_Test}" if $self->use_numbers;
853
854    if( defined $name ) {
855        $name =~ s|#|\\#|g;    # # in a name can confuse Test::Harness.
856        $out .= " - $name";
857        $result->{name} = $name;
858    }
859    else {
860        $result->{name} = '';
861    }
862
863    if( $self->in_todo ) {
864        $out .= " # TODO $todo";
865        $result->{reason} = $todo;
866        $result->{type}   = 'todo';
867    }
868    else {
869        $result->{reason} = '';
870        $result->{type}   = '';
871    }
872
873    $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = $result;
874    $out .= "\n";
875
876    $self->_print($out);
877
878    unless($test) {
879        my $msg = $self->in_todo ? "Failed (TODO)" : "Failed";
880        $self->_print_to_fh( $self->_diag_fh, "\n" ) if $ENV{HARNESS_ACTIVE};
881
882        my( undef, $file, $line ) = $self->caller;
883        if( defined $name ) {
884            $self->diag(qq[  $msg test '$name'\n]);
885            $self->diag(qq[  at $file line $line.\n]);
886        }
887        else {
888            $self->diag(qq[  $msg test at $file line $line.\n]);
889        }
890    }
891
892    $self->is_passing(0) unless $test || $self->in_todo;
893
894    # Check that we haven't violated the plan
895    $self->_check_is_passing_plan();
896
897    return $test ? 1 : 0;
898}
899
900
901# Check that we haven't yet violated the plan and set
902# is_passing() accordingly
903sub _check_is_passing_plan {
904    my $self = shift;
905
906    my $plan = $self->has_plan;
907    return unless defined $plan;        # no plan yet defined
908    return unless $plan !~ /\D/;        # no numeric plan
909    $self->is_passing(0) if $plan < $self->{Curr_Test};
910}
911
912
913sub _unoverload {
914    my $self = shift;
915    my $type = shift;
916
917    $self->_try(sub { require overload; }, die_on_fail => 1);
918
919    foreach my $thing (@_) {
920        if( $self->_is_object($$thing) ) {
921            if( my $string_meth = overload::Method( $$thing, $type ) ) {
922                $$thing = $$thing->$string_meth();
923            }
924        }
925    }
926
927    return;
928}
929
930sub _is_object {
931    my( $self, $thing ) = @_;
932
933    return $self->_try( sub { ref $thing && $thing->isa('UNIVERSAL') } ) ? 1 : 0;
934}
935
936sub _unoverload_str {
937    my $self = shift;
938
939    return $self->_unoverload( q[""], @_ );
940}
941
942sub _unoverload_num {
943    my $self = shift;
944
945    $self->_unoverload( '0+', @_ );
946
947    for my $val (@_) {
948        next unless $self->_is_dualvar($$val);
949        $$val = $$val + 0;
950    }
951
952    return;
953}
954
955# This is a hack to detect a dualvar such as $!
956sub _is_dualvar {
957    my( $self, $val ) = @_;
958
959    # Objects are not dualvars.
960    return 0 if ref $val;
961
962    no warnings 'numeric';
963    my $numval = $val + 0;
964    return ($numval != 0 and $numval ne $val ? 1 : 0);
965}
966
967=item B<is_eq>
968
969  $Test->is_eq($got, $expected, $name);
970
971Like Test::More's C<is()>.  Checks if C<$got eq $expected>.  This is the
972string version.
973
974C<undef> only ever matches another C<undef>.
975
976=item B<is_num>
977
978  $Test->is_num($got, $expected, $name);
979
980Like Test::More's C<is()>.  Checks if C<$got == $expected>.  This is the
981numeric version.
982
983C<undef> only ever matches another C<undef>.
984
985=cut
986
987sub is_eq {
988    my( $self, $got, $expect, $name ) = @_;
989    local $Level = $Level + 1;
990
991    if( !defined $got || !defined $expect ) {
992        # undef only matches undef and nothing else
993        my $test = !defined $got && !defined $expect;
994
995        $self->ok( $test, $name );
996        $self->_is_diag( $got, 'eq', $expect ) unless $test;
997        return $test;
998    }
999
1000    return $self->cmp_ok( $got, 'eq', $expect, $name );
1001}
1002
1003sub is_num {
1004    my( $self, $got, $expect, $name ) = @_;
1005    local $Level = $Level + 1;
1006
1007    if( !defined $got || !defined $expect ) {
1008        # undef only matches undef and nothing else
1009        my $test = !defined $got && !defined $expect;
1010
1011        $self->ok( $test, $name );
1012        $self->_is_diag( $got, '==', $expect ) unless $test;
1013        return $test;
1014    }
1015
1016    return $self->cmp_ok( $got, '==', $expect, $name );
1017}
1018
1019sub _diag_fmt {
1020    my( $self, $type, $val ) = @_;
1021
1022    if( defined $$val ) {
1023        if( $type eq 'eq' or $type eq 'ne' ) {
1024            # quote and force string context
1025            $$val = "'$$val'";
1026        }
1027        else {
1028            # force numeric context
1029            $self->_unoverload_num($val);
1030        }
1031    }
1032    else {
1033        $$val = 'undef';
1034    }
1035
1036    return;
1037}
1038
1039sub _is_diag {
1040    my( $self, $got, $type, $expect ) = @_;
1041
1042    $self->_diag_fmt( $type, $_ ) for \$got, \$expect;
1043
1044    local $Level = $Level + 1;
1045    return $self->diag(<<"DIAGNOSTIC");
1046         got: $got
1047    expected: $expect
1048DIAGNOSTIC
1049
1050}
1051
1052sub _isnt_diag {
1053    my( $self, $got, $type ) = @_;
1054
1055    $self->_diag_fmt( $type, \$got );
1056
1057    local $Level = $Level + 1;
1058    return $self->diag(<<"DIAGNOSTIC");
1059         got: $got
1060    expected: anything else
1061DIAGNOSTIC
1062}
1063
1064=item B<isnt_eq>
1065
1066  $Test->isnt_eq($got, $dont_expect, $name);
1067
1068Like L<Test::More>'s C<isnt()>.  Checks if C<$got ne $dont_expect>.  This is
1069the string version.
1070
1071=item B<isnt_num>
1072
1073  $Test->isnt_num($got, $dont_expect, $name);
1074
1075Like L<Test::More>'s C<isnt()>.  Checks if C<$got ne $dont_expect>.  This is
1076the numeric version.
1077
1078=cut
1079
1080sub isnt_eq {
1081    my( $self, $got, $dont_expect, $name ) = @_;
1082    local $Level = $Level + 1;
1083
1084    if( !defined $got || !defined $dont_expect ) {
1085        # undef only matches undef and nothing else
1086        my $test = defined $got || defined $dont_expect;
1087
1088        $self->ok( $test, $name );
1089        $self->_isnt_diag( $got, 'ne' ) unless $test;
1090        return $test;
1091    }
1092
1093    return $self->cmp_ok( $got, 'ne', $dont_expect, $name );
1094}
1095
1096sub isnt_num {
1097    my( $self, $got, $dont_expect, $name ) = @_;
1098    local $Level = $Level + 1;
1099
1100    if( !defined $got || !defined $dont_expect ) {
1101        # undef only matches undef and nothing else
1102        my $test = defined $got || defined $dont_expect;
1103
1104        $self->ok( $test, $name );
1105        $self->_isnt_diag( $got, '!=' ) unless $test;
1106        return $test;
1107    }
1108
1109    return $self->cmp_ok( $got, '!=', $dont_expect, $name );
1110}
1111
1112=item B<like>
1113
1114  $Test->like($thing, qr/$regex/, $name);
1115  $Test->like($thing, '/$regex/', $name);
1116
1117Like L<Test::More>'s C<like()>.  Checks if $thing matches the given C<$regex>.
1118
1119=item B<unlike>
1120
1121  $Test->unlike($thing, qr/$regex/, $name);
1122  $Test->unlike($thing, '/$regex/', $name);
1123
1124Like L<Test::More>'s C<unlike()>.  Checks if $thing B<does not match> the
1125given C<$regex>.
1126
1127=cut
1128
1129sub like {
1130    my( $self, $thing, $regex, $name ) = @_;
1131
1132    local $Level = $Level + 1;
1133    return $self->_regex_ok( $thing, $regex, '=~', $name );
1134}
1135
1136sub unlike {
1137    my( $self, $thing, $regex, $name ) = @_;
1138
1139    local $Level = $Level + 1;
1140    return $self->_regex_ok( $thing, $regex, '!~', $name );
1141}
1142
1143=item B<cmp_ok>
1144
1145  $Test->cmp_ok($thing, $type, $that, $name);
1146
1147Works just like L<Test::More>'s C<cmp_ok()>.
1148
1149    $Test->cmp_ok($big_num, '!=', $other_big_num);
1150
1151=cut
1152
1153my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );
1154
1155# Bad, these are not comparison operators. Should we include more?
1156my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "...");
1157
1158sub cmp_ok {
1159    my( $self, $got, $type, $expect, $name ) = @_;
1160
1161    if ($cmp_ok_bl{$type}) {
1162        $self->croak("$type is not a valid comparison operator in cmp_ok()");
1163    }
1164
1165    my ($test, $succ);
1166    my $error;
1167    {
1168        ## no critic (BuiltinFunctions::ProhibitStringyEval)
1169
1170        local( $@, $!, $SIG{__DIE__} );    # isolate eval
1171
1172        my($pack, $file, $line) = $self->caller();
1173
1174        # This is so that warnings come out at the caller's level
1175        $succ = eval qq[
1176#line $line "(eval in cmp_ok) $file"
1177\$test = (\$got $type \$expect);
11781;
1179];
1180        $error = $@;
1181    }
1182    local $Level = $Level + 1;
1183    my $ok = $self->ok( $test, $name );
1184
1185    # Treat overloaded objects as numbers if we're asked to do a
1186    # numeric comparison.
1187    my $unoverload
1188      = $numeric_cmps{$type}
1189      ? '_unoverload_num'
1190      : '_unoverload_str';
1191
1192    $self->diag(<<"END") unless $succ;
1193An error occurred while using $type:
1194------------------------------------
1195$error
1196------------------------------------
1197END
1198
1199    unless($ok) {
1200        $self->$unoverload( \$got, \$expect );
1201
1202        if( $type =~ /^(eq|==)$/ ) {
1203            $self->_is_diag( $got, $type, $expect );
1204        }
1205        elsif( $type =~ /^(ne|!=)$/ ) {
1206            $self->_isnt_diag( $got, $type );
1207        }
1208        else {
1209            $self->_cmp_diag( $got, $type, $expect );
1210        }
1211    }
1212    return $ok;
1213}
1214
1215sub _cmp_diag {
1216    my( $self, $got, $type, $expect ) = @_;
1217
1218    $got    = defined $got    ? "'$got'"    : 'undef';
1219    $expect = defined $expect ? "'$expect'" : 'undef';
1220
1221    local $Level = $Level + 1;
1222    return $self->diag(<<"DIAGNOSTIC");
1223    $got
1224        $type
1225    $expect
1226DIAGNOSTIC
1227}
1228
1229sub _caller_context {
1230    my $self = shift;
1231
1232    my( $pack, $file, $line ) = $self->caller(1);
1233
1234    my $code = '';
1235    $code .= "#line $line $file\n" if defined $file and defined $line;
1236
1237    return $code;
1238}
1239
1240=back
1241
1242
1243=head2 Other Testing Methods
1244
1245These are methods which are used in the course of writing a test but are not themselves tests.
1246
1247=over 4
1248
1249=item B<BAIL_OUT>
1250
1251    $Test->BAIL_OUT($reason);
1252
1253Indicates to the L<Test::Harness> that things are going so badly all
1254testing should terminate.  This includes running any additional test
1255scripts.
1256
1257It will exit with 255.
1258
1259=cut
1260
1261sub BAIL_OUT {
1262    my( $self, $reason ) = @_;
1263
1264    $self->{Bailed_Out} = 1;
1265
1266    if ($self->parent) {
1267        $self->{Bailed_Out_Reason} = $reason;
1268        $self->no_ending(1);
1269        die bless {} => 'Test::Builder::Exception';
1270    }
1271
1272    $self->_print("Bail out!  $reason");
1273    exit 255;
1274}
1275
1276=for deprecated
1277BAIL_OUT() used to be BAILOUT()
1278
1279=cut
1280
1281{
1282    no warnings 'once';
1283    *BAILOUT = \&BAIL_OUT;
1284}
1285
1286=item B<skip>
1287
1288    $Test->skip;
1289    $Test->skip($why);
1290
1291Skips the current test, reporting C<$why>.
1292
1293=cut
1294
1295sub skip {
1296    my( $self, $why, $name ) = @_;
1297    $why ||= '';
1298    $name = '' unless defined $name;
1299    $self->_unoverload_str( \$why );
1300
1301    lock( $self->{Curr_Test} );
1302    $self->{Curr_Test}++;
1303
1304    $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
1305        {
1306            'ok'      => 1,
1307            actual_ok => 1,
1308            name      => $name,
1309            type      => 'skip',
1310            reason    => $why,
1311        }
1312    );
1313
1314    my $out = "ok";
1315    $out .= " $self->{Curr_Test}" if $self->use_numbers;
1316    $out .= " # skip";
1317    $out .= " $why"               if length $why;
1318    $out .= "\n";
1319
1320    $self->_print($out);
1321
1322    return 1;
1323}
1324
1325=item B<todo_skip>
1326
1327  $Test->todo_skip;
1328  $Test->todo_skip($why);
1329
1330Like C<skip()>, only it will declare the test as failing and TODO.  Similar
1331to
1332
1333    print "not ok $tnum # TODO $why\n";
1334
1335=cut
1336
1337sub todo_skip {
1338    my( $self, $why ) = @_;
1339    $why ||= '';
1340
1341    lock( $self->{Curr_Test} );
1342    $self->{Curr_Test}++;
1343
1344    $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
1345        {
1346            'ok'      => 1,
1347            actual_ok => 0,
1348            name      => '',
1349            type      => 'todo_skip',
1350            reason    => $why,
1351        }
1352    );
1353
1354    my $out = "not ok";
1355    $out .= " $self->{Curr_Test}" if $self->use_numbers;
1356    $out .= " # TODO & SKIP $why\n";
1357
1358    $self->_print($out);
1359
1360    return 1;
1361}
1362
1363=begin _unimplemented
1364
1365=item B<skip_rest>
1366
1367  $Test->skip_rest;
1368  $Test->skip_rest($reason);
1369
1370Like C<skip()>, only it skips all the rest of the tests you plan to run
1371and terminates the test.
1372
1373If you're running under C<no_plan>, it skips once and terminates the
1374test.
1375
1376=end _unimplemented
1377
1378=back
1379
1380
1381=head2 Test building utility methods
1382
1383These methods are useful when writing your own test methods.
1384
1385=over 4
1386
1387=item B<maybe_regex>
1388
1389  $Test->maybe_regex(qr/$regex/);
1390  $Test->maybe_regex('/$regex/');
1391
1392This method used to be useful back when Test::Builder worked on Perls
1393before 5.6 which didn't have qr//.  Now its pretty useless.
1394
1395Convenience method for building testing functions that take regular
1396expressions as arguments.
1397
1398Takes a quoted regular expression produced by C<qr//>, or a string
1399representing a regular expression.
1400
1401Returns a Perl value which may be used instead of the corresponding
1402regular expression, or C<undef> if its argument is not recognised.
1403
1404For example, a version of C<like()>, sans the useful diagnostic messages,
1405could be written as:
1406
1407  sub laconic_like {
1408      my ($self, $thing, $regex, $name) = @_;
1409      my $usable_regex = $self->maybe_regex($regex);
1410      die "expecting regex, found '$regex'\n"
1411          unless $usable_regex;
1412      $self->ok($thing =~ m/$usable_regex/, $name);
1413  }
1414
1415=cut
1416
1417sub maybe_regex {
1418    my( $self, $regex ) = @_;
1419    my $usable_regex = undef;
1420
1421    return $usable_regex unless defined $regex;
1422
1423    my( $re, $opts );
1424
1425    # Check for qr/foo/
1426    if( _is_qr($regex) ) {
1427        $usable_regex = $regex;
1428    }
1429    # Check for '/foo/' or 'm,foo,'
1430    elsif(( $re, $opts )        = $regex =~ m{^ /(.*)/ (\w*) $ }sx              or
1431          ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
1432    )
1433    {
1434        $usable_regex = length $opts ? "(?$opts)$re" : $re;
1435    }
1436
1437    return $usable_regex;
1438}
1439
1440sub _is_qr {
1441    my $regex = shift;
1442
1443    # is_regexp() checks for regexes in a robust manner, say if they're
1444    # blessed.
1445    return re::is_regexp($regex) if defined &re::is_regexp;
1446    return ref $regex eq 'Regexp';
1447}
1448
1449sub _regex_ok {
1450    my( $self, $thing, $regex, $cmp, $name ) = @_;
1451
1452    my $ok           = 0;
1453    my $usable_regex = $self->maybe_regex($regex);
1454    unless( defined $usable_regex ) {
1455        local $Level = $Level + 1;
1456        $ok = $self->ok( 0, $name );
1457        $self->diag("    '$regex' doesn't look much like a regex to me.");
1458        return $ok;
1459    }
1460
1461    {
1462        my $test;
1463        my $context = $self->_caller_context;
1464
1465        {
1466            ## no critic (BuiltinFunctions::ProhibitStringyEval)
1467
1468            local( $@, $!, $SIG{__DIE__} );    # isolate eval
1469
1470            # No point in issuing an uninit warning, they'll see it in the diagnostics
1471            no warnings 'uninitialized';
1472
1473            $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0};
1474        }
1475
1476        $test = !$test if $cmp eq '!~';
1477
1478        local $Level = $Level + 1;
1479        $ok = $self->ok( $test, $name );
1480    }
1481
1482    unless($ok) {
1483        $thing = defined $thing ? "'$thing'" : 'undef';
1484        my $match = $cmp eq '=~' ? "doesn't match" : "matches";
1485
1486        local $Level = $Level + 1;
1487        $self->diag( sprintf <<'DIAGNOSTIC', $thing, $match, $regex );
1488                  %s
1489    %13s '%s'
1490DIAGNOSTIC
1491
1492    }
1493
1494    return $ok;
1495}
1496
1497# I'm not ready to publish this.  It doesn't deal with array return
1498# values from the code or context.
1499
1500=begin private
1501
1502=item B<_try>
1503
1504    my $return_from_code          = $Test->try(sub { code });
1505    my($return_from_code, $error) = $Test->try(sub { code });
1506
1507Works like eval BLOCK except it ensures it has no effect on the rest
1508of the test (ie. C<$@> is not set) nor is effected by outside
1509interference (ie. C<$SIG{__DIE__}>) and works around some quirks in older
1510Perls.
1511
1512C<$error> is what would normally be in C<$@>.
1513
1514It is suggested you use this in place of eval BLOCK.
1515
1516=cut
1517
1518sub _try {
1519    my( $self, $code, %opts ) = @_;
1520
1521    my $error;
1522    my $return;
1523    {
1524        local $!;               # eval can mess up $!
1525        local $@;               # don't set $@ in the test
1526        local $SIG{__DIE__};    # don't trip an outside DIE handler.
1527        $return = eval { $code->() };
1528        $error = $@;
1529    }
1530
1531    die $error if $error and $opts{die_on_fail};
1532
1533    return wantarray ? ( $return, $error ) : $return;
1534}
1535
1536=end private
1537
1538
1539=item B<is_fh>
1540
1541    my $is_fh = $Test->is_fh($thing);
1542
1543Determines if the given C<$thing> can be used as a filehandle.
1544
1545=cut
1546
1547sub is_fh {
1548    my $self     = shift;
1549    my $maybe_fh = shift;
1550    return 0 unless defined $maybe_fh;
1551
1552    return 1 if ref $maybe_fh  eq 'GLOB';    # its a glob ref
1553    return 1 if ref \$maybe_fh eq 'GLOB';    # its a glob
1554
1555    return eval { $maybe_fh->isa("IO::Handle") } ||
1556           eval { tied($maybe_fh)->can('TIEHANDLE') };
1557}
1558
1559=back
1560
1561
1562=head2 Test style
1563
1564
1565=over 4
1566
1567=item B<level>
1568
1569    $Test->level($how_high);
1570
1571How far up the call stack should C<$Test> look when reporting where the
1572test failed.
1573
1574Defaults to 1.
1575
1576Setting L<$Test::Builder::Level> overrides.  This is typically useful
1577localized:
1578
1579    sub my_ok {
1580        my $test = shift;
1581
1582        local $Test::Builder::Level = $Test::Builder::Level + 1;
1583        $TB->ok($test);
1584    }
1585
1586To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant.
1587
1588=cut
1589
1590sub level {
1591    my( $self, $level ) = @_;
1592
1593    if( defined $level ) {
1594        $Level = $level;
1595    }
1596    return $Level;
1597}
1598
1599=item B<use_numbers>
1600
1601    $Test->use_numbers($on_or_off);
1602
1603Whether or not the test should output numbers.  That is, this if true:
1604
1605  ok 1
1606  ok 2
1607  ok 3
1608
1609or this if false
1610
1611  ok
1612  ok
1613  ok
1614
1615Most useful when you can't depend on the test output order, such as
1616when threads or forking is involved.
1617
1618Defaults to on.
1619
1620=cut
1621
1622sub use_numbers {
1623    my( $self, $use_nums ) = @_;
1624
1625    if( defined $use_nums ) {
1626        $self->{Use_Nums} = $use_nums;
1627    }
1628    return $self->{Use_Nums};
1629}
1630
1631=item B<no_diag>
1632
1633    $Test->no_diag($no_diag);
1634
1635If set true no diagnostics will be printed.  This includes calls to
1636C<diag()>.
1637
1638=item B<no_ending>
1639
1640    $Test->no_ending($no_ending);
1641
1642Normally, Test::Builder does some extra diagnostics when the test
1643ends.  It also changes the exit code as described below.
1644
1645If this is true, none of that will be done.
1646
1647=item B<no_header>
1648
1649    $Test->no_header($no_header);
1650
1651If set to true, no "1..N" header will be printed.
1652
1653=cut
1654
1655foreach my $attribute (qw(No_Header No_Ending No_Diag)) {
1656    my $method = lc $attribute;
1657
1658    my $code = sub {
1659        my( $self, $no ) = @_;
1660
1661        if( defined $no ) {
1662            $self->{$attribute} = $no;
1663        }
1664        return $self->{$attribute};
1665    };
1666
1667    no strict 'refs';    ## no critic
1668    *{ __PACKAGE__ . '::' . $method } = $code;
1669}
1670
1671=back
1672
1673=head2 Output
1674
1675Controlling where the test output goes.
1676
1677It's ok for your test to change where STDOUT and STDERR point to,
1678Test::Builder's default output settings will not be affected.
1679
1680=over 4
1681
1682=item B<diag>
1683
1684    $Test->diag(@msgs);
1685
1686Prints out the given C<@msgs>.  Like C<print>, arguments are simply
1687appended together.
1688
1689Normally, it uses the C<failure_output()> handle, but if this is for a
1690TODO test, the C<todo_output()> handle is used.
1691
1692Output will be indented and marked with a # so as not to interfere
1693with test output.  A newline will be put on the end if there isn't one
1694already.
1695
1696We encourage using this rather than calling print directly.
1697
1698Returns false.  Why?  Because C<diag()> is often used in conjunction with
1699a failing test (C<ok() || diag()>) it "passes through" the failure.
1700
1701    return ok(...) || diag(...);
1702
1703=for blame transfer
1704Mark Fowler <mark@twoshortplanks.com>
1705
1706=cut
1707
1708sub diag {
1709    my $self = shift;
1710
1711    $self->_print_comment( $self->_diag_fh, @_ );
1712}
1713
1714=item B<note>
1715
1716    $Test->note(@msgs);
1717
1718Like C<diag()>, but it prints to the C<output()> handle so it will not
1719normally be seen by the user except in verbose mode.
1720
1721=cut
1722
1723sub note {
1724    my $self = shift;
1725
1726    $self->_print_comment( $self->output, @_ );
1727}
1728
1729sub _diag_fh {
1730    my $self = shift;
1731
1732    local $Level = $Level + 1;
1733    return $self->in_todo ? $self->todo_output : $self->failure_output;
1734}
1735
1736sub _print_comment {
1737    my( $self, $fh, @msgs ) = @_;
1738
1739    return if $self->no_diag;
1740    return unless @msgs;
1741
1742    # Prevent printing headers when compiling (i.e. -c)
1743    return if $^C;
1744
1745    # Smash args together like print does.
1746    # Convert undef to 'undef' so its readable.
1747    my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1748
1749    # Escape the beginning, _print will take care of the rest.
1750    $msg =~ s/^/# /;
1751
1752    local $Level = $Level + 1;
1753    $self->_print_to_fh( $fh, $msg );
1754
1755    return 0;
1756}
1757
1758=item B<explain>
1759
1760    my @dump = $Test->explain(@msgs);
1761
1762Will dump the contents of any references in a human readable format.
1763Handy for things like...
1764
1765    is_deeply($have, $want) || diag explain $have;
1766
1767or
1768
1769    is_deeply($have, $want) || note explain $have;
1770
1771=cut
1772
1773sub explain {
1774    my $self = shift;
1775
1776    return map {
1777        ref $_
1778          ? do {
1779            $self->_try(sub { require Data::Dumper }, die_on_fail => 1);
1780
1781            my $dumper = Data::Dumper->new( [$_] );
1782            $dumper->Indent(1)->Terse(1);
1783            $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
1784            $dumper->Dump;
1785          }
1786          : $_
1787    } @_;
1788}
1789
1790=begin _private
1791
1792=item B<_print>
1793
1794    $Test->_print(@msgs);
1795
1796Prints to the C<output()> filehandle.
1797
1798=end _private
1799
1800=cut
1801
1802sub _print {
1803    my $self = shift;
1804    return $self->_print_to_fh( $self->output, @_ );
1805}
1806
1807sub _print_to_fh {
1808    my( $self, $fh, @msgs ) = @_;
1809
1810    # Prevent printing headers when only compiling.  Mostly for when
1811    # tests are deparsed with B::Deparse
1812    return if $^C;
1813
1814    my $msg = join '', @msgs;
1815    my $indent = $self->_indent;
1816
1817    local( $\, $", $, ) = ( undef, ' ', '' );
1818
1819    # Escape each line after the first with a # so we don't
1820    # confuse Test::Harness.
1821    $msg =~ s{\n(?!\z)}{\n$indent# }sg;
1822
1823    # Stick a newline on the end if it needs it.
1824    $msg .= "\n" unless $msg =~ /\n\z/;
1825
1826    return print $fh $indent, $msg;
1827}
1828
1829=item B<output>
1830
1831=item B<failure_output>
1832
1833=item B<todo_output>
1834
1835    my $filehandle = $Test->output;
1836    $Test->output($filehandle);
1837    $Test->output($filename);
1838    $Test->output(\$scalar);
1839
1840These methods control where Test::Builder will print its output.
1841They take either an open C<$filehandle>, a C<$filename> to open and write to
1842or a C<$scalar> reference to append to.  It will always return a C<$filehandle>.
1843
1844B<output> is where normal "ok/not ok" test output goes.
1845
1846Defaults to STDOUT.
1847
1848B<failure_output> is where diagnostic output on test failures and
1849C<diag()> goes.  It is normally not read by Test::Harness and instead is
1850displayed to the user.
1851
1852Defaults to STDERR.
1853
1854C<todo_output> is used instead of C<failure_output()> for the
1855diagnostics of a failing TODO test.  These will not be seen by the
1856user.
1857
1858Defaults to STDOUT.
1859
1860=cut
1861
1862sub output {
1863    my( $self, $fh ) = @_;
1864
1865    if( defined $fh ) {
1866        $self->{Out_FH} = $self->_new_fh($fh);
1867    }
1868    return $self->{Out_FH};
1869}
1870
1871sub failure_output {
1872    my( $self, $fh ) = @_;
1873
1874    if( defined $fh ) {
1875        $self->{Fail_FH} = $self->_new_fh($fh);
1876    }
1877    return $self->{Fail_FH};
1878}
1879
1880sub todo_output {
1881    my( $self, $fh ) = @_;
1882
1883    if( defined $fh ) {
1884        $self->{Todo_FH} = $self->_new_fh($fh);
1885    }
1886    return $self->{Todo_FH};
1887}
1888
1889sub _new_fh {
1890    my $self = shift;
1891    my($file_or_fh) = shift;
1892
1893    my $fh;
1894    if( $self->is_fh($file_or_fh) ) {
1895        $fh = $file_or_fh;
1896    }
1897    elsif( ref $file_or_fh eq 'SCALAR' ) {
1898        # Scalar refs as filehandles was added in 5.8.
1899        if( $] >= 5.008 ) {
1900            open $fh, ">>", $file_or_fh
1901              or $self->croak("Can't open scalar ref $file_or_fh: $!");
1902        }
1903        # Emulate scalar ref filehandles with a tie.
1904        else {
1905            $fh = Test::Builder::IO::Scalar->new($file_or_fh)
1906              or $self->croak("Can't tie scalar ref $file_or_fh");
1907        }
1908    }
1909    else {
1910        open $fh, ">", $file_or_fh
1911          or $self->croak("Can't open test output log $file_or_fh: $!");
1912        _autoflush($fh);
1913    }
1914
1915    return $fh;
1916}
1917
1918sub _autoflush {
1919    my($fh) = shift;
1920    my $old_fh = select $fh;
1921    $| = 1;
1922    select $old_fh;
1923
1924    return;
1925}
1926
1927my( $Testout, $Testerr );
1928
1929sub _dup_stdhandles {
1930    my $self = shift;
1931
1932    $self->_open_testhandles;
1933
1934    # Set everything to unbuffered else plain prints to STDOUT will
1935    # come out in the wrong order from our own prints.
1936    _autoflush($Testout);
1937    _autoflush( \*STDOUT );
1938    _autoflush($Testerr);
1939    _autoflush( \*STDERR );
1940
1941    $self->reset_outputs;
1942
1943    return;
1944}
1945
1946sub _open_testhandles {
1947    my $self = shift;
1948
1949    return if $self->{Opened_Testhandles};
1950
1951    # We dup STDOUT and STDERR so people can change them in their
1952    # test suites while still getting normal test output.
1953    open( $Testout, ">&STDOUT" ) or die "Can't dup STDOUT:  $!";
1954    open( $Testerr, ">&STDERR" ) or die "Can't dup STDERR:  $!";
1955
1956    $self->_copy_io_layers( \*STDOUT, $Testout );
1957    $self->_copy_io_layers( \*STDERR, $Testerr );
1958
1959    $self->{Opened_Testhandles} = 1;
1960
1961    return;
1962}
1963
1964sub _copy_io_layers {
1965    my( $self, $src, $dst ) = @_;
1966
1967    $self->_try(
1968        sub {
1969            require PerlIO;
1970            my @src_layers = PerlIO::get_layers($src);
1971
1972            _apply_layers($dst, @src_layers) if @src_layers;
1973        }
1974    );
1975
1976    return;
1977}
1978
1979sub _apply_layers {
1980    my ($fh, @layers) = @_;
1981    my %seen;
1982    my @unique = grep { $_ ne 'unix' and !$seen{$_}++ } @layers;
1983    binmode($fh, join(":", "", "raw", @unique));
1984}
1985
1986
1987=item reset_outputs
1988
1989  $tb->reset_outputs;
1990
1991Resets all the output filehandles back to their defaults.
1992
1993=cut
1994
1995sub reset_outputs {
1996    my $self = shift;
1997
1998    $self->output        ($Testout);
1999    $self->failure_output($Testerr);
2000    $self->todo_output   ($Testout);
2001
2002    return;
2003}
2004
2005=item carp
2006
2007  $tb->carp(@message);
2008
2009Warns with C<@message> but the message will appear to come from the
2010point where the original test function was called (C<< $tb->caller >>).
2011
2012=item croak
2013
2014  $tb->croak(@message);
2015
2016Dies with C<@message> but the message will appear to come from the
2017point where the original test function was called (C<< $tb->caller >>).
2018
2019=cut
2020
2021sub _message_at_caller {
2022    my $self = shift;
2023
2024    local $Level = $Level + 1;
2025    my( $pack, $file, $line ) = $self->caller;
2026    return join( "", @_ ) . " at $file line $line.\n";
2027}
2028
2029sub carp {
2030    my $self = shift;
2031    return warn $self->_message_at_caller(@_);
2032}
2033
2034sub croak {
2035    my $self = shift;
2036    return die $self->_message_at_caller(@_);
2037}
2038
2039
2040=back
2041
2042
2043=head2 Test Status and Info
2044
2045=over 4
2046
2047=item B<current_test>
2048
2049    my $curr_test = $Test->current_test;
2050    $Test->current_test($num);
2051
2052Gets/sets the current test number we're on.  You usually shouldn't
2053have to set this.
2054
2055If set forward, the details of the missing tests are filled in as 'unknown'.
2056if set backward, the details of the intervening tests are deleted.  You
2057can erase history if you really want to.
2058
2059=cut
2060
2061sub current_test {
2062    my( $self, $num ) = @_;
2063
2064    lock( $self->{Curr_Test} );
2065    if( defined $num ) {
2066        $self->{Curr_Test} = $num;
2067
2068        # If the test counter is being pushed forward fill in the details.
2069        my $test_results = $self->{Test_Results};
2070        if( $num > @$test_results ) {
2071            my $start = @$test_results ? @$test_results : 0;
2072            for( $start .. $num - 1 ) {
2073                $test_results->[$_] = &share(
2074                    {
2075                        'ok'      => 1,
2076                        actual_ok => undef,
2077                        reason    => 'incrementing test number',
2078                        type      => 'unknown',
2079                        name      => undef
2080                    }
2081                );
2082            }
2083        }
2084        # If backward, wipe history.  Its their funeral.
2085        elsif( $num < @$test_results ) {
2086            $#{$test_results} = $num - 1;
2087        }
2088    }
2089    return $self->{Curr_Test};
2090}
2091
2092=item B<is_passing>
2093
2094   my $ok = $builder->is_passing;
2095
2096Indicates if the test suite is currently passing.
2097
2098More formally, it will be false if anything has happened which makes
2099it impossible for the test suite to pass.  True otherwise.
2100
2101For example, if no tests have run C<is_passing()> will be true because
2102even though a suite with no tests is a failure you can add a passing
2103test to it and start passing.
2104
2105Don't think about it too much.
2106
2107=cut
2108
2109sub is_passing {
2110    my $self = shift;
2111
2112    if( @_ ) {
2113        $self->{Is_Passing} = shift;
2114    }
2115
2116    return $self->{Is_Passing};
2117}
2118
2119
2120=item B<summary>
2121
2122    my @tests = $Test->summary;
2123
2124A simple summary of the tests so far.  True for pass, false for fail.
2125This is a logical pass/fail, so todos are passes.
2126
2127Of course, test #1 is $tests[0], etc...
2128
2129=cut
2130
2131sub summary {
2132    my($self) = shift;
2133
2134    return map { $_->{'ok'} } @{ $self->{Test_Results} };
2135}
2136
2137=item B<details>
2138
2139    my @tests = $Test->details;
2140
2141Like C<summary()>, but with a lot more detail.
2142
2143    $tests[$test_num - 1] =
2144            { 'ok'       => is the test considered a pass?
2145              actual_ok  => did it literally say 'ok'?
2146              name       => name of the test (if any)
2147              type       => type of test (if any, see below).
2148              reason     => reason for the above (if any)
2149            };
2150
2151'ok' is true if Test::Harness will consider the test to be a pass.
2152
2153'actual_ok' is a reflection of whether or not the test literally
2154printed 'ok' or 'not ok'.  This is for examining the result of 'todo'
2155tests.
2156
2157'name' is the name of the test.
2158
2159'type' indicates if it was a special test.  Normal tests have a type
2160of ''.  Type can be one of the following:
2161
2162    skip        see skip()
2163    todo        see todo()
2164    todo_skip   see todo_skip()
2165    unknown     see below
2166
2167Sometimes the Test::Builder test counter is incremented without it
2168printing any test output, for example, when C<current_test()> is changed.
2169In these cases, Test::Builder doesn't know the result of the test, so
2170its type is 'unknown'.  These details for these tests are filled in.
2171They are considered ok, but the name and actual_ok is left C<undef>.
2172
2173For example "not ok 23 - hole count # TODO insufficient donuts" would
2174result in this structure:
2175
2176    $tests[22] =    # 23 - 1, since arrays start from 0.
2177      { ok        => 1,   # logically, the test passed since its todo
2178        actual_ok => 0,   # in absolute terms, it failed
2179        name      => 'hole count',
2180        type      => 'todo',
2181        reason    => 'insufficient donuts'
2182      };
2183
2184=cut
2185
2186sub details {
2187    my $self = shift;
2188    return @{ $self->{Test_Results} };
2189}
2190
2191=item B<todo>
2192
2193    my $todo_reason = $Test->todo;
2194    my $todo_reason = $Test->todo($pack);
2195
2196If the current tests are considered "TODO" it will return the reason,
2197if any.  This reason can come from a C<$TODO> variable or the last call
2198to C<todo_start()>.
2199
2200Since a TODO test does not need a reason, this function can return an
2201empty string even when inside a TODO block.  Use C<< $Test->in_todo >>
2202to determine if you are currently inside a TODO block.
2203
2204C<todo()> is about finding the right package to look for C<$TODO> in.  It's
2205pretty good at guessing the right package to look at.  It first looks for
2206the caller based on C<$Level + 1>, since C<todo()> is usually called inside
2207a test function.  As a last resort it will use C<exported_to()>.
2208
2209Sometimes there is some confusion about where C<todo()> should be looking
2210for the C<$TODO> variable.  If you want to be sure, tell it explicitly
2211what $pack to use.
2212
2213=cut
2214
2215sub todo {
2216    my( $self, $pack ) = @_;
2217
2218    return $self->{Todo} if defined $self->{Todo};
2219
2220    local $Level = $Level + 1;
2221    my $todo = $self->find_TODO($pack);
2222    return $todo if defined $todo;
2223
2224    return '';
2225}
2226
2227=item B<find_TODO>
2228
2229    my $todo_reason = $Test->find_TODO();
2230    my $todo_reason = $Test->find_TODO($pack);
2231
2232Like C<todo()> but only returns the value of C<$TODO> ignoring
2233C<todo_start()>.
2234
2235Can also be used to set C<$TODO> to a new value while returning the
2236old value:
2237
2238    my $old_reason = $Test->find_TODO($pack, 1, $new_reason);
2239
2240=cut
2241
2242sub find_TODO {
2243    my( $self, $pack, $set, $new_value ) = @_;
2244
2245    $pack = $pack || $self->caller(1) || $self->exported_to;
2246    return unless $pack;
2247
2248    no strict 'refs';    ## no critic
2249    my $old_value = ${ $pack . '::TODO' };
2250    $set and ${ $pack . '::TODO' } = $new_value;
2251    return $old_value;
2252}
2253
2254=item B<in_todo>
2255
2256    my $in_todo = $Test->in_todo;
2257
2258Returns true if the test is currently inside a TODO block.
2259
2260=cut
2261
2262sub in_todo {
2263    my $self = shift;
2264
2265    local $Level = $Level + 1;
2266    return( defined $self->{Todo} || $self->find_TODO ) ? 1 : 0;
2267}
2268
2269=item B<todo_start>
2270
2271    $Test->todo_start();
2272    $Test->todo_start($message);
2273
2274This method allows you declare all subsequent tests as TODO tests, up until
2275the C<todo_end> method has been called.
2276
2277The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out
2278whether or not we're in a TODO test.  However, often we find that this is not
2279possible to determine (such as when we want to use C<$TODO> but
2280the tests are being executed in other packages which can't be inferred
2281beforehand).
2282
2283Note that you can use this to nest "todo" tests
2284
2285 $Test->todo_start('working on this');
2286 # lots of code
2287 $Test->todo_start('working on that');
2288 # more code
2289 $Test->todo_end;
2290 $Test->todo_end;
2291
2292This is generally not recommended, but large testing systems often have weird
2293internal needs.
2294
2295We've tried to make this also work with the TODO: syntax, but it's not
2296guaranteed and its use is also discouraged:
2297
2298 TODO: {
2299     local $TODO = 'We have work to do!';
2300     $Test->todo_start('working on this');
2301     # lots of code
2302     $Test->todo_start('working on that');
2303     # more code
2304     $Test->todo_end;
2305     $Test->todo_end;
2306 }
2307
2308Pick one style or another of "TODO" to be on the safe side.
2309
2310=cut
2311
2312sub todo_start {
2313    my $self = shift;
2314    my $message = @_ ? shift : '';
2315
2316    $self->{Start_Todo}++;
2317    if( $self->in_todo ) {
2318        push @{ $self->{Todo_Stack} } => $self->todo;
2319    }
2320    $self->{Todo} = $message;
2321
2322    return;
2323}
2324
2325=item C<todo_end>
2326
2327 $Test->todo_end;
2328
2329Stops running tests as "TODO" tests.  This method is fatal if called without a
2330preceding C<todo_start> method call.
2331
2332=cut
2333
2334sub todo_end {
2335    my $self = shift;
2336
2337    if( !$self->{Start_Todo} ) {
2338        $self->croak('todo_end() called without todo_start()');
2339    }
2340
2341    $self->{Start_Todo}--;
2342
2343    if( $self->{Start_Todo} && @{ $self->{Todo_Stack} } ) {
2344        $self->{Todo} = pop @{ $self->{Todo_Stack} };
2345    }
2346    else {
2347        delete $self->{Todo};
2348    }
2349
2350    return;
2351}
2352
2353=item B<caller>
2354
2355    my $package = $Test->caller;
2356    my($pack, $file, $line) = $Test->caller;
2357    my($pack, $file, $line) = $Test->caller($height);
2358
2359Like the normal C<caller()>, except it reports according to your C<level()>.
2360
2361C<$height> will be added to the C<level()>.
2362
2363If C<caller()> winds up off the top of the stack it report the highest context.
2364
2365=cut
2366
2367sub caller {    ## no critic (Subroutines::ProhibitBuiltinHomonyms)
2368    my( $self, $height ) = @_;
2369    $height ||= 0;
2370
2371    my $level = $self->level + $height + 1;
2372    my @caller;
2373    do {
2374        @caller = CORE::caller( $level );
2375        $level--;
2376    } until @caller;
2377    return wantarray ? @caller : $caller[0];
2378}
2379
2380=back
2381
2382=cut
2383
2384=begin _private
2385
2386=over 4
2387
2388=item B<_sanity_check>
2389
2390  $self->_sanity_check();
2391
2392Runs a bunch of end of test sanity checks to make sure reality came
2393through ok.  If anything is wrong it will die with a fairly friendly
2394error message.
2395
2396=cut
2397
2398#'#
2399sub _sanity_check {
2400    my $self = shift;
2401
2402    $self->_whoa( $self->{Curr_Test} < 0, 'Says here you ran a negative number of tests!' );
2403    $self->_whoa( $self->{Curr_Test} != @{ $self->{Test_Results} },
2404        'Somehow you got a different number of results than tests ran!' );
2405
2406    return;
2407}
2408
2409=item B<_whoa>
2410
2411  $self->_whoa($check, $description);
2412
2413A sanity check, similar to C<assert()>.  If the C<$check> is true, something
2414has gone horribly wrong.  It will die with the given C<$description> and
2415a note to contact the author.
2416
2417=cut
2418
2419sub _whoa {
2420    my( $self, $check, $desc ) = @_;
2421    if($check) {
2422        local $Level = $Level + 1;
2423        $self->croak(<<"WHOA");
2424WHOA!  $desc
2425This should never happen!  Please contact the author immediately!
2426WHOA
2427    }
2428
2429    return;
2430}
2431
2432=item B<_my_exit>
2433
2434  _my_exit($exit_num);
2435
2436Perl seems to have some trouble with exiting inside an C<END> block.
24375.6.1 does some odd things.  Instead, this function edits C<$?>
2438directly.  It should B<only> be called from inside an C<END> block.
2439It doesn't actually exit, that's your job.
2440
2441=cut
2442
2443sub _my_exit {
2444    $? = $_[0];    ## no critic (Variables::RequireLocalizedPunctuationVars)
2445
2446    return 1;
2447}
2448
2449=back
2450
2451=end _private
2452
2453=cut
2454
2455sub _ending {
2456    my $self = shift;
2457    return if $self->no_ending;
2458    return if $self->{Ending}++;
2459
2460    my $real_exit_code = $?;
2461
2462    # Don't bother with an ending if this is a forked copy.  Only the parent
2463    # should do the ending.
2464    if( $self->{Original_Pid} != $$ ) {
2465        return;
2466    }
2467
2468    # Ran tests but never declared a plan or hit done_testing
2469    if( !$self->{Have_Plan} and $self->{Curr_Test} ) {
2470        $self->is_passing(0);
2471        $self->diag("Tests were run but no plan was declared and done_testing() was not seen.");
2472
2473        if($real_exit_code) {
2474            $self->diag(<<"FAIL");
2475Looks like your test exited with $real_exit_code just after $self->{Curr_Test}.
2476FAIL
2477            $self->is_passing(0);
2478            _my_exit($real_exit_code) && return;
2479        }
2480
2481        # But if the tests ran, handle exit code.
2482        my $test_results = $self->{Test_Results};
2483        if(@$test_results) {
2484            my $num_failed = grep !$_->{'ok'}, @{$test_results}[ 0 .. $self->{Curr_Test} - 1 ];
2485            if ($num_failed > 0) {
2486
2487                my $exit_code = $num_failed <= 254 ? $num_failed : 254;
2488                _my_exit($exit_code) && return;
2489            }
2490        }
2491        _my_exit(254) && return;
2492    }
2493
2494    # Exit if plan() was never called.  This is so "require Test::Simple"
2495    # doesn't puke.
2496    if( !$self->{Have_Plan} ) {
2497        return;
2498    }
2499
2500    # Don't do an ending if we bailed out.
2501    if( $self->{Bailed_Out} ) {
2502        $self->is_passing(0);
2503        return;
2504    }
2505    # Figure out if we passed or failed and print helpful messages.
2506    my $test_results = $self->{Test_Results};
2507    if(@$test_results) {
2508        # The plan?  We have no plan.
2509        if( $self->{No_Plan} ) {
2510            $self->_output_plan($self->{Curr_Test}) unless $self->no_header;
2511            $self->{Expected_Tests} = $self->{Curr_Test};
2512        }
2513
2514        # Auto-extended arrays and elements which aren't explicitly
2515        # filled in with a shared reference will puke under 5.8.0
2516        # ithreads.  So we have to fill them in by hand. :(
2517        my $empty_result = &share( {} );
2518        for my $idx ( 0 .. $self->{Expected_Tests} - 1 ) {
2519            $test_results->[$idx] = $empty_result
2520              unless defined $test_results->[$idx];
2521        }
2522
2523        my $num_failed = grep !$_->{'ok'}, @{$test_results}[ 0 .. $self->{Curr_Test} - 1 ];
2524
2525        my $num_extra = $self->{Curr_Test} - $self->{Expected_Tests};
2526
2527        if( $num_extra != 0 ) {
2528            my $s = $self->{Expected_Tests} == 1 ? '' : 's';
2529            $self->diag(<<"FAIL");
2530Looks like you planned $self->{Expected_Tests} test$s but ran $self->{Curr_Test}.
2531FAIL
2532            $self->is_passing(0);
2533        }
2534
2535        if($num_failed) {
2536            my $num_tests = $self->{Curr_Test};
2537            my $s = $num_failed == 1 ? '' : 's';
2538
2539            my $qualifier = $num_extra == 0 ? '' : ' run';
2540
2541            $self->diag(<<"FAIL");
2542Looks like you failed $num_failed test$s of $num_tests$qualifier.
2543FAIL
2544            $self->is_passing(0);
2545        }
2546
2547        if($real_exit_code) {
2548            $self->diag(<<"FAIL");
2549Looks like your test exited with $real_exit_code just after $self->{Curr_Test}.
2550FAIL
2551            $self->is_passing(0);
2552            _my_exit($real_exit_code) && return;
2553        }
2554
2555        my $exit_code;
2556        if($num_failed) {
2557            $exit_code = $num_failed <= 254 ? $num_failed : 254;
2558        }
2559        elsif( $num_extra != 0 ) {
2560            $exit_code = 255;
2561        }
2562        else {
2563            $exit_code = 0;
2564        }
2565
2566        _my_exit($exit_code) && return;
2567    }
2568    elsif( $self->{Skip_All} ) {
2569        _my_exit(0) && return;
2570    }
2571    elsif($real_exit_code) {
2572        $self->diag(<<"FAIL");
2573Looks like your test exited with $real_exit_code before it could output anything.
2574FAIL
2575        $self->is_passing(0);
2576        _my_exit($real_exit_code) && return;
2577    }
2578    else {
2579        $self->diag("No tests run!\n");
2580        $self->is_passing(0);
2581        _my_exit(255) && return;
2582    }
2583
2584    $self->is_passing(0);
2585    $self->_whoa( 1, "We fell off the end of _ending()" );
2586}
2587
2588END {
2589    $Test->_ending if defined $Test;
2590}
2591
2592=head1 EXIT CODES
2593
2594If all your tests passed, Test::Builder will exit with zero (which is
2595normal).  If anything failed it will exit with how many failed.  If
2596you run less (or more) tests than you planned, the missing (or extras)
2597will be considered failures.  If no tests were ever run Test::Builder
2598will throw a warning and exit with 255.  If the test died, even after
2599having successfully completed all its tests, it will still be
2600considered a failure and will exit with 255.
2601
2602So the exit codes are...
2603
2604    0                   all tests successful
2605    255                 test died or all passed but wrong # of tests run
2606    any other number    how many failed (including missing or extras)
2607
2608If you fail more than 254 tests, it will be reported as 254.
2609
2610=head1 THREADS
2611
2612In perl 5.8.1 and later, Test::Builder is thread-safe.  The test
2613number is shared amongst all threads.  This means if one thread sets
2614the test number using C<current_test()> they will all be effected.
2615
2616While versions earlier than 5.8.1 had threads they contain too many
2617bugs to support.
2618
2619Test::Builder is only thread-aware if threads.pm is loaded I<before>
2620Test::Builder.
2621
2622=head1 MEMORY
2623
2624An informative hash, accessible via C<details()>, is stored for each
2625test you perform.  So memory usage will scale linearly with each test
2626run. Although this is not a problem for most test suites, it can
2627become an issue if you do large (hundred thousands to million)
2628combinatorics tests in the same run.
2629
2630In such cases, you are advised to either split the test file into smaller
2631ones, or use a reverse approach, doing "normal" (code) compares and
2632triggering C<fail()> should anything go unexpected.
2633
2634Future versions of Test::Builder will have a way to turn history off.
2635
2636
2637=head1 EXAMPLES
2638
2639CPAN can provide the best examples.  L<Test::Simple>, L<Test::More>,
2640L<Test::Exception> and L<Test::Differences> all use Test::Builder.
2641
2642=head1 SEE ALSO
2643
2644L<Test::Simple>, L<Test::More>, L<Test::Harness>
2645
2646=head1 AUTHORS
2647
2648Original code by chromatic, maintained by Michael G Schwern
2649E<lt>schwern@pobox.comE<gt>
2650
2651=head1 MAINTAINERS
2652
2653=over 4
2654
2655=item Chad Granum E<lt>exodist@cpan.orgE<gt>
2656
2657=back
2658
2659=head1 COPYRIGHT
2660
2661Copyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and
2662                       Michael G Schwern E<lt>schwern@pobox.comE<gt>.
2663
2664This program is free software; you can redistribute it and/or
2665modify it under the same terms as Perl itself.
2666
2667See F<http://www.perl.com/perl/misc/Artistic.html>
2668
2669=cut
2670
26711;
2672
2673