5 # $^C was only introduced in 5.005-ish. We do this to prevent
6 # use of uninitialized value warnings in older perls.
10 use vars qw($VERSION);
12 $VERSION = eval $VERSION; # make the alpha version come out as a number
14 # Make Test::Builder thread-safe for ithreads.
17 # Load threads::shared when threads are turned on
18 if( $] >= 5.008 && $Config{useithreads} && $INC{'threads.pm'}) {
19 require threads::shared;
21 # Hack around YET ANOTHER threads::shared bug. It would
22 # occassionally forget the contents of the variable when sharing it.
23 # So we first copy the data, then share, then put our copy back.
24 *share = sub (\[$@%]) {
28 if( $type eq 'HASH' ) {
31 elsif( $type eq 'ARRAY' ) {
34 elsif( $type eq 'SCALAR' ) {
38 die "Unknown type: ".$type;
41 $_[0] = &threads::shared::share($_[0]);
43 if( $type eq 'HASH' ) {
46 elsif( $type eq 'ARRAY' ) {
49 elsif( $type eq 'SCALAR' ) {
53 die "Unknown type: ".$type;
59 # 5.8.0's threads::shared is busted when threads are off.
62 *share = sub { return $_[0] };
70 Test::Builder - Backend for building test libraries
74 package My::Test::Module;
80 my $Test = Test::Builder->new;
81 $Test->output('my_logfile');
87 $Test->exported_to($pack);
90 $self->export_to_level(1, $self, 'ok');
94 my($test, $name) = @_;
96 $Test->ok($test, $name);
102 Test::Simple and Test::More have proven to be popular testing modules,
103 but they're not always flexible enough. Test::Builder provides the a
104 building block upon which to write your own test libraries I<which can
113 my $Test = Test::Builder->new;
115 Returns a Test::Builder object representing the current state of the
118 Since you only run one test per program C<new> always returns the same
119 Test::Builder object. No matter how many times you call new(), you're
120 getting the same object. This is called a singleton. This is done so that
121 multiple modules share such global information as the test counter and
122 where test output is going.
124 If you want a completely new Test::Builder object different from the
125 singleton, use C<create>.
129 my $Test = Test::Builder->new;
132 $Test ||= $class->create;
139 my $Test = Test::Builder->create;
141 Ok, so there can be more than one Test::Builder object and this is how
142 you get it. You might use this instead of C<new()> if you're testing
143 a Test::Builder based module, but otherwise you probably want C<new>.
145 B<NOTE>: the implementation is not complete. C<level>, for example, is
146 still shared amongst B<all> Test::Builder objects, even ones created using
147 this method. Also, the method name may change in the future.
154 my $self = bless {}, $class;
164 Reinitializes the Test::Builder singleton to its original state.
165 Mostly useful for tests run in persistent environments where the same
166 test might be run multiple times in the same process.
175 # We leave this a global because it has to be localized and localizing
176 # hash keys is just asking for pain. Also, it was documented.
179 $self->{Test_Died} = 0;
180 $self->{Have_Plan} = 0;
181 $self->{No_Plan} = 0;
182 $self->{Original_Pid} = $$;
184 share($self->{Curr_Test});
185 $self->{Curr_Test} = 0;
186 $self->{Test_Results} = &share([]);
188 $self->{Exported_To} = undef;
189 $self->{Expected_Tests} = 0;
191 $self->{Skip_All} = 0;
193 $self->{Use_Nums} = 1;
195 $self->{No_Header} = 0;
196 $self->{No_Ending} = 0;
198 $self->_dup_stdhandles unless $^C;
205 =head2 Setting up tests
207 These methods are for setting up tests and declaring how many there
208 are. You usually only want to call one of these methods.
214 my $pack = $Test->exported_to;
215 $Test->exported_to($pack);
217 Tells Test::Builder what package you exported your functions to.
218 This is important for getting TODO tests right.
223 my($self, $pack) = @_;
225 if( defined $pack ) {
226 $self->{Exported_To} = $pack;
228 return $self->{Exported_To};
233 $Test->plan('no_plan');
234 $Test->plan( skip_all => $reason );
235 $Test->plan( tests => $num_tests );
237 A convenient way to set up your tests. Call this and Test::Builder
238 will print the appropriate headers and take the appropriate actions.
240 If you call plan(), don't call any of the other methods below.
245 my($self, $cmd, $arg) = @_;
249 if( $self->{Have_Plan} ) {
250 die sprintf "You tried to plan twice! Second plan at %s line %d\n",
251 ($self->caller)[1,2];
254 if( $cmd eq 'no_plan' ) {
257 elsif( $cmd eq 'skip_all' ) {
258 return $self->skip_all($arg);
260 elsif( $cmd eq 'tests' ) {
262 return $self->expected_tests($arg);
264 elsif( !defined $arg ) {
265 die "Got an undefined number of tests. Looks like you tried to ".
266 "say how many tests you plan to run but made a mistake.\n";
269 die "You said to run 0 tests! You've got to run something.\n";
274 my @args = grep { defined } ($cmd, $arg);
275 Carp::croak("plan() doesn't understand @args");
281 =item B<expected_tests>
283 my $max = $Test->expected_tests;
284 $Test->expected_tests($max);
286 Gets/sets the # of tests we expect this test to run and prints out
287 the appropriate headers.
296 die "Number of tests must be a postive integer. You gave it '$max'.\n"
297 unless $max =~ /^\+?\d+$/ and $max > 0;
299 $self->{Expected_Tests} = $max;
300 $self->{Have_Plan} = 1;
302 $self->_print("1..$max\n") unless $self->no_header;
304 return $self->{Expected_Tests};
312 Declares that this test will run an indeterminate # of tests.
319 $self->{No_Plan} = 1;
320 $self->{Have_Plan} = 1;
325 $plan = $Test->has_plan
327 Find out whether a plan has been defined. $plan is either C<undef> (no plan has been set), C<no_plan> (indeterminate # of tests) or an integer (the number of expected tests).
334 return($self->{Expected_Tests}) if $self->{Expected_Tests};
335 return('no_plan') if $self->{No_Plan};
343 $Test->skip_all($reason);
345 Skips all the tests, using the given $reason. Exits immediately with 0.
350 my($self, $reason) = @_;
353 $out .= " # Skip $reason" if $reason;
356 $self->{Skip_All} = 1;
358 $self->_print($out) unless $self->no_header;
366 These actually run the tests, analogous to the functions in
369 $name is always optional.
375 $Test->ok($test, $name);
377 Your basic test. Pass if $test is true, fail if $test is false. Just
378 like Test::Simple's ok().
383 my($self, $test, $name) = @_;
385 # $test might contain an object which we don't want to accidentally
386 # store, so we turn it into a boolean.
387 $test = $test ? 1 : 0;
389 unless( $self->{Have_Plan} ) {
391 Carp::croak("You tried to run a test without a plan! Gotta have a plan.");
394 lock $self->{Curr_Test};
395 $self->{Curr_Test}++;
397 # In case $name is a string overloaded object, force it to stringify.
398 $self->_unoverload(\$name);
400 $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
401 You named your test '$name'. You shouldn't use numbers for your test names.
405 my($pack, $file, $line) = $self->caller;
407 my $todo = $self->todo($pack);
408 $self->_unoverload(\$todo);
411 my $result = &share({});
415 @$result{ 'ok', 'actual_ok' } = ( ( $todo ? 1 : 0 ), 0 );
418 @$result{ 'ok', 'actual_ok' } = ( 1, $test );
422 $out .= " $self->{Curr_Test}" if $self->use_numbers;
424 if( defined $name ) {
425 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
427 $result->{name} = $name;
430 $result->{name} = '';
434 $out .= " # TODO $todo";
435 $result->{reason} = $todo;
436 $result->{type} = 'todo';
439 $result->{reason} = '';
440 $result->{type} = '';
443 $self->{Test_Results}[$self->{Curr_Test}-1] = $result;
449 my $msg = $todo ? "Failed (TODO)" : "Failed";
450 $self->_print_diag("\n") if $ENV{HARNESS_ACTIVE};
451 $self->diag(" $msg test ($file at line $line)\n");
454 return $test ? 1 : 0;
463 eval { require overload } || return;
465 foreach my $thing (@_) {
467 if( defined $$thing ) {
468 if( my $string_meth = overload::Method($$thing, '""') ) {
469 $$thing = $$thing->$string_meth();
479 $Test->is_eq($got, $expected, $name);
481 Like Test::More's is(). Checks if $got eq $expected. This is the
486 $Test->is_num($got, $expected, $name);
488 Like Test::More's is(). Checks if $got == $expected. This is the
494 my($self, $got, $expect, $name) = @_;
495 local $Level = $Level + 1;
497 if( !defined $got || !defined $expect ) {
498 # undef only matches undef and nothing else
499 my $test = !defined $got && !defined $expect;
501 $self->ok($test, $name);
502 $self->_is_diag($got, 'eq', $expect) unless $test;
506 return $self->cmp_ok($got, 'eq', $expect, $name);
510 my($self, $got, $expect, $name) = @_;
511 local $Level = $Level + 1;
513 if( !defined $got || !defined $expect ) {
514 # undef only matches undef and nothing else
515 my $test = !defined $got && !defined $expect;
517 $self->ok($test, $name);
518 $self->_is_diag($got, '==', $expect) unless $test;
522 return $self->cmp_ok($got, '==', $expect, $name);
526 my($self, $got, $type, $expect) = @_;
528 foreach my $val (\$got, \$expect) {
529 if( defined $$val ) {
530 if( $type eq 'eq' ) {
531 # quote and force string context
535 # force numeric context
544 return $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
553 $Test->isnt_eq($got, $dont_expect, $name);
555 Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
560 $Test->is_num($got, $dont_expect, $name);
562 Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
568 my($self, $got, $dont_expect, $name) = @_;
569 local $Level = $Level + 1;
571 if( !defined $got || !defined $dont_expect ) {
572 # undef only matches undef and nothing else
573 my $test = defined $got || defined $dont_expect;
575 $self->ok($test, $name);
576 $self->_cmp_diag($got, 'ne', $dont_expect) unless $test;
580 return $self->cmp_ok($got, 'ne', $dont_expect, $name);
584 my($self, $got, $dont_expect, $name) = @_;
585 local $Level = $Level + 1;
587 if( !defined $got || !defined $dont_expect ) {
588 # undef only matches undef and nothing else
589 my $test = defined $got || defined $dont_expect;
591 $self->ok($test, $name);
592 $self->_cmp_diag($got, '!=', $dont_expect) unless $test;
596 return $self->cmp_ok($got, '!=', $dont_expect, $name);
602 $Test->like($this, qr/$regex/, $name);
603 $Test->like($this, '/$regex/', $name);
605 Like Test::More's like(). Checks if $this matches the given $regex.
607 You'll want to avoid qr// if you want your tests to work before 5.005.
611 $Test->unlike($this, qr/$regex/, $name);
612 $Test->unlike($this, '/$regex/', $name);
614 Like Test::More's unlike(). Checks if $this B<does not match> the
620 my($self, $this, $regex, $name) = @_;
622 local $Level = $Level + 1;
623 $self->_regex_ok($this, $regex, '=~', $name);
627 my($self, $this, $regex, $name) = @_;
629 local $Level = $Level + 1;
630 $self->_regex_ok($this, $regex, '!~', $name);
635 $Test->maybe_regex(qr/$regex/);
636 $Test->maybe_regex('/$regex/');
638 Convenience method for building testing functions that take regular
639 expressions as arguments, but need to work before perl 5.005.
641 Takes a quoted regular expression produced by qr//, or a string
642 representing a regular expression.
644 Returns a Perl value which may be used instead of the corresponding
645 regular expression, or undef if it's argument is not recognised.
647 For example, a version of like(), sans the useful diagnostic messages,
651 my ($self, $this, $regex, $name) = @_;
652 my $usable_regex = $self->maybe_regex($regex);
653 die "expecting regex, found '$regex'\n"
654 unless $usable_regex;
655 $self->ok($this =~ m/$usable_regex/, $name);
662 my ($self, $regex) = @_;
663 my $usable_regex = undef;
665 return $usable_regex unless defined $regex;
670 if( ref $regex eq 'Regexp' ) {
671 $usable_regex = $regex;
673 # Check for '/foo/' or 'm,foo,'
674 elsif( ($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
675 (undef, $re, $opts) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
678 $usable_regex = length $opts ? "(?$opts)$re" : $re;
681 return $usable_regex;
685 my($self, $this, $regex, $cmp, $name) = @_;
687 local $Level = $Level + 1;
690 my $usable_regex = $self->maybe_regex($regex);
691 unless (defined $usable_regex) {
692 $ok = $self->ok( 0, $name );
693 $self->diag(" '$regex' doesn't look much like a regex to me.");
699 my $test = $this =~ /$usable_regex/ ? 1 : 0;
700 $test = !$test if $cmp eq '!~';
701 $ok = $self->ok( $test, $name );
705 $this = defined $this ? "'$this'" : 'undef';
706 my $match = $cmp eq '=~' ? "doesn't match" : "matches";
707 $self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex);
719 $Test->cmp_ok($this, $type, $that, $name);
721 Works just like Test::More's cmp_ok().
723 $Test->cmp_ok($big_num, '!=', $other_big_num);
728 my($self, $got, $type, $expect, $name) = @_;
733 local($@,$!); # don't interfere with $@
734 # eval() sometimes resets $!
735 $test = eval "\$got $type \$expect";
737 local $Level = $Level + 1;
738 my $ok = $self->ok($test, $name);
741 if( $type =~ /^(eq|==)$/ ) {
742 $self->_is_diag($got, $type, $expect);
745 $self->_cmp_diag($got, $type, $expect);
752 my($self, $got, $type, $expect) = @_;
754 $got = defined $got ? "'$got'" : 'undef';
755 $expect = defined $expect ? "'$expect'" : 'undef';
756 return $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect);
765 $Test->BAILOUT($reason);
767 Indicates to the Test::Harness that things are going so badly all
768 testing should terminate. This includes running any additional test
771 It will exit with 255.
776 my($self, $reason) = @_;
778 $self->_print("Bail out! $reason");
787 Skips the current test, reporting $why.
792 my($self, $why) = @_;
794 $self->_unoverload(\$why);
796 unless( $self->{Have_Plan} ) {
798 Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
801 lock($self->{Curr_Test});
802 $self->{Curr_Test}++;
804 $self->{Test_Results}[$self->{Curr_Test}-1] = &share({
813 $out .= " $self->{Curr_Test}" if $self->use_numbers;
815 $out .= " $why" if length $why;
827 $Test->todo_skip($why);
829 Like skip(), only it will declare the test as failing and TODO. Similar
832 print "not ok $tnum # TODO $why\n";
837 my($self, $why) = @_;
840 unless( $self->{Have_Plan} ) {
842 Carp::croak("You tried to run tests without a plan! Gotta have a plan.");
845 lock($self->{Curr_Test});
846 $self->{Curr_Test}++;
848 $self->{Test_Results}[$self->{Curr_Test}-1] = &share({
857 $out .= " $self->{Curr_Test}" if $self->use_numbers;
858 $out .= " # TODO & SKIP $why\n";
866 =begin _unimplemented
871 $Test->skip_rest($reason);
873 Like skip(), only it skips all the rest of the tests you plan to run
874 and terminates the test.
876 If you're running under no_plan, it skips once and terminates the
890 $Test->level($how_high);
892 How far up the call stack should $Test look when reporting where the
897 Setting $Test::Builder::Level overrides. This is typically useful
901 local $Test::Builder::Level = 2;
908 my($self, $level) = @_;
910 if( defined $level ) {
919 $Test->use_numbers($on_or_off);
921 Whether or not the test should output numbers. That is, this if true:
933 Most useful when you can't depend on the test output order, such as
934 when threads or forking is involved.
936 Test::Harness will accept either, but avoid mixing the two styles.
943 my($self, $use_nums) = @_;
945 if( defined $use_nums ) {
946 $self->{Use_Nums} = $use_nums;
948 return $self->{Use_Nums};
953 $Test->no_header($no_header);
955 If set to true, no "1..N" header will be printed.
959 $Test->no_ending($no_ending);
961 Normally, Test::Builder does some extra diagnostics when the test
962 ends. It also changes the exit code as described below.
964 If this is true, none of that will be done.
969 my($self, $no_header) = @_;
971 if( defined $no_header ) {
972 $self->{No_Header} = $no_header;
974 return $self->{No_Header};
978 my($self, $no_ending) = @_;
980 if( defined $no_ending ) {
981 $self->{No_Ending} = $no_ending;
983 return $self->{No_Ending};
991 Controlling where the test output goes.
993 It's ok for your test to change where STDOUT and STDERR point to,
994 Test::Builder's default output settings will not be affected.
1002 Prints out the given @msgs. Like C<print>, arguments are simply
1005 Normally, it uses the failure_output() handle, but if this is for a
1006 TODO test, the todo_output() handle is used.
1008 Output will be indented and marked with a # so as not to interfere
1009 with test output. A newline will be put on the end if there isn't one
1012 We encourage using this rather than calling print directly.
1014 Returns false. Why? Because diag() is often used in conjunction with
1015 a failing test (C<ok() || diag()>) it "passes through" the failure.
1017 return ok(...) || diag(...);
1020 Mark Fowler <mark@twoshortplanks.com>
1025 my($self, @msgs) = @_;
1026 return unless @msgs;
1028 # Prevent printing headers when compiling (i.e. -c)
1031 # Smash args together like print does.
1032 # Convert undef to 'undef' so its readable.
1033 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1035 # Escape each line with a #.
1038 # Stick a newline on the end if it needs it.
1039 $msg .= "\n" unless $msg =~ /\n\Z/;
1041 local $Level = $Level + 1;
1042 $self->_print_diag($msg);
1051 $Test->_print(@msgs);
1053 Prints to the output() filehandle.
1060 my($self, @msgs) = @_;
1062 # Prevent printing headers when only compiling. Mostly for when
1063 # tests are deparsed with B::Deparse
1066 my $msg = join '', @msgs;
1068 local($\, $", $,) = (undef, ' ', '');
1069 my $fh = $self->output;
1071 # Escape each line after the first with a # so we don't
1072 # confuse Test::Harness.
1073 $msg =~ s/\n(.)/\n# $1/sg;
1075 # Stick a newline on the end if it needs it.
1076 $msg .= "\n" unless $msg =~ /\n\Z/;
1082 =item B<_print_diag>
1084 $Test->_print_diag(@msg);
1086 Like _print, but prints to the current diagnostic filehandle.
1093 local($\, $", $,) = (undef, ' ', '');
1094 my $fh = $self->todo ? $self->todo_output : $self->failure_output;
1101 $Test->output($file);
1103 Where normal "ok/not ok" test output should go.
1107 =item B<failure_output>
1109 $Test->failure_output($fh);
1110 $Test->failure_output($file);
1112 Where diagnostic output on test failures and diag() should go.
1116 =item B<todo_output>
1118 $Test->todo_output($fh);
1119 $Test->todo_output($file);
1121 Where diagnostics about todo test failures and diag() should go.
1128 my($self, $fh) = @_;
1131 $self->{Out_FH} = _new_fh($fh);
1133 return $self->{Out_FH};
1136 sub failure_output {
1137 my($self, $fh) = @_;
1140 $self->{Fail_FH} = _new_fh($fh);
1142 return $self->{Fail_FH};
1146 my($self, $fh) = @_;
1149 $self->{Todo_FH} = _new_fh($fh);
1151 return $self->{Todo_FH};
1156 my($file_or_fh) = shift;
1159 if( _is_fh($file_or_fh) ) {
1163 $fh = do { local *FH };
1164 open $fh, ">$file_or_fh" or
1165 die "Can't open test output log $file_or_fh: $!";
1174 my $maybe_fh = shift;
1176 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
1178 return UNIVERSAL::isa($maybe_fh, 'GLOB') ||
1179 UNIVERSAL::isa($maybe_fh, 'IO::Handle') ||
1181 # 5.5.4's tied() and can() doesn't like getting undef
1182 UNIVERSAL::can((tied($maybe_fh) || ''), 'TIEHANDLE');
1188 my $old_fh = select $fh;
1194 sub _dup_stdhandles {
1197 $self->_open_testhandles;
1199 # Set everything to unbuffered else plain prints to STDOUT will
1200 # come out in the wrong order from our own prints.
1201 _autoflush(\*TESTOUT);
1202 _autoflush(\*STDOUT);
1203 _autoflush(\*TESTERR);
1204 _autoflush(\*STDERR);
1206 $self->output(\*TESTOUT);
1207 $self->failure_output(\*TESTERR);
1208 $self->todo_output(\*TESTOUT);
1212 my $Opened_Testhandles = 0;
1213 sub _open_testhandles {
1214 return if $Opened_Testhandles;
1215 # We dup STDOUT and STDERR so people can change them in their
1216 # test suites while still getting normal test output.
1217 open(TESTOUT, ">&STDOUT") or die "Can't dup STDOUT: $!";
1218 open(TESTERR, ">&STDERR") or die "Can't dup STDERR: $!";
1219 $Opened_Testhandles = 1;
1226 =head2 Test Status and Info
1230 =item B<current_test>
1232 my $curr_test = $Test->current_test;
1233 $Test->current_test($num);
1235 Gets/sets the current test number we're on. You usually shouldn't
1238 If set forward, the details of the missing tests are filled in as 'unknown'.
1239 if set backward, the details of the intervening tests are deleted. You
1240 can erase history if you really want to.
1245 my($self, $num) = @_;
1247 lock($self->{Curr_Test});
1248 if( defined $num ) {
1249 unless( $self->{Have_Plan} ) {
1251 Carp::croak("Can't change the current test number without a plan!");
1254 $self->{Curr_Test} = $num;
1256 # If the test counter is being pushed forward fill in the details.
1257 my $test_results = $self->{Test_Results};
1258 if( $num > @$test_results ) {
1259 my $start = @$test_results ? @$test_results : 0;
1260 for ($start..$num-1) {
1261 $test_results->[$_] = &share({
1264 reason => 'incrementing test number',
1270 # If backward, wipe history. Its their funeral.
1271 elsif( $num < @$test_results ) {
1272 $#{$test_results} = $num - 1;
1275 return $self->{Curr_Test};
1281 my @tests = $Test->summary;
1283 A simple summary of the tests so far. True for pass, false for fail.
1284 This is a logical pass/fail, so todos are passes.
1286 Of course, test #1 is $tests[0], etc...
1293 return map { $_->{'ok'} } @{ $self->{Test_Results} };
1298 my @tests = $Test->details;
1300 Like summary(), but with a lot more detail.
1302 $tests[$test_num - 1] =
1303 { 'ok' => is the test considered a pass?
1304 actual_ok => did it literally say 'ok'?
1305 name => name of the test (if any)
1306 type => type of test (if any, see below).
1307 reason => reason for the above (if any)
1310 'ok' is true if Test::Harness will consider the test to be a pass.
1312 'actual_ok' is a reflection of whether or not the test literally
1313 printed 'ok' or 'not ok'. This is for examining the result of 'todo'
1316 'name' is the name of the test.
1318 'type' indicates if it was a special test. Normal tests have a type
1319 of ''. Type can be one of the following:
1323 todo_skip see todo_skip()
1326 Sometimes the Test::Builder test counter is incremented without it
1327 printing any test output, for example, when current_test() is changed.
1328 In these cases, Test::Builder doesn't know the result of the test, so
1329 it's type is 'unkown'. These details for these tests are filled in.
1330 They are considered ok, but the name and actual_ok is left undef.
1332 For example "not ok 23 - hole count # TODO insufficient donuts" would
1333 result in this structure:
1335 $tests[22] = # 23 - 1, since arrays start from 0.
1336 { ok => 1, # logically, the test passed since it's todo
1337 actual_ok => 0, # in absolute terms, it failed
1338 name => 'hole count',
1340 reason => 'insufficient donuts'
1347 return @{ $self->{Test_Results} };
1352 my $todo_reason = $Test->todo;
1353 my $todo_reason = $Test->todo($pack);
1355 todo() looks for a $TODO variable in your tests. If set, all tests
1356 will be considered 'todo' (see Test::More and Test::Harness for
1357 details). Returns the reason (ie. the value of $TODO) if running as
1358 todo tests, false otherwise.
1360 todo() is about finding the right package to look for $TODO in. It
1361 uses the exported_to() package to find it. If that's not set, it's
1362 pretty good at guessing the right package to look at based on $Level.
1364 Sometimes there is some confusion about where todo() should be looking
1365 for the $TODO variable. If you want to be sure, tell it explicitly
1371 my($self, $pack) = @_;
1373 $pack = $pack || $self->exported_to || $self->caller($Level);
1374 return 0 unless $pack;
1377 return defined ${$pack.'::TODO'} ? ${$pack.'::TODO'}
1383 my $package = $Test->caller;
1384 my($pack, $file, $line) = $Test->caller;
1385 my($pack, $file, $line) = $Test->caller($height);
1387 Like the normal caller(), except it reports according to your level().
1392 my($self, $height) = @_;
1395 my @caller = CORE::caller($self->level + $height + 1);
1396 return wantarray ? @caller : $caller[0];
1407 =item B<_sanity_check>
1409 $self->_sanity_check();
1411 Runs a bunch of end of test sanity checks to make sure reality came
1412 through ok. If anything is wrong it will die with a fairly friendly
1421 _whoa($self->{Curr_Test} < 0, 'Says here you ran a negative number of tests!');
1422 _whoa(!$self->{Have_Plan} and $self->{Curr_Test},
1423 'Somehow your tests ran without a plan!');
1424 _whoa($self->{Curr_Test} != @{ $self->{Test_Results} },
1425 'Somehow you got a different number of results than tests ran!');
1430 _whoa($check, $description);
1432 A sanity check, similar to assert(). If the $check is true, something
1433 has gone horribly wrong. It will die with the given $description and
1434 a note to contact the author.
1439 my($check, $desc) = @_;
1443 This should never happen! Please contact the author immediately!
1450 _my_exit($exit_num);
1452 Perl seems to have some trouble with exiting inside an END block. 5.005_03
1453 and 5.6.1 both seem to do odd things. Instead, this function edits $?
1454 directly. It should ONLY be called from inside an END block. It
1455 doesn't actually exit, that's your job.
1472 $SIG{__DIE__} = sub {
1473 # We don't want to muck with death in an eval, but $^S isn't
1474 # totally reliable. 5.005_03 and 5.6.1 both do the wrong thing
1475 # with it. Instead, we use caller. This also means it runs under
1478 for( my $stack = 1; my $sub = (CORE::caller($stack))[3]; $stack++ ) {
1479 $in_eval = 1 if $sub =~ /^\(eval\)/;
1481 $Test->{Test_Died} = 1 unless $in_eval;
1487 $self->_sanity_check();
1489 # Don't bother with an ending if this is a forked copy. Only the parent
1490 # should do the ending.
1491 # Exit if plan() was never called. This is so "require Test::Simple"
1493 if( ($self->{Original_Pid} != $$) or
1494 (!$self->{Have_Plan} && !$self->{Test_Died}) )
1500 # Figure out if we passed or failed and print helpful messages.
1501 my $test_results = $self->{Test_Results};
1502 if( @$test_results ) {
1503 # The plan? We have no plan.
1504 if( $self->{No_Plan} ) {
1505 $self->_print("1..$self->{Curr_Test}\n") unless $self->no_header;
1506 $self->{Expected_Tests} = $self->{Curr_Test};
1509 # Auto-extended arrays and elements which aren't explicitly
1510 # filled in with a shared reference will puke under 5.8.0
1511 # ithreads. So we have to fill them in by hand. :(
1512 my $empty_result = &share({});
1513 for my $idx ( 0..$self->{Expected_Tests}-1 ) {
1514 $test_results->[$idx] = $empty_result
1515 unless defined $test_results->[$idx];
1518 my $num_failed = grep !$_->{'ok'},
1519 @{$test_results}[0..$self->{Expected_Tests}-1];
1520 $num_failed += abs($self->{Expected_Tests} - @$test_results);
1522 if( $self->{Curr_Test} < $self->{Expected_Tests} ) {
1523 my $s = $self->{Expected_Tests} == 1 ? '' : 's';
1524 $self->diag(<<"FAIL");
1525 Looks like you planned $self->{Expected_Tests} test$s but only ran $self->{Curr_Test}.
1528 elsif( $self->{Curr_Test} > $self->{Expected_Tests} ) {
1529 my $num_extra = $self->{Curr_Test} - $self->{Expected_Tests};
1530 my $s = $self->{Expected_Tests} == 1 ? '' : 's';
1531 $self->diag(<<"FAIL");
1532 Looks like you planned $self->{Expected_Tests} test$s but ran $num_extra extra.
1535 elsif ( $num_failed ) {
1536 my $s = $num_failed == 1 ? '' : 's';
1537 $self->diag(<<"FAIL");
1538 Looks like you failed $num_failed test$s of $self->{Expected_Tests}.
1542 if( $self->{Test_Died} ) {
1543 $self->diag(<<"FAIL");
1544 Looks like your test died just after $self->{Curr_Test}.
1547 _my_exit( 255 ) && return;
1550 _my_exit( $num_failed <= 254 ? $num_failed : 254 ) && return;
1552 elsif ( $self->{Skip_All} ) {
1553 _my_exit( 0 ) && return;
1555 elsif ( $self->{Test_Died} ) {
1556 $self->diag(<<'FAIL');
1557 Looks like your test died before it could output anything.
1559 _my_exit( 255 ) && return;
1562 $self->diag("No tests run!\n");
1563 _my_exit( 255 ) && return;
1568 $Test->_ending if defined $Test and !$Test->no_ending;
1573 If all your tests passed, Test::Builder will exit with zero (which is
1574 normal). If anything failed it will exit with how many failed. If
1575 you run less (or more) tests than you planned, the missing (or extras)
1576 will be considered failures. If no tests were ever run Test::Builder
1577 will throw a warning and exit with 255. If the test died, even after
1578 having successfully completed all its tests, it will still be
1579 considered a failure and will exit with 255.
1581 So the exit codes are...
1583 0 all tests successful
1585 any other number how many failed (including missing or extras)
1587 If you fail more than 254 tests, it will be reported as 254.
1592 In perl 5.8.0 and later, Test::Builder is thread-safe. The test
1593 number is shared amongst all threads. This means if one thread sets
1594 the test number using current_test() they will all be effected.
1596 Test::Builder is only thread-aware if threads.pm is loaded I<before>
1601 CPAN can provide the best examples. Test::Simple, Test::More,
1602 Test::Exception and Test::Differences all use Test::Builder.
1606 Test::Simple, Test::More, Test::Harness
1610 Original code by chromatic, maintained by Michael G Schwern
1611 E<lt>schwern@pobox.comE<gt>
1615 Copyright 2002, 2004 by chromatic E<lt>chromatic@wgz.orgE<gt> and
1616 Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1618 This program is free software; you can redistribute it and/or
1619 modify it under the same terms as Perl itself.
1621 See F<http://www.perl.com/perl/misc/Artistic.html>