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