Debian lenny version packages
[pkg-perl] / deb-src / libtest-harness-perl / libtest-harness-perl-3.12 / t / lib / Test / Builder.pm
1 package Test::Builder;
2
3 use 5.004;
4
5 # $^C was only introduced in 5.005-ish.  We do this to prevent
6 # use of uninitialized value warnings in older perls.
7 $^C ||= 0;
8
9 use strict;
10 use vars qw($VERSION);
11 $VERSION = '0.72';
12 $VERSION = eval $VERSION;    # make the alpha version come out as a number
13
14 # Make Test::Builder thread-safe for ithreads.
15 BEGIN {
16     use Config;
17
18     # Load threads::shared when threads are turned on.
19     # 5.8.0's threads are so busted we no longer support them.
20     if ( $] >= 5.008001 && $Config{useithreads} && $INC{'threads.pm'} ) {
21         require threads::shared;
22
23         # Hack around YET ANOTHER threads::shared bug.  It would
24         # occassionally forget the contents of the variable when sharing it.
25         # So we first copy the data, then share, then put our copy back.
26         *share = sub (\[$@%]) {
27             my $type = ref $_[0];
28             my $data;
29
30             if ( $type eq 'HASH' ) {
31                 %$data = %{ $_[0] };
32             }
33             elsif ( $type eq 'ARRAY' ) {
34                 @$data = @{ $_[0] };
35             }
36             elsif ( $type eq 'SCALAR' ) {
37                 $$data = ${ $_[0] };
38             }
39             else {
40                 die( "Unknown type: " . $type );
41             }
42
43             $_[0] = &threads::shared::share( $_[0] );
44
45             if ( $type eq 'HASH' ) {
46                 %{ $_[0] } = %$data;
47             }
48             elsif ( $type eq 'ARRAY' ) {
49                 @{ $_[0] } = @$data;
50             }
51             elsif ( $type eq 'SCALAR' ) {
52                 ${ $_[0] } = $$data;
53             }
54             else {
55                 die( "Unknown type: " . $type );
56             }
57
58             return $_[0];
59         };
60     }
61
62     # 5.8.0's threads::shared is busted when threads are off
63     # and earlier Perls just don't have that module at all.
64     else {
65         *share = sub { return $_[0] };
66         *lock  = sub {0};
67     }
68 }
69
70 =head1 NAME
71
72 Test::Builder - Backend for building test libraries
73
74 =head1 SYNOPSIS
75
76   package My::Test::Module;
77   use Test::Builder;
78   require Exporter;
79   @ISA = qw(Exporter);
80   @EXPORT = qw(ok);
81
82   my $Test = Test::Builder->new;
83   $Test->output('my_logfile');
84
85   sub import {
86       my($self) = shift;
87       my $pack = caller;
88
89       $Test->exported_to($pack);
90       $Test->plan(@_);
91
92       $self->export_to_level(1, $self, 'ok');
93   }
94
95   sub ok {
96       my($test, $name) = @_;
97
98       $Test->ok($test, $name);
99   }
100
101
102 =head1 DESCRIPTION
103
104 Test::Simple and Test::More have proven to be popular testing modules,
105 but they're not always flexible enough.  Test::Builder provides the a
106 building block upon which to write your own test libraries I<which can
107 work together>.
108
109 =head2 Construction
110
111 =over 4
112
113 =item B<new>
114
115   my $Test = Test::Builder->new;
116
117 Returns a Test::Builder object representing the current state of the
118 test.
119
120 Since you only run one test per program C<new> always returns the same
121 Test::Builder object.  No matter how many times you call new(), you're
122 getting the same object.  This is called a singleton.  This is done so that
123 multiple modules share such global information as the test counter and
124 where test output is going.
125
126 If you want a completely new Test::Builder object different from the
127 singleton, use C<create>.
128
129 =cut
130
131 my $Test = Test::Builder->new;
132
133 sub new {
134     my ($class) = shift;
135     $Test ||= $class->create;
136     return $Test;
137 }
138
139 =item B<create>
140
141   my $Test = Test::Builder->create;
142
143 Ok, so there can be more than one Test::Builder object and this is how
144 you get it.  You might use this instead of C<new()> if you're testing
145 a Test::Builder based module, but otherwise you probably want C<new>.
146
147 B<NOTE>: the implementation is not complete.  C<level>, for example, is
148 still shared amongst B<all> Test::Builder objects, even ones created using
149 this method.  Also, the method name may change in the future.
150
151 =cut
152
153 sub create {
154     my $class = shift;
155
156     my $self = bless {}, $class;
157     $self->reset;
158
159     return $self;
160 }
161
162 =item B<reset>
163
164   $Test->reset;
165
166 Reinitializes the Test::Builder singleton to its original state.
167 Mostly useful for tests run in persistent environments where the same
168 test might be run multiple times in the same process.
169
170 =cut
171
172 use vars qw($Level);
173
174 sub reset {
175     my ($self) = @_;
176
177     # We leave this a global because it has to be localized and localizing
178     # hash keys is just asking for pain.  Also, it was documented.
179     $Level = 1;
180
181     $self->{Test_Died}    = 0;
182     $self->{Have_Plan}    = 0;
183     $self->{No_Plan}      = 0;
184     $self->{Original_Pid} = $$;
185
186     share( $self->{Curr_Test} );
187     $self->{Curr_Test} = 0;
188     $self->{Test_Results} = &share( [] );
189
190     $self->{Exported_To}    = undef;
191     $self->{Expected_Tests} = 0;
192
193     $self->{Skip_All} = 0;
194
195     $self->{Use_Nums} = 1;
196
197     $self->{No_Header} = 0;
198     $self->{No_Ending} = 0;
199
200     $self->_dup_stdhandles unless $^C;
201
202     return undef;
203 }
204
205 =back
206
207 =head2 Setting up tests
208
209 These methods are for setting up tests and declaring how many there
210 are.  You usually only want to call one of these methods.
211
212 =over 4
213
214 =item B<exported_to>
215
216   my $pack = $Test->exported_to;
217   $Test->exported_to($pack);
218
219 Tells Test::Builder what package you exported your functions to.
220 This is important for getting TODO tests right.
221
222 =cut
223
224 sub exported_to {
225     my ( $self, $pack ) = @_;
226
227     if ( defined $pack ) {
228         $self->{Exported_To} = $pack;
229     }
230     return $self->{Exported_To};
231 }
232
233 =item B<plan>
234
235   $Test->plan('no_plan');
236   $Test->plan( skip_all => $reason );
237   $Test->plan( tests => $num_tests );
238
239 A convenient way to set up your tests.  Call this and Test::Builder
240 will print the appropriate headers and take the appropriate actions.
241
242 If you call plan(), don't call any of the other methods below.
243
244 =cut
245
246 sub plan {
247     my ( $self, $cmd, $arg ) = @_;
248
249     return unless $cmd;
250
251     local $Level = $Level + 1;
252
253     if ( $self->{Have_Plan} ) {
254         $self->croak("You tried to plan twice");
255     }
256
257     if ( $cmd eq 'no_plan' ) {
258         $self->no_plan;
259     }
260     elsif ( $cmd eq 'skip_all' ) {
261         return $self->skip_all($arg);
262     }
263     elsif ( $cmd eq 'tests' ) {
264         if ($arg) {
265             local $Level = $Level + 1;
266             return $self->expected_tests($arg);
267         }
268         elsif ( !defined $arg ) {
269             $self->croak("Got an undefined number of tests");
270         }
271         elsif ( !$arg ) {
272             $self->croak("You said to run 0 tests");
273         }
274     }
275     else {
276         my @args = grep {defined} ( $cmd, $arg );
277         $self->croak("plan() doesn't understand @args");
278     }
279
280     return 1;
281 }
282
283 =item B<expected_tests>
284
285     my $max = $Test->expected_tests;
286     $Test->expected_tests($max);
287
288 Gets/sets the # of tests we expect this test to run and prints out
289 the appropriate headers.
290
291 =cut
292
293 sub expected_tests {
294     my $self = shift;
295     my ($max) = @_;
296
297     if (@_) {
298         $self->croak(
299             "Number of tests must be a positive integer.  You gave it '$max'")
300           unless $max =~ /^\+?\d+$/ and $max > 0;
301
302         $self->{Expected_Tests} = $max;
303         $self->{Have_Plan}      = 1;
304
305         $self->_print("1..$max\n") unless $self->no_header;
306     }
307     return $self->{Expected_Tests};
308 }
309
310 =item B<no_plan>
311
312   $Test->no_plan;
313
314 Declares that this test will run an indeterminate # of tests.
315
316 =cut
317
318 sub no_plan {
319     my $self = shift;
320
321     $self->{No_Plan}   = 1;
322     $self->{Have_Plan} = 1;
323 }
324
325 =item B<has_plan>
326
327   $plan = $Test->has_plan
328
329 Find out whether a plan has been defined. $plan is either C<undef> (no plan has been set), C<no_plan> (indeterminate # of tests) or an integer (the number of expected tests).
330
331 =cut
332
333 sub has_plan {
334     my $self = shift;
335
336     return ( $self->{Expected_Tests} ) if $self->{Expected_Tests};
337     return ('no_plan') if $self->{No_Plan};
338     return (undef);
339 }
340
341 =item B<skip_all>
342
343   $Test->skip_all;
344   $Test->skip_all($reason);
345
346 Skips all the tests, using the given $reason.  Exits immediately with 0.
347
348 =cut
349
350 sub skip_all {
351     my ( $self, $reason ) = @_;
352
353     my $out = "1..0";
354     $out .= " # Skip $reason" if $reason;
355     $out .= "\n";
356
357     $self->{Skip_All} = 1;
358
359     $self->_print($out) unless $self->no_header;
360     exit(0);
361 }
362
363 =back
364
365 =head2 Running tests
366
367 These actually run the tests, analogous to the functions in Test::More.
368
369 They all return true if the test passed, false if the test failed.
370
371 $name is always optional.
372
373 =over 4
374
375 =item B<ok>
376
377   $Test->ok($test, $name);
378
379 Your basic test.  Pass if $test is true, fail if $test is false.  Just
380 like Test::Simple's ok().
381
382 =cut
383
384 sub ok {
385     my ( $self, $test, $name ) = @_;
386
387     # $test might contain an object which we don't want to accidentally
388     # store, so we turn it into a boolean.
389     $test = $test ? 1 : 0;
390
391     $self->_plan_check;
392
393     lock $self->{Curr_Test};
394     $self->{Curr_Test}++;
395
396     # In case $name is a string overloaded object, force it to stringify.
397     $self->_unoverload_str( \$name );
398
399     $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
400     You named your test '$name'.  You shouldn't use numbers for your test names.
401     Very confusing.
402 ERR
403
404     my ( $pack, $file, $line ) = $self->caller;
405
406     my $todo = $self->todo($pack);
407     $self->_unoverload_str( \$todo );
408
409     my $out;
410     my $result = &share( {} );
411
412     unless ($test) {
413         $out .= "not ";
414         @$result{ 'ok', 'actual_ok' } = ( ( $todo ? 1 : 0 ), 0 );
415     }
416     else {
417         @$result{ 'ok', 'actual_ok' } = ( 1, $test );
418     }
419
420     $out .= "ok";
421     $out .= " $self->{Curr_Test}" if $self->use_numbers;
422
423     if ( defined $name ) {
424         $name =~ s|#|\\#|g;    # # in a name can confuse Test::Harness.
425         $out .= " - $name";
426         $result->{name} = $name;
427     }
428     else {
429         $result->{name} = '';
430     }
431
432     if ($todo) {
433         $out .= " # TODO $todo";
434         $result->{reason} = $todo;
435         $result->{type}   = 'todo';
436     }
437     else {
438         $result->{reason} = '';
439         $result->{type}   = '';
440     }
441
442     $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = $result;
443     $out .= "\n";
444
445     $self->_print($out);
446
447     unless ($test) {
448         my $msg = $todo ? "Failed (TODO)" : "Failed";
449         $self->_print_diag("\n") if $ENV{HARNESS_ACTIVE};
450
451         if ( defined $name ) {
452             $self->diag(qq[  $msg test '$name'\n]);
453             $self->diag(qq[  at $file line $line.\n]);
454         }
455         else {
456             $self->diag(qq[  $msg test at $file line $line.\n]);
457         }
458     }
459
460     return $test ? 1 : 0;
461 }
462
463 sub _unoverload {
464     my $self = shift;
465     my $type = shift;
466
467     $self->_try( sub { require overload } ) || return;
468
469     foreach my $thing (@_) {
470         if ( $self->_is_object($$thing) ) {
471             if ( my $string_meth = overload::Method( $$thing, $type ) ) {
472                 $$thing = $$thing->$string_meth();
473             }
474         }
475     }
476 }
477
478 sub _is_object {
479     my ( $self, $thing ) = @_;
480
481     return $self->_try( sub { ref $thing && $thing->isa('UNIVERSAL') } )
482       ? 1
483       : 0;
484 }
485
486 sub _unoverload_str {
487     my $self = shift;
488
489     $self->_unoverload( q[""], @_ );
490 }
491
492 sub _unoverload_num {
493     my $self = shift;
494
495     $self->_unoverload( '0+', @_ );
496
497     for my $val (@_) {
498         next unless $self->_is_dualvar($$val);
499         $$val = $$val + 0;
500     }
501 }
502
503 # This is a hack to detect a dualvar such as $!
504 sub _is_dualvar {
505     my ( $self, $val ) = @_;
506
507     local $^W = 0;
508     my $numval = $val + 0;
509     return 1 if $numval != 0 and $numval ne $val;
510 }
511
512 =item B<is_eq>
513
514   $Test->is_eq($got, $expected, $name);
515
516 Like Test::More's is().  Checks if $got eq $expected.  This is the
517 string version.
518
519 =item B<is_num>
520
521   $Test->is_num($got, $expected, $name);
522
523 Like Test::More's is().  Checks if $got == $expected.  This is the
524 numeric version.
525
526 =cut
527
528 sub is_eq {
529     my ( $self, $got, $expect, $name ) = @_;
530     local $Level = $Level + 1;
531
532     $self->_unoverload_str( \$got, \$expect );
533
534     if ( !defined $got || !defined $expect ) {
535
536         # undef only matches undef and nothing else
537         my $test = !defined $got && !defined $expect;
538
539         $self->ok( $test, $name );
540         $self->_is_diag( $got, 'eq', $expect ) unless $test;
541         return $test;
542     }
543
544     return $self->cmp_ok( $got, 'eq', $expect, $name );
545 }
546
547 sub is_num {
548     my ( $self, $got, $expect, $name ) = @_;
549     local $Level = $Level + 1;
550
551     $self->_unoverload_num( \$got, \$expect );
552
553     if ( !defined $got || !defined $expect ) {
554
555         # undef only matches undef and nothing else
556         my $test = !defined $got && !defined $expect;
557
558         $self->ok( $test, $name );
559         $self->_is_diag( $got, '==', $expect ) unless $test;
560         return $test;
561     }
562
563     return $self->cmp_ok( $got, '==', $expect, $name );
564 }
565
566 sub _is_diag {
567     my ( $self, $got, $type, $expect ) = @_;
568
569     foreach my $val ( \$got, \$expect ) {
570         if ( defined $$val ) {
571             if ( $type eq 'eq' ) {
572
573                 # quote and force string context
574                 $$val = "'$$val'";
575             }
576             else {
577
578                 # force numeric context
579                 $self->_unoverload_num($val);
580             }
581         }
582         else {
583             $$val = 'undef';
584         }
585     }
586
587     return $self->diag( sprintf <<DIAGNOSTIC, $got, $expect );
588          got: %s
589     expected: %s
590 DIAGNOSTIC
591
592 }
593
594 =item B<isnt_eq>
595
596   $Test->isnt_eq($got, $dont_expect, $name);
597
598 Like Test::More's isnt().  Checks if $got ne $dont_expect.  This is
599 the string version.
600
601 =item B<isnt_num>
602
603   $Test->isnt_num($got, $dont_expect, $name);
604
605 Like Test::More's isnt().  Checks if $got ne $dont_expect.  This is
606 the numeric version.
607
608 =cut
609
610 sub isnt_eq {
611     my ( $self, $got, $dont_expect, $name ) = @_;
612     local $Level = $Level + 1;
613
614     if ( !defined $got || !defined $dont_expect ) {
615
616         # undef only matches undef and nothing else
617         my $test = defined $got || defined $dont_expect;
618
619         $self->ok( $test, $name );
620         $self->_cmp_diag( $got, 'ne', $dont_expect ) unless $test;
621         return $test;
622     }
623
624     return $self->cmp_ok( $got, 'ne', $dont_expect, $name );
625 }
626
627 sub isnt_num {
628     my ( $self, $got, $dont_expect, $name ) = @_;
629     local $Level = $Level + 1;
630
631     if ( !defined $got || !defined $dont_expect ) {
632
633         # undef only matches undef and nothing else
634         my $test = defined $got || defined $dont_expect;
635
636         $self->ok( $test, $name );
637         $self->_cmp_diag( $got, '!=', $dont_expect ) unless $test;
638         return $test;
639     }
640
641     return $self->cmp_ok( $got, '!=', $dont_expect, $name );
642 }
643
644 =item B<like>
645
646   $Test->like($this, qr/$regex/, $name);
647   $Test->like($this, '/$regex/', $name);
648
649 Like Test::More's like().  Checks if $this matches the given $regex.
650
651 You'll want to avoid qr// if you want your tests to work before 5.005.
652
653 =item B<unlike>
654
655   $Test->unlike($this, qr/$regex/, $name);
656   $Test->unlike($this, '/$regex/', $name);
657
658 Like Test::More's unlike().  Checks if $this B<does not match> the
659 given $regex.
660
661 =cut
662
663 sub like {
664     my ( $self, $this, $regex, $name ) = @_;
665
666     local $Level = $Level + 1;
667     $self->_regex_ok( $this, $regex, '=~', $name );
668 }
669
670 sub unlike {
671     my ( $self, $this, $regex, $name ) = @_;
672
673     local $Level = $Level + 1;
674     $self->_regex_ok( $this, $regex, '!~', $name );
675 }
676
677 =item B<cmp_ok>
678
679   $Test->cmp_ok($this, $type, $that, $name);
680
681 Works just like Test::More's cmp_ok().
682
683     $Test->cmp_ok($big_num, '!=', $other_big_num);
684
685 =cut
686
687 my %numeric_cmps
688   = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );
689
690 sub cmp_ok {
691     my ( $self, $got, $type, $expect, $name ) = @_;
692
693     # Treat overloaded objects as numbers if we're asked to do a
694     # numeric comparison.
695     my $unoverload
696       = $numeric_cmps{$type}
697       ? '_unoverload_num'
698       : '_unoverload_str';
699
700     $self->$unoverload( \$got, \$expect );
701
702     my $test;
703     {
704         local ( $@, $!, $SIG{__DIE__} );    # isolate eval
705
706         my $code = $self->_caller_context;
707
708        # Yes, it has to look like this or 5.4.5 won't see the #line directive.
709        # Don't ask me, man, I just work here.
710         $test = eval "
711 $code" . "\$got $type \$expect;";
712
713     }
714     local $Level = $Level + 1;
715     my $ok = $self->ok( $test, $name );
716
717     unless ($ok) {
718         if ( $type =~ /^(eq|==)$/ ) {
719             $self->_is_diag( $got, $type, $expect );
720         }
721         else {
722             $self->_cmp_diag( $got, $type, $expect );
723         }
724     }
725     return $ok;
726 }
727
728 sub _cmp_diag {
729     my ( $self, $got, $type, $expect ) = @_;
730
731     $got    = defined $got    ? "'$got'"    : 'undef';
732     $expect = defined $expect ? "'$expect'" : 'undef';
733     return $self->diag( sprintf <<DIAGNOSTIC, $got, $type, $expect );
734     %s
735         %s
736     %s
737 DIAGNOSTIC
738 }
739
740 sub _caller_context {
741     my $self = shift;
742
743     my ( $pack, $file, $line ) = $self->caller(1);
744
745     my $code = '';
746     $code .= "#line $line $file\n" if defined $file and defined $line;
747
748     return $code;
749 }
750
751 =back
752
753
754 =head2 Other Testing Methods
755
756 These are methods which are used in the course of writing a test but are not themselves tests.
757
758 =over 4
759
760 =item B<BAIL_OUT>
761
762     $Test->BAIL_OUT($reason);
763
764 Indicates to the Test::Harness that things are going so badly all
765 testing should terminate.  This includes running any additional test
766 scripts.
767
768 It will exit with 255.
769
770 =cut
771
772 sub BAIL_OUT {
773     my ( $self, $reason ) = @_;
774
775     $self->{Bailed_Out} = 1;
776     $self->_print("Bail out!  $reason");
777     exit 255;
778 }
779
780 =for deprecated
781 BAIL_OUT() used to be BAILOUT()
782
783 =cut
784
785 *BAILOUT = \&BAIL_OUT;
786
787 =item B<skip>
788
789     $Test->skip;
790     $Test->skip($why);
791
792 Skips the current test, reporting $why.
793
794 =cut
795
796 sub skip {
797     my ( $self, $why ) = @_;
798     $why ||= '';
799     $self->_unoverload_str( \$why );
800
801     $self->_plan_check;
802
803     lock( $self->{Curr_Test} );
804     $self->{Curr_Test}++;
805
806     $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
807         {   'ok'      => 1,
808             actual_ok => 1,
809             name      => '',
810             type      => 'skip',
811             reason    => $why,
812         }
813     );
814
815     my $out = "ok";
816     $out .= " $self->{Curr_Test}" if $self->use_numbers;
817     $out .= " # skip";
818     $out .= " $why"               if length $why;
819     $out .= "\n";
820
821     $self->_print($out);
822
823     return 1;
824 }
825
826 =item B<todo_skip>
827
828   $Test->todo_skip;
829   $Test->todo_skip($why);
830
831 Like skip(), only it will declare the test as failing and TODO.  Similar
832 to
833
834     print "not ok $tnum # TODO $why\n";
835
836 =cut
837
838 sub todo_skip {
839     my ( $self, $why ) = @_;
840     $why ||= '';
841
842     $self->_plan_check;
843
844     lock( $self->{Curr_Test} );
845     $self->{Curr_Test}++;
846
847     $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
848         {   'ok'      => 1,
849             actual_ok => 0,
850             name      => '',
851             type      => 'todo_skip',
852             reason    => $why,
853         }
854     );
855
856     my $out = "not ok";
857     $out .= " $self->{Curr_Test}" if $self->use_numbers;
858     $out .= " # TODO & SKIP $why\n";
859
860     $self->_print($out);
861
862     return 1;
863 }
864
865 =begin _unimplemented
866
867 =item B<skip_rest>
868
869   $Test->skip_rest;
870   $Test->skip_rest($reason);
871
872 Like skip(), only it skips all the rest of the tests you plan to run
873 and terminates the test.
874
875 If you're running under no_plan, it skips once and terminates the
876 test.
877
878 =end _unimplemented
879
880 =back
881
882
883 =head2 Test building utility methods
884
885 These methods are useful when writing your own test methods.
886
887 =over 4
888
889 =item B<maybe_regex>
890
891   $Test->maybe_regex(qr/$regex/);
892   $Test->maybe_regex('/$regex/');
893
894 Convenience method for building testing functions that take regular
895 expressions as arguments, but need to work before perl 5.005.
896
897 Takes a quoted regular expression produced by qr//, or a string
898 representing a regular expression.
899
900 Returns a Perl value which may be used instead of the corresponding
901 regular expression, or undef if it's argument is not recognised.
902
903 For example, a version of like(), sans the useful diagnostic messages,
904 could be written as:
905
906   sub laconic_like {
907       my ($self, $this, $regex, $name) = @_;
908       my $usable_regex = $self->maybe_regex($regex);
909       die "expecting regex, found '$regex'\n"
910           unless $usable_regex;
911       $self->ok($this =~ m/$usable_regex/, $name);
912   }
913
914 =cut
915
916 sub maybe_regex {
917     my ( $self, $regex ) = @_;
918     my $usable_regex = undef;
919
920     return $usable_regex unless defined $regex;
921
922     my ( $re, $opts );
923
924     # Check for qr/foo/
925     if ( ref $regex eq 'Regexp' ) {
926         $usable_regex = $regex;
927     }
928
929     # Check for '/foo/' or 'm,foo,'
930     elsif (( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx
931         or ( undef, $re, $opts )
932         = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx )
933     {
934         $usable_regex = length $opts ? "(?$opts)$re" : $re;
935     }
936
937     return $usable_regex;
938 }
939
940 sub _regex_ok {
941     my ( $self, $this, $regex, $cmp, $name ) = @_;
942
943     my $ok           = 0;
944     my $usable_regex = $self->maybe_regex($regex);
945     unless ( defined $usable_regex ) {
946         $ok = $self->ok( 0, $name );
947         $self->diag("    '$regex' doesn't look much like a regex to me.");
948         return $ok;
949     }
950
951     {
952         my $test;
953         my $code = $self->_caller_context;
954
955         local ( $@, $!, $SIG{__DIE__} );    # isolate eval
956
957        # Yes, it has to look like this or 5.4.5 won't see the #line directive.
958        # Don't ask me, man, I just work here.
959         $test = eval "
960 $code" . q{$test = $this =~ /$usable_regex/ ? 1 : 0};
961
962         $test = !$test if $cmp eq '!~';
963
964         local $Level = $Level + 1;
965         $ok = $self->ok( $test, $name );
966     }
967
968     unless ($ok) {
969         $this = defined $this ? "'$this'" : 'undef';
970         my $match = $cmp eq '=~' ? "doesn't match" : "matches";
971         $self->diag( sprintf <<DIAGNOSTIC, $this, $match, $regex );
972                   %s
973     %13s '%s'
974 DIAGNOSTIC
975
976     }
977
978     return $ok;
979 }
980
981 # I'm not ready to publish this.  It doesn't deal with array return
982 # values from the code or context.
983
984 =begin private
985
986 =item B<_try>
987
988     my $return_from_code          = $Test->try(sub { code });
989     my($return_from_code, $error) = $Test->try(sub { code });
990
991 Works like eval BLOCK except it ensures it has no effect on the rest of the test (ie. $@ is not set) nor is effected by outside interference (ie. $SIG{__DIE__}) and works around some quirks in older Perls.
992
993 $error is what would normally be in $@.
994
995 It is suggested you use this in place of eval BLOCK.
996
997 =cut
998
999 sub _try {
1000     my ( $self, $code ) = @_;
1001
1002     local $!;               # eval can mess up $!
1003     local $@;               # don't set $@ in the test
1004     local $SIG{__DIE__};    # don't trip an outside DIE handler.
1005     my $return = eval { $code->() };
1006
1007     return wantarray ? ( $return, $@ ) : $return;
1008 }
1009
1010 =end private
1011
1012
1013 =item B<is_fh>
1014
1015     my $is_fh = $Test->is_fh($thing);
1016
1017 Determines if the given $thing can be used as a filehandle.
1018
1019 =cut
1020
1021 sub is_fh {
1022     my $self     = shift;
1023     my $maybe_fh = shift;
1024     return 0 unless defined $maybe_fh;
1025
1026     return 1 if ref $maybe_fh  eq 'GLOB';    # its a glob ref
1027     return 1 if ref \$maybe_fh eq 'GLOB';    # its a glob
1028
1029     return eval { $maybe_fh->isa("IO::Handle") } ||
1030
1031       # 5.5.4's tied() and can() doesn't like getting undef
1032       eval { ( tied($maybe_fh) || '' )->can('TIEHANDLE') };
1033 }
1034
1035 =back
1036
1037
1038 =head2 Test style
1039
1040
1041 =over 4
1042
1043 =item B<level>
1044
1045     $Test->level($how_high);
1046
1047 How far up the call stack should $Test look when reporting where the
1048 test failed.
1049
1050 Defaults to 1.
1051
1052 Setting L<$Test::Builder::Level> overrides.  This is typically useful
1053 localized:
1054
1055     sub my_ok {
1056         my $test = shift;
1057
1058         local $Test::Builder::Level = $Test::Builder::Level + 1;
1059         $TB->ok($test);
1060     }
1061
1062 To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant.
1063
1064 =cut
1065
1066 sub level {
1067     my ( $self, $level ) = @_;
1068
1069     if ( defined $level ) {
1070         $Level = $level;
1071     }
1072     return $Level;
1073 }
1074
1075 =item B<use_numbers>
1076
1077     $Test->use_numbers($on_or_off);
1078
1079 Whether or not the test should output numbers.  That is, this if true:
1080
1081   ok 1
1082   ok 2
1083   ok 3
1084
1085 or this if false
1086
1087   ok
1088   ok
1089   ok
1090
1091 Most useful when you can't depend on the test output order, such as
1092 when threads or forking is involved.
1093
1094 Defaults to on.
1095
1096 =cut
1097
1098 sub use_numbers {
1099     my ( $self, $use_nums ) = @_;
1100
1101     if ( defined $use_nums ) {
1102         $self->{Use_Nums} = $use_nums;
1103     }
1104     return $self->{Use_Nums};
1105 }
1106
1107 =item B<no_diag>
1108
1109     $Test->no_diag($no_diag);
1110
1111 If set true no diagnostics will be printed.  This includes calls to
1112 diag().
1113
1114 =item B<no_ending>
1115
1116     $Test->no_ending($no_ending);
1117
1118 Normally, Test::Builder does some extra diagnostics when the test
1119 ends.  It also changes the exit code as described below.
1120
1121 If this is true, none of that will be done.
1122
1123 =item B<no_header>
1124
1125     $Test->no_header($no_header);
1126
1127 If set to true, no "1..N" header will be printed.
1128
1129 =cut
1130
1131 foreach my $attribute (qw(No_Header No_Ending No_Diag)) {
1132     my $method = lc $attribute;
1133
1134     my $code = sub {
1135         my ( $self, $no ) = @_;
1136
1137         if ( defined $no ) {
1138             $self->{$attribute} = $no;
1139         }
1140         return $self->{$attribute};
1141     };
1142
1143     no strict 'refs';
1144     *{ __PACKAGE__ . '::' . $method } = $code;
1145 }
1146
1147 =back
1148
1149 =head2 Output
1150
1151 Controlling where the test output goes.
1152
1153 It's ok for your test to change where STDOUT and STDERR point to,
1154 Test::Builder's default output settings will not be affected.
1155
1156 =over 4
1157
1158 =item B<diag>
1159
1160     $Test->diag(@msgs);
1161
1162 Prints out the given @msgs.  Like C<print>, arguments are simply
1163 appended together.
1164
1165 Normally, it uses the failure_output() handle, but if this is for a
1166 TODO test, the todo_output() handle is used.
1167
1168 Output will be indented and marked with a # so as not to interfere
1169 with test output.  A newline will be put on the end if there isn't one
1170 already.
1171
1172 We encourage using this rather than calling print directly.
1173
1174 Returns false.  Why?  Because diag() is often used in conjunction with
1175 a failing test (C<ok() || diag()>) it "passes through" the failure.
1176
1177     return ok(...) || diag(...);
1178
1179 =for blame transfer
1180 Mark Fowler <mark@twoshortplanks.com>
1181
1182 =cut
1183
1184 sub diag {
1185     my ( $self, @msgs ) = @_;
1186
1187     return if $self->no_diag;
1188     return unless @msgs;
1189
1190     # Prevent printing headers when compiling (i.e. -c)
1191     return if $^C;
1192
1193     # Smash args together like print does.
1194     # Convert undef to 'undef' so its readable.
1195     my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1196
1197     # Escape each line with a #.
1198     $msg =~ s/^/# /gm;
1199
1200     # Stick a newline on the end if it needs it.
1201     $msg .= "\n" unless $msg =~ /\n\Z/;
1202
1203     local $Level = $Level + 1;
1204     $self->_print_diag($msg);
1205
1206     return 0;
1207 }
1208
1209 =begin _private
1210
1211 =item B<_print>
1212
1213     $Test->_print(@msgs);
1214
1215 Prints to the output() filehandle.
1216
1217 =end _private
1218
1219 =cut
1220
1221 sub _print {
1222     my ( $self, @msgs ) = @_;
1223
1224     # Prevent printing headers when only compiling.  Mostly for when
1225     # tests are deparsed with B::Deparse
1226     return if $^C;
1227
1228     my $msg = join '', @msgs;
1229
1230     local ( $\, $", $, ) = ( undef, ' ', '' );
1231     my $fh = $self->output;
1232
1233     # Escape each line after the first with a # so we don't
1234     # confuse Test::Harness.
1235     $msg =~ s/\n(.)/\n# $1/sg;
1236
1237     # Stick a newline on the end if it needs it.
1238     $msg .= "\n" unless $msg =~ /\n\Z/;
1239
1240     print $fh $msg;
1241 }
1242
1243 =begin private
1244
1245 =item B<_print_diag>
1246
1247     $Test->_print_diag(@msg);
1248
1249 Like _print, but prints to the current diagnostic filehandle.
1250
1251 =end private
1252
1253 =cut
1254
1255 sub _print_diag {
1256     my $self = shift;
1257
1258     local ( $\, $", $, ) = ( undef, ' ', '' );
1259     my $fh = $self->todo ? $self->todo_output : $self->failure_output;
1260     print $fh @_;
1261 }
1262
1263 =item B<output>
1264
1265     $Test->output($fh);
1266     $Test->output($file);
1267
1268 Where normal "ok/not ok" test output should go.
1269
1270 Defaults to STDOUT.
1271
1272 =item B<failure_output>
1273
1274     $Test->failure_output($fh);
1275     $Test->failure_output($file);
1276
1277 Where diagnostic output on test failures and diag() should go.
1278
1279 Defaults to STDERR.
1280
1281 =item B<todo_output>
1282
1283     $Test->todo_output($fh);
1284     $Test->todo_output($file);
1285
1286 Where diagnostics about todo test failures and diag() should go.
1287
1288 Defaults to STDOUT.
1289
1290 =cut
1291
1292 sub output {
1293     my ( $self, $fh ) = @_;
1294
1295     if ( defined $fh ) {
1296         $self->{Out_FH} = $self->_new_fh($fh);
1297     }
1298     return $self->{Out_FH};
1299 }
1300
1301 sub failure_output {
1302     my ( $self, $fh ) = @_;
1303
1304     if ( defined $fh ) {
1305         $self->{Fail_FH} = $self->_new_fh($fh);
1306     }
1307     return $self->{Fail_FH};
1308 }
1309
1310 sub todo_output {
1311     my ( $self, $fh ) = @_;
1312
1313     if ( defined $fh ) {
1314         $self->{Todo_FH} = $self->_new_fh($fh);
1315     }
1316     return $self->{Todo_FH};
1317 }
1318
1319 sub _new_fh {
1320     my $self = shift;
1321     my ($file_or_fh) = shift;
1322
1323     my $fh;
1324     if ( $self->is_fh($file_or_fh) ) {
1325         $fh = $file_or_fh;
1326     }
1327     else {
1328         $fh = do { local *FH };
1329         open $fh, ">$file_or_fh"
1330           or $self->croak("Can't open test output log $file_or_fh: $!");
1331         _autoflush($fh);
1332     }
1333
1334     return $fh;
1335 }
1336
1337 sub _autoflush {
1338     my ($fh) = shift;
1339     my $old_fh = select $fh;
1340     $| = 1;
1341     select $old_fh;
1342 }
1343
1344 sub _dup_stdhandles {
1345     my $self = shift;
1346
1347     $self->_open_testhandles;
1348
1349     # Set everything to unbuffered else plain prints to STDOUT will
1350     # come out in the wrong order from our own prints.
1351     _autoflush( \*TESTOUT );
1352     _autoflush( \*STDOUT );
1353     _autoflush( \*TESTERR );
1354     _autoflush( \*STDERR );
1355
1356     $self->output( \*TESTOUT );
1357     $self->failure_output( \*TESTERR );
1358     $self->todo_output( \*TESTOUT );
1359 }
1360
1361 my $Opened_Testhandles = 0;
1362
1363 sub _open_testhandles {
1364     return if $Opened_Testhandles;
1365
1366     # We dup STDOUT and STDERR so people can change them in their
1367     # test suites while still getting normal test output.
1368     open( TESTOUT, ">&STDOUT" ) or die "Can't dup STDOUT:  $!";
1369     open( TESTERR, ">&STDERR" ) or die "Can't dup STDERR:  $!";
1370     $Opened_Testhandles = 1;
1371 }
1372
1373 =item carp
1374
1375   $tb->carp(@message);
1376
1377 Warns with C<@message> but the message will appear to come from the
1378 point where the original test function was called (C<$tb->caller>).
1379
1380 =item croak
1381
1382   $tb->croak(@message);
1383
1384 Dies with C<@message> but the message will appear to come from the
1385 point where the original test function was called (C<$tb->caller>).
1386
1387 =cut
1388
1389 sub _message_at_caller {
1390     my $self = shift;
1391
1392     local $Level = $Level + 1;
1393     my ( $pack, $file, $line ) = $self->caller;
1394     return join( "", @_ ) . " at $file line $line.\n";
1395 }
1396
1397 sub carp {
1398     my $self = shift;
1399     warn $self->_message_at_caller(@_);
1400 }
1401
1402 sub croak {
1403     my $self = shift;
1404     die $self->_message_at_caller(@_);
1405 }
1406
1407 sub _plan_check {
1408     my $self = shift;
1409
1410     unless ( $self->{Have_Plan} ) {
1411         local $Level = $Level + 2;
1412         $self->croak("You tried to run a test without a plan");
1413     }
1414 }
1415
1416 =back
1417
1418
1419 =head2 Test Status and Info
1420
1421 =over 4
1422
1423 =item B<current_test>
1424
1425     my $curr_test = $Test->current_test;
1426     $Test->current_test($num);
1427
1428 Gets/sets the current test number we're on.  You usually shouldn't
1429 have to set this.
1430
1431 If set forward, the details of the missing tests are filled in as 'unknown'.
1432 if set backward, the details of the intervening tests are deleted.  You
1433 can erase history if you really want to.
1434
1435 =cut
1436
1437 sub current_test {
1438     my ( $self, $num ) = @_;
1439
1440     lock( $self->{Curr_Test} );
1441     if ( defined $num ) {
1442         unless ( $self->{Have_Plan} ) {
1443             $self->croak(
1444                 "Can't change the current test number without a plan!");
1445         }
1446
1447         $self->{Curr_Test} = $num;
1448
1449         # If the test counter is being pushed forward fill in the details.
1450         my $test_results = $self->{Test_Results};
1451         if ( $num > @$test_results ) {
1452             my $start = @$test_results ? @$test_results : 0;
1453             for ( $start .. $num - 1 ) {
1454                 $test_results->[$_] = &share(
1455                     {   'ok'      => 1,
1456                         actual_ok => undef,
1457                         reason    => 'incrementing test number',
1458                         type      => 'unknown',
1459                         name      => undef
1460                     }
1461                 );
1462             }
1463         }
1464
1465         # If backward, wipe history.  Its their funeral.
1466         elsif ( $num < @$test_results ) {
1467             $#{$test_results} = $num - 1;
1468         }
1469     }
1470     return $self->{Curr_Test};
1471 }
1472
1473 =item B<summary>
1474
1475     my @tests = $Test->summary;
1476
1477 A simple summary of the tests so far.  True for pass, false for fail.
1478 This is a logical pass/fail, so todos are passes.
1479
1480 Of course, test #1 is $tests[0], etc...
1481
1482 =cut
1483
1484 sub summary {
1485     my ($self) = shift;
1486
1487     return map { $_->{'ok'} } @{ $self->{Test_Results} };
1488 }
1489
1490 =item B<details>
1491
1492     my @tests = $Test->details;
1493
1494 Like summary(), but with a lot more detail.
1495
1496     $tests[$test_num - 1] = 
1497             { 'ok'       => is the test considered a pass?
1498               actual_ok  => did it literally say 'ok'?
1499               name       => name of the test (if any)
1500               type       => type of test (if any, see below).
1501               reason     => reason for the above (if any)
1502             };
1503
1504 'ok' is true if Test::Harness will consider the test to be a pass.
1505
1506 'actual_ok' is a reflection of whether or not the test literally
1507 printed 'ok' or 'not ok'.  This is for examining the result of 'todo'
1508 tests.  
1509
1510 'name' is the name of the test.
1511
1512 'type' indicates if it was a special test.  Normal tests have a type
1513 of ''.  Type can be one of the following:
1514
1515     skip        see skip()
1516     todo        see todo()
1517     todo_skip   see todo_skip()
1518     unknown     see below
1519
1520 Sometimes the Test::Builder test counter is incremented without it
1521 printing any test output, for example, when current_test() is changed.
1522 In these cases, Test::Builder doesn't know the result of the test, so
1523 it's type is 'unkown'.  These details for these tests are filled in.
1524 They are considered ok, but the name and actual_ok is left undef.
1525
1526 For example "not ok 23 - hole count # TODO insufficient donuts" would
1527 result in this structure:
1528
1529     $tests[22] =    # 23 - 1, since arrays start from 0.
1530       { ok        => 1,   # logically, the test passed since it's todo
1531         actual_ok => 0,   # in absolute terms, it failed
1532         name      => 'hole count',
1533         type      => 'todo',
1534         reason    => 'insufficient donuts'
1535       };
1536
1537 =cut
1538
1539 sub details {
1540     my $self = shift;
1541     return @{ $self->{Test_Results} };
1542 }
1543
1544 =item B<todo>
1545
1546     my $todo_reason = $Test->todo;
1547     my $todo_reason = $Test->todo($pack);
1548
1549 todo() looks for a $TODO variable in your tests.  If set, all tests
1550 will be considered 'todo' (see Test::More and Test::Harness for
1551 details).  Returns the reason (ie. the value of $TODO) if running as
1552 todo tests, false otherwise.
1553
1554 todo() is about finding the right package to look for $TODO in.  It
1555 uses the exported_to() package to find it.  If that's not set, it's
1556 pretty good at guessing the right package to look at based on $Level.
1557
1558 Sometimes there is some confusion about where todo() should be looking
1559 for the $TODO variable.  If you want to be sure, tell it explicitly
1560 what $pack to use.
1561
1562 =cut
1563
1564 sub todo {
1565     my ( $self, $pack ) = @_;
1566
1567     $pack = $pack || $self->exported_to || $self->caller($Level);
1568     return 0 unless $pack;
1569
1570     no strict 'refs';
1571     return defined ${ $pack . '::TODO' }
1572       ? ${ $pack . '::TODO' }
1573       : 0;
1574 }
1575
1576 =item B<caller>
1577
1578     my $package = $Test->caller;
1579     my($pack, $file, $line) = $Test->caller;
1580     my($pack, $file, $line) = $Test->caller($height);
1581
1582 Like the normal caller(), except it reports according to your level().
1583
1584 =cut
1585
1586 sub caller {
1587     my ( $self, $height ) = @_;
1588     $height ||= 0;
1589
1590     my @caller = CORE::caller( $self->level + $height + 1 );
1591     return wantarray ? @caller : $caller[0];
1592 }
1593
1594 =back
1595
1596 =cut
1597
1598 =begin _private
1599
1600 =over 4
1601
1602 =item B<_sanity_check>
1603
1604   $self->_sanity_check();
1605
1606 Runs a bunch of end of test sanity checks to make sure reality came
1607 through ok.  If anything is wrong it will die with a fairly friendly
1608 error message.
1609
1610 =cut
1611
1612 #'#
1613 sub _sanity_check {
1614     my $self = shift;
1615
1616     $self->_whoa(
1617         $self->{Curr_Test} < 0,
1618         'Says here you ran a negative number of tests!'
1619     );
1620     $self->_whoa(
1621         !$self->{Have_Plan} and $self->{Curr_Test},
1622         'Somehow your tests ran without a plan!'
1623     );
1624     $self->_whoa(
1625         $self->{Curr_Test} != @{ $self->{Test_Results} },
1626         'Somehow you got a different number of results than tests ran!'
1627     );
1628 }
1629
1630 =item B<_whoa>
1631
1632   $self->_whoa($check, $description);
1633
1634 A sanity check, similar to assert().  If the $check is true, something
1635 has gone horribly wrong.  It will die with the given $description and
1636 a note to contact the author.
1637
1638 =cut
1639
1640 sub _whoa {
1641     my ( $self, $check, $desc ) = @_;
1642     if ($check) {
1643         local $Level = $Level + 1;
1644         $self->croak(<<"WHOA");
1645 WHOA!  $desc
1646 This should never happen!  Please contact the author immediately!
1647 WHOA
1648     }
1649 }
1650
1651 =item B<_my_exit>
1652
1653   _my_exit($exit_num);
1654
1655 Perl seems to have some trouble with exiting inside an END block.  5.005_03
1656 and 5.6.1 both seem to do odd things.  Instead, this function edits $?
1657 directly.  It should ONLY be called from inside an END block.  It
1658 doesn't actually exit, that's your job.
1659
1660 =cut
1661
1662 sub _my_exit {
1663     $? = $_[0];
1664
1665     return 1;
1666 }
1667
1668 =back
1669
1670 =end _private
1671
1672 =cut
1673
1674 $SIG{__DIE__} = sub {
1675
1676     # We don't want to muck with death in an eval, but $^S isn't
1677     # totally reliable.  5.005_03 and 5.6.1 both do the wrong thing
1678     # with it.  Instead, we use caller.  This also means it runs under
1679     # 5.004!
1680     my $in_eval = 0;
1681     for ( my $stack = 1; my $sub = ( CORE::caller($stack) )[3]; $stack++ ) {
1682         $in_eval = 1 if $sub =~ /^\(eval\)/;
1683     }
1684     $Test->{Test_Died} = 1 unless $in_eval;
1685 };
1686
1687 sub _ending {
1688     my $self = shift;
1689
1690     $self->_sanity_check();
1691
1692     # Don't bother with an ending if this is a forked copy.  Only the parent
1693     # should do the ending.
1694     # Exit if plan() was never called.  This is so "require Test::Simple"
1695     # doesn't puke.
1696     # Don't do an ending if we bailed out.
1697     if (   ( $self->{Original_Pid} != $$ )
1698         or ( !$self->{Have_Plan} && !$self->{Test_Died} )
1699         or $self->{Bailed_Out} )
1700     {
1701         _my_exit($?);
1702         return;
1703     }
1704
1705     # Figure out if we passed or failed and print helpful messages.
1706     my $test_results = $self->{Test_Results};
1707     if (@$test_results) {
1708
1709         # The plan?  We have no plan.
1710         if ( $self->{No_Plan} ) {
1711             $self->_print("1..$self->{Curr_Test}\n") unless $self->no_header;
1712             $self->{Expected_Tests} = $self->{Curr_Test};
1713         }
1714
1715         # Auto-extended arrays and elements which aren't explicitly
1716         # filled in with a shared reference will puke under 5.8.0
1717         # ithreads.  So we have to fill them in by hand. :(
1718         my $empty_result = &share( {} );
1719         for my $idx ( 0 .. $self->{Expected_Tests} - 1 ) {
1720             $test_results->[$idx] = $empty_result
1721               unless defined $test_results->[$idx];
1722         }
1723
1724         my $num_failed = grep !$_->{'ok'},
1725           @{$test_results}[ 0 .. $self->{Curr_Test} - 1 ];
1726
1727         my $num_extra = $self->{Curr_Test} - $self->{Expected_Tests};
1728
1729         if ( $num_extra < 0 ) {
1730             my $s = $self->{Expected_Tests} == 1 ? '' : 's';
1731             $self->diag(<<"FAIL");
1732 Looks like you planned $self->{Expected_Tests} test$s but only ran $self->{Curr_Test}.
1733 FAIL
1734         }
1735         elsif ( $num_extra > 0 ) {
1736             my $s = $self->{Expected_Tests} == 1 ? '' : 's';
1737             $self->diag(<<"FAIL");
1738 Looks like you planned $self->{Expected_Tests} test$s but ran $num_extra extra.
1739 FAIL
1740         }
1741
1742         if ($num_failed) {
1743             my $num_tests = $self->{Curr_Test};
1744             my $s = $num_failed == 1 ? '' : 's';
1745
1746             my $qualifier = $num_extra == 0 ? '' : ' run';
1747
1748             $self->diag(<<"FAIL");
1749 Looks like you failed $num_failed test$s of $num_tests$qualifier.
1750 FAIL
1751         }
1752
1753         if ( $self->{Test_Died} ) {
1754             $self->diag(<<"FAIL");
1755 Looks like your test died just after $self->{Curr_Test}.
1756 FAIL
1757
1758             _my_exit(255) && return;
1759         }
1760
1761         my $exit_code;
1762         if ($num_failed) {
1763             $exit_code = $num_failed <= 254 ? $num_failed : 254;
1764         }
1765         elsif ( $num_extra != 0 ) {
1766             $exit_code = 255;
1767         }
1768         else {
1769             $exit_code = 0;
1770         }
1771
1772         _my_exit($exit_code) && return;
1773     }
1774     elsif ( $self->{Skip_All} ) {
1775         _my_exit(0) && return;
1776     }
1777     elsif ( $self->{Test_Died} ) {
1778         $self->diag(<<'FAIL');
1779 Looks like your test died before it could output anything.
1780 FAIL
1781         _my_exit(255) && return;
1782     }
1783     else {
1784         $self->diag("No tests run!\n");
1785         _my_exit(255) && return;
1786     }
1787 }
1788
1789 END {
1790     $Test->_ending if defined $Test and !$Test->no_ending;
1791 }
1792
1793 =head1 EXIT CODES
1794
1795 If all your tests passed, Test::Builder will exit with zero (which is
1796 normal).  If anything failed it will exit with how many failed.  If
1797 you run less (or more) tests than you planned, the missing (or extras)
1798 will be considered failures.  If no tests were ever run Test::Builder
1799 will throw a warning and exit with 255.  If the test died, even after
1800 having successfully completed all its tests, it will still be
1801 considered a failure and will exit with 255.
1802
1803 So the exit codes are...
1804
1805     0                   all tests successful
1806     255                 test died or all passed but wrong # of tests run
1807     any other number    how many failed (including missing or extras)
1808
1809 If you fail more than 254 tests, it will be reported as 254.
1810
1811
1812 =head1 THREADS
1813
1814 In perl 5.8.1 and later, Test::Builder is thread-safe.  The test
1815 number is shared amongst all threads.  This means if one thread sets
1816 the test number using current_test() they will all be effected.
1817
1818 While versions earlier than 5.8.1 had threads they contain too many
1819 bugs to support.
1820
1821 Test::Builder is only thread-aware if threads.pm is loaded I<before>
1822 Test::Builder.
1823
1824 =head1 EXAMPLES
1825
1826 CPAN can provide the best examples.  Test::Simple, Test::More,
1827 Test::Exception and Test::Differences all use Test::Builder.
1828
1829 =head1 SEE ALSO
1830
1831 Test::Simple, Test::More, Test::Harness
1832
1833 =head1 AUTHORS
1834
1835 Original code by chromatic, maintained by Michael G Schwern
1836 E<lt>schwern@pobox.comE<gt>
1837
1838 =head1 COPYRIGHT
1839
1840 Copyright 2002, 2004 by chromatic E<lt>chromatic@wgz.orgE<gt> and
1841                         Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1842
1843 This program is free software; you can redistribute it and/or 
1844 modify it under the same terms as Perl itself.
1845
1846 See F<http://www.perl.com/perl/misc/Artistic.html>
1847
1848 =cut
1849
1850 1;