1package Test::Builder; 2 3use 5.006; 4use strict; 5use warnings; 6 7our $VERSION = '1.302162'; 8 9BEGIN { 10 if( $] < 5.008 ) { 11 require Test::Builder::IO::Scalar; 12 } 13} 14 15use Scalar::Util qw/blessed reftype weaken/; 16 17use Test2::Util qw/USE_THREADS try get_tid/; 18use Test2::API qw/context release/; 19# Make Test::Builder thread-safe for ithreads. 20BEGIN { 21 warn "Test::Builder was loaded after Test2 initialization, this is not recommended." 22 if Test2::API::test2_init_done() || Test2::API::test2_load_done(); 23 24 if (USE_THREADS && ! Test2::API::test2_ipc_disabled()) { 25 require Test2::IPC; 26 require Test2::IPC::Driver::Files; 27 Test2::IPC::Driver::Files->import; 28 Test2::API::test2_ipc_enable_polling(); 29 Test2::API::test2_no_wait(1); 30 } 31} 32 33use Test2::Event::Subtest; 34use Test2::Hub::Subtest; 35 36use Test::Builder::Formatter; 37use Test::Builder::TodoDiag; 38 39our $Level = 1; 40our $Test = $ENV{TB_NO_EARLY_INIT} ? undef : Test::Builder->new; 41 42sub _add_ts_hooks { 43 my $self = shift; 44 45 my $hub = $self->{Stack}->top; 46 47 # Take a reference to the hash key, we do this to avoid closing over $self 48 # which is the singleton. We use a reference because the value could change 49 # in rare cases. 50 my $epkgr = \$self->{Exported_To}; 51 52 #$hub->add_context_aquire(sub {$_[0]->{level} += $Level - 1}); 53 54 $hub->pre_filter(sub { 55 my ($active_hub, $e) = @_; 56 57 my $epkg = $$epkgr; 58 my $cpkg = $e->{trace} ? $e->{trace}->{frame}->[0] : undef; 59 60 no strict 'refs'; 61 no warnings 'once'; 62 my $todo; 63 $todo = ${"$cpkg\::TODO"} if $cpkg; 64 $todo = ${"$epkg\::TODO"} if $epkg && !$todo; 65 66 return $e unless defined $todo; 67 68 # Turn a diag into a todo diag 69 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 70 71 $e->set_todo($todo) if $e->can('set_todo'); 72 $e->add_amnesty({tag => 'TODO', details => $todo}); 73 74 # Set todo on ok's 75 if ($e->isa('Test2::Event::Ok')) { 76 $e->set_effective_pass(1); 77 78 if (my $result = $e->get_meta(__PACKAGE__)) { 79 $result->{reason} ||= $todo; 80 $result->{type} ||= 'todo'; 81 $result->{ok} = 1; 82 } 83 } 84 85 return $e; 86 }, inherit => 1); 87} 88 89{ 90 no warnings; 91 INIT { 92 use warnings; 93 Test2::API::test2_load() unless Test2::API::test2_in_preload(); 94 } 95} 96 97sub new { 98 my($class) = shift; 99 unless($Test) { 100 $Test = $class->create(singleton => 1); 101 102 Test2::API::test2_add_callback_post_load( 103 sub { 104 $Test->{Original_Pid} = $$ if !$Test->{Original_Pid} || $Test->{Original_Pid} == 0; 105 $Test->reset(singleton => 1); 106 $Test->_add_ts_hooks; 107 } 108 ); 109 110 # Non-TB tools normally expect 0 added to the level. $Level is normally 1. So 111 # we only want the level to change if $Level != 1. 112 # TB->ctx compensates for this later. 113 Test2::API::test2_add_callback_context_aquire(sub { $_[0]->{level} += $Level - 1 }); 114 115 Test2::API::test2_add_callback_exit(sub { $Test->_ending(@_) }); 116 117 Test2::API::test2_ipc()->set_no_fatal(1) if Test2::API::test2_has_ipc(); 118 } 119 return $Test; 120} 121 122sub create { 123 my $class = shift; 124 my %params = @_; 125 126 my $self = bless {}, $class; 127 if ($params{singleton}) { 128 $self->{Stack} = Test2::API::test2_stack(); 129 } 130 else { 131 $self->{Stack} = Test2::API::Stack->new; 132 $self->{Stack}->new_hub( 133 formatter => Test::Builder::Formatter->new, 134 ipc => Test2::API::test2_ipc(), 135 ); 136 137 $self->reset(%params); 138 $self->_add_ts_hooks; 139 } 140 141 return $self; 142} 143 144sub ctx { 145 my $self = shift; 146 context( 147 # 1 for our frame, another for the -1 off of $Level in our hook at the top. 148 level => 2, 149 fudge => 1, 150 stack => $self->{Stack}, 151 hub => $self->{Hub}, 152 wrapped => 1, 153 @_ 154 ); 155} 156 157sub parent { 158 my $self = shift; 159 my $ctx = $self->ctx; 160 my $chub = $self->{Hub} || $ctx->hub; 161 $ctx->release; 162 163 my $meta = $chub->meta(__PACKAGE__, {}); 164 my $parent = $meta->{parent}; 165 166 return undef unless $parent; 167 168 return bless { 169 Original_Pid => $$, 170 Stack => $self->{Stack}, 171 Hub => $parent, 172 }, blessed($self); 173} 174 175sub child { 176 my( $self, $name ) = @_; 177 178 $name ||= "Child of " . $self->name; 179 my $ctx = $self->ctx; 180 181 my $parent = $ctx->hub; 182 my $pmeta = $parent->meta(__PACKAGE__, {}); 183 $self->croak("You already have a child named ($pmeta->{child}) running") 184 if $pmeta->{child}; 185 186 $pmeta->{child} = $name; 187 188 # Clear $TODO for the child. 189 my $orig_TODO = $self->find_TODO(undef, 1, undef); 190 191 my $subevents = []; 192 193 my $hub = $ctx->stack->new_hub( 194 class => 'Test2::Hub::Subtest', 195 ); 196 197 $hub->pre_filter(sub { 198 my ($active_hub, $e) = @_; 199 200 # Turn a diag into a todo diag 201 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 202 203 return $e; 204 }, inherit => 1) if $orig_TODO; 205 206 $hub->listen(sub { push @$subevents => $_[1] }); 207 208 $hub->set_nested( $parent->nested + 1 ); 209 210 my $meta = $hub->meta(__PACKAGE__, {}); 211 $meta->{Name} = $name; 212 $meta->{TODO} = $orig_TODO; 213 $meta->{TODO_PKG} = $ctx->trace->package; 214 $meta->{parent} = $parent; 215 $meta->{Test_Results} = []; 216 $meta->{subevents} = $subevents; 217 $meta->{subtest_id} = $hub->id; 218 $meta->{subtest_uuid} = $hub->uuid; 219 $meta->{subtest_buffered} = $parent->format ? 0 : 1; 220 221 $self->_add_ts_hooks; 222 223 $ctx->release; 224 return bless { Original_Pid => $$, Stack => $self->{Stack}, Hub => $hub, no_log_results => $self->{no_log_results} }, blessed($self); 225} 226 227sub finalize { 228 my $self = shift; 229 my $ok = 1; 230 ($ok) = @_ if @_; 231 232 my $st_ctx = $self->ctx; 233 my $chub = $self->{Hub} || return $st_ctx->release; 234 235 my $meta = $chub->meta(__PACKAGE__, {}); 236 if ($meta->{child}) { 237 $self->croak("Can't call finalize() with child ($meta->{child}) active"); 238 } 239 240 local $? = 0; # don't fail if $subtests happened to set $? nonzero 241 242 $self->{Stack}->pop($chub); 243 244 $self->find_TODO($meta->{TODO_PKG}, 1, $meta->{TODO}); 245 246 my $parent = $self->parent; 247 my $ctx = $parent->ctx; 248 my $trace = $ctx->trace; 249 delete $ctx->hub->meta(__PACKAGE__, {})->{child}; 250 251 $chub->finalize($trace->snapshot(hid => $chub->hid, nested => $chub->nested), 1) 252 if $ok 253 && $chub->count 254 && !$chub->no_ending 255 && !$chub->ended; 256 257 my $plan = $chub->plan || 0; 258 my $count = $chub->count; 259 my $failed = $chub->failed; 260 my $passed = $chub->is_passing; 261 262 my $num_extra = $plan =~ m/\D/ ? 0 : $count - $plan; 263 if ($count && $num_extra != 0) { 264 my $s = $plan == 1 ? '' : 's'; 265 $st_ctx->diag(<<"FAIL"); 266Looks like you planned $plan test$s but ran $count. 267FAIL 268 } 269 270 if ($failed) { 271 my $s = $failed == 1 ? '' : 's'; 272 273 my $qualifier = $num_extra == 0 ? '' : ' run'; 274 275 $st_ctx->diag(<<"FAIL"); 276Looks like you failed $failed test$s of $count$qualifier. 277FAIL 278 } 279 280 if (!$passed && !$failed && $count && !$num_extra) { 281 $st_ctx->diag(<<"FAIL"); 282All assertions inside the subtest passed, but errors were encountered. 283FAIL 284 } 285 286 $st_ctx->release; 287 288 unless ($chub->bailed_out) { 289 my $plan = $chub->plan; 290 if ( $plan && $plan eq 'SKIP' ) { 291 $parent->skip($chub->skip_reason, $meta->{Name}); 292 } 293 elsif ( !$chub->count ) { 294 $parent->ok( 0, sprintf q[No tests run for subtest "%s"], $meta->{Name} ); 295 } 296 else { 297 $parent->{subevents} = $meta->{subevents}; 298 $parent->{subtest_id} = $meta->{subtest_id}; 299 $parent->{subtest_uuid} = $meta->{subtest_uuid}; 300 $parent->{subtest_buffered} = $meta->{subtest_buffered}; 301 $parent->ok( $chub->is_passing, $meta->{Name} ); 302 } 303 } 304 305 $ctx->release; 306 return $chub->is_passing; 307} 308 309sub subtest { 310 my $self = shift; 311 my ($name, $code, @args) = @_; 312 my $ctx = $self->ctx; 313 $ctx->throw("subtest()'s second argument must be a code ref") 314 unless $code && reftype($code) eq 'CODE'; 315 316 $name ||= "Child of " . $self->name; 317 318 319 $_->($name,$code,@args) 320 for Test2::API::test2_list_pre_subtest_callbacks(); 321 322 $ctx->note("Subtest: $name"); 323 324 my $child = $self->child($name); 325 326 my $start_pid = $$; 327 my $st_ctx; 328 my ($ok, $err, $finished, $child_error); 329 T2_SUBTEST_WRAPPER: { 330 my $ctx = $self->ctx; 331 $st_ctx = $ctx->snapshot; 332 $ctx->release; 333 $ok = eval { local $Level = 1; $code->(@args); 1 }; 334 ($err, $child_error) = ($@, $?); 335 336 # They might have done 'BEGIN { skip_all => "whatever" }' 337 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) { 338 $ok = undef; 339 $err = undef; 340 } 341 else { 342 $finished = 1; 343 } 344 } 345 346 if ($start_pid != $$ && !$INC{'Test2/IPC.pm'}) { 347 warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err; 348 exit 255; 349 } 350 351 my $trace = $ctx->trace; 352 353 if (!$finished) { 354 if(my $bailed = $st_ctx->hub->bailed_out) { 355 my $chub = $child->{Hub}; 356 $self->{Stack}->pop($chub); 357 $ctx->bail($bailed->reason); 358 } 359 my $code = $st_ctx->hub->exit_code; 360 $ok = !$code; 361 $err = "Subtest ended with exit code $code" if $code; 362 } 363 364 my $st_hub = $st_ctx->hub; 365 my $plan = $st_hub->plan; 366 my $count = $st_hub->count; 367 368 if (!$count && (!defined($plan) || "$plan" ne 'SKIP')) { 369 $st_ctx->plan(0) unless defined $plan; 370 $st_ctx->diag('No tests run!'); 371 } 372 373 $child->finalize($st_ctx->trace); 374 375 $ctx->release; 376 377 die $err unless $ok; 378 379 $? = $child_error if defined $child_error; 380 381 return $st_hub->is_passing; 382} 383 384sub name { 385 my $self = shift; 386 my $ctx = $self->ctx; 387 release $ctx, $ctx->hub->meta(__PACKAGE__, {})->{Name}; 388} 389 390sub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms) 391 my ($self, %params) = @_; 392 393 Test2::API::test2_set_is_end(0); 394 395 # We leave this a global because it has to be localized and localizing 396 # hash keys is just asking for pain. Also, it was documented. 397 $Level = 1; 398 399 $self->{no_log_results} = $ENV{TEST_NO_LOG_RESULTS} ? 1 : 0 400 unless $params{singleton}; 401 402 $self->{Original_Pid} = Test2::API::test2_in_preload() ? -1 : $$; 403 404 my $ctx = $self->ctx; 405 my $hub = $ctx->hub; 406 $ctx->release; 407 unless ($params{singleton}) { 408 $hub->reset_state(); 409 $hub->_tb_reset(); 410 } 411 412 $ctx = $self->ctx; 413 414 my $meta = $ctx->hub->meta(__PACKAGE__, {}); 415 %$meta = ( 416 Name => $0, 417 Ending => 0, 418 Done_Testing => undef, 419 Skip_All => 0, 420 Test_Results => [], 421 parent => $meta->{parent}, 422 ); 423 424 $self->{Exported_To} = undef unless $params{singleton}; 425 426 $self->{Orig_Handles} ||= do { 427 my $format = $ctx->hub->format; 428 my $out; 429 if ($format && $format->isa('Test2::Formatter::TAP')) { 430 $out = $format->handles; 431 } 432 $out ? [@$out] : []; 433 }; 434 435 $self->use_numbers(1); 436 $self->no_header(0) unless $params{singleton}; 437 $self->no_ending(0) unless $params{singleton}; 438 $self->reset_outputs; 439 440 $ctx->release; 441 442 return; 443} 444 445 446my %plan_cmds = ( 447 no_plan => \&no_plan, 448 skip_all => \&skip_all, 449 tests => \&_plan_tests, 450); 451 452sub plan { 453 my( $self, $cmd, $arg ) = @_; 454 455 return unless $cmd; 456 457 my $ctx = $self->ctx; 458 my $hub = $ctx->hub; 459 460 $ctx->throw("You tried to plan twice") if $hub->plan; 461 462 local $Level = $Level + 1; 463 464 if( my $method = $plan_cmds{$cmd} ) { 465 local $Level = $Level + 1; 466 $self->$method($arg); 467 } 468 else { 469 my @args = grep { defined } ( $cmd, $arg ); 470 $ctx->throw("plan() doesn't understand @args"); 471 } 472 473 release $ctx, 1; 474} 475 476 477sub _plan_tests { 478 my($self, $arg) = @_; 479 480 my $ctx = $self->ctx; 481 482 if($arg) { 483 local $Level = $Level + 1; 484 $self->expected_tests($arg); 485 } 486 elsif( !defined $arg ) { 487 $ctx->throw("Got an undefined number of tests"); 488 } 489 else { 490 $ctx->throw("You said to run 0 tests"); 491 } 492 493 $ctx->release; 494} 495 496 497sub expected_tests { 498 my $self = shift; 499 my($max) = @_; 500 501 my $ctx = $self->ctx; 502 503 if(@_) { 504 $self->croak("Number of tests must be a positive integer. You gave it '$max'") 505 unless $max =~ /^\+?\d+$/; 506 507 $ctx->plan($max); 508 } 509 510 my $hub = $ctx->hub; 511 512 $ctx->release; 513 514 my $plan = $hub->plan; 515 return 0 unless $plan; 516 return 0 if $plan =~ m/\D/; 517 return $plan; 518} 519 520 521sub no_plan { 522 my($self, $arg) = @_; 523 524 my $ctx = $self->ctx; 525 526 if (defined $ctx->hub->plan) { 527 warn "Plan already set, no_plan() is a no-op, this will change to a hard failure in the future."; 528 $ctx->release; 529 return; 530 } 531 532 $ctx->alert("no_plan takes no arguments") if $arg; 533 534 $ctx->hub->plan('NO PLAN'); 535 536 release $ctx, 1; 537} 538 539 540sub done_testing { 541 my($self, $num_tests) = @_; 542 543 my $ctx = $self->ctx; 544 545 my $meta = $ctx->hub->meta(__PACKAGE__, {}); 546 547 if ($meta->{Done_Testing}) { 548 my ($file, $line) = @{$meta->{Done_Testing}}[1,2]; 549 local $ctx->hub->{ended}; # OMG This is awful. 550 $self->ok(0, "done_testing() was already called at $file line $line"); 551 $ctx->release; 552 return; 553 } 554 $meta->{Done_Testing} = [$ctx->trace->call]; 555 556 my $plan = $ctx->hub->plan; 557 my $count = $ctx->hub->count; 558 559 # If done_testing() specified the number of tests, shut off no_plan 560 if( defined $num_tests ) { 561 $ctx->plan($num_tests) if !$plan || $plan eq 'NO PLAN'; 562 } 563 elsif ($count && defined $num_tests && $count != $num_tests) { 564 $self->ok(0, "planned to run @{[ $self->expected_tests ]} but done_testing() expects $num_tests"); 565 } 566 else { 567 $num_tests = $self->current_test; 568 } 569 570 if( $self->expected_tests && $num_tests != $self->expected_tests ) { 571 $self->ok(0, "planned to run @{[ $self->expected_tests ]} ". 572 "but done_testing() expects $num_tests"); 573 } 574 575 $ctx->plan($num_tests) if $ctx->hub->plan && $ctx->hub->plan eq 'NO PLAN'; 576 577 $ctx->hub->finalize($ctx->trace, 1); 578 579 release $ctx, 1; 580} 581 582 583sub has_plan { 584 my $self = shift; 585 586 my $ctx = $self->ctx; 587 my $plan = $ctx->hub->plan; 588 $ctx->release; 589 590 return( $plan ) if $plan && $plan !~ m/\D/; 591 return('no_plan') if $plan && $plan eq 'NO PLAN'; 592 return(undef); 593} 594 595 596sub skip_all { 597 my( $self, $reason ) = @_; 598 599 my $ctx = $self->ctx; 600 601 $ctx->hub->meta(__PACKAGE__, {})->{Skip_All} = $reason || 1; 602 603 # Work around old perl bug 604 if ($] < 5.020000) { 605 my $begin = 0; 606 my $level = 0; 607 while (my @call = caller($level++)) { 608 last unless @call && $call[0]; 609 next unless $call[3] =~ m/::BEGIN$/; 610 $begin++; 611 last; 612 } 613 # HACK! 614 die 'Label not found for "last T2_SUBTEST_WRAPPER"' if $begin && $ctx->hub->meta(__PACKAGE__, {})->{parent}; 615 } 616 617 $ctx->plan(0, SKIP => $reason); 618} 619 620 621sub exported_to { 622 my( $self, $pack ) = @_; 623 624 if( defined $pack ) { 625 $self->{Exported_To} = $pack; 626 } 627 return $self->{Exported_To}; 628} 629 630 631sub ok { 632 my( $self, $test, $name ) = @_; 633 634 my $ctx = $self->ctx; 635 636 # $test might contain an object which we don't want to accidentally 637 # store, so we turn it into a boolean. 638 $test = $test ? 1 : 0; 639 640 # In case $name is a string overloaded object, force it to stringify. 641 no warnings qw/uninitialized numeric/; 642 $name = "$name" if defined $name; 643 644 # Profiling showed that the regex here was a huge time waster, doing the 645 # numeric addition first cuts our profile time from ~300ms to ~50ms 646 $self->diag(<<" ERR") if 0 + $name && $name =~ /^[\d\s]+$/; 647 You named your test '$name'. You shouldn't use numbers for your test names. 648 Very confusing. 649 ERR 650 use warnings qw/uninitialized numeric/; 651 652 my $trace = $ctx->{trace}; 653 my $hub = $ctx->{hub}; 654 655 my $result = { 656 ok => $test, 657 actual_ok => $test, 658 reason => '', 659 type => '', 660 (name => defined($name) ? $name : ''), 661 }; 662 663 $hub->{_meta}->{+__PACKAGE__}->{Test_Results}[ $hub->{count} ] = $result unless $self->{no_log_results}; 664 665 my $orig_name = $name; 666 667 my @attrs; 668 my $subevents = delete $self->{subevents}; 669 my $subtest_id = delete $self->{subtest_id}; 670 my $subtest_uuid = delete $self->{subtest_uuid}; 671 my $subtest_buffered = delete $self->{subtest_buffered}; 672 my $epkg = 'Test2::Event::Ok'; 673 if ($subevents) { 674 $epkg = 'Test2::Event::Subtest'; 675 push @attrs => (subevents => $subevents, subtest_id => $subtest_id, subtest_uuid => $subtest_uuid, buffered => $subtest_buffered); 676 } 677 678 my $e = bless { 679 trace => bless( {%$trace}, 'Test2::EventFacet::Trace'), 680 pass => $test, 681 name => $name, 682 _meta => {'Test::Builder' => $result}, 683 effective_pass => $test, 684 @attrs, 685 }, $epkg; 686 $hub->send($e); 687 688 $self->_ok_debug($trace, $orig_name) unless($test); 689 690 $ctx->release; 691 return $test; 692} 693 694sub _ok_debug { 695 my $self = shift; 696 my ($trace, $orig_name) = @_; 697 698 my $is_todo = defined($self->todo); 699 700 my $msg = $is_todo ? "Failed (TODO)" : "Failed"; 701 702 my (undef, $file, $line) = $trace->call; 703 if (defined $orig_name) { 704 $self->diag(qq[ $msg test '$orig_name'\n at $file line $line.\n]); 705 } 706 else { 707 $self->diag(qq[ $msg test at $file line $line.\n]); 708 } 709} 710 711sub _diag_fh { 712 my $self = shift; 713 local $Level = $Level + 1; 714 return $self->in_todo ? $self->todo_output : $self->failure_output; 715} 716 717sub _unoverload { 718 my ($self, $type, $thing) = @_; 719 720 return unless ref $$thing; 721 return unless blessed($$thing) || scalar $self->_try(sub{ $$thing->isa('UNIVERSAL') }); 722 { 723 local ($!, $@); 724 require overload; 725 } 726 my $string_meth = overload::Method( $$thing, $type ) || return; 727 $$thing = $$thing->$string_meth(); 728} 729 730sub _unoverload_str { 731 my $self = shift; 732 733 $self->_unoverload( q[""], $_ ) for @_; 734} 735 736sub _unoverload_num { 737 my $self = shift; 738 739 $self->_unoverload( '0+', $_ ) for @_; 740 741 for my $val (@_) { 742 next unless $self->_is_dualvar($$val); 743 $$val = $$val + 0; 744 } 745} 746 747# This is a hack to detect a dualvar such as $! 748sub _is_dualvar { 749 my( $self, $val ) = @_; 750 751 # Objects are not dualvars. 752 return 0 if ref $val; 753 754 no warnings 'numeric'; 755 my $numval = $val + 0; 756 return ($numval != 0 and $numval ne $val ? 1 : 0); 757} 758 759 760sub is_eq { 761 my( $self, $got, $expect, $name ) = @_; 762 763 my $ctx = $self->ctx; 764 765 local $Level = $Level + 1; 766 767 if( !defined $got || !defined $expect ) { 768 # undef only matches undef and nothing else 769 my $test = !defined $got && !defined $expect; 770 771 $self->ok( $test, $name ); 772 $self->_is_diag( $got, 'eq', $expect ) unless $test; 773 $ctx->release; 774 return $test; 775 } 776 777 release $ctx, $self->cmp_ok( $got, 'eq', $expect, $name ); 778} 779 780 781sub is_num { 782 my( $self, $got, $expect, $name ) = @_; 783 my $ctx = $self->ctx; 784 local $Level = $Level + 1; 785 786 if( !defined $got || !defined $expect ) { 787 # undef only matches undef and nothing else 788 my $test = !defined $got && !defined $expect; 789 790 $self->ok( $test, $name ); 791 $self->_is_diag( $got, '==', $expect ) unless $test; 792 $ctx->release; 793 return $test; 794 } 795 796 release $ctx, $self->cmp_ok( $got, '==', $expect, $name ); 797} 798 799 800sub _diag_fmt { 801 my( $self, $type, $val ) = @_; 802 803 if( defined $$val ) { 804 if( $type eq 'eq' or $type eq 'ne' ) { 805 # quote and force string context 806 $$val = "'$$val'"; 807 } 808 else { 809 # force numeric context 810 $self->_unoverload_num($val); 811 } 812 } 813 else { 814 $$val = 'undef'; 815 } 816 817 return; 818} 819 820 821sub _is_diag { 822 my( $self, $got, $type, $expect ) = @_; 823 824 $self->_diag_fmt( $type, $_ ) for \$got, \$expect; 825 826 local $Level = $Level + 1; 827 return $self->diag(<<"DIAGNOSTIC"); 828 got: $got 829 expected: $expect 830DIAGNOSTIC 831 832} 833 834sub _isnt_diag { 835 my( $self, $got, $type ) = @_; 836 837 $self->_diag_fmt( $type, \$got ); 838 839 local $Level = $Level + 1; 840 return $self->diag(<<"DIAGNOSTIC"); 841 got: $got 842 expected: anything else 843DIAGNOSTIC 844} 845 846 847sub isnt_eq { 848 my( $self, $got, $dont_expect, $name ) = @_; 849 my $ctx = $self->ctx; 850 local $Level = $Level + 1; 851 852 if( !defined $got || !defined $dont_expect ) { 853 # undef only matches undef and nothing else 854 my $test = defined $got || defined $dont_expect; 855 856 $self->ok( $test, $name ); 857 $self->_isnt_diag( $got, 'ne' ) unless $test; 858 $ctx->release; 859 return $test; 860 } 861 862 release $ctx, $self->cmp_ok( $got, 'ne', $dont_expect, $name ); 863} 864 865sub isnt_num { 866 my( $self, $got, $dont_expect, $name ) = @_; 867 my $ctx = $self->ctx; 868 local $Level = $Level + 1; 869 870 if( !defined $got || !defined $dont_expect ) { 871 # undef only matches undef and nothing else 872 my $test = defined $got || defined $dont_expect; 873 874 $self->ok( $test, $name ); 875 $self->_isnt_diag( $got, '!=' ) unless $test; 876 $ctx->release; 877 return $test; 878 } 879 880 release $ctx, $self->cmp_ok( $got, '!=', $dont_expect, $name ); 881} 882 883 884sub like { 885 my( $self, $thing, $regex, $name ) = @_; 886 my $ctx = $self->ctx; 887 888 local $Level = $Level + 1; 889 890 release $ctx, $self->_regex_ok( $thing, $regex, '=~', $name ); 891} 892 893sub unlike { 894 my( $self, $thing, $regex, $name ) = @_; 895 my $ctx = $self->ctx; 896 897 local $Level = $Level + 1; 898 899 release $ctx, $self->_regex_ok( $thing, $regex, '!~', $name ); 900} 901 902 903my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" ); 904 905# Bad, these are not comparison operators. Should we include more? 906my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "..."); 907 908sub cmp_ok { 909 my( $self, $got, $type, $expect, $name ) = @_; 910 my $ctx = $self->ctx; 911 912 if ($cmp_ok_bl{$type}) { 913 $ctx->throw("$type is not a valid comparison operator in cmp_ok()"); 914 } 915 916 my ($test, $succ); 917 my $error; 918 { 919 ## no critic (BuiltinFunctions::ProhibitStringyEval) 920 921 local( $@, $!, $SIG{__DIE__} ); # isolate eval 922 923 my($pack, $file, $line) = $ctx->trace->call(); 924 925 # This is so that warnings come out at the caller's level 926 $succ = eval qq[ 927#line $line "(eval in cmp_ok) $file" 928\$test = (\$got $type \$expect); 9291; 930]; 931 $error = $@; 932 } 933 local $Level = $Level + 1; 934 my $ok = $self->ok( $test, $name ); 935 936 # Treat overloaded objects as numbers if we're asked to do a 937 # numeric comparison. 938 my $unoverload 939 = $numeric_cmps{$type} 940 ? '_unoverload_num' 941 : '_unoverload_str'; 942 943 $self->diag(<<"END") unless $succ; 944An error occurred while using $type: 945------------------------------------ 946$error 947------------------------------------ 948END 949 950 unless($ok) { 951 $self->$unoverload( \$got, \$expect ); 952 953 if( $type =~ /^(eq|==)$/ ) { 954 $self->_is_diag( $got, $type, $expect ); 955 } 956 elsif( $type =~ /^(ne|!=)$/ ) { 957 no warnings; 958 my $eq = ($got eq $expect || $got == $expect) 959 && ( 960 (defined($got) xor defined($expect)) 961 || (length($got) != length($expect)) 962 ); 963 use warnings; 964 965 if ($eq) { 966 $self->_cmp_diag( $got, $type, $expect ); 967 } 968 else { 969 $self->_isnt_diag( $got, $type ); 970 } 971 } 972 else { 973 $self->_cmp_diag( $got, $type, $expect ); 974 } 975 } 976 return release $ctx, $ok; 977} 978 979sub _cmp_diag { 980 my( $self, $got, $type, $expect ) = @_; 981 982 $got = defined $got ? "'$got'" : 'undef'; 983 $expect = defined $expect ? "'$expect'" : 'undef'; 984 985 local $Level = $Level + 1; 986 return $self->diag(<<"DIAGNOSTIC"); 987 $got 988 $type 989 $expect 990DIAGNOSTIC 991} 992 993sub _caller_context { 994 my $self = shift; 995 996 my( $pack, $file, $line ) = $self->caller(1); 997 998 my $code = ''; 999 $code .= "#line $line $file\n" if defined $file and defined $line; 1000 1001 return $code; 1002} 1003 1004 1005sub BAIL_OUT { 1006 my( $self, $reason ) = @_; 1007 1008 my $ctx = $self->ctx; 1009 1010 $self->{Bailed_Out} = 1; 1011 1012 $ctx->bail($reason); 1013} 1014 1015 1016{ 1017 no warnings 'once'; 1018 *BAILOUT = \&BAIL_OUT; 1019} 1020 1021sub skip { 1022 my( $self, $why, $name ) = @_; 1023 $why ||= ''; 1024 $name = '' unless defined $name; 1025 $self->_unoverload_str( \$why ); 1026 1027 my $ctx = $self->ctx; 1028 1029 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = { 1030 'ok' => 1, 1031 actual_ok => 1, 1032 name => $name, 1033 type => 'skip', 1034 reason => $why, 1035 } unless $self->{no_log_results}; 1036 1037 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness. 1038 $name =~ s{\n}{\n# }sg; 1039 $why =~ s{\n}{\n# }sg; 1040 1041 my $tctx = $ctx->snapshot; 1042 $tctx->skip('', $why); 1043 1044 return release $ctx, 1; 1045} 1046 1047 1048sub todo_skip { 1049 my( $self, $why ) = @_; 1050 $why ||= ''; 1051 1052 my $ctx = $self->ctx; 1053 1054 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = { 1055 'ok' => 1, 1056 actual_ok => 0, 1057 name => '', 1058 type => 'todo_skip', 1059 reason => $why, 1060 } unless $self->{no_log_results}; 1061 1062 $why =~ s{\n}{\n# }sg; 1063 my $tctx = $ctx->snapshot; 1064 $tctx->send_event( 'Skip', todo => $why, todo_diag => 1, reason => $why, pass => 0); 1065 1066 return release $ctx, 1; 1067} 1068 1069 1070sub maybe_regex { 1071 my( $self, $regex ) = @_; 1072 my $usable_regex = undef; 1073 1074 return $usable_regex unless defined $regex; 1075 1076 my( $re, $opts ); 1077 1078 # Check for qr/foo/ 1079 if( _is_qr($regex) ) { 1080 $usable_regex = $regex; 1081 } 1082 # Check for '/foo/' or 'm,foo,' 1083 elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or 1084 ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx 1085 ) 1086 { 1087 $usable_regex = length $opts ? "(?$opts)$re" : $re; 1088 } 1089 1090 return $usable_regex; 1091} 1092 1093sub _is_qr { 1094 my $regex = shift; 1095 1096 # is_regexp() checks for regexes in a robust manner, say if they're 1097 # blessed. 1098 return re::is_regexp($regex) if defined &re::is_regexp; 1099 return ref $regex eq 'Regexp'; 1100} 1101 1102sub _regex_ok { 1103 my( $self, $thing, $regex, $cmp, $name ) = @_; 1104 1105 my $ok = 0; 1106 my $usable_regex = $self->maybe_regex($regex); 1107 unless( defined $usable_regex ) { 1108 local $Level = $Level + 1; 1109 $ok = $self->ok( 0, $name ); 1110 $self->diag(" '$regex' doesn't look much like a regex to me."); 1111 return $ok; 1112 } 1113 1114 { 1115 my $test; 1116 my $context = $self->_caller_context; 1117 1118 { 1119 ## no critic (BuiltinFunctions::ProhibitStringyEval) 1120 1121 local( $@, $!, $SIG{__DIE__} ); # isolate eval 1122 1123 # No point in issuing an uninit warning, they'll see it in the diagnostics 1124 no warnings 'uninitialized'; 1125 1126 $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0}; 1127 } 1128 1129 $test = !$test if $cmp eq '!~'; 1130 1131 local $Level = $Level + 1; 1132 $ok = $self->ok( $test, $name ); 1133 } 1134 1135 unless($ok) { 1136 $thing = defined $thing ? "'$thing'" : 'undef'; 1137 my $match = $cmp eq '=~' ? "doesn't match" : "matches"; 1138 1139 local $Level = $Level + 1; 1140 $self->diag( sprintf <<'DIAGNOSTIC', $thing, $match, $regex ); 1141 %s 1142 %13s '%s' 1143DIAGNOSTIC 1144 1145 } 1146 1147 return $ok; 1148} 1149 1150 1151sub is_fh { 1152 my $self = shift; 1153 my $maybe_fh = shift; 1154 return 0 unless defined $maybe_fh; 1155 1156 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref 1157 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob 1158 1159 return eval { $maybe_fh->isa("IO::Handle") } || 1160 eval { tied($maybe_fh)->can('TIEHANDLE') }; 1161} 1162 1163 1164sub level { 1165 my( $self, $level ) = @_; 1166 1167 if( defined $level ) { 1168 $Level = $level; 1169 } 1170 return $Level; 1171} 1172 1173 1174sub use_numbers { 1175 my( $self, $use_nums ) = @_; 1176 1177 my $ctx = $self->ctx; 1178 my $format = $ctx->hub->format; 1179 unless ($format && $format->can('no_numbers') && $format->can('set_no_numbers')) { 1180 warn "The current formatter does not support 'use_numbers'" if $format; 1181 return release $ctx, 0; 1182 } 1183 1184 $format->set_no_numbers(!$use_nums) if defined $use_nums; 1185 1186 return release $ctx, $format->no_numbers ? 0 : 1; 1187} 1188 1189BEGIN { 1190 for my $method (qw(no_header no_diag)) { 1191 my $set = "set_$method"; 1192 my $code = sub { 1193 my( $self, $no ) = @_; 1194 1195 my $ctx = $self->ctx; 1196 my $format = $ctx->hub->format; 1197 unless ($format && $format->can($set)) { 1198 warn "The current formatter does not support '$method'" if $format; 1199 $ctx->release; 1200 return 1201 } 1202 1203 $format->$set($no) if defined $no; 1204 1205 return release $ctx, $format->$method ? 1 : 0; 1206 }; 1207 1208 no strict 'refs'; ## no critic 1209 *$method = $code; 1210 } 1211} 1212 1213sub no_ending { 1214 my( $self, $no ) = @_; 1215 1216 my $ctx = $self->ctx; 1217 1218 $ctx->hub->set_no_ending($no) if defined $no; 1219 1220 return release $ctx, $ctx->hub->no_ending; 1221} 1222 1223sub diag { 1224 my $self = shift; 1225 return unless @_; 1226 1227 my $text = join '' => map {defined($_) ? $_ : 'undef'} @_; 1228 1229 if (Test2::API::test2_in_preload()) { 1230 chomp($text); 1231 $text =~ s/^/# /msg; 1232 print STDERR $text, "\n"; 1233 return 0; 1234 } 1235 1236 my $ctx = $self->ctx; 1237 $ctx->diag($text); 1238 $ctx->release; 1239 return 0; 1240} 1241 1242 1243sub note { 1244 my $self = shift; 1245 return unless @_; 1246 1247 my $text = join '' => map {defined($_) ? $_ : 'undef'} @_; 1248 1249 if (Test2::API::test2_in_preload()) { 1250 chomp($text); 1251 $text =~ s/^/# /msg; 1252 print STDOUT $text, "\n"; 1253 return 0; 1254 } 1255 1256 my $ctx = $self->ctx; 1257 $ctx->note($text); 1258 $ctx->release; 1259 return 0; 1260} 1261 1262 1263sub explain { 1264 my $self = shift; 1265 1266 local ($@, $!); 1267 require Data::Dumper; 1268 1269 return map { 1270 ref $_ 1271 ? do { 1272 my $dumper = Data::Dumper->new( [$_] ); 1273 $dumper->Indent(1)->Terse(1); 1274 $dumper->Sortkeys(1) if $dumper->can("Sortkeys"); 1275 $dumper->Dump; 1276 } 1277 : $_ 1278 } @_; 1279} 1280 1281 1282sub output { 1283 my( $self, $fh ) = @_; 1284 1285 my $ctx = $self->ctx; 1286 my $format = $ctx->hub->format; 1287 $ctx->release; 1288 return unless $format && $format->isa('Test2::Formatter::TAP'); 1289 1290 $format->handles->[Test2::Formatter::TAP::OUT_STD()] = $self->_new_fh($fh) 1291 if defined $fh; 1292 1293 return $format->handles->[Test2::Formatter::TAP::OUT_STD()]; 1294} 1295 1296sub failure_output { 1297 my( $self, $fh ) = @_; 1298 1299 my $ctx = $self->ctx; 1300 my $format = $ctx->hub->format; 1301 $ctx->release; 1302 return unless $format && $format->isa('Test2::Formatter::TAP'); 1303 1304 $format->handles->[Test2::Formatter::TAP::OUT_ERR()] = $self->_new_fh($fh) 1305 if defined $fh; 1306 1307 return $format->handles->[Test2::Formatter::TAP::OUT_ERR()]; 1308} 1309 1310sub todo_output { 1311 my( $self, $fh ) = @_; 1312 1313 my $ctx = $self->ctx; 1314 my $format = $ctx->hub->format; 1315 $ctx->release; 1316 return unless $format && $format->isa('Test::Builder::Formatter'); 1317 1318 $format->handles->[Test::Builder::Formatter::OUT_TODO()] = $self->_new_fh($fh) 1319 if defined $fh; 1320 1321 return $format->handles->[Test::Builder::Formatter::OUT_TODO()]; 1322} 1323 1324sub _new_fh { 1325 my $self = shift; 1326 my($file_or_fh) = shift; 1327 1328 my $fh; 1329 if( $self->is_fh($file_or_fh) ) { 1330 $fh = $file_or_fh; 1331 } 1332 elsif( ref $file_or_fh eq 'SCALAR' ) { 1333 # Scalar refs as filehandles was added in 5.8. 1334 if( $] >= 5.008 ) { 1335 open $fh, ">>", $file_or_fh 1336 or $self->croak("Can't open scalar ref $file_or_fh: $!"); 1337 } 1338 # Emulate scalar ref filehandles with a tie. 1339 else { 1340 $fh = Test::Builder::IO::Scalar->new($file_or_fh) 1341 or $self->croak("Can't tie scalar ref $file_or_fh"); 1342 } 1343 } 1344 else { 1345 open $fh, ">", $file_or_fh 1346 or $self->croak("Can't open test output log $file_or_fh: $!"); 1347 _autoflush($fh); 1348 } 1349 1350 return $fh; 1351} 1352 1353sub _autoflush { 1354 my($fh) = shift; 1355 my $old_fh = select $fh; 1356 $| = 1; 1357 select $old_fh; 1358 1359 return; 1360} 1361 1362 1363sub reset_outputs { 1364 my $self = shift; 1365 1366 my $ctx = $self->ctx; 1367 my $format = $ctx->hub->format; 1368 $ctx->release; 1369 return unless $format && $format->isa('Test2::Formatter::TAP'); 1370 $format->set_handles([@{$self->{Orig_Handles}}]) if $self->{Orig_Handles}; 1371 1372 return; 1373} 1374 1375 1376sub carp { 1377 my $self = shift; 1378 my $ctx = $self->ctx; 1379 $ctx->alert(join "", @_); 1380 $ctx->release; 1381} 1382 1383sub croak { 1384 my $self = shift; 1385 my $ctx = $self->ctx; 1386 $ctx->throw(join "", @_); 1387 $ctx->release; 1388} 1389 1390 1391sub current_test { 1392 my( $self, $num ) = @_; 1393 1394 my $ctx = $self->ctx; 1395 my $hub = $ctx->hub; 1396 1397 if( defined $num ) { 1398 $hub->set_count($num); 1399 1400 unless ($self->{no_log_results}) { 1401 # If the test counter is being pushed forward fill in the details. 1402 my $test_results = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 1403 if ($num > @$test_results) { 1404 my $start = @$test_results ? @$test_results : 0; 1405 for ($start .. $num - 1) { 1406 $test_results->[$_] = { 1407 'ok' => 1, 1408 actual_ok => undef, 1409 reason => 'incrementing test number', 1410 type => 'unknown', 1411 name => undef 1412 }; 1413 } 1414 } 1415 # If backward, wipe history. Its their funeral. 1416 elsif ($num < @$test_results) { 1417 $#{$test_results} = $num - 1; 1418 } 1419 } 1420 } 1421 return release $ctx, $hub->count; 1422} 1423 1424 1425sub is_passing { 1426 my $self = shift; 1427 1428 my $ctx = $self->ctx; 1429 my $hub = $ctx->hub; 1430 1431 if( @_ ) { 1432 my ($bool) = @_; 1433 $hub->set_failed(0) if $bool; 1434 $hub->is_passing($bool); 1435 } 1436 1437 return release $ctx, $hub->is_passing; 1438} 1439 1440 1441sub summary { 1442 my($self) = shift; 1443 1444 return if $self->{no_log_results}; 1445 1446 my $ctx = $self->ctx; 1447 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 1448 $ctx->release; 1449 return map { $_ ? $_->{'ok'} : () } @$data; 1450} 1451 1452 1453sub details { 1454 my $self = shift; 1455 1456 return if $self->{no_log_results}; 1457 1458 my $ctx = $self->ctx; 1459 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 1460 $ctx->release; 1461 return @$data; 1462} 1463 1464 1465sub find_TODO { 1466 my( $self, $pack, $set, $new_value ) = @_; 1467 1468 my $ctx = $self->ctx; 1469 1470 $pack ||= $ctx->trace->package || $self->exported_to; 1471 $ctx->release; 1472 1473 return unless $pack; 1474 1475 no strict 'refs'; ## no critic 1476 no warnings 'once'; 1477 my $old_value = ${ $pack . '::TODO' }; 1478 $set and ${ $pack . '::TODO' } = $new_value; 1479 return $old_value; 1480} 1481 1482sub todo { 1483 my( $self, $pack ) = @_; 1484 1485 local $Level = $Level + 1; 1486 my $ctx = $self->ctx; 1487 $ctx->release; 1488 1489 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}; 1490 return $meta->[-1]->[1] if $meta && @$meta; 1491 1492 $pack ||= $ctx->trace->package; 1493 1494 return unless $pack; 1495 1496 no strict 'refs'; ## no critic 1497 no warnings 'once'; 1498 return ${ $pack . '::TODO' }; 1499} 1500 1501sub in_todo { 1502 my $self = shift; 1503 1504 local $Level = $Level + 1; 1505 my $ctx = $self->ctx; 1506 $ctx->release; 1507 1508 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}; 1509 return 1 if $meta && @$meta; 1510 1511 my $pack = $ctx->trace->package || return 0; 1512 1513 no strict 'refs'; ## no critic 1514 no warnings 'once'; 1515 my $todo = ${ $pack . '::TODO' }; 1516 1517 return 0 unless defined $todo; 1518 return 0 if "$todo" eq ''; 1519 return 1; 1520} 1521 1522sub todo_start { 1523 my $self = shift; 1524 my $message = @_ ? shift : ''; 1525 1526 my $ctx = $self->ctx; 1527 1528 my $hub = $ctx->hub; 1529 my $filter = $hub->pre_filter(sub { 1530 my ($active_hub, $e) = @_; 1531 1532 # Turn a diag into a todo diag 1533 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 1534 1535 # Set todo on ok's 1536 if ($hub == $active_hub && $e->isa('Test2::Event::Ok')) { 1537 $e->set_todo($message); 1538 $e->set_effective_pass(1); 1539 1540 if (my $result = $e->get_meta(__PACKAGE__)) { 1541 $result->{reason} ||= $message; 1542 $result->{type} ||= 'todo'; 1543 $result->{ok} = 1; 1544 } 1545 } 1546 1547 return $e; 1548 }, inherit => 1); 1549 1550 push @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}} => [$filter, $message]; 1551 1552 $ctx->release; 1553 1554 return; 1555} 1556 1557sub todo_end { 1558 my $self = shift; 1559 1560 my $ctx = $self->ctx; 1561 1562 my $set = pop @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}}; 1563 1564 $ctx->throw('todo_end() called without todo_start()') unless $set; 1565 1566 $ctx->hub->pre_unfilter($set->[0]); 1567 1568 $ctx->release; 1569 1570 return; 1571} 1572 1573 1574sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms) 1575 my( $self ) = @_; 1576 1577 my $ctx = $self->ctx; 1578 1579 my $trace = $ctx->trace; 1580 $ctx->release; 1581 return wantarray ? $trace->call : $trace->package; 1582} 1583 1584 1585sub _try { 1586 my( $self, $code, %opts ) = @_; 1587 1588 my $error; 1589 my $return; 1590 { 1591 local $!; # eval can mess up $! 1592 local $@; # don't set $@ in the test 1593 local $SIG{__DIE__}; # don't trip an outside DIE handler. 1594 $return = eval { $code->() }; 1595 $error = $@; 1596 } 1597 1598 die $error if $error and $opts{die_on_fail}; 1599 1600 return wantarray ? ( $return, $error ) : $return; 1601} 1602 1603sub _ending { 1604 my $self = shift; 1605 my ($ctx, $real_exit_code, $new) = @_; 1606 1607 unless ($ctx) { 1608 my $octx = $self->ctx; 1609 $ctx = $octx->snapshot; 1610 $octx->release; 1611 } 1612 1613 return if $ctx->hub->no_ending; 1614 return if $ctx->hub->meta(__PACKAGE__, {})->{Ending}++; 1615 1616 # Don't bother with an ending if this is a forked copy. Only the parent 1617 # should do the ending. 1618 return unless $self->{Original_Pid} == $$; 1619 1620 my $hub = $ctx->hub; 1621 return if $hub->bailed_out; 1622 1623 my $plan = $hub->plan; 1624 my $count = $hub->count; 1625 my $failed = $hub->failed; 1626 my $passed = $hub->is_passing; 1627 return unless $plan || $count || $failed; 1628 1629 # Ran tests but never declared a plan or hit done_testing 1630 if( !$hub->plan and $hub->count ) { 1631 $self->diag("Tests were run but no plan was declared and done_testing() was not seen."); 1632 1633 if($real_exit_code) { 1634 $self->diag(<<"FAIL"); 1635Looks like your test exited with $real_exit_code just after $count. 1636FAIL 1637 $$new ||= $real_exit_code; 1638 return; 1639 } 1640 1641 # But if the tests ran, handle exit code. 1642 if($failed > 0) { 1643 my $exit_code = $failed <= 254 ? $failed : 254; 1644 $$new ||= $exit_code; 1645 return; 1646 } 1647 1648 $$new ||= 254; 1649 return; 1650 } 1651 1652 if ($real_exit_code && !$count) { 1653 $self->diag("Looks like your test exited with $real_exit_code before it could output anything."); 1654 $$new ||= $real_exit_code; 1655 return; 1656 } 1657 1658 return if $plan && "$plan" eq 'SKIP'; 1659 1660 if (!$count) { 1661 $self->diag('No tests run!'); 1662 $$new ||= 255; 1663 return; 1664 } 1665 1666 if ($real_exit_code) { 1667 $self->diag(<<"FAIL"); 1668Looks like your test exited with $real_exit_code just after $count. 1669FAIL 1670 $$new ||= $real_exit_code; 1671 return; 1672 } 1673 1674 if ($plan eq 'NO PLAN') { 1675 $ctx->plan( $count ); 1676 $plan = $hub->plan; 1677 } 1678 1679 # Figure out if we passed or failed and print helpful messages. 1680 my $num_extra = $count - $plan; 1681 1682 if ($num_extra != 0) { 1683 my $s = $plan == 1 ? '' : 's'; 1684 $self->diag(<<"FAIL"); 1685Looks like you planned $plan test$s but ran $count. 1686FAIL 1687 } 1688 1689 if ($failed) { 1690 my $s = $failed == 1 ? '' : 's'; 1691 1692 my $qualifier = $num_extra == 0 ? '' : ' run'; 1693 1694 $self->diag(<<"FAIL"); 1695Looks like you failed $failed test$s of $count$qualifier. 1696FAIL 1697 } 1698 1699 if (!$passed && !$failed && $count && !$num_extra) { 1700 $ctx->diag(<<"FAIL"); 1701All assertions passed, but errors were encountered. 1702FAIL 1703 } 1704 1705 my $exit_code = 0; 1706 if ($failed) { 1707 $exit_code = $failed <= 254 ? $failed : 254; 1708 } 1709 elsif ($num_extra != 0) { 1710 $exit_code = 255; 1711 } 1712 elsif (!$passed) { 1713 $exit_code = 255; 1714 } 1715 1716 $$new ||= $exit_code; 1717 return; 1718} 1719 1720# Some things used this even though it was private... I am looking at you 1721# Test::Builder::Prefix... 1722sub _print_comment { 1723 my( $self, $fh, @msgs ) = @_; 1724 1725 return if $self->no_diag; 1726 return unless @msgs; 1727 1728 # Prevent printing headers when compiling (i.e. -c) 1729 return if $^C; 1730 1731 # Smash args together like print does. 1732 # Convert undef to 'undef' so its readable. 1733 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs; 1734 1735 # Escape the beginning, _print will take care of the rest. 1736 $msg =~ s/^/# /; 1737 1738 local( $\, $", $, ) = ( undef, ' ', '' ); 1739 print $fh $msg; 1740 1741 return 0; 1742} 1743 1744# This is used by Test::SharedFork to turn on IPC after the fact. Not 1745# documenting because I do not want it used. The method name is borrowed from 1746# Test::Builder 2 1747# Once Test2 stuff goes stable this method will be removed and Test::SharedFork 1748# will be made smarter. 1749sub coordinate_forks { 1750 my $self = shift; 1751 1752 { 1753 local ($@, $!); 1754 require Test2::IPC; 1755 } 1756 Test2::IPC->import; 1757 Test2::API::test2_ipc_enable_polling(); 1758 Test2::API::test2_load(); 1759 my $ipc = Test2::IPC::apply_ipc($self->{Stack}); 1760 $ipc->set_no_fatal(1); 1761 Test2::API::test2_no_wait(1); 1762} 1763 1764sub no_log_results { $_[0]->{no_log_results} = 1 } 1765 17661; 1767 1768__END__ 1769 1770=head1 NAME 1771 1772Test::Builder - Backend for building test libraries 1773 1774=head1 SYNOPSIS 1775 1776 package My::Test::Module; 1777 use base 'Test::Builder::Module'; 1778 1779 my $CLASS = __PACKAGE__; 1780 1781 sub ok { 1782 my($test, $name) = @_; 1783 my $tb = $CLASS->builder; 1784 1785 $tb->ok($test, $name); 1786 } 1787 1788 1789=head1 DESCRIPTION 1790 1791L<Test::Simple> and L<Test::More> have proven to be popular testing modules, 1792but they're not always flexible enough. Test::Builder provides a 1793building block upon which to write your own test libraries I<which can 1794work together>. 1795 1796=head2 Construction 1797 1798=over 4 1799 1800=item B<new> 1801 1802 my $Test = Test::Builder->new; 1803 1804Returns a Test::Builder object representing the current state of the 1805test. 1806 1807Since you only run one test per program C<new> always returns the same 1808Test::Builder object. No matter how many times you call C<new()>, you're 1809getting the same object. This is called a singleton. This is done so that 1810multiple modules share such global information as the test counter and 1811where test output is going. 1812 1813If you want a completely new Test::Builder object different from the 1814singleton, use C<create>. 1815 1816=item B<create> 1817 1818 my $Test = Test::Builder->create; 1819 1820Ok, so there can be more than one Test::Builder object and this is how 1821you get it. You might use this instead of C<new()> if you're testing 1822a Test::Builder based module, but otherwise you probably want C<new>. 1823 1824B<NOTE>: the implementation is not complete. C<level>, for example, is still 1825shared by B<all> Test::Builder objects, even ones created using this method. 1826Also, the method name may change in the future. 1827 1828=item B<subtest> 1829 1830 $builder->subtest($name, \&subtests, @args); 1831 1832See documentation of C<subtest> in Test::More. 1833 1834C<subtest> also, and optionally, accepts arguments which will be passed to the 1835subtests reference. 1836 1837=item B<name> 1838 1839 diag $builder->name; 1840 1841Returns the name of the current builder. Top level builders default to C<$0> 1842(the name of the executable). Child builders are named via the C<child> 1843method. If no name is supplied, will be named "Child of $parent->name". 1844 1845=item B<reset> 1846 1847 $Test->reset; 1848 1849Reinitializes the Test::Builder singleton to its original state. 1850Mostly useful for tests run in persistent environments where the same 1851test might be run multiple times in the same process. 1852 1853=back 1854 1855=head2 Setting up tests 1856 1857These methods are for setting up tests and declaring how many there 1858are. You usually only want to call one of these methods. 1859 1860=over 4 1861 1862=item B<plan> 1863 1864 $Test->plan('no_plan'); 1865 $Test->plan( skip_all => $reason ); 1866 $Test->plan( tests => $num_tests ); 1867 1868A convenient way to set up your tests. Call this and Test::Builder 1869will print the appropriate headers and take the appropriate actions. 1870 1871If you call C<plan()>, don't call any of the other methods below. 1872 1873=item B<expected_tests> 1874 1875 my $max = $Test->expected_tests; 1876 $Test->expected_tests($max); 1877 1878Gets/sets the number of tests we expect this test to run and prints out 1879the appropriate headers. 1880 1881 1882=item B<no_plan> 1883 1884 $Test->no_plan; 1885 1886Declares that this test will run an indeterminate number of tests. 1887 1888 1889=item B<done_testing> 1890 1891 $Test->done_testing(); 1892 $Test->done_testing($num_tests); 1893 1894Declares that you are done testing, no more tests will be run after this point. 1895 1896If a plan has not yet been output, it will do so. 1897 1898$num_tests is the number of tests you planned to run. If a numbered 1899plan was already declared, and if this contradicts, a failing test 1900will be run to reflect the planning mistake. If C<no_plan> was declared, 1901this will override. 1902 1903If C<done_testing()> is called twice, the second call will issue a 1904failing test. 1905 1906If C<$num_tests> is omitted, the number of tests run will be used, like 1907no_plan. 1908 1909C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but 1910safer. You'd use it like so: 1911 1912 $Test->ok($a == $b); 1913 $Test->done_testing(); 1914 1915Or to plan a variable number of tests: 1916 1917 for my $test (@tests) { 1918 $Test->ok($test); 1919 } 1920 $Test->done_testing(scalar @tests); 1921 1922 1923=item B<has_plan> 1924 1925 $plan = $Test->has_plan 1926 1927Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan 1928has been set), C<no_plan> (indeterminate # of tests) or an integer (the number 1929of expected tests). 1930 1931=item B<skip_all> 1932 1933 $Test->skip_all; 1934 $Test->skip_all($reason); 1935 1936Skips all the tests, using the given C<$reason>. Exits immediately with 0. 1937 1938=item B<exported_to> 1939 1940 my $pack = $Test->exported_to; 1941 $Test->exported_to($pack); 1942 1943Tells Test::Builder what package you exported your functions to. 1944 1945This method isn't terribly useful since modules which share the same 1946Test::Builder object might get exported to different packages and only 1947the last one will be honored. 1948 1949=back 1950 1951=head2 Running tests 1952 1953These actually run the tests, analogous to the functions in Test::More. 1954 1955They all return true if the test passed, false if the test failed. 1956 1957C<$name> is always optional. 1958 1959=over 4 1960 1961=item B<ok> 1962 1963 $Test->ok($test, $name); 1964 1965Your basic test. Pass if C<$test> is true, fail if $test is false. Just 1966like Test::Simple's C<ok()>. 1967 1968=item B<is_eq> 1969 1970 $Test->is_eq($got, $expected, $name); 1971 1972Like Test::More's C<is()>. Checks if C<$got eq $expected>. This is the 1973string version. 1974 1975C<undef> only ever matches another C<undef>. 1976 1977=item B<is_num> 1978 1979 $Test->is_num($got, $expected, $name); 1980 1981Like Test::More's C<is()>. Checks if C<$got == $expected>. This is the 1982numeric version. 1983 1984C<undef> only ever matches another C<undef>. 1985 1986=item B<isnt_eq> 1987 1988 $Test->isnt_eq($got, $dont_expect, $name); 1989 1990Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is 1991the string version. 1992 1993=item B<isnt_num> 1994 1995 $Test->isnt_num($got, $dont_expect, $name); 1996 1997Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is 1998the numeric version. 1999 2000=item B<like> 2001 2002 $Test->like($thing, qr/$regex/, $name); 2003 $Test->like($thing, '/$regex/', $name); 2004 2005Like L<Test::More>'s C<like()>. Checks if $thing matches the given C<$regex>. 2006 2007=item B<unlike> 2008 2009 $Test->unlike($thing, qr/$regex/, $name); 2010 $Test->unlike($thing, '/$regex/', $name); 2011 2012Like L<Test::More>'s C<unlike()>. Checks if $thing B<does not match> the 2013given C<$regex>. 2014 2015=item B<cmp_ok> 2016 2017 $Test->cmp_ok($thing, $type, $that, $name); 2018 2019Works just like L<Test::More>'s C<cmp_ok()>. 2020 2021 $Test->cmp_ok($big_num, '!=', $other_big_num); 2022 2023=back 2024 2025=head2 Other Testing Methods 2026 2027These are methods which are used in the course of writing a test but are not themselves tests. 2028 2029=over 4 2030 2031=item B<BAIL_OUT> 2032 2033 $Test->BAIL_OUT($reason); 2034 2035Indicates to the L<Test::Harness> that things are going so badly all 2036testing should terminate. This includes running any additional test 2037scripts. 2038 2039It will exit with 255. 2040 2041=for deprecated 2042BAIL_OUT() used to be BAILOUT() 2043 2044=item B<skip> 2045 2046 $Test->skip; 2047 $Test->skip($why); 2048 2049Skips the current test, reporting C<$why>. 2050 2051=item B<todo_skip> 2052 2053 $Test->todo_skip; 2054 $Test->todo_skip($why); 2055 2056Like C<skip()>, only it will declare the test as failing and TODO. Similar 2057to 2058 2059 print "not ok $tnum # TODO $why\n"; 2060 2061=begin _unimplemented 2062 2063=item B<skip_rest> 2064 2065 $Test->skip_rest; 2066 $Test->skip_rest($reason); 2067 2068Like C<skip()>, only it skips all the rest of the tests you plan to run 2069and terminates the test. 2070 2071If you're running under C<no_plan>, it skips once and terminates the 2072test. 2073 2074=end _unimplemented 2075 2076=back 2077 2078 2079=head2 Test building utility methods 2080 2081These methods are useful when writing your own test methods. 2082 2083=over 4 2084 2085=item B<maybe_regex> 2086 2087 $Test->maybe_regex(qr/$regex/); 2088 $Test->maybe_regex('/$regex/'); 2089 2090This method used to be useful back when Test::Builder worked on Perls 2091before 5.6 which didn't have qr//. Now its pretty useless. 2092 2093Convenience method for building testing functions that take regular 2094expressions as arguments. 2095 2096Takes a quoted regular expression produced by C<qr//>, or a string 2097representing a regular expression. 2098 2099Returns a Perl value which may be used instead of the corresponding 2100regular expression, or C<undef> if its argument is not recognized. 2101 2102For example, a version of C<like()>, sans the useful diagnostic messages, 2103could be written as: 2104 2105 sub laconic_like { 2106 my ($self, $thing, $regex, $name) = @_; 2107 my $usable_regex = $self->maybe_regex($regex); 2108 die "expecting regex, found '$regex'\n" 2109 unless $usable_regex; 2110 $self->ok($thing =~ m/$usable_regex/, $name); 2111 } 2112 2113 2114=item B<is_fh> 2115 2116 my $is_fh = $Test->is_fh($thing); 2117 2118Determines if the given C<$thing> can be used as a filehandle. 2119 2120=cut 2121 2122 2123=back 2124 2125 2126=head2 Test style 2127 2128 2129=over 4 2130 2131=item B<level> 2132 2133 $Test->level($how_high); 2134 2135How far up the call stack should C<$Test> look when reporting where the 2136test failed. 2137 2138Defaults to 1. 2139 2140Setting C<$Test::Builder::Level> overrides. This is typically useful 2141localized: 2142 2143 sub my_ok { 2144 my $test = shift; 2145 2146 local $Test::Builder::Level = $Test::Builder::Level + 1; 2147 $TB->ok($test); 2148 } 2149 2150To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant. 2151 2152=item B<use_numbers> 2153 2154 $Test->use_numbers($on_or_off); 2155 2156Whether or not the test should output numbers. That is, this if true: 2157 2158 ok 1 2159 ok 2 2160 ok 3 2161 2162or this if false 2163 2164 ok 2165 ok 2166 ok 2167 2168Most useful when you can't depend on the test output order, such as 2169when threads or forking is involved. 2170 2171Defaults to on. 2172 2173=item B<no_diag> 2174 2175 $Test->no_diag($no_diag); 2176 2177If set true no diagnostics will be printed. This includes calls to 2178C<diag()>. 2179 2180=item B<no_ending> 2181 2182 $Test->no_ending($no_ending); 2183 2184Normally, Test::Builder does some extra diagnostics when the test 2185ends. It also changes the exit code as described below. 2186 2187If this is true, none of that will be done. 2188 2189=item B<no_header> 2190 2191 $Test->no_header($no_header); 2192 2193If set to true, no "1..N" header will be printed. 2194 2195=back 2196 2197=head2 Output 2198 2199Controlling where the test output goes. 2200 2201It's ok for your test to change where STDOUT and STDERR point to, 2202Test::Builder's default output settings will not be affected. 2203 2204=over 4 2205 2206=item B<diag> 2207 2208 $Test->diag(@msgs); 2209 2210Prints out the given C<@msgs>. Like C<print>, arguments are simply 2211appended together. 2212 2213Normally, it uses the C<failure_output()> handle, but if this is for a 2214TODO test, the C<todo_output()> handle is used. 2215 2216Output will be indented and marked with a # so as not to interfere 2217with test output. A newline will be put on the end if there isn't one 2218already. 2219 2220We encourage using this rather than calling print directly. 2221 2222Returns false. Why? Because C<diag()> is often used in conjunction with 2223a failing test (C<ok() || diag()>) it "passes through" the failure. 2224 2225 return ok(...) || diag(...); 2226 2227=for blame transfer 2228Mark Fowler <mark@twoshortplanks.com> 2229 2230=item B<note> 2231 2232 $Test->note(@msgs); 2233 2234Like C<diag()>, but it prints to the C<output()> handle so it will not 2235normally be seen by the user except in verbose mode. 2236 2237=item B<explain> 2238 2239 my @dump = $Test->explain(@msgs); 2240 2241Will dump the contents of any references in a human readable format. 2242Handy for things like... 2243 2244 is_deeply($have, $want) || diag explain $have; 2245 2246or 2247 2248 is_deeply($have, $want) || note explain $have; 2249 2250=item B<output> 2251 2252=item B<failure_output> 2253 2254=item B<todo_output> 2255 2256 my $filehandle = $Test->output; 2257 $Test->output($filehandle); 2258 $Test->output($filename); 2259 $Test->output(\$scalar); 2260 2261These methods control where Test::Builder will print its output. 2262They take either an open C<$filehandle>, a C<$filename> to open and write to 2263or a C<$scalar> reference to append to. It will always return a C<$filehandle>. 2264 2265B<output> is where normal "ok/not ok" test output goes. 2266 2267Defaults to STDOUT. 2268 2269B<failure_output> is where diagnostic output on test failures and 2270C<diag()> goes. It is normally not read by Test::Harness and instead is 2271displayed to the user. 2272 2273Defaults to STDERR. 2274 2275C<todo_output> is used instead of C<failure_output()> for the 2276diagnostics of a failing TODO test. These will not be seen by the 2277user. 2278 2279Defaults to STDOUT. 2280 2281=item reset_outputs 2282 2283 $tb->reset_outputs; 2284 2285Resets all the output filehandles back to their defaults. 2286 2287=item carp 2288 2289 $tb->carp(@message); 2290 2291Warns with C<@message> but the message will appear to come from the 2292point where the original test function was called (C<< $tb->caller >>). 2293 2294=item croak 2295 2296 $tb->croak(@message); 2297 2298Dies with C<@message> but the message will appear to come from the 2299point where the original test function was called (C<< $tb->caller >>). 2300 2301 2302=back 2303 2304 2305=head2 Test Status and Info 2306 2307=over 4 2308 2309=item B<no_log_results> 2310 2311This will turn off result long-term storage. Calling this method will make 2312C<details> and C<summary> useless. You may want to use this if you are running 2313enough tests to fill up all available memory. 2314 2315 Test::Builder->new->no_log_results(); 2316 2317There is no way to turn it back on. 2318 2319=item B<current_test> 2320 2321 my $curr_test = $Test->current_test; 2322 $Test->current_test($num); 2323 2324Gets/sets the current test number we're on. You usually shouldn't 2325have to set this. 2326 2327If set forward, the details of the missing tests are filled in as 'unknown'. 2328if set backward, the details of the intervening tests are deleted. You 2329can erase history if you really want to. 2330 2331 2332=item B<is_passing> 2333 2334 my $ok = $builder->is_passing; 2335 2336Indicates if the test suite is currently passing. 2337 2338More formally, it will be false if anything has happened which makes 2339it impossible for the test suite to pass. True otherwise. 2340 2341For example, if no tests have run C<is_passing()> will be true because 2342even though a suite with no tests is a failure you can add a passing 2343test to it and start passing. 2344 2345Don't think about it too much. 2346 2347 2348=item B<summary> 2349 2350 my @tests = $Test->summary; 2351 2352A simple summary of the tests so far. True for pass, false for fail. 2353This is a logical pass/fail, so todos are passes. 2354 2355Of course, test #1 is $tests[0], etc... 2356 2357 2358=item B<details> 2359 2360 my @tests = $Test->details; 2361 2362Like C<summary()>, but with a lot more detail. 2363 2364 $tests[$test_num - 1] = 2365 { 'ok' => is the test considered a pass? 2366 actual_ok => did it literally say 'ok'? 2367 name => name of the test (if any) 2368 type => type of test (if any, see below). 2369 reason => reason for the above (if any) 2370 }; 2371 2372'ok' is true if Test::Harness will consider the test to be a pass. 2373 2374'actual_ok' is a reflection of whether or not the test literally 2375printed 'ok' or 'not ok'. This is for examining the result of 'todo' 2376tests. 2377 2378'name' is the name of the test. 2379 2380'type' indicates if it was a special test. Normal tests have a type 2381of ''. Type can be one of the following: 2382 2383 skip see skip() 2384 todo see todo() 2385 todo_skip see todo_skip() 2386 unknown see below 2387 2388Sometimes the Test::Builder test counter is incremented without it 2389printing any test output, for example, when C<current_test()> is changed. 2390In these cases, Test::Builder doesn't know the result of the test, so 2391its type is 'unknown'. These details for these tests are filled in. 2392They are considered ok, but the name and actual_ok is left C<undef>. 2393 2394For example "not ok 23 - hole count # TODO insufficient donuts" would 2395result in this structure: 2396 2397 $tests[22] = # 23 - 1, since arrays start from 0. 2398 { ok => 1, # logically, the test passed since its todo 2399 actual_ok => 0, # in absolute terms, it failed 2400 name => 'hole count', 2401 type => 'todo', 2402 reason => 'insufficient donuts' 2403 }; 2404 2405 2406=item B<todo> 2407 2408 my $todo_reason = $Test->todo; 2409 my $todo_reason = $Test->todo($pack); 2410 2411If the current tests are considered "TODO" it will return the reason, 2412if any. This reason can come from a C<$TODO> variable or the last call 2413to C<todo_start()>. 2414 2415Since a TODO test does not need a reason, this function can return an 2416empty string even when inside a TODO block. Use C<< $Test->in_todo >> 2417to determine if you are currently inside a TODO block. 2418 2419C<todo()> is about finding the right package to look for C<$TODO> in. It's 2420pretty good at guessing the right package to look at. It first looks for 2421the caller based on C<$Level + 1>, since C<todo()> is usually called inside 2422a test function. As a last resort it will use C<exported_to()>. 2423 2424Sometimes there is some confusion about where C<todo()> should be looking 2425for the C<$TODO> variable. If you want to be sure, tell it explicitly 2426what $pack to use. 2427 2428=item B<find_TODO> 2429 2430 my $todo_reason = $Test->find_TODO(); 2431 my $todo_reason = $Test->find_TODO($pack); 2432 2433Like C<todo()> but only returns the value of C<$TODO> ignoring 2434C<todo_start()>. 2435 2436Can also be used to set C<$TODO> to a new value while returning the 2437old value: 2438 2439 my $old_reason = $Test->find_TODO($pack, 1, $new_reason); 2440 2441=item B<in_todo> 2442 2443 my $in_todo = $Test->in_todo; 2444 2445Returns true if the test is currently inside a TODO block. 2446 2447=item B<todo_start> 2448 2449 $Test->todo_start(); 2450 $Test->todo_start($message); 2451 2452This method allows you declare all subsequent tests as TODO tests, up until 2453the C<todo_end> method has been called. 2454 2455The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out 2456whether or not we're in a TODO test. However, often we find that this is not 2457possible to determine (such as when we want to use C<$TODO> but 2458the tests are being executed in other packages which can't be inferred 2459beforehand). 2460 2461Note that you can use this to nest "todo" tests 2462 2463 $Test->todo_start('working on this'); 2464 # lots of code 2465 $Test->todo_start('working on that'); 2466 # more code 2467 $Test->todo_end; 2468 $Test->todo_end; 2469 2470This is generally not recommended, but large testing systems often have weird 2471internal needs. 2472 2473We've tried to make this also work with the TODO: syntax, but it's not 2474guaranteed and its use is also discouraged: 2475 2476 TODO: { 2477 local $TODO = 'We have work to do!'; 2478 $Test->todo_start('working on this'); 2479 # lots of code 2480 $Test->todo_start('working on that'); 2481 # more code 2482 $Test->todo_end; 2483 $Test->todo_end; 2484 } 2485 2486Pick one style or another of "TODO" to be on the safe side. 2487 2488 2489=item C<todo_end> 2490 2491 $Test->todo_end; 2492 2493Stops running tests as "TODO" tests. This method is fatal if called without a 2494preceding C<todo_start> method call. 2495 2496=item B<caller> 2497 2498 my $package = $Test->caller; 2499 my($pack, $file, $line) = $Test->caller; 2500 my($pack, $file, $line) = $Test->caller($height); 2501 2502Like the normal C<caller()>, except it reports according to your C<level()>. 2503 2504C<$height> will be added to the C<level()>. 2505 2506If C<caller()> winds up off the top of the stack it report the highest context. 2507 2508=back 2509 2510=head1 EXIT CODES 2511 2512If all your tests passed, Test::Builder will exit with zero (which is 2513normal). If anything failed it will exit with how many failed. If 2514you run less (or more) tests than you planned, the missing (or extras) 2515will be considered failures. If no tests were ever run Test::Builder 2516will throw a warning and exit with 255. If the test died, even after 2517having successfully completed all its tests, it will still be 2518considered a failure and will exit with 255. 2519 2520So the exit codes are... 2521 2522 0 all tests successful 2523 255 test died or all passed but wrong # of tests run 2524 any other number how many failed (including missing or extras) 2525 2526If you fail more than 254 tests, it will be reported as 254. 2527 2528=head1 THREADS 2529 2530In perl 5.8.1 and later, Test::Builder is thread-safe. The test number is 2531shared by all threads. This means if one thread sets the test number using 2532C<current_test()> they will all be effected. 2533 2534While versions earlier than 5.8.1 had threads they contain too many 2535bugs to support. 2536 2537Test::Builder is only thread-aware if threads.pm is loaded I<before> 2538Test::Builder. 2539 2540You can directly disable thread support with one of the following: 2541 2542 $ENV{T2_NO_IPC} = 1 2543 2544or 2545 2546 no Test2::IPC; 2547 2548or 2549 2550 Test2::API::test2_ipc_disable() 2551 2552=head1 MEMORY 2553 2554An informative hash, accessible via C<details()>, is stored for each 2555test you perform. So memory usage will scale linearly with each test 2556run. Although this is not a problem for most test suites, it can 2557become an issue if you do large (hundred thousands to million) 2558combinatorics tests in the same run. 2559 2560In such cases, you are advised to either split the test file into smaller 2561ones, or use a reverse approach, doing "normal" (code) compares and 2562triggering C<fail()> should anything go unexpected. 2563 2564Future versions of Test::Builder will have a way to turn history off. 2565 2566 2567=head1 EXAMPLES 2568 2569CPAN can provide the best examples. L<Test::Simple>, L<Test::More>, 2570L<Test::Exception> and L<Test::Differences> all use Test::Builder. 2571 2572=head1 SEE ALSO 2573 2574=head2 INTERNALS 2575 2576L<Test2>, L<Test2::API> 2577 2578=head2 LEGACY 2579 2580L<Test::Simple>, L<Test::More> 2581 2582=head2 EXTERNAL 2583 2584L<Test::Harness> 2585 2586=head1 AUTHORS 2587 2588Original code by chromatic, maintained by Michael G Schwern 2589E<lt>schwern@pobox.comE<gt> 2590 2591=head1 MAINTAINERS 2592 2593=over 4 2594 2595=item Chad Granum E<lt>exodist@cpan.orgE<gt> 2596 2597=back 2598 2599=head1 COPYRIGHT 2600 2601Copyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and 2602 Michael G Schwern E<lt>schwern@pobox.comE<gt>. 2603 2604This program is free software; you can redistribute it and/or 2605modify it under the same terms as Perl itself. 2606 2607See F<http://www.perl.com/perl/misc/Artistic.html> 2608