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