Wrap the macro arguments for ck_proto in ().
[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);
452a7a81 11$VERSION = '0.36';
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
367These actually run the tests, analogous to the functions in
368Test::More.
369
370$name is always optional.
371
372=over 4
373
374=item B<ok>
375
376 $Test->ok($test, $name);
377
378Your basic test. Pass if $test is true, fail if $test is false. Just
379like Test::Simple's ok().
380
381=cut
382
383sub ok {
384 my($self, $test, $name) = @_;
385
60ffb308 386 # $test might contain an object which we don't want to accidentally
387 # store, so we turn it into a boolean.
388 $test = $test ? 1 : 0;
389
b7f9bbeb 390 $self->_plan_check;
33459055 391
5143c659 392 lock $self->{Curr_Test};
393 $self->{Curr_Test}++;
a344be10 394
30e302f8 395 # In case $name is a string overloaded object, force it to stringify.
b1ddf169 396 $self->_unoverload_str(\$name);
30e302f8 397
33459055 398 $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
a9153838 399 You named your test '$name'. You shouldn't use numbers for your test names.
400 Very confusing.
33459055 401ERR
402
403 my($pack, $file, $line) = $self->caller;
404
405 my $todo = $self->todo($pack);
b1ddf169 406 $self->_unoverload_str(\$todo);
33459055 407
408 my $out;
30e302f8 409 my $result = &share({});
60ffb308 410
33459055 411 unless( $test ) {
412 $out .= "not ";
60ffb308 413 @$result{ 'ok', 'actual_ok' } = ( ( $todo ? 1 : 0 ), 0 );
33459055 414 }
415 else {
60ffb308 416 @$result{ 'ok', 'actual_ok' } = ( 1, $test );
33459055 417 }
418
419 $out .= "ok";
5143c659 420 $out .= " $self->{Curr_Test}" if $self->use_numbers;
33459055 421
422 if( defined $name ) {
423 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
424 $out .= " - $name";
7483b81c 425 $result->{name} = $name;
60ffb308 426 }
427 else {
428 $result->{name} = '';
33459055 429 }
430
431 if( $todo ) {
7483b81c 432 $out .= " # TODO $todo";
433 $result->{reason} = $todo;
60ffb308 434 $result->{type} = 'todo';
435 }
436 else {
437 $result->{reason} = '';
438 $result->{type} = '';
33459055 439 }
440
5143c659 441 $self->{Test_Results}[$self->{Curr_Test}-1] = $result;
33459055 442 $out .= "\n";
443
444 $self->_print($out);
445
446 unless( $test ) {
447 my $msg = $todo ? "Failed (TODO)" : "Failed";
30e302f8 448 $self->_print_diag("\n") if $ENV{HARNESS_ACTIVE};
b1ddf169 449
450 if( defined $name ) {
451 $self->diag(qq[ $msg test '$name'\n]);
b7f9bbeb 452 $self->diag(qq[ at $file line $line.\n]);
b1ddf169 453 }
454 else {
b7f9bbeb 455 $self->diag(qq[ $msg test at $file line $line.\n]);
b1ddf169 456 }
33459055 457 }
458
459 return $test ? 1 : 0;
460}
461
7483b81c 462
463sub _unoverload {
464 my $self = shift;
b1ddf169 465 my $type = shift;
7483b81c 466
467 local($@,$!);
468
469 eval { require overload } || return;
470
471 foreach my $thing (@_) {
472 eval {
b1ddf169 473 if( _is_object($$thing) ) {
474 if( my $string_meth = overload::Method($$thing, $type) ) {
7483b81c 475 $$thing = $$thing->$string_meth();
476 }
477 }
478 };
479 }
480}
481
482
b1ddf169 483sub _is_object {
484 my $thing = shift;
485
486 return eval { ref $thing && $thing->isa('UNIVERSAL') } ? 1 : 0;
487}
488
489
490sub _unoverload_str {
491 my $self = shift;
492
493 $self->_unoverload(q[""], @_);
494}
495
496sub _unoverload_num {
497 my $self = shift;
498
499 $self->_unoverload('0+', @_);
500
501 for my $val (@_) {
502 next unless $self->_is_dualvar($$val);
503 $$val = $$val+0;
504 }
505}
506
507
508# This is a hack to detect a dualvar such as $!
509sub _is_dualvar {
510 my($self, $val) = @_;
511
512 local $^W = 0;
513 my $numval = $val+0;
514 return 1 if $numval != 0 and $numval ne $val;
515}
516
517
518
33459055 519=item B<is_eq>
520
521 $Test->is_eq($got, $expected, $name);
522
523Like Test::More's is(). Checks if $got eq $expected. This is the
524string version.
525
526=item B<is_num>
527
a9153838 528 $Test->is_num($got, $expected, $name);
33459055 529
530Like Test::More's is(). Checks if $got == $expected. This is the
531numeric version.
532
533=cut
534
535sub is_eq {
a9153838 536 my($self, $got, $expect, $name) = @_;
33459055 537 local $Level = $Level + 1;
a9153838 538
b1ddf169 539 $self->_unoverload_str(\$got, \$expect);
540
a9153838 541 if( !defined $got || !defined $expect ) {
542 # undef only matches undef and nothing else
543 my $test = !defined $got && !defined $expect;
544
545 $self->ok($test, $name);
546 $self->_is_diag($got, 'eq', $expect) unless $test;
547 return $test;
548 }
549
550 return $self->cmp_ok($got, 'eq', $expect, $name);
33459055 551}
552
553sub is_num {
a9153838 554 my($self, $got, $expect, $name) = @_;
33459055 555 local $Level = $Level + 1;
a9153838 556
b1ddf169 557 $self->_unoverload_num(\$got, \$expect);
558
a9153838 559 if( !defined $got || !defined $expect ) {
560 # undef only matches undef and nothing else
561 my $test = !defined $got && !defined $expect;
562
563 $self->ok($test, $name);
564 $self->_is_diag($got, '==', $expect) unless $test;
565 return $test;
566 }
567
568 return $self->cmp_ok($got, '==', $expect, $name);
33459055 569}
570
a9153838 571sub _is_diag {
572 my($self, $got, $type, $expect) = @_;
573
574 foreach my $val (\$got, \$expect) {
575 if( defined $$val ) {
576 if( $type eq 'eq' ) {
577 # quote and force string context
578 $$val = "'$$val'"
579 }
580 else {
581 # force numeric context
b1ddf169 582 $self->_unoverload_num($val);
a9153838 583 }
584 }
585 else {
586 $$val = 'undef';
587 }
588 }
33459055 589
89c1e84a 590 return $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
a9153838 591 got: %s
592 expected: %s
593DIAGNOSTIC
594
595}
596
597=item B<isnt_eq>
598
599 $Test->isnt_eq($got, $dont_expect, $name);
600
601Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
602the string version.
603
604=item B<isnt_num>
605
68938d83 606 $Test->isnt_num($got, $dont_expect, $name);
a9153838 607
608Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
609the numeric version.
610
611=cut
612
613sub isnt_eq {
614 my($self, $got, $dont_expect, $name) = @_;
615 local $Level = $Level + 1;
616
617 if( !defined $got || !defined $dont_expect ) {
618 # undef only matches undef and nothing else
619 my $test = defined $got || defined $dont_expect;
620
621 $self->ok($test, $name);
30e302f8 622 $self->_cmp_diag($got, 'ne', $dont_expect) unless $test;
a9153838 623 return $test;
33459055 624 }
a9153838 625
626 return $self->cmp_ok($got, 'ne', $dont_expect, $name);
627}
628
629sub isnt_num {
630 my($self, $got, $dont_expect, $name) = @_;
33459055 631 local $Level = $Level + 1;
33459055 632
a9153838 633 if( !defined $got || !defined $dont_expect ) {
634 # undef only matches undef and nothing else
635 my $test = defined $got || defined $dont_expect;
33459055 636
a9153838 637 $self->ok($test, $name);
30e302f8 638 $self->_cmp_diag($got, '!=', $dont_expect) unless $test;
a9153838 639 return $test;
640 }
641
642 return $self->cmp_ok($got, '!=', $dont_expect, $name);
33459055 643}
644
a9153838 645
33459055 646=item B<like>
647
648 $Test->like($this, qr/$regex/, $name);
649 $Test->like($this, '/$regex/', $name);
650
651Like Test::More's like(). Checks if $this matches the given $regex.
652
653You'll want to avoid qr// if you want your tests to work before 5.005.
654
a9153838 655=item B<unlike>
656
657 $Test->unlike($this, qr/$regex/, $name);
658 $Test->unlike($this, '/$regex/', $name);
659
660Like Test::More's unlike(). Checks if $this B<does not match> the
661given $regex.
662
33459055 663=cut
664
665sub like {
666 my($self, $this, $regex, $name) = @_;
667
668 local $Level = $Level + 1;
a9153838 669 $self->_regex_ok($this, $regex, '=~', $name);
670}
671
672sub unlike {
673 my($self, $this, $regex, $name) = @_;
674
675 local $Level = $Level + 1;
676 $self->_regex_ok($this, $regex, '!~', $name);
677}
678
89c1e84a 679=item B<maybe_regex>
a9153838 680
89c1e84a 681 $Test->maybe_regex(qr/$regex/);
682 $Test->maybe_regex('/$regex/');
33459055 683
89c1e84a 684Convenience method for building testing functions that take regular
685expressions as arguments, but need to work before perl 5.005.
686
687Takes a quoted regular expression produced by qr//, or a string
688representing a regular expression.
689
690Returns a Perl value which may be used instead of the corresponding
691regular expression, or undef if it's argument is not recognised.
692
693For example, a version of like(), sans the useful diagnostic messages,
694could be written as:
695
696 sub laconic_like {
697 my ($self, $this, $regex, $name) = @_;
698 my $usable_regex = $self->maybe_regex($regex);
699 die "expecting regex, found '$regex'\n"
700 unless $usable_regex;
701 $self->ok($this =~ m/$usable_regex/, $name);
702 }
703
704=cut
705
706
707sub maybe_regex {
0257f296 708 my ($self, $regex) = @_;
89c1e84a 709 my $usable_regex = undef;
0257f296 710
711 return $usable_regex unless defined $regex;
712
713 my($re, $opts);
714
715 # Check for qr/foo/
33459055 716 if( ref $regex eq 'Regexp' ) {
a9153838 717 $usable_regex = $regex;
33459055 718 }
0257f296 719 # Check for '/foo/' or 'm,foo,'
720 elsif( ($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
721 (undef, $re, $opts) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
722 )
723 {
89c1e84a 724 $usable_regex = length $opts ? "(?$opts)$re" : $re;
0257f296 725 }
726
727 return $usable_regex;
89c1e84a 728};
33459055 729
89c1e84a 730sub _regex_ok {
731 my($self, $this, $regex, $cmp, $name) = @_;
33459055 732
89c1e84a 733 my $ok = 0;
734 my $usable_regex = $self->maybe_regex($regex);
735 unless (defined $usable_regex) {
736 $ok = $self->ok( 0, $name );
737 $self->diag(" '$regex' doesn't look much like a regex to me.");
33459055 738 return $ok;
739 }
740
a9153838 741 {
b1ddf169 742 my $test;
743 my $code = $self->_caller_context;
744
745 local($@, $!);
746
747 # Yes, it has to look like this or 5.4.5 won't see the #line directive.
748 # Don't ask me, man, I just work here.
749 $test = eval "
750$code" . q{$test = $this =~ /$usable_regex/ ? 1 : 0};
751
a9153838 752 $test = !$test if $cmp eq '!~';
b1ddf169 753
754 local $Level = $Level + 1;
a9153838 755 $ok = $self->ok( $test, $name );
756 }
757
33459055 758 unless( $ok ) {
759 $this = defined $this ? "'$this'" : 'undef';
a9153838 760 my $match = $cmp eq '=~' ? "doesn't match" : "matches";
761 $self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex);
762 %s
763 %13s '%s'
33459055 764DIAGNOSTIC
765
766 }
767
768 return $ok;
769}
770
a9153838 771=item B<cmp_ok>
772
773 $Test->cmp_ok($this, $type, $that, $name);
774
775Works just like Test::More's cmp_ok().
776
777 $Test->cmp_ok($big_num, '!=', $other_big_num);
778
779=cut
780
b1ddf169 781
782my %numeric_cmps = map { ($_, 1) }
783 ("<", "<=", ">", ">=", "==", "!=", "<=>");
784
a9153838 785sub cmp_ok {
786 my($self, $got, $type, $expect, $name) = @_;
787
b1ddf169 788 # Treat overloaded objects as numbers if we're asked to do a
789 # numeric comparison.
790 my $unoverload = $numeric_cmps{$type} ? '_unoverload_num'
791 : '_unoverload_str';
792
793 $self->$unoverload(\$got, \$expect);
794
795
a9153838 796 my $test;
797 {
a9153838 798 local($@,$!); # don't interfere with $@
799 # eval() sometimes resets $!
b1ddf169 800
801 my $code = $self->_caller_context;
802
803 # Yes, it has to look like this or 5.4.5 won't see the #line directive.
804 # Don't ask me, man, I just work here.
805 $test = eval "
806$code" . "\$got $type \$expect;";
807
a9153838 808 }
809 local $Level = $Level + 1;
810 my $ok = $self->ok($test, $name);
811
812 unless( $ok ) {
813 if( $type =~ /^(eq|==)$/ ) {
814 $self->_is_diag($got, $type, $expect);
815 }
816 else {
817 $self->_cmp_diag($got, $type, $expect);
818 }
819 }
820 return $ok;
821}
822
823sub _cmp_diag {
824 my($self, $got, $type, $expect) = @_;
825
826 $got = defined $got ? "'$got'" : 'undef';
827 $expect = defined $expect ? "'$expect'" : 'undef';
89c1e84a 828 return $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect);
a9153838 829 %s
830 %s
831 %s
832DIAGNOSTIC
833}
834
a9153838 835
b1ddf169 836sub _caller_context {
837 my $self = shift;
838
839 my($pack, $file, $line) = $self->caller(1);
840
841 my $code = '';
842 $code .= "#line $line $file\n" if defined $file and defined $line;
843
844 return $code;
845}
846
847
848=item B<BAIL_OUT>
849
850 $Test->BAIL_OUT($reason);
a9153838 851
852Indicates to the Test::Harness that things are going so badly all
853testing should terminate. This includes running any additional test
854scripts.
855
856It will exit with 255.
857
858=cut
859
b1ddf169 860sub BAIL_OUT {
a9153838 861 my($self, $reason) = @_;
862
b1ddf169 863 $self->{Bailed_Out} = 1;
a9153838 864 $self->_print("Bail out! $reason");
865 exit 255;
866}
867
b1ddf169 868=for deprecated
869BAIL_OUT() used to be BAILOUT()
870
845d7e37 871=cut
872
b1ddf169 873*BAILOUT = \&BAIL_OUT;
874
875
33459055 876=item B<skip>
877
878 $Test->skip;
879 $Test->skip($why);
880
881Skips the current test, reporting $why.
882
883=cut
884
885sub skip {
886 my($self, $why) = @_;
887 $why ||= '';
b1ddf169 888 $self->_unoverload_str(\$why);
33459055 889
b7f9bbeb 890 $self->_plan_check;
33459055 891
5143c659 892 lock($self->{Curr_Test});
893 $self->{Curr_Test}++;
33459055 894
5143c659 895 $self->{Test_Results}[$self->{Curr_Test}-1] = &share({
60ffb308 896 'ok' => 1,
897 actual_ok => 1,
898 name => '',
899 type => 'skip',
900 reason => $why,
30e302f8 901 });
33459055 902
903 my $out = "ok";
5143c659 904 $out .= " $self->{Curr_Test}" if $self->use_numbers;
0257f296 905 $out .= " # skip";
906 $out .= " $why" if length $why;
907 $out .= "\n";
33459055 908
5143c659 909 $self->_print($out);
33459055 910
911 return 1;
912}
913
a9153838 914
915=item B<todo_skip>
916
917 $Test->todo_skip;
918 $Test->todo_skip($why);
919
920Like skip(), only it will declare the test as failing and TODO. Similar
921to
922
923 print "not ok $tnum # TODO $why\n";
924
925=cut
926
927sub todo_skip {
928 my($self, $why) = @_;
929 $why ||= '';
930
b7f9bbeb 931 $self->_plan_check;
a9153838 932
5143c659 933 lock($self->{Curr_Test});
934 $self->{Curr_Test}++;
a9153838 935
5143c659 936 $self->{Test_Results}[$self->{Curr_Test}-1] = &share({
60ffb308 937 'ok' => 1,
938 actual_ok => 0,
939 name => '',
940 type => 'todo_skip',
941 reason => $why,
30e302f8 942 });
a9153838 943
944 my $out = "not ok";
5143c659 945 $out .= " $self->{Curr_Test}" if $self->use_numbers;
89c1e84a 946 $out .= " # TODO & SKIP $why\n";
a9153838 947
5143c659 948 $self->_print($out);
a9153838 949
950 return 1;
951}
952
953
33459055 954=begin _unimplemented
955
956=item B<skip_rest>
957
958 $Test->skip_rest;
959 $Test->skip_rest($reason);
960
961Like skip(), only it skips all the rest of the tests you plan to run
962and terminates the test.
963
964If you're running under no_plan, it skips once and terminates the
965test.
966
967=end _unimplemented
968
969=back
970
971
972=head2 Test style
973
974=over 4
975
976=item B<level>
977
978 $Test->level($how_high);
979
980How far up the call stack should $Test look when reporting where the
981test failed.
982
983Defaults to 1.
984
985Setting $Test::Builder::Level overrides. This is typically useful
986localized:
987
988 {
989 local $Test::Builder::Level = 2;
990 $Test->ok($test);
991 }
992
993=cut
994
995sub level {
996 my($self, $level) = @_;
997
998 if( defined $level ) {
999 $Level = $level;
1000 }
1001 return $Level;
1002}
1003
33459055 1004
1005=item B<use_numbers>
1006
1007 $Test->use_numbers($on_or_off);
1008
1009Whether or not the test should output numbers. That is, this if true:
1010
1011 ok 1
1012 ok 2
1013 ok 3
1014
1015or this if false
1016
1017 ok
1018 ok
1019 ok
1020
1021Most useful when you can't depend on the test output order, such as
1022when threads or forking is involved.
1023
33459055 1024Defaults to on.
1025
1026=cut
1027
33459055 1028sub use_numbers {
1029 my($self, $use_nums) = @_;
1030
1031 if( defined $use_nums ) {
5143c659 1032 $self->{Use_Nums} = $use_nums;
33459055 1033 }
5143c659 1034 return $self->{Use_Nums};
33459055 1035}
1036
33459055 1037
b1ddf169 1038=item B<no_diag>
33459055 1039
b1ddf169 1040 $Test->no_diag($no_diag);
1041
1042If set true no diagnostics will be printed. This includes calls to
1043diag().
33459055 1044
1045=item B<no_ending>
1046
1047 $Test->no_ending($no_ending);
1048
1049Normally, Test::Builder does some extra diagnostics when the test
30e302f8 1050ends. It also changes the exit code as described below.
33459055 1051
1052If this is true, none of that will be done.
1053
b1ddf169 1054=item B<no_header>
1055
1056 $Test->no_header($no_header);
1057
1058If set to true, no "1..N" header will be printed.
1059
33459055 1060=cut
1061
b1ddf169 1062foreach my $attribute (qw(No_Header No_Ending No_Diag)) {
1063 my $method = lc $attribute;
33459055 1064
b1ddf169 1065 my $code = sub {
1066 my($self, $no) = @_;
33459055 1067
b1ddf169 1068 if( defined $no ) {
1069 $self->{$attribute} = $no;
1070 }
1071 return $self->{$attribute};
1072 };
33459055 1073
b1ddf169 1074 no strict 'refs';
1075 *{__PACKAGE__.'::'.$method} = $code;
33459055 1076}
1077
1078
1079=back
1080
1081=head2 Output
1082
1083Controlling where the test output goes.
1084
4bd4e70a 1085It's ok for your test to change where STDOUT and STDERR point to,
71373de2 1086Test::Builder's default output settings will not be affected.
4bd4e70a 1087
33459055 1088=over 4
1089
1090=item B<diag>
1091
1092 $Test->diag(@msgs);
1093
7483b81c 1094Prints out the given @msgs. Like C<print>, arguments are simply
1095appended together.
1096
1097Normally, it uses the failure_output() handle, but if this is for a
1098TODO test, the todo_output() handle is used.
33459055 1099
71373de2 1100Output will be indented and marked with a # so as not to interfere
a9153838 1101with test output. A newline will be put on the end if there isn't one
1102already.
33459055 1103
1104We encourage using this rather than calling print directly.
1105
89c1e84a 1106Returns false. Why? Because diag() is often used in conjunction with
1107a failing test (C<ok() || diag()>) it "passes through" the failure.
1108
1109 return ok(...) || diag(...);
1110
1111=for blame transfer
1112Mark Fowler <mark@twoshortplanks.com>
1113
33459055 1114=cut
1115
1116sub diag {
1117 my($self, @msgs) = @_;
b1ddf169 1118
1119 return if $self->no_diag;
a9153838 1120 return unless @msgs;
33459055 1121
4bd4e70a 1122 # Prevent printing headers when compiling (i.e. -c)
33459055 1123 return if $^C;
1124
7483b81c 1125 # Smash args together like print does.
1126 # Convert undef to 'undef' so its readable.
1127 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1128
33459055 1129 # Escape each line with a #.
7483b81c 1130 $msg =~ s/^/# /gm;
33459055 1131
7483b81c 1132 # Stick a newline on the end if it needs it.
1133 $msg .= "\n" unless $msg =~ /\n\Z/;
a9153838 1134
33459055 1135 local $Level = $Level + 1;
7483b81c 1136 $self->_print_diag($msg);
89c1e84a 1137
1138 return 0;
33459055 1139}
1140
1141=begin _private
1142
1143=item B<_print>
1144
1145 $Test->_print(@msgs);
1146
1147Prints to the output() filehandle.
1148
1149=end _private
1150
1151=cut
1152
1153sub _print {
1154 my($self, @msgs) = @_;
1155
1156 # Prevent printing headers when only compiling. Mostly for when
1157 # tests are deparsed with B::Deparse
1158 return if $^C;
1159
7483b81c 1160 my $msg = join '', @msgs;
1161
33459055 1162 local($\, $", $,) = (undef, ' ', '');
1163 my $fh = $self->output;
89c1e84a 1164
1165 # Escape each line after the first with a # so we don't
1166 # confuse Test::Harness.
7483b81c 1167 $msg =~ s/\n(.)/\n# $1/sg;
89c1e84a 1168
7483b81c 1169 # Stick a newline on the end if it needs it.
1170 $msg .= "\n" unless $msg =~ /\n\Z/;
89c1e84a 1171
7483b81c 1172 print $fh $msg;
33459055 1173}
1174
b7f9bbeb 1175=begin private
33459055 1176
30e302f8 1177=item B<_print_diag>
1178
1179 $Test->_print_diag(@msg);
1180
1181Like _print, but prints to the current diagnostic filehandle.
1182
b7f9bbeb 1183=end private
1184
30e302f8 1185=cut
1186
1187sub _print_diag {
1188 my $self = shift;
1189
1190 local($\, $", $,) = (undef, ' ', '');
1191 my $fh = $self->todo ? $self->todo_output : $self->failure_output;
1192 print $fh @_;
1193}
1194
33459055 1195=item B<output>
1196
1197 $Test->output($fh);
1198 $Test->output($file);
1199
1200Where normal "ok/not ok" test output should go.
1201
1202Defaults to STDOUT.
1203
1204=item B<failure_output>
1205
1206 $Test->failure_output($fh);
1207 $Test->failure_output($file);
1208
1209Where diagnostic output on test failures and diag() should go.
1210
1211Defaults to STDERR.
1212
1213=item B<todo_output>
1214
1215 $Test->todo_output($fh);
1216 $Test->todo_output($file);
1217
1218Where diagnostics about todo test failures and diag() should go.
1219
1220Defaults to STDOUT.
1221
1222=cut
1223
33459055 1224sub output {
1225 my($self, $fh) = @_;
1226
1227 if( defined $fh ) {
b7f9bbeb 1228 $self->{Out_FH} = $self->_new_fh($fh);
33459055 1229 }
5143c659 1230 return $self->{Out_FH};
33459055 1231}
1232
1233sub failure_output {
1234 my($self, $fh) = @_;
1235
1236 if( defined $fh ) {
b7f9bbeb 1237 $self->{Fail_FH} = $self->_new_fh($fh);
33459055 1238 }
5143c659 1239 return $self->{Fail_FH};
33459055 1240}
1241
1242sub todo_output {
1243 my($self, $fh) = @_;
1244
1245 if( defined $fh ) {
b7f9bbeb 1246 $self->{Todo_FH} = $self->_new_fh($fh);
33459055 1247 }
5143c659 1248 return $self->{Todo_FH};
33459055 1249}
1250
0257f296 1251
33459055 1252sub _new_fh {
b7f9bbeb 1253 my $self = shift;
33459055 1254 my($file_or_fh) = shift;
1255
1256 my $fh;
b7f9bbeb 1257 if( $self->_is_fh($file_or_fh) ) {
0257f296 1258 $fh = $file_or_fh;
1259 }
1260 else {
33459055 1261 $fh = do { local *FH };
b7f9bbeb 1262 open $fh, ">$file_or_fh" or
1263 $self->croak("Can't open test output log $file_or_fh: $!");
5143c659 1264 _autoflush($fh);
33459055 1265 }
33459055 1266
1267 return $fh;
1268}
1269
0257f296 1270
1271sub _is_fh {
b7f9bbeb 1272 my $self = shift;
0257f296 1273 my $maybe_fh = shift;
b1ddf169 1274 return 0 unless defined $maybe_fh;
0257f296 1275
1276 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
1277
1278 return UNIVERSAL::isa($maybe_fh, 'GLOB') ||
1279 UNIVERSAL::isa($maybe_fh, 'IO::Handle') ||
1280
1281 # 5.5.4's tied() and can() doesn't like getting undef
1282 UNIVERSAL::can((tied($maybe_fh) || ''), 'TIEHANDLE');
1283}
1284
1285
30e302f8 1286sub _autoflush {
1287 my($fh) = shift;
1288 my $old_fh = select $fh;
1289 $| = 1;
1290 select $old_fh;
1291}
1292
1293
30e302f8 1294sub _dup_stdhandles {
1295 my $self = shift;
1296
5143c659 1297 $self->_open_testhandles;
a9153838 1298
1299 # Set everything to unbuffered else plain prints to STDOUT will
1300 # come out in the wrong order from our own prints.
33459055 1301 _autoflush(\*TESTOUT);
a9153838 1302 _autoflush(\*STDOUT);
33459055 1303 _autoflush(\*TESTERR);
a9153838 1304 _autoflush(\*STDERR);
1305
5143c659 1306 $self->output(\*TESTOUT);
1307 $self->failure_output(\*TESTERR);
1308 $self->todo_output(\*TESTOUT);
33459055 1309}
1310
5143c659 1311
1312my $Opened_Testhandles = 0;
30e302f8 1313sub _open_testhandles {
5143c659 1314 return if $Opened_Testhandles;
30e302f8 1315 # We dup STDOUT and STDERR so people can change them in their
1316 # test suites while still getting normal test output.
1317 open(TESTOUT, ">&STDOUT") or die "Can't dup STDOUT: $!";
1318 open(TESTERR, ">&STDERR") or die "Can't dup STDERR: $!";
1319 $Opened_Testhandles = 1;
33459055 1320}
1321
1322
b7f9bbeb 1323=item carp
1324
1325 $tb->carp(@message);
1326
1327Warns with C<@message> but the message will appear to come from the
1328point where the original test function was called (C<$tb->caller>).
1329
1330=item croak
1331
1332 $tb->croak(@message);
1333
1334Dies with C<@message> but the message will appear to come from the
1335point where the original test function was called (C<$tb->caller>).
1336
1337=cut
1338
1339sub _message_at_caller {
1340 my $self = shift;
1341
004caa16 1342 local $Level = $Level + 1;
b7f9bbeb 1343 my($pack, $file, $line) = $self->caller;
1344 return join("", @_) . " at $file line $line.\n";
1345}
1346
1347sub carp {
1348 my $self = shift;
1349 warn $self->_message_at_caller(@_);
1350}
1351
1352sub croak {
1353 my $self = shift;
1354 die $self->_message_at_caller(@_);
1355}
1356
1357sub _plan_check {
1358 my $self = shift;
1359
1360 unless( $self->{Have_Plan} ) {
004caa16 1361 local $Level = $Level + 2;
b7f9bbeb 1362 $self->croak("You tried to run a test without a plan");
1363 }
1364}
1365
33459055 1366=back
1367
1368
1369=head2 Test Status and Info
1370
1371=over 4
1372
1373=item B<current_test>
1374
1375 my $curr_test = $Test->current_test;
1376 $Test->current_test($num);
1377
0257f296 1378Gets/sets the current test number we're on. You usually shouldn't
1379have to set this.
33459055 1380
0257f296 1381If set forward, the details of the missing tests are filled in as 'unknown'.
1382if set backward, the details of the intervening tests are deleted. You
1383can erase history if you really want to.
33459055 1384
1385=cut
1386
1387sub current_test {
1388 my($self, $num) = @_;
1389
5143c659 1390 lock($self->{Curr_Test});
33459055 1391 if( defined $num ) {
5143c659 1392 unless( $self->{Have_Plan} ) {
b7f9bbeb 1393 $self->croak("Can't change the current test number without a plan!");
89c1e84a 1394 }
1395
5143c659 1396 $self->{Curr_Test} = $num;
0257f296 1397
1398 # If the test counter is being pushed forward fill in the details.
5143c659 1399 my $test_results = $self->{Test_Results};
1400 if( $num > @$test_results ) {
1401 my $start = @$test_results ? @$test_results : 0;
89c1e84a 1402 for ($start..$num-1) {
5143c659 1403 $test_results->[$_] = &share({
30e302f8 1404 'ok' => 1,
1405 actual_ok => undef,
1406 reason => 'incrementing test number',
1407 type => 'unknown',
1408 name => undef
1409 });
6686786d 1410 }
1411 }
0257f296 1412 # If backward, wipe history. Its their funeral.
5143c659 1413 elsif( $num < @$test_results ) {
1414 $#{$test_results} = $num - 1;
0257f296 1415 }
33459055 1416 }
5143c659 1417 return $self->{Curr_Test};
33459055 1418}
1419
1420
1421=item B<summary>
1422
1423 my @tests = $Test->summary;
1424
1425A simple summary of the tests so far. True for pass, false for fail.
1426This is a logical pass/fail, so todos are passes.
1427
1428Of course, test #1 is $tests[0], etc...
1429
1430=cut
1431
1432sub summary {
1433 my($self) = shift;
1434
5143c659 1435 return map { $_->{'ok'} } @{ $self->{Test_Results} };
33459055 1436}
1437
60ffb308 1438=item B<details>
33459055 1439
1440 my @tests = $Test->details;
1441
1442Like summary(), but with a lot more detail.
1443
1444 $tests[$test_num - 1] =
60ffb308 1445 { 'ok' => is the test considered a pass?
33459055 1446 actual_ok => did it literally say 'ok'?
1447 name => name of the test (if any)
60ffb308 1448 type => type of test (if any, see below).
33459055 1449 reason => reason for the above (if any)
1450 };
1451
60ffb308 1452'ok' is true if Test::Harness will consider the test to be a pass.
1453
1454'actual_ok' is a reflection of whether or not the test literally
1455printed 'ok' or 'not ok'. This is for examining the result of 'todo'
1456tests.
1457
1458'name' is the name of the test.
1459
1460'type' indicates if it was a special test. Normal tests have a type
1461of ''. Type can be one of the following:
1462
1463 skip see skip()
1464 todo see todo()
1465 todo_skip see todo_skip()
1466 unknown see below
1467
1468Sometimes the Test::Builder test counter is incremented without it
1469printing any test output, for example, when current_test() is changed.
1470In these cases, Test::Builder doesn't know the result of the test, so
1471it's type is 'unkown'. These details for these tests are filled in.
1472They are considered ok, but the name and actual_ok is left undef.
1473
1474For example "not ok 23 - hole count # TODO insufficient donuts" would
1475result in this structure:
1476
1477 $tests[22] = # 23 - 1, since arrays start from 0.
1478 { ok => 1, # logically, the test passed since it's todo
1479 actual_ok => 0, # in absolute terms, it failed
1480 name => 'hole count',
1481 type => 'todo',
1482 reason => 'insufficient donuts'
1483 };
1484
1485=cut
1486
1487sub details {
5143c659 1488 my $self = shift;
1489 return @{ $self->{Test_Results} };
60ffb308 1490}
1491
33459055 1492=item B<todo>
1493
1494 my $todo_reason = $Test->todo;
1495 my $todo_reason = $Test->todo($pack);
1496
1497todo() looks for a $TODO variable in your tests. If set, all tests
1498will be considered 'todo' (see Test::More and Test::Harness for
1499details). Returns the reason (ie. the value of $TODO) if running as
1500todo tests, false otherwise.
1501
5143c659 1502todo() is about finding the right package to look for $TODO in. It
1503uses the exported_to() package to find it. If that's not set, it's
1504pretty good at guessing the right package to look at based on $Level.
33459055 1505
1506Sometimes there is some confusion about where todo() should be looking
1507for the $TODO variable. If you want to be sure, tell it explicitly
1508what $pack to use.
1509
1510=cut
1511
1512sub todo {
1513 my($self, $pack) = @_;
1514
5143c659 1515 $pack = $pack || $self->exported_to || $self->caller($Level);
1516 return 0 unless $pack;
33459055 1517
1518 no strict 'refs';
1519 return defined ${$pack.'::TODO'} ? ${$pack.'::TODO'}
1520 : 0;
1521}
1522
1523=item B<caller>
1524
1525 my $package = $Test->caller;
1526 my($pack, $file, $line) = $Test->caller;
1527 my($pack, $file, $line) = $Test->caller($height);
1528
1529Like the normal caller(), except it reports according to your level().
1530
1531=cut
1532
1533sub caller {
1534 my($self, $height) = @_;
1535 $height ||= 0;
a344be10 1536
33459055 1537 my @caller = CORE::caller($self->level + $height + 1);
1538 return wantarray ? @caller : $caller[0];
1539}
1540
1541=back
1542
1543=cut
1544
1545=begin _private
1546
1547=over 4
1548
1549=item B<_sanity_check>
1550
5143c659 1551 $self->_sanity_check();
33459055 1552
1553Runs a bunch of end of test sanity checks to make sure reality came
1554through ok. If anything is wrong it will die with a fairly friendly
1555error message.
1556
1557=cut
1558
1559#'#
1560sub _sanity_check {
5143c659 1561 my $self = shift;
1562
b7f9bbeb 1563 $self->_whoa($self->{Curr_Test} < 0, 'Says here you ran a negative number of tests!');
1564 $self->_whoa(!$self->{Have_Plan} and $self->{Curr_Test},
33459055 1565 'Somehow your tests ran without a plan!');
b7f9bbeb 1566 $self->_whoa($self->{Curr_Test} != @{ $self->{Test_Results} },
33459055 1567 'Somehow you got a different number of results than tests ran!');
1568}
1569
1570=item B<_whoa>
1571
b7f9bbeb 1572 $self->_whoa($check, $description);
33459055 1573
1574A sanity check, similar to assert(). If the $check is true, something
1575has gone horribly wrong. It will die with the given $description and
1576a note to contact the author.
1577
1578=cut
1579
1580sub _whoa {
b7f9bbeb 1581 my($self, $check, $desc) = @_;
33459055 1582 if( $check ) {
b7f9bbeb 1583 local $Level = $Level + 1;
1584 $self->croak(<<"WHOA");
33459055 1585WHOA! $desc
1586This should never happen! Please contact the author immediately!
1587WHOA
1588 }
1589}
1590
1591=item B<_my_exit>
1592
1593 _my_exit($exit_num);
1594
1595Perl seems to have some trouble with exiting inside an END block. 5.005_03
1596and 5.6.1 both seem to do odd things. Instead, this function edits $?
1597directly. It should ONLY be called from inside an END block. It
1598doesn't actually exit, that's your job.
1599
1600=cut
1601
1602sub _my_exit {
1603 $? = $_[0];
1604
1605 return 1;
1606}
1607
1608
1609=back
1610
1611=end _private
1612
1613=cut
1614
1615$SIG{__DIE__} = sub {
1616 # We don't want to muck with death in an eval, but $^S isn't
1617 # totally reliable. 5.005_03 and 5.6.1 both do the wrong thing
1618 # with it. Instead, we use caller. This also means it runs under
1619 # 5.004!
1620 my $in_eval = 0;
1621 for( my $stack = 1; my $sub = (CORE::caller($stack))[3]; $stack++ ) {
1622 $in_eval = 1 if $sub =~ /^\(eval\)/;
1623 }
5143c659 1624 $Test->{Test_Died} = 1 unless $in_eval;
33459055 1625};
1626
1627sub _ending {
1628 my $self = shift;
1629
5143c659 1630 $self->_sanity_check();
33459055 1631
60ffb308 1632 # Don't bother with an ending if this is a forked copy. Only the parent
1633 # should do the ending.
5143c659 1634 # Exit if plan() was never called. This is so "require Test::Simple"
1635 # doesn't puke.
b1ddf169 1636 # Don't do an ending if we bailed out.
1637 if( ($self->{Original_Pid} != $$) or
1638 (!$self->{Have_Plan} && !$self->{Test_Died}) or
1639 $self->{Bailed_Out}
1640 )
5143c659 1641 {
1642 _my_exit($?);
1643 return;
1644 }
33459055 1645
1646 # Figure out if we passed or failed and print helpful messages.
5143c659 1647 my $test_results = $self->{Test_Results};
1648 if( @$test_results ) {
33459055 1649 # The plan? We have no plan.
5143c659 1650 if( $self->{No_Plan} ) {
1651 $self->_print("1..$self->{Curr_Test}\n") unless $self->no_header;
1652 $self->{Expected_Tests} = $self->{Curr_Test};
33459055 1653 }
1654
30e302f8 1655 # Auto-extended arrays and elements which aren't explicitly
1656 # filled in with a shared reference will puke under 5.8.0
1657 # ithreads. So we have to fill them in by hand. :(
1658 my $empty_result = &share({});
5143c659 1659 for my $idx ( 0..$self->{Expected_Tests}-1 ) {
1660 $test_results->[$idx] = $empty_result
1661 unless defined $test_results->[$idx];
60ffb308 1662 }
a344be10 1663
5143c659 1664 my $num_failed = grep !$_->{'ok'},
b1ddf169 1665 @{$test_results}[0..$self->{Curr_Test}-1];
33459055 1666
b1ddf169 1667 my $num_extra = $self->{Curr_Test} - $self->{Expected_Tests};
1668
1669 if( $num_extra < 0 ) {
5143c659 1670 my $s = $self->{Expected_Tests} == 1 ? '' : 's';
33459055 1671 $self->diag(<<"FAIL");
5143c659 1672Looks like you planned $self->{Expected_Tests} test$s but only ran $self->{Curr_Test}.
33459055 1673FAIL
1674 }
b1ddf169 1675 elsif( $num_extra > 0 ) {
5143c659 1676 my $s = $self->{Expected_Tests} == 1 ? '' : 's';
33459055 1677 $self->diag(<<"FAIL");
5143c659 1678Looks like you planned $self->{Expected_Tests} test$s but ran $num_extra extra.
33459055 1679FAIL
1680 }
b1ddf169 1681
1682 if ( $num_failed ) {
1683 my $num_tests = $self->{Curr_Test};
30e302f8 1684 my $s = $num_failed == 1 ? '' : 's';
b1ddf169 1685
1686 my $qualifier = $num_extra == 0 ? '' : ' run';
1687
33459055 1688 $self->diag(<<"FAIL");
b1ddf169 1689Looks like you failed $num_failed test$s of $num_tests$qualifier.
33459055 1690FAIL
1691 }
1692
5143c659 1693 if( $self->{Test_Died} ) {
33459055 1694 $self->diag(<<"FAIL");
5143c659 1695Looks like your test died just after $self->{Curr_Test}.
33459055 1696FAIL
1697
1698 _my_exit( 255 ) && return;
1699 }
1700
b1ddf169 1701 my $exit_code;
1702 if( $num_failed ) {
1703 $exit_code = $num_failed <= 254 ? $num_failed : 254;
1704 }
1705 elsif( $num_extra != 0 ) {
1706 $exit_code = 255;
1707 }
1708 else {
1709 $exit_code = 0;
1710 }
1711
1712 _my_exit( $exit_code ) && return;
33459055 1713 }
5143c659 1714 elsif ( $self->{Skip_All} ) {
33459055 1715 _my_exit( 0 ) && return;
1716 }
5143c659 1717 elsif ( $self->{Test_Died} ) {
60ffb308 1718 $self->diag(<<'FAIL');
1719Looks like your test died before it could output anything.
1720FAIL
30e302f8 1721 _my_exit( 255 ) && return;
60ffb308 1722 }
33459055 1723 else {
a9153838 1724 $self->diag("No tests run!\n");
33459055 1725 _my_exit( 255 ) && return;
1726 }
1727}
1728
1729END {
1730 $Test->_ending if defined $Test and !$Test->no_ending;
1731}
1732
30e302f8 1733=head1 EXIT CODES
1734
1735If all your tests passed, Test::Builder will exit with zero (which is
1736normal). If anything failed it will exit with how many failed. If
1737you run less (or more) tests than you planned, the missing (or extras)
1738will be considered failures. If no tests were ever run Test::Builder
1739will throw a warning and exit with 255. If the test died, even after
1740having successfully completed all its tests, it will still be
1741considered a failure and will exit with 255.
1742
1743So the exit codes are...
1744
1745 0 all tests successful
b1ddf169 1746 255 test died or all passed but wrong # of tests run
30e302f8 1747 any other number how many failed (including missing or extras)
1748
1749If you fail more than 254 tests, it will be reported as 254.
1750
1751
a344be10 1752=head1 THREADS
1753
b7f9bbeb 1754In perl 5.8.1 and later, Test::Builder is thread-safe. The test
a344be10 1755number is shared amongst all threads. This means if one thread sets
1756the test number using current_test() they will all be effected.
1757
b7f9bbeb 1758While versions earlier than 5.8.1 had threads they contain too many
1759bugs to support.
1760
30e302f8 1761Test::Builder is only thread-aware if threads.pm is loaded I<before>
1762Test::Builder.
1763
33459055 1764=head1 EXAMPLES
1765
a344be10 1766CPAN can provide the best examples. Test::Simple, Test::More,
1767Test::Exception and Test::Differences all use Test::Builder.
33459055 1768
4bd4e70a 1769=head1 SEE ALSO
1770
1771Test::Simple, Test::More, Test::Harness
1772
1773=head1 AUTHORS
33459055 1774
1775Original code by chromatic, maintained by Michael G Schwern
1776E<lt>schwern@pobox.comE<gt>
1777
4bd4e70a 1778=head1 COPYRIGHT
33459055 1779
7483b81c 1780Copyright 2002, 2004 by chromatic E<lt>chromatic@wgz.orgE<gt> and
1781 Michael G Schwern E<lt>schwern@pobox.comE<gt>.
4bd4e70a 1782
1783This program is free software; you can redistribute it and/or
1784modify it under the same terms as Perl itself.
1785
a9153838 1786See F<http://www.perl.com/perl/misc/Artistic.html>
33459055 1787
1788=cut
1789
17901;