Upgrade to Test-Simple-0.82.
[p5sagit/p5-mst-13.2.git] / lib / Test / Builder.pm
CommitLineData
33459055 1package Test::Builder;
ccbd73a4 2# $Id: /mirror/googlecode/test-more-trunk/lib/Test/Builder.pm 67223 2008-10-15T03:08:18.888155Z schwern $
33459055 3
cd06ac21 4use 5.006;
33459055 5use strict;
ccbd73a4 6use warnings;
cd06ac21 7
ccbd73a4 8our $VERSION = '0.82';
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
ccbd73a4 460 $self->_try( sub { require overload } ) || return;
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
ccbd73a4 502 no warnings 'numeric';
503 my $numval = $val + 0;
504 return $numval != 0 and $numval ne $val ? 1 : 0;
b1ddf169 505}
506
33459055 507=item B<is_eq>
508
509 $Test->is_eq($got, $expected, $name);
510
511Like Test::More's is(). Checks if $got eq $expected. This is the
512string version.
513
514=item B<is_num>
515
a9153838 516 $Test->is_num($got, $expected, $name);
33459055 517
518Like Test::More's is(). Checks if $got == $expected. This is the
519numeric version.
520
521=cut
522
523sub is_eq {
ccbd73a4 524 my( $self, $got, $expect, $name ) = @_;
33459055 525 local $Level = $Level + 1;
a9153838 526
ccbd73a4 527 $self->_unoverload_str( \$got, \$expect );
b1ddf169 528
a9153838 529 if( !defined $got || !defined $expect ) {
530 # undef only matches undef and nothing else
531 my $test = !defined $got && !defined $expect;
532
ccbd73a4 533 $self->ok( $test, $name );
534 $self->_is_diag( $got, 'eq', $expect ) unless $test;
a9153838 535 return $test;
536 }
537
ccbd73a4 538 return $self->cmp_ok( $got, 'eq', $expect, $name );
33459055 539}
540
541sub is_num {
ccbd73a4 542 my( $self, $got, $expect, $name ) = @_;
33459055 543 local $Level = $Level + 1;
a9153838 544
ccbd73a4 545 $self->_unoverload_num( \$got, \$expect );
b1ddf169 546
a9153838 547 if( !defined $got || !defined $expect ) {
548 # undef only matches undef and nothing else
549 my $test = !defined $got && !defined $expect;
550
ccbd73a4 551 $self->ok( $test, $name );
552 $self->_is_diag( $got, '==', $expect ) unless $test;
a9153838 553 return $test;
554 }
555
ccbd73a4 556 return $self->cmp_ok( $got, '==', $expect, $name );
33459055 557}
558
ccbd73a4 559sub _diag_fmt {
560 my( $self, $type, $val ) = @_;
a9153838 561
ccbd73a4 562 if( defined $$val ) {
563 if( $type eq 'eq' or $type eq 'ne' ) {
564 # quote and force string context
565 $$val = "'$$val'";
a9153838 566 }
567 else {
ccbd73a4 568 # force numeric context
569 $self->_unoverload_num($val);
a9153838 570 }
571 }
ccbd73a4 572 else {
573 $$val = 'undef';
574 }
575
576 return;
577}
578
579sub _is_diag {
580 my( $self, $got, $type, $expect ) = @_;
581
582 $self->_diag_fmt( $type, $_ ) for \$got, \$expect;
33459055 583
04955c14 584 local $Level = $Level + 1;
ccbd73a4 585 return $self->diag(<<"DIAGNOSTIC");
586 got: $got
587 expected: $expect
a9153838 588DIAGNOSTIC
589
ccbd73a4 590}
591
592sub _isnt_diag {
593 my( $self, $got, $type ) = @_;
594
595 $self->_diag_fmt( $type, \$got );
596
597 local $Level = $Level + 1;
598 return $self->diag(<<"DIAGNOSTIC");
599 got: $got
600 expected: anything else
601DIAGNOSTIC
602}
a9153838 603
604=item B<isnt_eq>
605
606 $Test->isnt_eq($got, $dont_expect, $name);
607
608Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
609the string version.
610
611=item B<isnt_num>
612
68938d83 613 $Test->isnt_num($got, $dont_expect, $name);
a9153838 614
615Like Test::More's isnt(). Checks if $got ne $dont_expect. This is
616the numeric version.
617
618=cut
619
620sub isnt_eq {
ccbd73a4 621 my( $self, $got, $dont_expect, $name ) = @_;
a9153838 622 local $Level = $Level + 1;
623
624 if( !defined $got || !defined $dont_expect ) {
625 # undef only matches undef and nothing else
626 my $test = defined $got || defined $dont_expect;
627
ccbd73a4 628 $self->ok( $test, $name );
629 $self->_isnt_diag( $got, 'ne' ) unless $test;
a9153838 630 return $test;
33459055 631 }
a9153838 632
ccbd73a4 633 return $self->cmp_ok( $got, 'ne', $dont_expect, $name );
a9153838 634}
635
636sub isnt_num {
ccbd73a4 637 my( $self, $got, $dont_expect, $name ) = @_;
33459055 638 local $Level = $Level + 1;
33459055 639
a9153838 640 if( !defined $got || !defined $dont_expect ) {
641 # undef only matches undef and nothing else
642 my $test = defined $got || defined $dont_expect;
33459055 643
ccbd73a4 644 $self->ok( $test, $name );
645 $self->_isnt_diag( $got, '!=' ) unless $test;
a9153838 646 return $test;
647 }
648
ccbd73a4 649 return $self->cmp_ok( $got, '!=', $dont_expect, $name );
33459055 650}
651
652=item B<like>
653
654 $Test->like($this, qr/$regex/, $name);
655 $Test->like($this, '/$regex/', $name);
656
657Like Test::More's like(). Checks if $this matches the given $regex.
658
659You'll want to avoid qr// if you want your tests to work before 5.005.
660
a9153838 661=item B<unlike>
662
663 $Test->unlike($this, qr/$regex/, $name);
664 $Test->unlike($this, '/$regex/', $name);
665
666Like Test::More's unlike(). Checks if $this B<does not match> the
667given $regex.
668
33459055 669=cut
670
671sub like {
ccbd73a4 672 my( $self, $this, $regex, $name ) = @_;
33459055 673
674 local $Level = $Level + 1;
ccbd73a4 675 return $self->_regex_ok( $this, $regex, '=~', $name );
a9153838 676}
677
678sub unlike {
ccbd73a4 679 my( $self, $this, $regex, $name ) = @_;
a9153838 680
681 local $Level = $Level + 1;
ccbd73a4 682 return $self->_regex_ok( $this, $regex, '!~', $name );
a9153838 683}
684
a9153838 685=item B<cmp_ok>
686
687 $Test->cmp_ok($this, $type, $that, $name);
688
689Works just like Test::More's cmp_ok().
690
691 $Test->cmp_ok($big_num, '!=', $other_big_num);
692
693=cut
694
ccbd73a4 695my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );
b1ddf169 696
a9153838 697sub cmp_ok {
ccbd73a4 698 my( $self, $got, $type, $expect, $name ) = @_;
a9153838 699
b1ddf169 700 # Treat overloaded objects as numbers if we're asked to do a
701 # numeric comparison.
ccbd73a4 702 my $unoverload
703 = $numeric_cmps{$type}
704 ? '_unoverload_num'
705 : '_unoverload_str';
b1ddf169 706
ccbd73a4 707 $self->$unoverload( \$got, \$expect );
b1ddf169 708
a9153838 709 my $test;
710 {
ccbd73a4 711 ## no critic (BuiltinFunctions::ProhibitStringyEval)
712
713 local( $@, $!, $SIG{__DIE__} ); # isolate eval
b1ddf169 714
715 my $code = $self->_caller_context;
716
ccbd73a4 717 # Yes, it has to look like this or 5.4.5 won't see the #line
705e6672 718 # directive.
b1ddf169 719 # Don't ask me, man, I just work here.
720 $test = eval "
721$code" . "\$got $type \$expect;";
722
a9153838 723 }
724 local $Level = $Level + 1;
ccbd73a4 725 my $ok = $self->ok( $test, $name );
a9153838 726
ccbd73a4 727 unless($ok) {
a9153838 728 if( $type =~ /^(eq|==)$/ ) {
ccbd73a4 729 $self->_is_diag( $got, $type, $expect );
730 }
731 elsif( $type =~ /^(ne|!=)$/ ) {
732 $self->_isnt_diag( $got, $type );
a9153838 733 }
734 else {
ccbd73a4 735 $self->_cmp_diag( $got, $type, $expect );
a9153838 736 }
737 }
738 return $ok;
739}
740
741sub _cmp_diag {
ccbd73a4 742 my( $self, $got, $type, $expect ) = @_;
743
a9153838 744 $got = defined $got ? "'$got'" : 'undef';
745 $expect = defined $expect ? "'$expect'" : 'undef';
ccbd73a4 746
04955c14 747 local $Level = $Level + 1;
ccbd73a4 748 return $self->diag(<<"DIAGNOSTIC");
749 $got
750 $type
751 $expect
a9153838 752DIAGNOSTIC
753}
754
b1ddf169 755sub _caller_context {
756 my $self = shift;
757
ccbd73a4 758 my( $pack, $file, $line ) = $self->caller(1);
b1ddf169 759
760 my $code = '';
761 $code .= "#line $line $file\n" if defined $file and defined $line;
762
763 return $code;
764}
765
c00d8759 766=back
767
768
769=head2 Other Testing Methods
770
771These are methods which are used in the course of writing a test but are not themselves tests.
772
773=over 4
b1ddf169 774
775=item B<BAIL_OUT>
776
777 $Test->BAIL_OUT($reason);
a9153838 778
779Indicates to the Test::Harness that things are going so badly all
780testing should terminate. This includes running any additional test
781scripts.
782
783It will exit with 255.
784
785=cut
786
b1ddf169 787sub BAIL_OUT {
ccbd73a4 788 my( $self, $reason ) = @_;
a9153838 789
b1ddf169 790 $self->{Bailed_Out} = 1;
a9153838 791 $self->_print("Bail out! $reason");
792 exit 255;
793}
794
b1ddf169 795=for deprecated
796BAIL_OUT() used to be BAILOUT()
797
845d7e37 798=cut
799
b1ddf169 800*BAILOUT = \&BAIL_OUT;
801
33459055 802=item B<skip>
803
804 $Test->skip;
805 $Test->skip($why);
806
807Skips the current test, reporting $why.
808
809=cut
810
811sub skip {
ccbd73a4 812 my( $self, $why ) = @_;
33459055 813 $why ||= '';
ccbd73a4 814 $self->_unoverload_str( \$why );
33459055 815
b7f9bbeb 816 $self->_plan_check;
33459055 817
ccbd73a4 818 lock( $self->{Curr_Test} );
5143c659 819 $self->{Curr_Test}++;
33459055 820
ccbd73a4 821 $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
822 {
823 'ok' => 1,
824 actual_ok => 1,
825 name => '',
826 type => 'skip',
827 reason => $why,
828 }
829 );
33459055 830
831 my $out = "ok";
ccbd73a4 832 $out .= " $self->{Curr_Test}" if $self->use_numbers;
833 $out .= " # skip";
834 $out .= " $why" if length $why;
835 $out .= "\n";
33459055 836
5143c659 837 $self->_print($out);
33459055 838
839 return 1;
840}
841
a9153838 842=item B<todo_skip>
843
844 $Test->todo_skip;
845 $Test->todo_skip($why);
846
847Like skip(), only it will declare the test as failing and TODO. Similar
848to
849
850 print "not ok $tnum # TODO $why\n";
851
852=cut
853
854sub todo_skip {
ccbd73a4 855 my( $self, $why ) = @_;
a9153838 856 $why ||= '';
857
b7f9bbeb 858 $self->_plan_check;
a9153838 859
ccbd73a4 860 lock( $self->{Curr_Test} );
5143c659 861 $self->{Curr_Test}++;
a9153838 862
ccbd73a4 863 $self->{Test_Results}[ $self->{Curr_Test} - 1 ] = &share(
864 {
865 'ok' => 1,
866 actual_ok => 0,
867 name => '',
868 type => 'todo_skip',
869 reason => $why,
870 }
871 );
a9153838 872
873 my $out = "not ok";
ccbd73a4 874 $out .= " $self->{Curr_Test}" if $self->use_numbers;
875 $out .= " # TODO & SKIP $why\n";
a9153838 876
5143c659 877 $self->_print($out);
a9153838 878
879 return 1;
880}
881
33459055 882=begin _unimplemented
883
884=item B<skip_rest>
885
886 $Test->skip_rest;
887 $Test->skip_rest($reason);
888
889Like skip(), only it skips all the rest of the tests you plan to run
890and terminates the test.
891
892If you're running under no_plan, it skips once and terminates the
893test.
894
895=end _unimplemented
896
897=back
898
899
c00d8759 900=head2 Test building utility methods
901
902These methods are useful when writing your own test methods.
903
904=over 4
905
906=item B<maybe_regex>
907
908 $Test->maybe_regex(qr/$regex/);
909 $Test->maybe_regex('/$regex/');
910
911Convenience method for building testing functions that take regular
912expressions as arguments, but need to work before perl 5.005.
913
914Takes a quoted regular expression produced by qr//, or a string
915representing a regular expression.
916
917Returns a Perl value which may be used instead of the corresponding
ccbd73a4 918regular expression, or undef if its argument is not recognised.
c00d8759 919
920For example, a version of like(), sans the useful diagnostic messages,
921could be written as:
922
923 sub laconic_like {
924 my ($self, $this, $regex, $name) = @_;
925 my $usable_regex = $self->maybe_regex($regex);
926 die "expecting regex, found '$regex'\n"
927 unless $usable_regex;
928 $self->ok($this =~ m/$usable_regex/, $name);
929 }
930
931=cut
932
c00d8759 933sub maybe_regex {
ccbd73a4 934 my( $self, $regex ) = @_;
c00d8759 935 my $usable_regex = undef;
936
937 return $usable_regex unless defined $regex;
938
ccbd73a4 939 my( $re, $opts );
c00d8759 940
941 # Check for qr/foo/
bdff39c7 942 if( _is_qr($regex) ) {
c00d8759 943 $usable_regex = $regex;
944 }
945 # Check for '/foo/' or 'm,foo,'
ccbd73a4 946 elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
947 ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
948 )
c00d8759 949 {
950 $usable_regex = length $opts ? "(?$opts)$re" : $re;
951 }
952
953 return $usable_regex;
04955c14 954}
955
04955c14 956sub _is_qr {
957 my $regex = shift;
ccbd73a4 958
04955c14 959 # is_regexp() checks for regexes in a robust manner, say if they're
960 # blessed.
961 return re::is_regexp($regex) if defined &re::is_regexp;
962 return ref $regex eq 'Regexp';
963}
964
c00d8759 965sub _regex_ok {
ccbd73a4 966 my( $self, $this, $regex, $cmp, $name ) = @_;
c00d8759 967
ccbd73a4 968 my $ok = 0;
c00d8759 969 my $usable_regex = $self->maybe_regex($regex);
ccbd73a4 970 unless( defined $usable_regex ) {
971 local $Level = $Level + 1;
c00d8759 972 $ok = $self->ok( 0, $name );
973 $self->diag(" '$regex' doesn't look much like a regex to me.");
974 return $ok;
975 }
976
977 {
ccbd73a4 978 ## no critic (BuiltinFunctions::ProhibitStringyEval)
979
c00d8759 980 my $test;
981 my $code = $self->_caller_context;
982
ccbd73a4 983 local( $@, $!, $SIG{__DIE__} ); # isolate eval
c00d8759 984
ccbd73a4 985 # Yes, it has to look like this or 5.4.5 won't see the #line
705e6672 986 # directive.
c00d8759 987 # Don't ask me, man, I just work here.
988 $test = eval "
989$code" . q{$test = $this =~ /$usable_regex/ ? 1 : 0};
990
991 $test = !$test if $cmp eq '!~';
992
993 local $Level = $Level + 1;
994 $ok = $self->ok( $test, $name );
995 }
996
ccbd73a4 997 unless($ok) {
c00d8759 998 $this = defined $this ? "'$this'" : 'undef';
999 my $match = $cmp eq '=~' ? "doesn't match" : "matches";
04955c14 1000
1001 local $Level = $Level + 1;
ccbd73a4 1002 $self->diag( sprintf <<'DIAGNOSTIC', $this, $match, $regex );
c00d8759 1003 %s
1004 %13s '%s'
1005DIAGNOSTIC
1006
1007 }
1008
1009 return $ok;
1010}
1011
c00d8759 1012# I'm not ready to publish this. It doesn't deal with array return
1013# values from the code or context.
eb820256 1014
c00d8759 1015=begin private
1016
1017=item B<_try>
1018
1019 my $return_from_code = $Test->try(sub { code });
1020 my($return_from_code, $error) = $Test->try(sub { code });
1021
ccbd73a4 1022Works like eval BLOCK except it ensures it has no effect on the rest
1023of the test (ie. $@ is not set) nor is effected by outside
1024interference (ie. $SIG{__DIE__}) and works around some quirks in older
1025Perls.
c00d8759 1026
1027$error is what would normally be in $@.
1028
1029It is suggested you use this in place of eval BLOCK.
1030
1031=cut
1032
1033sub _try {
ccbd73a4 1034 my( $self, $code ) = @_;
1035
c00d8759 1036 local $!; # eval can mess up $!
1037 local $@; # don't set $@ in the test
1038 local $SIG{__DIE__}; # don't trip an outside DIE handler.
1039 my $return = eval { $code->() };
ccbd73a4 1040
1041 return wantarray ? ( $return, $@ ) : $return;
c00d8759 1042}
1043
1044=end private
1045
1046
1047=item B<is_fh>
1048
1049 my $is_fh = $Test->is_fh($thing);
1050
1051Determines if the given $thing can be used as a filehandle.
1052
1053=cut
1054
1055sub is_fh {
ccbd73a4 1056 my $self = shift;
c00d8759 1057 my $maybe_fh = shift;
1058 return 0 unless defined $maybe_fh;
1059
ccbd73a4 1060 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
1061 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
c00d8759 1062
0753bcb5 1063 return eval { $maybe_fh->isa("IO::Handle") } ||
c00d8759 1064 # 5.5.4's tied() and can() doesn't like getting undef
ccbd73a4 1065 eval { ( tied($maybe_fh) || '' )->can('TIEHANDLE') };
c00d8759 1066}
1067
c00d8759 1068=back
1069
1070
33459055 1071=head2 Test style
1072
c00d8759 1073
33459055 1074=over 4
1075
1076=item B<level>
1077
1078 $Test->level($how_high);
1079
1080How far up the call stack should $Test look when reporting where the
1081test failed.
1082
1083Defaults to 1.
1084
c00d8759 1085Setting L<$Test::Builder::Level> overrides. This is typically useful
33459055 1086localized:
1087
c00d8759 1088 sub my_ok {
1089 my $test = shift;
1090
1091 local $Test::Builder::Level = $Test::Builder::Level + 1;
1092 $TB->ok($test);
33459055 1093 }
1094
c00d8759 1095To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant.
1096
33459055 1097=cut
1098
1099sub level {
ccbd73a4 1100 my( $self, $level ) = @_;
33459055 1101
1102 if( defined $level ) {
1103 $Level = $level;
1104 }
1105 return $Level;
1106}
1107
33459055 1108=item B<use_numbers>
1109
1110 $Test->use_numbers($on_or_off);
1111
1112Whether or not the test should output numbers. That is, this if true:
1113
1114 ok 1
1115 ok 2
1116 ok 3
1117
1118or this if false
1119
1120 ok
1121 ok
1122 ok
1123
1124Most useful when you can't depend on the test output order, such as
1125when threads or forking is involved.
1126
33459055 1127Defaults to on.
1128
1129=cut
1130
33459055 1131sub use_numbers {
ccbd73a4 1132 my( $self, $use_nums ) = @_;
33459055 1133
1134 if( defined $use_nums ) {
5143c659 1135 $self->{Use_Nums} = $use_nums;
33459055 1136 }
5143c659 1137 return $self->{Use_Nums};
33459055 1138}
1139
b1ddf169 1140=item B<no_diag>
33459055 1141
b1ddf169 1142 $Test->no_diag($no_diag);
1143
1144If set true no diagnostics will be printed. This includes calls to
1145diag().
33459055 1146
1147=item B<no_ending>
1148
1149 $Test->no_ending($no_ending);
1150
1151Normally, Test::Builder does some extra diagnostics when the test
30e302f8 1152ends. It also changes the exit code as described below.
33459055 1153
1154If this is true, none of that will be done.
1155
b1ddf169 1156=item B<no_header>
1157
1158 $Test->no_header($no_header);
1159
1160If set to true, no "1..N" header will be printed.
1161
33459055 1162=cut
1163
b1ddf169 1164foreach my $attribute (qw(No_Header No_Ending No_Diag)) {
1165 my $method = lc $attribute;
33459055 1166
b1ddf169 1167 my $code = sub {
ccbd73a4 1168 my( $self, $no ) = @_;
33459055 1169
b1ddf169 1170 if( defined $no ) {
1171 $self->{$attribute} = $no;
1172 }
1173 return $self->{$attribute};
1174 };
33459055 1175
ccbd73a4 1176 no strict 'refs'; ## no critic
1177 *{ __PACKAGE__ . '::' . $method } = $code;
33459055 1178}
1179
33459055 1180=back
1181
1182=head2 Output
1183
1184Controlling where the test output goes.
1185
4bd4e70a 1186It's ok for your test to change where STDOUT and STDERR point to,
71373de2 1187Test::Builder's default output settings will not be affected.
4bd4e70a 1188
33459055 1189=over 4
1190
1191=item B<diag>
1192
1193 $Test->diag(@msgs);
1194
7483b81c 1195Prints out the given @msgs. Like C<print>, arguments are simply
1196appended together.
1197
1198Normally, it uses the failure_output() handle, but if this is for a
1199TODO test, the todo_output() handle is used.
33459055 1200
71373de2 1201Output will be indented and marked with a # so as not to interfere
a9153838 1202with test output. A newline will be put on the end if there isn't one
1203already.
33459055 1204
1205We encourage using this rather than calling print directly.
1206
89c1e84a 1207Returns false. Why? Because diag() is often used in conjunction with
1208a failing test (C<ok() || diag()>) it "passes through" the failure.
1209
1210 return ok(...) || diag(...);
1211
1212=for blame transfer
1213Mark Fowler <mark@twoshortplanks.com>
1214
33459055 1215=cut
1216
1217sub diag {
ccbd73a4 1218 my $self = shift;
1219
1220 $self->_print_comment( $self->_diag_fh, @_ );
1221}
1222
1223=item B<note>
1224
1225 $Test->note(@msgs);
1226
1227Like diag(), but it prints to the C<output()> handle so it will not
1228normally be seen by the user except in verbose mode.
1229
1230=cut
1231
1232sub note {
1233 my $self = shift;
1234
1235 $self->_print_comment( $self->output, @_ );
1236}
1237
1238sub _diag_fh {
1239 my $self = shift;
1240
1241 local $Level = $Level + 1;
1242 return $self->in_todo ? $self->todo_output : $self->failure_output;
1243}
1244
1245sub _print_comment {
1246 my( $self, $fh, @msgs ) = @_;
b1ddf169 1247
1248 return if $self->no_diag;
a9153838 1249 return unless @msgs;
33459055 1250
4bd4e70a 1251 # Prevent printing headers when compiling (i.e. -c)
33459055 1252 return if $^C;
1253
7483b81c 1254 # Smash args together like print does.
1255 # Convert undef to 'undef' so its readable.
1256 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1257
ccbd73a4 1258 # Escape the beginning, _print will take care of the rest.
1259 $msg =~ s/^/# /;
a9153838 1260
33459055 1261 local $Level = $Level + 1;
ccbd73a4 1262 $self->_print_to_fh( $fh, $msg );
89c1e84a 1263
1264 return 0;
33459055 1265}
1266
ccbd73a4 1267=item B<explain>
1268
1269 my @dump = $Test->explain(@msgs);
1270
1271Will dump the contents of any references in a human readable format.
1272Handy for things like...
1273
1274 is_deeply($have, $want) || diag explain $have;
1275
1276or
1277
1278 is_deeply($have, $want) || note explain $have;
1279
1280=cut
1281
1282sub explain {
1283 my $self = shift;
1284
1285 return map {
1286 ref $_
1287 ? do {
1288 require Data::Dumper;
1289
1290 my $dumper = Data::Dumper->new( [$_] );
1291 $dumper->Indent(1)->Terse(1);
1292 $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
1293 $dumper->Dump;
1294 }
1295 : $_
1296 } @_;
1297}
1298
33459055 1299=begin _private
1300
1301=item B<_print>
1302
1303 $Test->_print(@msgs);
1304
1305Prints to the output() filehandle.
1306
1307=end _private
1308
1309=cut
1310
1311sub _print {
ccbd73a4 1312 my $self = shift;
1313 return $self->_print_to_fh( $self->output, @_ );
1314}
1315
1316sub _print_to_fh {
1317 my( $self, $fh, @msgs ) = @_;
33459055 1318
1319 # Prevent printing headers when only compiling. Mostly for when
1320 # tests are deparsed with B::Deparse
1321 return if $^C;
1322
7483b81c 1323 my $msg = join '', @msgs;
1324
ccbd73a4 1325 local( $\, $", $, ) = ( undef, ' ', '' );
89c1e84a 1326
1327 # Escape each line after the first with a # so we don't
1328 # confuse Test::Harness.
7483b81c 1329 $msg =~ s/\n(.)/\n# $1/sg;
89c1e84a 1330
7483b81c 1331 # Stick a newline on the end if it needs it.
1332 $msg .= "\n" unless $msg =~ /\n\Z/;
89c1e84a 1333
ccbd73a4 1334 return print $fh $msg;
33459055 1335}
1336
33459055 1337=item B<output>
1338
1339 $Test->output($fh);
1340 $Test->output($file);
1341
1342Where normal "ok/not ok" test output should go.
1343
1344Defaults to STDOUT.
1345
1346=item B<failure_output>
1347
1348 $Test->failure_output($fh);
1349 $Test->failure_output($file);
1350
1351Where diagnostic output on test failures and diag() should go.
1352
1353Defaults to STDERR.
1354
1355=item B<todo_output>
1356
1357 $Test->todo_output($fh);
1358 $Test->todo_output($file);
1359
1360Where diagnostics about todo test failures and diag() should go.
1361
1362Defaults to STDOUT.
1363
1364=cut
1365
33459055 1366sub output {
ccbd73a4 1367 my( $self, $fh ) = @_;
33459055 1368
1369 if( defined $fh ) {
b7f9bbeb 1370 $self->{Out_FH} = $self->_new_fh($fh);
33459055 1371 }
5143c659 1372 return $self->{Out_FH};
33459055 1373}
1374
1375sub failure_output {
ccbd73a4 1376 my( $self, $fh ) = @_;
33459055 1377
1378 if( defined $fh ) {
b7f9bbeb 1379 $self->{Fail_FH} = $self->_new_fh($fh);
33459055 1380 }
5143c659 1381 return $self->{Fail_FH};
33459055 1382}
1383
1384sub todo_output {
ccbd73a4 1385 my( $self, $fh ) = @_;
33459055 1386
1387 if( defined $fh ) {
b7f9bbeb 1388 $self->{Todo_FH} = $self->_new_fh($fh);
33459055 1389 }
5143c659 1390 return $self->{Todo_FH};
33459055 1391}
1392
1393sub _new_fh {
b7f9bbeb 1394 my $self = shift;
33459055 1395 my($file_or_fh) = shift;
1396
1397 my $fh;
c00d8759 1398 if( $self->is_fh($file_or_fh) ) {
0257f296 1399 $fh = $file_or_fh;
1400 }
1401 else {
ccbd73a4 1402 open $fh, ">", $file_or_fh
1403 or $self->croak("Can't open test output log $file_or_fh: $!");
705e6672 1404 _autoflush($fh);
33459055 1405 }
33459055 1406
1407 return $fh;
1408}
1409
30e302f8 1410sub _autoflush {
1411 my($fh) = shift;
1412 my $old_fh = select $fh;
1413 $| = 1;
1414 select $old_fh;
ccbd73a4 1415
1416 return;
30e302f8 1417}
1418
ccbd73a4 1419my( $Testout, $Testerr );
30e302f8 1420
30e302f8 1421sub _dup_stdhandles {
1422 my $self = shift;
1423
5143c659 1424 $self->_open_testhandles;
a9153838 1425
1426 # Set everything to unbuffered else plain prints to STDOUT will
1427 # come out in the wrong order from our own prints.
04955c14 1428 _autoflush($Testout);
ccbd73a4 1429 _autoflush( \*STDOUT );
04955c14 1430 _autoflush($Testerr);
ccbd73a4 1431 _autoflush( \*STDERR );
a9153838 1432
ccbd73a4 1433 $self->reset_outputs;
33459055 1434
ccbd73a4 1435 return;
1436}
5143c659 1437
1438my $Opened_Testhandles = 0;
ccbd73a4 1439
30e302f8 1440sub _open_testhandles {
04955c14 1441 my $self = shift;
ccbd73a4 1442
5143c659 1443 return if $Opened_Testhandles;
ccbd73a4 1444
30e302f8 1445 # We dup STDOUT and STDERR so people can change them in their
1446 # test suites while still getting normal test output.
ccbd73a4 1447 open( $Testout, ">&STDOUT" ) or die "Can't dup STDOUT: $!";
1448 open( $Testerr, ">&STDERR" ) or die "Can't dup STDERR: $!";
1449
1450 # $self->_copy_io_layers( \*STDOUT, $Testout );
1451 # $self->_copy_io_layers( \*STDERR, $Testerr );
04955c14 1452
30e302f8 1453 $Opened_Testhandles = 1;
33459055 1454
ccbd73a4 1455 return;
1456}
33459055 1457
04955c14 1458sub _copy_io_layers {
ccbd73a4 1459 my( $self, $src, $dst ) = @_;
1460
1461 $self->_try(
1462 sub {
1463 require PerlIO;
1464 my @src_layers = PerlIO::get_layers($src);
1465
1466 binmode $dst, join " ", map ":$_", @src_layers if @src_layers;
1467 }
1468 );
1469
1470 return;
1471}
1472
1473=item reset_outputs
1474
1475 $tb->reset_outputs;
1476
1477Resets all the output filehandles back to their defaults.
1478
1479=cut
1480
1481sub reset_outputs {
1482 my $self = shift;
bdff39c7 1483
ccbd73a4 1484 $self->output ($Testout);
1485 $self->failure_output($Testerr);
1486 $self->todo_output ($Testout);
1487
1488 return;
04955c14 1489}
1490
b7f9bbeb 1491=item carp
1492
1493 $tb->carp(@message);
1494
1495Warns with C<@message> but the message will appear to come from the
1496point where the original test function was called (C<$tb->caller>).
1497
1498=item croak
1499
1500 $tb->croak(@message);
1501
1502Dies with C<@message> but the message will appear to come from the
1503point where the original test function was called (C<$tb->caller>).
1504
1505=cut
1506
1507sub _message_at_caller {
1508 my $self = shift;
1509
004caa16 1510 local $Level = $Level + 1;
ccbd73a4 1511 my( $pack, $file, $line ) = $self->caller;
1512 return join( "", @_ ) . " at $file line $line.\n";
b7f9bbeb 1513}
1514
1515sub carp {
1516 my $self = shift;
ccbd73a4 1517 return warn $self->_message_at_caller(@_);
b7f9bbeb 1518}
1519
1520sub croak {
1521 my $self = shift;
ccbd73a4 1522 return die $self->_message_at_caller(@_);
b7f9bbeb 1523}
1524
1525sub _plan_check {
1526 my $self = shift;
1527
1528 unless( $self->{Have_Plan} ) {
004caa16 1529 local $Level = $Level + 2;
b7f9bbeb 1530 $self->croak("You tried to run a test without a plan");
1531 }
ccbd73a4 1532
1533 return;
b7f9bbeb 1534}
1535
33459055 1536=back
1537
1538
1539=head2 Test Status and Info
1540
1541=over 4
1542
1543=item B<current_test>
1544
1545 my $curr_test = $Test->current_test;
1546 $Test->current_test($num);
1547
0257f296 1548Gets/sets the current test number we're on. You usually shouldn't
1549have to set this.
33459055 1550
0257f296 1551If set forward, the details of the missing tests are filled in as 'unknown'.
1552if set backward, the details of the intervening tests are deleted. You
1553can erase history if you really want to.
33459055 1554
1555=cut
1556
1557sub current_test {
ccbd73a4 1558 my( $self, $num ) = @_;
33459055 1559
ccbd73a4 1560 lock( $self->{Curr_Test} );
33459055 1561 if( defined $num ) {
ccbd73a4 1562 $self->croak("Can't change the current test number without a plan!")
1563 unless $self->{Have_Plan};
89c1e84a 1564
5143c659 1565 $self->{Curr_Test} = $num;
0257f296 1566
1567 # If the test counter is being pushed forward fill in the details.
5143c659 1568 my $test_results = $self->{Test_Results};
1569 if( $num > @$test_results ) {
1570 my $start = @$test_results ? @$test_results : 0;
ccbd73a4 1571 for( $start .. $num - 1 ) {
1572 $test_results->[$_] = &share(
1573 {
1574 'ok' => 1,
1575 actual_ok => undef,
1576 reason => 'incrementing test number',
1577 type => 'unknown',
1578 name => undef
1579 }
1580 );
6686786d 1581 }
1582 }
0257f296 1583 # If backward, wipe history. Its their funeral.
5143c659 1584 elsif( $num < @$test_results ) {
1585 $#{$test_results} = $num - 1;
0257f296 1586 }
33459055 1587 }
5143c659 1588 return $self->{Curr_Test};
33459055 1589}
1590
33459055 1591=item B<summary>
1592
1593 my @tests = $Test->summary;
1594
1595A simple summary of the tests so far. True for pass, false for fail.
1596This is a logical pass/fail, so todos are passes.
1597
1598Of course, test #1 is $tests[0], etc...
1599
1600=cut
1601
1602sub summary {
1603 my($self) = shift;
1604
5143c659 1605 return map { $_->{'ok'} } @{ $self->{Test_Results} };
33459055 1606}
1607
60ffb308 1608=item B<details>
33459055 1609
1610 my @tests = $Test->details;
1611
1612Like summary(), but with a lot more detail.
1613
1614 $tests[$test_num - 1] =
60ffb308 1615 { 'ok' => is the test considered a pass?
33459055 1616 actual_ok => did it literally say 'ok'?
1617 name => name of the test (if any)
60ffb308 1618 type => type of test (if any, see below).
33459055 1619 reason => reason for the above (if any)
1620 };
1621
60ffb308 1622'ok' is true if Test::Harness will consider the test to be a pass.
1623
1624'actual_ok' is a reflection of whether or not the test literally
1625printed 'ok' or 'not ok'. This is for examining the result of 'todo'
1626tests.
1627
1628'name' is the name of the test.
1629
1630'type' indicates if it was a special test. Normal tests have a type
1631of ''. Type can be one of the following:
1632
1633 skip see skip()
1634 todo see todo()
1635 todo_skip see todo_skip()
1636 unknown see below
1637
1638Sometimes the Test::Builder test counter is incremented without it
1639printing any test output, for example, when current_test() is changed.
1640In these cases, Test::Builder doesn't know the result of the test, so
ccbd73a4 1641its type is 'unknown'. These details for these tests are filled in.
60ffb308 1642They are considered ok, but the name and actual_ok is left undef.
1643
1644For example "not ok 23 - hole count # TODO insufficient donuts" would
1645result in this structure:
1646
1647 $tests[22] = # 23 - 1, since arrays start from 0.
1648 { ok => 1, # logically, the test passed since it's todo
1649 actual_ok => 0, # in absolute terms, it failed
1650 name => 'hole count',
1651 type => 'todo',
1652 reason => 'insufficient donuts'
1653 };
1654
1655=cut
1656
1657sub details {
5143c659 1658 my $self = shift;
1659 return @{ $self->{Test_Results} };
60ffb308 1660}
1661
33459055 1662=item B<todo>
1663
1664 my $todo_reason = $Test->todo;
1665 my $todo_reason = $Test->todo($pack);
1666
ccbd73a4 1667If the current tests are considered "TODO" it will return the reason,
1668if any. This reason can come from a $TODO variable or the last call
1669to C<<todo_start()>>.
1670
1671Since a TODO test does not need a reason, this function can return an
1672empty string even when inside a TODO block. Use C<<$Test->in_todo>>
1673to determine if you are currently inside a TODO block.
33459055 1674
04955c14 1675todo() is about finding the right package to look for $TODO in. It's
1676pretty good at guessing the right package to look at. It first looks for
1677the caller based on C<$Level + 1>, since C<todo()> is usually called inside
1678a test function. As a last resort it will use C<exported_to()>.
33459055 1679
1680Sometimes there is some confusion about where todo() should be looking
1681for the $TODO variable. If you want to be sure, tell it explicitly
1682what $pack to use.
1683
1684=cut
1685
1686sub todo {
ccbd73a4 1687 my( $self, $pack ) = @_;
1688
1689 return $self->{Todo} if defined $self->{Todo};
1690
1691 local $Level = $Level + 1;
1692 my $todo = $self->find_TODO($pack);
1693 return $todo if defined $todo;
1694
1695 return '';
1696}
1697
1698=item B<find_TODO>
33459055 1699
ccbd73a4 1700 my $todo_reason = $Test->find_TODO();
1701 my $todo_reason = $Test->find_TODO($pack):
1702
1703Like C<<todo()>> but only returns the value of C<<$TODO>> ignoring
1704C<<todo_start()>>.
1705
1706=cut
1707
1708sub find_TODO {
1709 my( $self, $pack ) = @_;
04955c14 1710
1711 $pack = $pack || $self->caller(1) || $self->exported_to;
ccbd73a4 1712 return unless $pack;
33459055 1713
ccbd73a4 1714 no strict 'refs'; ## no critic
1715 return ${ $pack . '::TODO' };
1716}
1717
1718=item B<in_todo>
1719
1720 my $in_todo = $Test->in_todo;
1721
1722Returns true if the test is currently inside a TODO block.
1723
1724=cut
1725
1726sub in_todo {
1727 my $self = shift;
1728
1729 local $Level = $Level + 1;
1730 return( defined $self->{Todo} || $self->find_TODO ) ? 1 : 0;
1731}
1732
1733=item B<todo_start>
1734
1735 $Test->todo_start();
1736 $Test->todo_start($message);
1737
1738This method allows you declare all subsequent tests as TODO tests, up until
1739the C<todo_end> method has been called.
1740
1741The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out
1742whether or not we're in a TODO test. However, often we find that this is not
1743possible to determine (such as when we want to use C<$TODO> but
1744the tests are being executed in other packages which can't be inferred
1745beforehand).
1746
1747Note that you can use this to nest "todo" tests
1748
1749 $Test->todo_start('working on this');
1750 # lots of code
1751 $Test->todo_start('working on that');
1752 # more code
1753 $Test->todo_end;
1754 $Test->todo_end;
1755
1756This is generally not recommended, but large testing systems often have weird
1757internal needs.
1758
1759We've tried to make this also work with the TODO: syntax, but it's not
1760guaranteed and its use is also discouraged:
1761
1762 TODO: {
1763 local $TODO = 'We have work to do!';
1764 $Test->todo_start('working on this');
1765 # lots of code
1766 $Test->todo_start('working on that');
1767 # more code
1768 $Test->todo_end;
1769 $Test->todo_end;
1770 }
1771
1772Pick one style or another of "TODO" to be on the safe side.
1773
1774=cut
1775
1776sub todo_start {
1777 my $self = shift;
1778 my $message = @_ ? shift : '';
1779
1780 $self->{Start_Todo}++;
1781 if( $self->in_todo ) {
1782 push @{ $self->{Todo_Stack} } => $self->todo;
1783 }
1784 $self->{Todo} = $message;
1785
1786 return;
1787}
1788
1789=item C<todo_end>
1790
1791 $Test->todo_end;
1792
1793Stops running tests as "TODO" tests. This method is fatal if called without a
1794preceding C<todo_start> method call.
1795
1796=cut
1797
1798sub todo_end {
1799 my $self = shift;
1800
1801 if( !$self->{Start_Todo} ) {
1802 $self->croak('todo_end() called without todo_start()');
1803 }
1804
1805 $self->{Start_Todo}--;
1806
1807 if( $self->{Start_Todo} && @{ $self->{Todo_Stack} } ) {
1808 $self->{Todo} = pop @{ $self->{Todo_Stack} };
1809 }
1810 else {
1811 delete $self->{Todo};
1812 }
1813
1814 return;
33459055 1815}
1816
1817=item B<caller>
1818
1819 my $package = $Test->caller;
1820 my($pack, $file, $line) = $Test->caller;
1821 my($pack, $file, $line) = $Test->caller($height);
1822
1823Like the normal caller(), except it reports according to your level().
1824
04955c14 1825C<$height> will be added to the level().
1826
33459055 1827=cut
1828
ccbd73a4 1829sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
1830 my( $self, $height ) = @_;
33459055 1831 $height ||= 0;
a344be10 1832
ccbd73a4 1833 my @caller = CORE::caller( $self->level + $height + 1 );
33459055 1834 return wantarray ? @caller : $caller[0];
1835}
1836
1837=back
1838
1839=cut
1840
1841=begin _private
1842
1843=over 4
1844
1845=item B<_sanity_check>
1846
5143c659 1847 $self->_sanity_check();
33459055 1848
1849Runs a bunch of end of test sanity checks to make sure reality came
1850through ok. If anything is wrong it will die with a fairly friendly
1851error message.
1852
1853=cut
1854
1855#'#
1856sub _sanity_check {
5143c659 1857 my $self = shift;
1858
ccbd73a4 1859 $self->_whoa( $self->{Curr_Test} < 0, 'Says here you ran a negative number of tests!' );
1860 $self->_whoa( !$self->{Have_Plan} and $self->{Curr_Test},
1861 'Somehow your tests ran without a plan!' );
1862 $self->_whoa( $self->{Curr_Test} != @{ $self->{Test_Results} },
1863 'Somehow you got a different number of results than tests ran!' );
1864
1865 return;
33459055 1866}
1867
1868=item B<_whoa>
1869
b7f9bbeb 1870 $self->_whoa($check, $description);
33459055 1871
1872A sanity check, similar to assert(). If the $check is true, something
1873has gone horribly wrong. It will die with the given $description and
1874a note to contact the author.
1875
1876=cut
1877
1878sub _whoa {
ccbd73a4 1879 my( $self, $check, $desc ) = @_;
1880 if($check) {
b7f9bbeb 1881 local $Level = $Level + 1;
1882 $self->croak(<<"WHOA");
33459055 1883WHOA! $desc
1884This should never happen! Please contact the author immediately!
1885WHOA
1886 }
ccbd73a4 1887
1888 return;
33459055 1889}
1890
1891=item B<_my_exit>
1892
1893 _my_exit($exit_num);
1894
1895Perl seems to have some trouble with exiting inside an END block. 5.005_03
1896and 5.6.1 both seem to do odd things. Instead, this function edits $?
1897directly. It should ONLY be called from inside an END block. It
1898doesn't actually exit, that's your job.
1899
1900=cut
1901
1902sub _my_exit {
ccbd73a4 1903 $? = $_[0]; ## no critic (Variables::RequireLocalizedPunctuationVars)
33459055 1904
1905 return 1;
1906}
1907
33459055 1908=back
1909
1910=end _private
1911
1912=cut
1913
33459055 1914sub _ending {
1915 my $self = shift;
1916
04955c14 1917 my $real_exit_code = $?;
5143c659 1918 $self->_sanity_check();
33459055 1919
60ffb308 1920 # Don't bother with an ending if this is a forked copy. Only the parent
1921 # should do the ending.
04955c14 1922 if( $self->{Original_Pid} != $$ ) {
1923 return;
1924 }
ccbd73a4 1925
1926 # Exit if plan() was never called. This is so "require Test::Simple"
5143c659 1927 # doesn't puke.
04955c14 1928 if( !$self->{Have_Plan} ) {
1929 return;
1930 }
1931
b1ddf169 1932 # Don't do an ending if we bailed out.
04955c14 1933 if( $self->{Bailed_Out} ) {
1934 return;
5143c659 1935 }
33459055 1936
1937 # Figure out if we passed or failed and print helpful messages.
5143c659 1938 my $test_results = $self->{Test_Results};
ccbd73a4 1939 if(@$test_results) {
33459055 1940 # The plan? We have no plan.
5143c659 1941 if( $self->{No_Plan} ) {
1942 $self->_print("1..$self->{Curr_Test}\n") unless $self->no_header;
1943 $self->{Expected_Tests} = $self->{Curr_Test};
33459055 1944 }
1945
30e302f8 1946 # Auto-extended arrays and elements which aren't explicitly
1947 # filled in with a shared reference will puke under 5.8.0
1948 # ithreads. So we have to fill them in by hand. :(
ccbd73a4 1949 my $empty_result = &share( {} );
1950 for my $idx ( 0 .. $self->{Expected_Tests} - 1 ) {
5143c659 1951 $test_results->[$idx] = $empty_result
1952 unless defined $test_results->[$idx];
60ffb308 1953 }
a344be10 1954
ccbd73a4 1955 my $num_failed = grep !$_->{'ok'}, @{$test_results}[ 0 .. $self->{Curr_Test} - 1 ];
33459055 1956
b1ddf169 1957 my $num_extra = $self->{Curr_Test} - $self->{Expected_Tests};
1958
ccbd73a4 1959 if( $num_extra != 0 ) {
5143c659 1960 my $s = $self->{Expected_Tests} == 1 ? '' : 's';
33459055 1961 $self->diag(<<"FAIL");
ccbd73a4 1962Looks like you planned $self->{Expected_Tests} test$s but ran $self->{Curr_Test}.
33459055 1963FAIL
1964 }
b1ddf169 1965
ccbd73a4 1966 if($num_failed) {
b1ddf169 1967 my $num_tests = $self->{Curr_Test};
30e302f8 1968 my $s = $num_failed == 1 ? '' : 's';
b1ddf169 1969
1970 my $qualifier = $num_extra == 0 ? '' : ' run';
1971
33459055 1972 $self->diag(<<"FAIL");
b1ddf169 1973Looks like you failed $num_failed test$s of $num_tests$qualifier.
33459055 1974FAIL
1975 }
1976
ccbd73a4 1977 if($real_exit_code) {
33459055 1978 $self->diag(<<"FAIL");
ccbd73a4 1979Looks like your test exited with $real_exit_code just after $self->{Curr_Test}.
33459055 1980FAIL
1981
ccbd73a4 1982 _my_exit($real_exit_code) && return;
33459055 1983 }
1984
b1ddf169 1985 my $exit_code;
ccbd73a4 1986 if($num_failed) {
b1ddf169 1987 $exit_code = $num_failed <= 254 ? $num_failed : 254;
1988 }
1989 elsif( $num_extra != 0 ) {
1990 $exit_code = 255;
1991 }
1992 else {
1993 $exit_code = 0;
1994 }
1995
ccbd73a4 1996 _my_exit($exit_code) && return;
33459055 1997 }
ccbd73a4 1998 elsif( $self->{Skip_All} ) {
1999 _my_exit(0) && return;
33459055 2000 }
ccbd73a4 2001 elsif($real_exit_code) {
2002 $self->diag(<<"FAIL");
2003Looks like your test exited with $real_exit_code before it could output anything.
60ffb308 2004FAIL
ccbd73a4 2005 _my_exit($real_exit_code) && return;
60ffb308 2006 }
33459055 2007 else {
a9153838 2008 $self->diag("No tests run!\n");
ccbd73a4 2009 _my_exit(255) && return;
33459055 2010 }
ccbd73a4 2011
2012 $self->_whoa( 1, "We fell off the end of _ending()" );
33459055 2013}
2014
2015END {
2016 $Test->_ending if defined $Test and !$Test->no_ending;
2017}
2018
30e302f8 2019=head1 EXIT CODES
2020
2021If all your tests passed, Test::Builder will exit with zero (which is
2022normal). If anything failed it will exit with how many failed. If
2023you run less (or more) tests than you planned, the missing (or extras)
2024will be considered failures. If no tests were ever run Test::Builder
2025will throw a warning and exit with 255. If the test died, even after
2026having successfully completed all its tests, it will still be
2027considered a failure and will exit with 255.
2028
2029So the exit codes are...
2030
2031 0 all tests successful
b1ddf169 2032 255 test died or all passed but wrong # of tests run
30e302f8 2033 any other number how many failed (including missing or extras)
2034
2035If you fail more than 254 tests, it will be reported as 254.
2036
2037
a344be10 2038=head1 THREADS
2039
b7f9bbeb 2040In perl 5.8.1 and later, Test::Builder is thread-safe. The test
a344be10 2041number is shared amongst all threads. This means if one thread sets
2042the test number using current_test() they will all be effected.
2043
b7f9bbeb 2044While versions earlier than 5.8.1 had threads they contain too many
2045bugs to support.
2046
30e302f8 2047Test::Builder is only thread-aware if threads.pm is loaded I<before>
2048Test::Builder.
2049
33459055 2050=head1 EXAMPLES
2051
a344be10 2052CPAN can provide the best examples. Test::Simple, Test::More,
2053Test::Exception and Test::Differences all use Test::Builder.
33459055 2054
4bd4e70a 2055=head1 SEE ALSO
2056
2057Test::Simple, Test::More, Test::Harness
2058
2059=head1 AUTHORS
33459055 2060
2061Original code by chromatic, maintained by Michael G Schwern
2062E<lt>schwern@pobox.comE<gt>
2063
4bd4e70a 2064=head1 COPYRIGHT
33459055 2065
ccbd73a4 2066Copyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and
2067 Michael G Schwern E<lt>schwern@pobox.comE<gt>.
4bd4e70a 2068
2069This program is free software; you can redistribute it and/or
2070modify it under the same terms as Perl itself.
2071
a9153838 2072See F<http://www.perl.com/perl/misc/Artistic.html>
33459055 2073
2074=cut
2075
20761;
ccbd73a4 2077