1b39c5158Smillertpackage Test::Builder; 2b39c5158Smillert 3b39c5158Smillertuse 5.006; 4b39c5158Smillertuse strict; 5b39c5158Smillertuse warnings; 6b39c5158Smillert 7*3d61058aSafresh1our $VERSION = '1.302199'; 8b39c5158Smillert 9b39c5158SmillertBEGIN { 10b39c5158Smillert if( $] < 5.008 ) { 11b39c5158Smillert require Test::Builder::IO::Scalar; 12b39c5158Smillert } 13b39c5158Smillert} 14b39c5158Smillert 159f11ffb7Safresh1use Scalar::Util qw/blessed reftype weaken/; 16b39c5158Smillert 179f11ffb7Safresh1use Test2::Util qw/USE_THREADS try get_tid/; 189f11ffb7Safresh1use Test2::API qw/context release/; 19b39c5158Smillert# Make Test::Builder thread-safe for ithreads. 20b39c5158SmillertBEGIN { 219f11ffb7Safresh1 warn "Test::Builder was loaded after Test2 initialization, this is not recommended." 229f11ffb7Safresh1 if Test2::API::test2_init_done() || Test2::API::test2_load_done(); 23b39c5158Smillert 249f11ffb7Safresh1 if (USE_THREADS && ! Test2::API::test2_ipc_disabled()) { 259f11ffb7Safresh1 require Test2::IPC; 269f11ffb7Safresh1 require Test2::IPC::Driver::Files; 279f11ffb7Safresh1 Test2::IPC::Driver::Files->import; 289f11ffb7Safresh1 Test2::API::test2_ipc_enable_polling(); 299f11ffb7Safresh1 Test2::API::test2_no_wait(1); 309f11ffb7Safresh1 } 319f11ffb7Safresh1} 32b39c5158Smillert 339f11ffb7Safresh1use Test2::Event::Subtest; 349f11ffb7Safresh1use Test2::Hub::Subtest; 359f11ffb7Safresh1 369f11ffb7Safresh1use Test::Builder::Formatter; 379f11ffb7Safresh1use Test::Builder::TodoDiag; 389f11ffb7Safresh1 399f11ffb7Safresh1our $Level = 1; 409f11ffb7Safresh1our $Test = $ENV{TB_NO_EARLY_INIT} ? undef : Test::Builder->new; 419f11ffb7Safresh1 429f11ffb7Safresh1sub _add_ts_hooks { 439f11ffb7Safresh1 my $self = shift; 449f11ffb7Safresh1 459f11ffb7Safresh1 my $hub = $self->{Stack}->top; 469f11ffb7Safresh1 479f11ffb7Safresh1 # Take a reference to the hash key, we do this to avoid closing over $self 489f11ffb7Safresh1 # which is the singleton. We use a reference because the value could change 499f11ffb7Safresh1 # in rare cases. 509f11ffb7Safresh1 my $epkgr = \$self->{Exported_To}; 519f11ffb7Safresh1 529f11ffb7Safresh1 #$hub->add_context_aquire(sub {$_[0]->{level} += $Level - 1}); 539f11ffb7Safresh1 54eac174f2Safresh1 $hub->pre_filter( 55eac174f2Safresh1 sub { 569f11ffb7Safresh1 my ($active_hub, $e) = @_; 579f11ffb7Safresh1 589f11ffb7Safresh1 my $epkg = $$epkgr; 599f11ffb7Safresh1 my $cpkg = $e->{trace} ? $e->{trace}->{frame}->[0] : undef; 609f11ffb7Safresh1 619f11ffb7Safresh1 no strict 'refs'; 629f11ffb7Safresh1 no warnings 'once'; 639f11ffb7Safresh1 my $todo; 649f11ffb7Safresh1 $todo = ${"$cpkg\::TODO"} if $cpkg; 659f11ffb7Safresh1 $todo = ${"$epkg\::TODO"} if $epkg && !$todo; 669f11ffb7Safresh1 6756d68f1eSafresh1 return $e unless defined($todo); 6856d68f1eSafresh1 return $e unless length($todo); 699f11ffb7Safresh1 709f11ffb7Safresh1 # Turn a diag into a todo diag 719f11ffb7Safresh1 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 729f11ffb7Safresh1 739f11ffb7Safresh1 $e->set_todo($todo) if $e->can('set_todo'); 749f11ffb7Safresh1 $e->add_amnesty({tag => 'TODO', details => $todo}); 75b39c5158Smillert 769f11ffb7Safresh1 # Set todo on ok's 779f11ffb7Safresh1 if ($e->isa('Test2::Event::Ok')) { 789f11ffb7Safresh1 $e->set_effective_pass(1); 79b39c5158Smillert 809f11ffb7Safresh1 if (my $result = $e->get_meta(__PACKAGE__)) { 819f11ffb7Safresh1 $result->{reason} ||= $todo; 829f11ffb7Safresh1 $result->{type} ||= 'todo'; 839f11ffb7Safresh1 $result->{ok} = 1; 84b39c5158Smillert } 85b39c5158Smillert } 86b39c5158Smillert 879f11ffb7Safresh1 return $e; 88eac174f2Safresh1 }, 89eac174f2Safresh1 90eac174f2Safresh1 inherit => 1, 91eac174f2Safresh1 92eac174f2Safresh1 intercept_inherit => { 93eac174f2Safresh1 clean => sub { 94eac174f2Safresh1 my %params = @_; 95eac174f2Safresh1 96eac174f2Safresh1 my $state = $params{state}; 97eac174f2Safresh1 my $trace = $params{trace}; 98eac174f2Safresh1 99eac174f2Safresh1 my $epkg = $$epkgr; 100eac174f2Safresh1 my $cpkg = $trace->{frame}->[0]; 101eac174f2Safresh1 102eac174f2Safresh1 no strict 'refs'; 103eac174f2Safresh1 no warnings 'once'; 104eac174f2Safresh1 105eac174f2Safresh1 $state->{+__PACKAGE__} = {}; 106eac174f2Safresh1 $state->{+__PACKAGE__}->{"$cpkg\::TODO"} = ${"$cpkg\::TODO"} if $cpkg; 107eac174f2Safresh1 $state->{+__PACKAGE__}->{"$epkg\::TODO"} = ${"$epkg\::TODO"} if $epkg; 108eac174f2Safresh1 109eac174f2Safresh1 ${"$cpkg\::TODO"} = undef if $cpkg; 110eac174f2Safresh1 ${"$epkg\::TODO"} = undef if $epkg; 111eac174f2Safresh1 }, 112eac174f2Safresh1 restore => sub { 113eac174f2Safresh1 my %params = @_; 114eac174f2Safresh1 my $state = $params{state}; 115eac174f2Safresh1 116eac174f2Safresh1 no strict 'refs'; 117eac174f2Safresh1 no warnings 'once'; 118eac174f2Safresh1 119eac174f2Safresh1 for my $item (keys %{$state->{+__PACKAGE__}}) { 120eac174f2Safresh1 no strict 'refs'; 121eac174f2Safresh1 no warnings 'once'; 122eac174f2Safresh1 123eac174f2Safresh1 ${"$item"} = $state->{+__PACKAGE__}->{$item}; 124eac174f2Safresh1 } 125eac174f2Safresh1 }, 126eac174f2Safresh1 }, 127eac174f2Safresh1 ); 128b39c5158Smillert} 129b39c5158Smillert 1309f11ffb7Safresh1{ 1319f11ffb7Safresh1 no warnings; 1329f11ffb7Safresh1 INIT { 1339f11ffb7Safresh1 use warnings; 1349f11ffb7Safresh1 Test2::API::test2_load() unless Test2::API::test2_in_preload(); 1359f11ffb7Safresh1 } 1369f11ffb7Safresh1} 137b39c5158Smillert 138b39c5158Smillertsub new { 139b39c5158Smillert my($class) = shift; 1409f11ffb7Safresh1 unless($Test) { 1419f11ffb7Safresh1 $Test = $class->create(singleton => 1); 1429f11ffb7Safresh1 1439f11ffb7Safresh1 Test2::API::test2_add_callback_post_load( 1449f11ffb7Safresh1 sub { 1459f11ffb7Safresh1 $Test->{Original_Pid} = $$ if !$Test->{Original_Pid} || $Test->{Original_Pid} == 0; 1469f11ffb7Safresh1 $Test->reset(singleton => 1); 1479f11ffb7Safresh1 $Test->_add_ts_hooks; 1489f11ffb7Safresh1 } 1499f11ffb7Safresh1 ); 1509f11ffb7Safresh1 1519f11ffb7Safresh1 # Non-TB tools normally expect 0 added to the level. $Level is normally 1. So 1529f11ffb7Safresh1 # we only want the level to change if $Level != 1. 1539f11ffb7Safresh1 # TB->ctx compensates for this later. 1549f11ffb7Safresh1 Test2::API::test2_add_callback_context_aquire(sub { $_[0]->{level} += $Level - 1 }); 1559f11ffb7Safresh1 1569f11ffb7Safresh1 Test2::API::test2_add_callback_exit(sub { $Test->_ending(@_) }); 1579f11ffb7Safresh1 1589f11ffb7Safresh1 Test2::API::test2_ipc()->set_no_fatal(1) if Test2::API::test2_has_ipc(); 1599f11ffb7Safresh1 } 160b39c5158Smillert return $Test; 161b39c5158Smillert} 162b39c5158Smillert 163b39c5158Smillertsub create { 164b39c5158Smillert my $class = shift; 1659f11ffb7Safresh1 my %params = @_; 166b39c5158Smillert 167b39c5158Smillert my $self = bless {}, $class; 1689f11ffb7Safresh1 if ($params{singleton}) { 1699f11ffb7Safresh1 $self->{Stack} = Test2::API::test2_stack(); 1709f11ffb7Safresh1 } 1719f11ffb7Safresh1 else { 1729f11ffb7Safresh1 $self->{Stack} = Test2::API::Stack->new; 1739f11ffb7Safresh1 $self->{Stack}->new_hub( 1749f11ffb7Safresh1 formatter => Test::Builder::Formatter->new, 1759f11ffb7Safresh1 ipc => Test2::API::test2_ipc(), 1769f11ffb7Safresh1 ); 1779f11ffb7Safresh1 1789f11ffb7Safresh1 $self->reset(%params); 1799f11ffb7Safresh1 $self->_add_ts_hooks; 1809f11ffb7Safresh1 } 181b39c5158Smillert 182b39c5158Smillert return $self; 183b39c5158Smillert} 184b39c5158Smillert 1859f11ffb7Safresh1sub ctx { 1869f11ffb7Safresh1 my $self = shift; 1879f11ffb7Safresh1 context( 1889f11ffb7Safresh1 # 1 for our frame, another for the -1 off of $Level in our hook at the top. 1899f11ffb7Safresh1 level => 2, 1909f11ffb7Safresh1 fudge => 1, 1919f11ffb7Safresh1 stack => $self->{Stack}, 1929f11ffb7Safresh1 hub => $self->{Hub}, 1939f11ffb7Safresh1 wrapped => 1, 1949f11ffb7Safresh1 @_ 1959f11ffb7Safresh1 ); 196e5157e49Safresh1} 197e5157e49Safresh1 1989f11ffb7Safresh1sub parent { 1999f11ffb7Safresh1 my $self = shift; 2009f11ffb7Safresh1 my $ctx = $self->ctx; 2019f11ffb7Safresh1 my $chub = $self->{Hub} || $ctx->hub; 2029f11ffb7Safresh1 $ctx->release; 203e5157e49Safresh1 2049f11ffb7Safresh1 my $meta = $chub->meta(__PACKAGE__, {}); 2059f11ffb7Safresh1 my $parent = $meta->{parent}; 206b39c5158Smillert 2079f11ffb7Safresh1 return undef unless $parent; 208b39c5158Smillert 2099f11ffb7Safresh1 return bless { 2109f11ffb7Safresh1 Original_Pid => $$, 2119f11ffb7Safresh1 Stack => $self->{Stack}, 2129f11ffb7Safresh1 Hub => $parent, 2139f11ffb7Safresh1 }, blessed($self); 2149f11ffb7Safresh1} 215b39c5158Smillert 216b39c5158Smillertsub child { 217b39c5158Smillert my( $self, $name ) = @_; 218b39c5158Smillert 2199f11ffb7Safresh1 $name ||= "Child of " . $self->name; 2209f11ffb7Safresh1 my $ctx = $self->ctx; 221b39c5158Smillert 2229f11ffb7Safresh1 my $parent = $ctx->hub; 2239f11ffb7Safresh1 my $pmeta = $parent->meta(__PACKAGE__, {}); 2249f11ffb7Safresh1 $self->croak("You already have a child named ($pmeta->{child}) running") 2259f11ffb7Safresh1 if $pmeta->{child}; 2269f11ffb7Safresh1 2279f11ffb7Safresh1 $pmeta->{child} = $name; 22865d9bffcSjasper 22965d9bffcSjasper # Clear $TODO for the child. 23065d9bffcSjasper my $orig_TODO = $self->find_TODO(undef, 1, undef); 23165d9bffcSjasper 2329f11ffb7Safresh1 my $subevents = []; 233b39c5158Smillert 2349f11ffb7Safresh1 my $hub = $ctx->stack->new_hub( 2359f11ffb7Safresh1 class => 'Test2::Hub::Subtest', 2369f11ffb7Safresh1 ); 23765d9bffcSjasper 2389f11ffb7Safresh1 $hub->pre_filter(sub { 2399f11ffb7Safresh1 my ($active_hub, $e) = @_; 2409f11ffb7Safresh1 2419f11ffb7Safresh1 # Turn a diag into a todo diag 2429f11ffb7Safresh1 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 2439f11ffb7Safresh1 2449f11ffb7Safresh1 return $e; 2459f11ffb7Safresh1 }, inherit => 1) if $orig_TODO; 2469f11ffb7Safresh1 2479f11ffb7Safresh1 $hub->listen(sub { push @$subevents => $_[1] }); 2489f11ffb7Safresh1 2499f11ffb7Safresh1 $hub->set_nested( $parent->nested + 1 ); 2509f11ffb7Safresh1 2519f11ffb7Safresh1 my $meta = $hub->meta(__PACKAGE__, {}); 2529f11ffb7Safresh1 $meta->{Name} = $name; 2539f11ffb7Safresh1 $meta->{TODO} = $orig_TODO; 2549f11ffb7Safresh1 $meta->{TODO_PKG} = $ctx->trace->package; 2559f11ffb7Safresh1 $meta->{parent} = $parent; 2569f11ffb7Safresh1 $meta->{Test_Results} = []; 2579f11ffb7Safresh1 $meta->{subevents} = $subevents; 2589f11ffb7Safresh1 $meta->{subtest_id} = $hub->id; 2599f11ffb7Safresh1 $meta->{subtest_uuid} = $hub->uuid; 2609f11ffb7Safresh1 $meta->{subtest_buffered} = $parent->format ? 0 : 1; 2619f11ffb7Safresh1 2629f11ffb7Safresh1 $self->_add_ts_hooks; 2639f11ffb7Safresh1 2649f11ffb7Safresh1 $ctx->release; 2659f11ffb7Safresh1 return bless { Original_Pid => $$, Stack => $self->{Stack}, Hub => $hub, no_log_results => $self->{no_log_results} }, blessed($self); 266e5157e49Safresh1} 267e5157e49Safresh1 268b39c5158Smillertsub finalize { 269b39c5158Smillert my $self = shift; 2709f11ffb7Safresh1 my $ok = 1; 2719f11ffb7Safresh1 ($ok) = @_ if @_; 272b39c5158Smillert 2739f11ffb7Safresh1 my $st_ctx = $self->ctx; 2749f11ffb7Safresh1 my $chub = $self->{Hub} || return $st_ctx->release; 2759f11ffb7Safresh1 2769f11ffb7Safresh1 my $meta = $chub->meta(__PACKAGE__, {}); 2779f11ffb7Safresh1 if ($meta->{child}) { 2789f11ffb7Safresh1 $self->croak("Can't call finalize() with child ($meta->{child}) active"); 279b39c5158Smillert } 2806eddea87Sjasper 2816eddea87Sjasper local $? = 0; # don't fail if $subtests happened to set $? nonzero 282b39c5158Smillert 2839f11ffb7Safresh1 $self->{Stack}->pop($chub); 284b39c5158Smillert 2859f11ffb7Safresh1 $self->find_TODO($meta->{TODO_PKG}, 1, $meta->{TODO}); 2869f11ffb7Safresh1 2879f11ffb7Safresh1 my $parent = $self->parent; 2889f11ffb7Safresh1 my $ctx = $parent->ctx; 2899f11ffb7Safresh1 my $trace = $ctx->trace; 2909f11ffb7Safresh1 delete $ctx->hub->meta(__PACKAGE__, {})->{child}; 2919f11ffb7Safresh1 2929f11ffb7Safresh1 $chub->finalize($trace->snapshot(hid => $chub->hid, nested => $chub->nested), 1) 2939f11ffb7Safresh1 if $ok 2949f11ffb7Safresh1 && $chub->count 2959f11ffb7Safresh1 && !$chub->no_ending 2969f11ffb7Safresh1 && !$chub->ended; 2979f11ffb7Safresh1 2989f11ffb7Safresh1 my $plan = $chub->plan || 0; 2999f11ffb7Safresh1 my $count = $chub->count; 3009f11ffb7Safresh1 my $failed = $chub->failed; 3019f11ffb7Safresh1 my $passed = $chub->is_passing; 3029f11ffb7Safresh1 3039f11ffb7Safresh1 my $num_extra = $plan =~ m/\D/ ? 0 : $count - $plan; 3049f11ffb7Safresh1 if ($count && $num_extra != 0) { 3059f11ffb7Safresh1 my $s = $plan == 1 ? '' : 's'; 3069f11ffb7Safresh1 $st_ctx->diag(<<"FAIL"); 3079f11ffb7Safresh1Looks like you planned $plan test$s but ran $count. 3089f11ffb7Safresh1FAIL 309b39c5158Smillert } 3109f11ffb7Safresh1 3119f11ffb7Safresh1 if ($failed) { 3129f11ffb7Safresh1 my $s = $failed == 1 ? '' : 's'; 3139f11ffb7Safresh1 3149f11ffb7Safresh1 my $qualifier = $num_extra == 0 ? '' : ' run'; 3159f11ffb7Safresh1 3169f11ffb7Safresh1 $st_ctx->diag(<<"FAIL"); 3179f11ffb7Safresh1Looks like you failed $failed test$s of $count$qualifier. 3189f11ffb7Safresh1FAIL 3199f11ffb7Safresh1 } 3209f11ffb7Safresh1 3219f11ffb7Safresh1 if (!$passed && !$failed && $count && !$num_extra) { 3229f11ffb7Safresh1 $st_ctx->diag(<<"FAIL"); 3239f11ffb7Safresh1All assertions inside the subtest passed, but errors were encountered. 3249f11ffb7Safresh1FAIL 3259f11ffb7Safresh1 } 3269f11ffb7Safresh1 3279f11ffb7Safresh1 $st_ctx->release; 3289f11ffb7Safresh1 3299f11ffb7Safresh1 unless ($chub->bailed_out) { 3309f11ffb7Safresh1 my $plan = $chub->plan; 3319f11ffb7Safresh1 if ( $plan && $plan eq 'SKIP' ) { 3329f11ffb7Safresh1 $parent->skip($chub->skip_reason, $meta->{Name}); 3339f11ffb7Safresh1 } 3349f11ffb7Safresh1 elsif ( !$chub->count ) { 3359f11ffb7Safresh1 $parent->ok( 0, sprintf q[No tests run for subtest "%s"], $meta->{Name} ); 336b39c5158Smillert } 337b39c5158Smillert else { 3389f11ffb7Safresh1 $parent->{subevents} = $meta->{subevents}; 3399f11ffb7Safresh1 $parent->{subtest_id} = $meta->{subtest_id}; 3409f11ffb7Safresh1 $parent->{subtest_uuid} = $meta->{subtest_uuid}; 3419f11ffb7Safresh1 $parent->{subtest_buffered} = $meta->{subtest_buffered}; 3429f11ffb7Safresh1 $parent->ok( $chub->is_passing, $meta->{Name} ); 343b39c5158Smillert } 344e5157e49Safresh1 } 345b39c5158Smillert 3469f11ffb7Safresh1 $ctx->release; 3479f11ffb7Safresh1 return $chub->is_passing; 3489f11ffb7Safresh1} 3499f11ffb7Safresh1 3509f11ffb7Safresh1sub subtest { 351b39c5158Smillert my $self = shift; 3529f11ffb7Safresh1 my ($name, $code, @args) = @_; 3539f11ffb7Safresh1 my $ctx = $self->ctx; 3549f11ffb7Safresh1 $ctx->throw("subtest()'s second argument must be a code ref") 3559f11ffb7Safresh1 unless $code && reftype($code) eq 'CODE'; 356b39c5158Smillert 3579f11ffb7Safresh1 $name ||= "Child of " . $self->name; 3589f11ffb7Safresh1 3599f11ffb7Safresh1 3609f11ffb7Safresh1 $_->($name,$code,@args) 3619f11ffb7Safresh1 for Test2::API::test2_list_pre_subtest_callbacks(); 3629f11ffb7Safresh1 3639f11ffb7Safresh1 $ctx->note("Subtest: $name"); 3649f11ffb7Safresh1 3659f11ffb7Safresh1 my $child = $self->child($name); 3669f11ffb7Safresh1 3679f11ffb7Safresh1 my $start_pid = $$; 3689f11ffb7Safresh1 my $st_ctx; 3699f11ffb7Safresh1 my ($ok, $err, $finished, $child_error); 3709f11ffb7Safresh1 T2_SUBTEST_WRAPPER: { 3719f11ffb7Safresh1 my $ctx = $self->ctx; 3729f11ffb7Safresh1 $st_ctx = $ctx->snapshot; 3739f11ffb7Safresh1 $ctx->release; 3749f11ffb7Safresh1 $ok = eval { local $Level = 1; $code->(@args); 1 }; 3759f11ffb7Safresh1 ($err, $child_error) = ($@, $?); 3769f11ffb7Safresh1 3779f11ffb7Safresh1 # They might have done 'BEGIN { skip_all => "whatever" }' 3789f11ffb7Safresh1 if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) { 3799f11ffb7Safresh1 $ok = undef; 3809f11ffb7Safresh1 $err = undef; 3819f11ffb7Safresh1 } 3829f11ffb7Safresh1 else { 3839f11ffb7Safresh1 $finished = 1; 3849f11ffb7Safresh1 } 385b39c5158Smillert } 386b39c5158Smillert 3879f11ffb7Safresh1 if ($start_pid != $$ && !$INC{'Test2/IPC.pm'}) { 3889f11ffb7Safresh1 warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err; 3899f11ffb7Safresh1 exit 255; 390b39c5158Smillert } 391b39c5158Smillert 3929f11ffb7Safresh1 my $trace = $ctx->trace; 393b39c5158Smillert 3949f11ffb7Safresh1 if (!$finished) { 3959f11ffb7Safresh1 if(my $bailed = $st_ctx->hub->bailed_out) { 3969f11ffb7Safresh1 my $chub = $child->{Hub}; 3979f11ffb7Safresh1 $self->{Stack}->pop($chub); 3989f11ffb7Safresh1 $ctx->bail($bailed->reason); 3999f11ffb7Safresh1 } 4009f11ffb7Safresh1 my $code = $st_ctx->hub->exit_code; 4019f11ffb7Safresh1 $ok = !$code; 4029f11ffb7Safresh1 $err = "Subtest ended with exit code $code" if $code; 403b39c5158Smillert } 404b39c5158Smillert 4059f11ffb7Safresh1 my $st_hub = $st_ctx->hub; 4069f11ffb7Safresh1 my $plan = $st_hub->plan; 4079f11ffb7Safresh1 my $count = $st_hub->count; 408b39c5158Smillert 4099f11ffb7Safresh1 if (!$count && (!defined($plan) || "$plan" ne 'SKIP')) { 4109f11ffb7Safresh1 $st_ctx->plan(0) unless defined $plan; 4119f11ffb7Safresh1 $st_ctx->diag('No tests run!'); 4129f11ffb7Safresh1 } 413b39c5158Smillert 4149f11ffb7Safresh1 $child->finalize($st_ctx->trace); 415b39c5158Smillert 4169f11ffb7Safresh1 $ctx->release; 417b39c5158Smillert 4189f11ffb7Safresh1 die $err unless $ok; 419b39c5158Smillert 4209f11ffb7Safresh1 $? = $child_error if defined $child_error; 421b39c5158Smillert 4229f11ffb7Safresh1 return $st_hub->is_passing; 4239f11ffb7Safresh1} 424b39c5158Smillert 4259f11ffb7Safresh1sub name { 426b39c5158Smillert my $self = shift; 4279f11ffb7Safresh1 my $ctx = $self->ctx; 4289f11ffb7Safresh1 release $ctx, $ctx->hub->meta(__PACKAGE__, {})->{Name}; 429b39c5158Smillert} 430b39c5158Smillert 431b39c5158Smillertsub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms) 4329f11ffb7Safresh1 my ($self, %params) = @_; 4339f11ffb7Safresh1 43456d68f1eSafresh1 Test2::API::test2_unset_is_end(); 435b39c5158Smillert 436b39c5158Smillert # We leave this a global because it has to be localized and localizing 437b39c5158Smillert # hash keys is just asking for pain. Also, it was documented. 438b39c5158Smillert $Level = 1; 439b39c5158Smillert 4409f11ffb7Safresh1 $self->{no_log_results} = $ENV{TEST_NO_LOG_RESULTS} ? 1 : 0 4419f11ffb7Safresh1 unless $params{singleton}; 442b39c5158Smillert 4439f11ffb7Safresh1 $self->{Original_Pid} = Test2::API::test2_in_preload() ? -1 : $$; 444b39c5158Smillert 4459f11ffb7Safresh1 my $ctx = $self->ctx; 4469f11ffb7Safresh1 my $hub = $ctx->hub; 4479f11ffb7Safresh1 $ctx->release; 4489f11ffb7Safresh1 unless ($params{singleton}) { 4499f11ffb7Safresh1 $hub->reset_state(); 4509f11ffb7Safresh1 $hub->_tb_reset(); 4519f11ffb7Safresh1 } 452b39c5158Smillert 4539f11ffb7Safresh1 $ctx = $self->ctx; 454b39c5158Smillert 4559f11ffb7Safresh1 my $meta = $ctx->hub->meta(__PACKAGE__, {}); 4569f11ffb7Safresh1 %$meta = ( 4579f11ffb7Safresh1 Name => $0, 4589f11ffb7Safresh1 Ending => 0, 4599f11ffb7Safresh1 Done_Testing => undef, 4609f11ffb7Safresh1 Skip_All => 0, 4619f11ffb7Safresh1 Test_Results => [], 4629f11ffb7Safresh1 parent => $meta->{parent}, 4639f11ffb7Safresh1 ); 464b39c5158Smillert 4659f11ffb7Safresh1 $self->{Exported_To} = undef unless $params{singleton}; 466b39c5158Smillert 4679f11ffb7Safresh1 $self->{Orig_Handles} ||= do { 4689f11ffb7Safresh1 my $format = $ctx->hub->format; 4699f11ffb7Safresh1 my $out; 4709f11ffb7Safresh1 if ($format && $format->isa('Test2::Formatter::TAP')) { 4719f11ffb7Safresh1 $out = $format->handles; 4729f11ffb7Safresh1 } 4739f11ffb7Safresh1 $out ? [@$out] : []; 4749f11ffb7Safresh1 }; 475b39c5158Smillert 4769f11ffb7Safresh1 $self->use_numbers(1); 4779f11ffb7Safresh1 $self->no_header(0) unless $params{singleton}; 4789f11ffb7Safresh1 $self->no_ending(0) unless $params{singleton}; 4799f11ffb7Safresh1 $self->reset_outputs; 480b39c5158Smillert 4819f11ffb7Safresh1 $ctx->release; 482b39c5158Smillert 483b39c5158Smillert return; 484b39c5158Smillert} 485b39c5158Smillert 486e5157e49Safresh1 487b39c5158Smillertmy %plan_cmds = ( 488b39c5158Smillert no_plan => \&no_plan, 489b39c5158Smillert skip_all => \&skip_all, 490b39c5158Smillert tests => \&_plan_tests, 491b39c5158Smillert); 492b39c5158Smillert 493b39c5158Smillertsub plan { 494b39c5158Smillert my( $self, $cmd, $arg ) = @_; 495b39c5158Smillert 496b39c5158Smillert return unless $cmd; 497b39c5158Smillert 4989f11ffb7Safresh1 my $ctx = $self->ctx; 4999f11ffb7Safresh1 my $hub = $ctx->hub; 500b39c5158Smillert 5019f11ffb7Safresh1 $ctx->throw("You tried to plan twice") if $hub->plan; 5029f11ffb7Safresh1 5039f11ffb7Safresh1 local $Level = $Level + 1; 504b39c5158Smillert 505b39c5158Smillert if( my $method = $plan_cmds{$cmd} ) { 506b39c5158Smillert local $Level = $Level + 1; 507b39c5158Smillert $self->$method($arg); 508b39c5158Smillert } 509b39c5158Smillert else { 510b39c5158Smillert my @args = grep { defined } ( $cmd, $arg ); 5119f11ffb7Safresh1 $ctx->throw("plan() doesn't understand @args"); 512b39c5158Smillert } 513b39c5158Smillert 5149f11ffb7Safresh1 release $ctx, 1; 515b39c5158Smillert} 516b39c5158Smillert 517b39c5158Smillert 518b39c5158Smillertsub _plan_tests { 519b39c5158Smillert my($self, $arg) = @_; 520b39c5158Smillert 5219f11ffb7Safresh1 my $ctx = $self->ctx; 5229f11ffb7Safresh1 523b39c5158Smillert if($arg) { 524b39c5158Smillert local $Level = $Level + 1; 5259f11ffb7Safresh1 $self->expected_tests($arg); 526b39c5158Smillert } 527b39c5158Smillert elsif( !defined $arg ) { 5289f11ffb7Safresh1 $ctx->throw("Got an undefined number of tests"); 529b39c5158Smillert } 530b39c5158Smillert else { 5319f11ffb7Safresh1 $ctx->throw("You said to run 0 tests"); 532b39c5158Smillert } 533b39c5158Smillert 5349f11ffb7Safresh1 $ctx->release; 535b39c5158Smillert} 536b39c5158Smillert 537b39c5158Smillert 538b39c5158Smillertsub expected_tests { 539b39c5158Smillert my $self = shift; 540b39c5158Smillert my($max) = @_; 541b39c5158Smillert 5429f11ffb7Safresh1 my $ctx = $self->ctx; 5439f11ffb7Safresh1 544b39c5158Smillert if(@_) { 545b39c5158Smillert $self->croak("Number of tests must be a positive integer. You gave it '$max'") 546b39c5158Smillert unless $max =~ /^\+?\d+$/; 547b39c5158Smillert 5489f11ffb7Safresh1 $ctx->plan($max); 549b39c5158Smillert } 550b39c5158Smillert 5519f11ffb7Safresh1 my $hub = $ctx->hub; 552b39c5158Smillert 5539f11ffb7Safresh1 $ctx->release; 554b39c5158Smillert 5559f11ffb7Safresh1 my $plan = $hub->plan; 5569f11ffb7Safresh1 return 0 unless $plan; 5579f11ffb7Safresh1 return 0 if $plan =~ m/\D/; 5589f11ffb7Safresh1 return $plan; 5599f11ffb7Safresh1} 560b39c5158Smillert 561b39c5158Smillert 562b39c5158Smillertsub no_plan { 563b39c5158Smillert my($self, $arg) = @_; 564b39c5158Smillert 5659f11ffb7Safresh1 my $ctx = $self->ctx; 566b39c5158Smillert 5679f11ffb7Safresh1 if (defined $ctx->hub->plan) { 5689f11ffb7Safresh1 warn "Plan already set, no_plan() is a no-op, this will change to a hard failure in the future."; 5699f11ffb7Safresh1 $ctx->release; 570b39c5158Smillert return; 571b39c5158Smillert } 572b39c5158Smillert 5739f11ffb7Safresh1 $ctx->alert("no_plan takes no arguments") if $arg; 57465d9bffcSjasper 5759f11ffb7Safresh1 $ctx->hub->plan('NO PLAN'); 576b39c5158Smillert 5779f11ffb7Safresh1 release $ctx, 1; 578b39c5158Smillert} 579b39c5158Smillert 580b39c5158Smillert 581b39c5158Smillertsub done_testing { 582b39c5158Smillert my($self, $num_tests) = @_; 583b39c5158Smillert 5849f11ffb7Safresh1 my $ctx = $self->ctx; 5859f11ffb7Safresh1 5869f11ffb7Safresh1 my $meta = $ctx->hub->meta(__PACKAGE__, {}); 5879f11ffb7Safresh1 5889f11ffb7Safresh1 if ($meta->{Done_Testing}) { 5899f11ffb7Safresh1 my ($file, $line) = @{$meta->{Done_Testing}}[1,2]; 5909f11ffb7Safresh1 local $ctx->hub->{ended}; # OMG This is awful. 5919f11ffb7Safresh1 $self->ok(0, "done_testing() was already called at $file line $line"); 5929f11ffb7Safresh1 $ctx->release; 5939f11ffb7Safresh1 return; 5949f11ffb7Safresh1 } 5959f11ffb7Safresh1 $meta->{Done_Testing} = [$ctx->trace->call]; 5969f11ffb7Safresh1 5979f11ffb7Safresh1 my $plan = $ctx->hub->plan; 5989f11ffb7Safresh1 my $count = $ctx->hub->count; 5999f11ffb7Safresh1 6009f11ffb7Safresh1 # If done_testing() specified the number of tests, shut off no_plan 601b39c5158Smillert if( defined $num_tests ) { 6029f11ffb7Safresh1 $ctx->plan($num_tests) if !$plan || $plan eq 'NO PLAN'; 6039f11ffb7Safresh1 } 6049f11ffb7Safresh1 elsif ($count && defined $num_tests && $count != $num_tests) { 6059f11ffb7Safresh1 $self->ok(0, "planned to run @{[ $self->expected_tests ]} but done_testing() expects $num_tests"); 606b39c5158Smillert } 607b39c5158Smillert else { 608b39c5158Smillert $num_tests = $self->current_test; 609b39c5158Smillert } 610b39c5158Smillert 611b39c5158Smillert if( $self->expected_tests && $num_tests != $self->expected_tests ) { 612b39c5158Smillert $self->ok(0, "planned to run @{[ $self->expected_tests ]} ". 613b39c5158Smillert "but done_testing() expects $num_tests"); 614b39c5158Smillert } 6159f11ffb7Safresh1 6169f11ffb7Safresh1 $ctx->plan($num_tests) if $ctx->hub->plan && $ctx->hub->plan eq 'NO PLAN'; 6179f11ffb7Safresh1 6189f11ffb7Safresh1 $ctx->hub->finalize($ctx->trace, 1); 6199f11ffb7Safresh1 6209f11ffb7Safresh1 release $ctx, 1; 621b39c5158Smillert} 622b39c5158Smillert 623b39c5158Smillert 624b39c5158Smillertsub has_plan { 625b39c5158Smillert my $self = shift; 626b39c5158Smillert 6279f11ffb7Safresh1 my $ctx = $self->ctx; 6289f11ffb7Safresh1 my $plan = $ctx->hub->plan; 6299f11ffb7Safresh1 $ctx->release; 6309f11ffb7Safresh1 6319f11ffb7Safresh1 return( $plan ) if $plan && $plan !~ m/\D/; 6329f11ffb7Safresh1 return('no_plan') if $plan && $plan eq 'NO PLAN'; 633b39c5158Smillert return(undef); 634b39c5158Smillert} 635b39c5158Smillert 636b39c5158Smillert 637b39c5158Smillertsub skip_all { 638b39c5158Smillert my( $self, $reason ) = @_; 639b39c5158Smillert 6409f11ffb7Safresh1 my $ctx = $self->ctx; 641b39c5158Smillert 6429f11ffb7Safresh1 $ctx->hub->meta(__PACKAGE__, {})->{Skip_All} = $reason || 1; 6439f11ffb7Safresh1 6449f11ffb7Safresh1 # Work around old perl bug 6459f11ffb7Safresh1 if ($] < 5.020000) { 6469f11ffb7Safresh1 my $begin = 0; 6479f11ffb7Safresh1 my $level = 0; 6489f11ffb7Safresh1 while (my @call = caller($level++)) { 6499f11ffb7Safresh1 last unless @call && $call[0]; 6509f11ffb7Safresh1 next unless $call[3] =~ m/::BEGIN$/; 6519f11ffb7Safresh1 $begin++; 6529f11ffb7Safresh1 last; 653b39c5158Smillert } 6549f11ffb7Safresh1 # HACK! 6559f11ffb7Safresh1 die 'Label not found for "last T2_SUBTEST_WRAPPER"' if $begin && $ctx->hub->meta(__PACKAGE__, {})->{parent}; 656b39c5158Smillert } 657b39c5158Smillert 658eac174f2Safresh1 $reason = "$reason" if defined $reason; 659eac174f2Safresh1 6609f11ffb7Safresh1 $ctx->plan(0, SKIP => $reason); 6619f11ffb7Safresh1} 662b39c5158Smillert 663b39c5158Smillert 664b39c5158Smillertsub exported_to { 665b39c5158Smillert my( $self, $pack ) = @_; 666b39c5158Smillert 667b39c5158Smillert if( defined $pack ) { 668b39c5158Smillert $self->{Exported_To} = $pack; 669b39c5158Smillert } 670b39c5158Smillert return $self->{Exported_To}; 671b39c5158Smillert} 672b39c5158Smillert 673b39c5158Smillert 674b39c5158Smillertsub ok { 675b39c5158Smillert my( $self, $test, $name ) = @_; 676b39c5158Smillert 6779f11ffb7Safresh1 my $ctx = $self->ctx; 6789f11ffb7Safresh1 679b39c5158Smillert # $test might contain an object which we don't want to accidentally 680b39c5158Smillert # store, so we turn it into a boolean. 681b39c5158Smillert $test = $test ? 1 : 0; 682b39c5158Smillert 683b39c5158Smillert # In case $name is a string overloaded object, force it to stringify. 6849f11ffb7Safresh1 no warnings qw/uninitialized numeric/; 6859f11ffb7Safresh1 $name = "$name" if defined $name; 686b39c5158Smillert 6879f11ffb7Safresh1 # Profiling showed that the regex here was a huge time waster, doing the 6889f11ffb7Safresh1 # numeric addition first cuts our profile time from ~300ms to ~50ms 6899f11ffb7Safresh1 $self->diag(<<" ERR") if 0 + $name && $name =~ /^[\d\s]+$/; 690b39c5158Smillert You named your test '$name'. You shouldn't use numbers for your test names. 691b39c5158Smillert Very confusing. 692b39c5158Smillert ERR 6939f11ffb7Safresh1 use warnings qw/uninitialized numeric/; 694b39c5158Smillert 6959f11ffb7Safresh1 my $trace = $ctx->{trace}; 6969f11ffb7Safresh1 my $hub = $ctx->{hub}; 697b39c5158Smillert 6989f11ffb7Safresh1 my $result = { 6999f11ffb7Safresh1 ok => $test, 7009f11ffb7Safresh1 actual_ok => $test, 7019f11ffb7Safresh1 reason => '', 7029f11ffb7Safresh1 type => '', 7039f11ffb7Safresh1 (name => defined($name) ? $name : ''), 7049f11ffb7Safresh1 }; 705b39c5158Smillert 7069f11ffb7Safresh1 $hub->{_meta}->{+__PACKAGE__}->{Test_Results}[ $hub->{count} ] = $result unless $self->{no_log_results}; 707b39c5158Smillert 7089f11ffb7Safresh1 my $orig_name = $name; 7099f11ffb7Safresh1 7109f11ffb7Safresh1 my @attrs; 7119f11ffb7Safresh1 my $subevents = delete $self->{subevents}; 7129f11ffb7Safresh1 my $subtest_id = delete $self->{subtest_id}; 7139f11ffb7Safresh1 my $subtest_uuid = delete $self->{subtest_uuid}; 7149f11ffb7Safresh1 my $subtest_buffered = delete $self->{subtest_buffered}; 7159f11ffb7Safresh1 my $epkg = 'Test2::Event::Ok'; 7169f11ffb7Safresh1 if ($subevents) { 7179f11ffb7Safresh1 $epkg = 'Test2::Event::Subtest'; 7189f11ffb7Safresh1 push @attrs => (subevents => $subevents, subtest_id => $subtest_id, subtest_uuid => $subtest_uuid, buffered => $subtest_buffered); 719b39c5158Smillert } 720b39c5158Smillert 7219f11ffb7Safresh1 my $e = bless { 7229f11ffb7Safresh1 trace => bless( {%$trace}, 'Test2::EventFacet::Trace'), 7239f11ffb7Safresh1 pass => $test, 7249f11ffb7Safresh1 name => $name, 7259f11ffb7Safresh1 _meta => {'Test::Builder' => $result}, 7269f11ffb7Safresh1 effective_pass => $test, 7279f11ffb7Safresh1 @attrs, 7289f11ffb7Safresh1 }, $epkg; 7299f11ffb7Safresh1 $hub->send($e); 730b39c5158Smillert 7319f11ffb7Safresh1 $self->_ok_debug($trace, $orig_name) unless($test); 7329f11ffb7Safresh1 7339f11ffb7Safresh1 $ctx->release; 7349f11ffb7Safresh1 return $test; 735b39c5158Smillert} 736b39c5158Smillert 7379f11ffb7Safresh1sub _ok_debug { 7389f11ffb7Safresh1 my $self = shift; 7399f11ffb7Safresh1 my ($trace, $orig_name) = @_; 740b39c5158Smillert 74156d68f1eSafresh1 my $is_todo = $self->in_todo; 742b39c5158Smillert 7439f11ffb7Safresh1 my $msg = $is_todo ? "Failed (TODO)" : "Failed"; 744b39c5158Smillert 7459f11ffb7Safresh1 my (undef, $file, $line) = $trace->call; 7469f11ffb7Safresh1 if (defined $orig_name) { 7479f11ffb7Safresh1 $self->diag(qq[ $msg test '$orig_name'\n at $file line $line.\n]); 748b39c5158Smillert } 749b39c5158Smillert else { 750b39c5158Smillert $self->diag(qq[ $msg test at $file line $line.\n]); 751b39c5158Smillert } 752b39c5158Smillert} 753b39c5158Smillert 7549f11ffb7Safresh1sub _diag_fh { 755b39c5158Smillert my $self = shift; 7569f11ffb7Safresh1 local $Level = $Level + 1; 7579f11ffb7Safresh1 return $self->in_todo ? $self->todo_output : $self->failure_output; 758b39c5158Smillert} 759b39c5158Smillert 760b39c5158Smillertsub _unoverload { 7619f11ffb7Safresh1 my ($self, $type, $thing) = @_; 762b39c5158Smillert 7639f11ffb7Safresh1 return unless ref $$thing; 7649f11ffb7Safresh1 return unless blessed($$thing) || scalar $self->_try(sub{ $$thing->isa('UNIVERSAL') }); 7659f11ffb7Safresh1 { 7669f11ffb7Safresh1 local ($!, $@); 7679f11ffb7Safresh1 require overload; 7689f11ffb7Safresh1 } 7699f11ffb7Safresh1 my $string_meth = overload::Method( $$thing, $type ) || return; 770eac174f2Safresh1 $$thing = $$thing->$string_meth(undef, 0); 771b39c5158Smillert} 772b39c5158Smillert 773b39c5158Smillertsub _unoverload_str { 774b39c5158Smillert my $self = shift; 775b39c5158Smillert 7769f11ffb7Safresh1 $self->_unoverload( q[""], $_ ) for @_; 777b39c5158Smillert} 778b39c5158Smillert 779b39c5158Smillertsub _unoverload_num { 780b39c5158Smillert my $self = shift; 781b39c5158Smillert 7829f11ffb7Safresh1 $self->_unoverload( '0+', $_ ) for @_; 783b39c5158Smillert 784b39c5158Smillert for my $val (@_) { 785b39c5158Smillert next unless $self->_is_dualvar($$val); 786b39c5158Smillert $$val = $$val + 0; 787b39c5158Smillert } 788b39c5158Smillert} 789b39c5158Smillert 790b39c5158Smillert# This is a hack to detect a dualvar such as $! 791b39c5158Smillertsub _is_dualvar { 792b39c5158Smillert my( $self, $val ) = @_; 793b39c5158Smillert 794b39c5158Smillert # Objects are not dualvars. 795b39c5158Smillert return 0 if ref $val; 796b39c5158Smillert 797b39c5158Smillert no warnings 'numeric'; 798b39c5158Smillert my $numval = $val + 0; 799e5157e49Safresh1 return ($numval != 0 and $numval ne $val ? 1 : 0); 800b39c5158Smillert} 801b39c5158Smillert 802b39c5158Smillert 803b39c5158Smillertsub is_eq { 804b39c5158Smillert my( $self, $got, $expect, $name ) = @_; 8059f11ffb7Safresh1 8069f11ffb7Safresh1 my $ctx = $self->ctx; 8079f11ffb7Safresh1 808b39c5158Smillert local $Level = $Level + 1; 809b39c5158Smillert 810b39c5158Smillert if( !defined $got || !defined $expect ) { 811b39c5158Smillert # undef only matches undef and nothing else 812b39c5158Smillert my $test = !defined $got && !defined $expect; 813b39c5158Smillert 814b39c5158Smillert $self->ok( $test, $name ); 815b39c5158Smillert $self->_is_diag( $got, 'eq', $expect ) unless $test; 8169f11ffb7Safresh1 $ctx->release; 817b39c5158Smillert return $test; 818b39c5158Smillert } 819b39c5158Smillert 8209f11ffb7Safresh1 release $ctx, $self->cmp_ok( $got, 'eq', $expect, $name ); 821b39c5158Smillert} 822b39c5158Smillert 8239f11ffb7Safresh1 824b39c5158Smillertsub is_num { 825b39c5158Smillert my( $self, $got, $expect, $name ) = @_; 8269f11ffb7Safresh1 my $ctx = $self->ctx; 827b39c5158Smillert local $Level = $Level + 1; 828b39c5158Smillert 829b39c5158Smillert if( !defined $got || !defined $expect ) { 830b39c5158Smillert # undef only matches undef and nothing else 831b39c5158Smillert my $test = !defined $got && !defined $expect; 832b39c5158Smillert 833b39c5158Smillert $self->ok( $test, $name ); 834b39c5158Smillert $self->_is_diag( $got, '==', $expect ) unless $test; 8359f11ffb7Safresh1 $ctx->release; 836b39c5158Smillert return $test; 837b39c5158Smillert } 838b39c5158Smillert 8399f11ffb7Safresh1 release $ctx, $self->cmp_ok( $got, '==', $expect, $name ); 840b39c5158Smillert} 841b39c5158Smillert 8429f11ffb7Safresh1 843b39c5158Smillertsub _diag_fmt { 844b39c5158Smillert my( $self, $type, $val ) = @_; 845b39c5158Smillert 846b39c5158Smillert if( defined $$val ) { 847b39c5158Smillert if( $type eq 'eq' or $type eq 'ne' ) { 848b39c5158Smillert # quote and force string context 849b39c5158Smillert $$val = "'$$val'"; 850b39c5158Smillert } 851b39c5158Smillert else { 852b39c5158Smillert # force numeric context 853b39c5158Smillert $self->_unoverload_num($val); 854b39c5158Smillert } 855b39c5158Smillert } 856b39c5158Smillert else { 857b39c5158Smillert $$val = 'undef'; 858b39c5158Smillert } 859b39c5158Smillert 860b39c5158Smillert return; 861b39c5158Smillert} 862b39c5158Smillert 8639f11ffb7Safresh1 864b39c5158Smillertsub _is_diag { 865b39c5158Smillert my( $self, $got, $type, $expect ) = @_; 866b39c5158Smillert 867b39c5158Smillert $self->_diag_fmt( $type, $_ ) for \$got, \$expect; 868b39c5158Smillert 869b39c5158Smillert local $Level = $Level + 1; 870b39c5158Smillert return $self->diag(<<"DIAGNOSTIC"); 871b39c5158Smillert got: $got 872b39c5158Smillert expected: $expect 873b39c5158SmillertDIAGNOSTIC 874b39c5158Smillert 875b39c5158Smillert} 876b39c5158Smillert 877b39c5158Smillertsub _isnt_diag { 878b39c5158Smillert my( $self, $got, $type ) = @_; 879b39c5158Smillert 880b39c5158Smillert $self->_diag_fmt( $type, \$got ); 881b39c5158Smillert 882b39c5158Smillert local $Level = $Level + 1; 883b39c5158Smillert return $self->diag(<<"DIAGNOSTIC"); 884b39c5158Smillert got: $got 885b39c5158Smillert expected: anything else 886b39c5158SmillertDIAGNOSTIC 887b39c5158Smillert} 888b39c5158Smillert 889b39c5158Smillert 890b39c5158Smillertsub isnt_eq { 891b39c5158Smillert my( $self, $got, $dont_expect, $name ) = @_; 8929f11ffb7Safresh1 my $ctx = $self->ctx; 893b39c5158Smillert local $Level = $Level + 1; 894b39c5158Smillert 895b39c5158Smillert if( !defined $got || !defined $dont_expect ) { 896b39c5158Smillert # undef only matches undef and nothing else 897b39c5158Smillert my $test = defined $got || defined $dont_expect; 898b39c5158Smillert 899b39c5158Smillert $self->ok( $test, $name ); 900b39c5158Smillert $self->_isnt_diag( $got, 'ne' ) unless $test; 9019f11ffb7Safresh1 $ctx->release; 902b39c5158Smillert return $test; 903b39c5158Smillert } 904b39c5158Smillert 9059f11ffb7Safresh1 release $ctx, $self->cmp_ok( $got, 'ne', $dont_expect, $name ); 906b39c5158Smillert} 907b39c5158Smillert 908b39c5158Smillertsub isnt_num { 909b39c5158Smillert my( $self, $got, $dont_expect, $name ) = @_; 9109f11ffb7Safresh1 my $ctx = $self->ctx; 911b39c5158Smillert local $Level = $Level + 1; 912b39c5158Smillert 913b39c5158Smillert if( !defined $got || !defined $dont_expect ) { 914b39c5158Smillert # undef only matches undef and nothing else 915b39c5158Smillert my $test = defined $got || defined $dont_expect; 916b39c5158Smillert 917b39c5158Smillert $self->ok( $test, $name ); 918b39c5158Smillert $self->_isnt_diag( $got, '!=' ) unless $test; 9199f11ffb7Safresh1 $ctx->release; 920b39c5158Smillert return $test; 921b39c5158Smillert } 922b39c5158Smillert 9239f11ffb7Safresh1 release $ctx, $self->cmp_ok( $got, '!=', $dont_expect, $name ); 924b39c5158Smillert} 925b39c5158Smillert 926b39c5158Smillert 927b39c5158Smillertsub like { 928e5157e49Safresh1 my( $self, $thing, $regex, $name ) = @_; 9299f11ffb7Safresh1 my $ctx = $self->ctx; 930b39c5158Smillert 931b39c5158Smillert local $Level = $Level + 1; 9329f11ffb7Safresh1 9339f11ffb7Safresh1 release $ctx, $self->_regex_ok( $thing, $regex, '=~', $name ); 934b39c5158Smillert} 935b39c5158Smillert 936b39c5158Smillertsub unlike { 937e5157e49Safresh1 my( $self, $thing, $regex, $name ) = @_; 9389f11ffb7Safresh1 my $ctx = $self->ctx; 939b39c5158Smillert 940b39c5158Smillert local $Level = $Level + 1; 9419f11ffb7Safresh1 9429f11ffb7Safresh1 release $ctx, $self->_regex_ok( $thing, $regex, '!~', $name ); 943b39c5158Smillert} 944b39c5158Smillert 945b39c5158Smillert 946b39c5158Smillertmy %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" ); 947b39c5158Smillert 948e5157e49Safresh1# Bad, these are not comparison operators. Should we include more? 949e5157e49Safresh1my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "..."); 950e5157e49Safresh1 951b39c5158Smillertsub cmp_ok { 952b39c5158Smillert my( $self, $got, $type, $expect, $name ) = @_; 9539f11ffb7Safresh1 my $ctx = $self->ctx; 954b39c5158Smillert 955e5157e49Safresh1 if ($cmp_ok_bl{$type}) { 9569f11ffb7Safresh1 $ctx->throw("$type is not a valid comparison operator in cmp_ok()"); 957e5157e49Safresh1 } 958e5157e49Safresh1 959b8851fccSafresh1 my ($test, $succ); 960b39c5158Smillert my $error; 961b39c5158Smillert { 962b39c5158Smillert ## no critic (BuiltinFunctions::ProhibitStringyEval) 963b39c5158Smillert 964b39c5158Smillert local( $@, $!, $SIG{__DIE__} ); # isolate eval 965b39c5158Smillert 9669f11ffb7Safresh1 my($pack, $file, $line) = $ctx->trace->call(); 967eac174f2Safresh1 my $warning_bits = $ctx->trace->warning_bits; 968eac174f2Safresh1 # convert this to a code string so the BEGIN doesn't have to close 969eac174f2Safresh1 # over it, which can lead to issues with Devel::Cover 970eac174f2Safresh1 my $bits_code = defined $warning_bits ? qq["\Q$warning_bits\E"] : 'undef'; 971b39c5158Smillert 97265d9bffcSjasper # This is so that warnings come out at the caller's level 973b8851fccSafresh1 $succ = eval qq[ 974eac174f2Safresh1BEGIN {\${^WARNING_BITS} = $bits_code}; 97565d9bffcSjasper#line $line "(eval in cmp_ok) $file" 976b8851fccSafresh1\$test = (\$got $type \$expect); 977b8851fccSafresh11; 978b39c5158Smillert]; 979b39c5158Smillert $error = $@; 980b39c5158Smillert } 981b39c5158Smillert local $Level = $Level + 1; 982b39c5158Smillert my $ok = $self->ok( $test, $name ); 983b39c5158Smillert 984b39c5158Smillert # Treat overloaded objects as numbers if we're asked to do a 985b39c5158Smillert # numeric comparison. 986b39c5158Smillert my $unoverload 987b39c5158Smillert = $numeric_cmps{$type} 988b39c5158Smillert ? '_unoverload_num' 989b39c5158Smillert : '_unoverload_str'; 990b39c5158Smillert 991b8851fccSafresh1 $self->diag(<<"END") unless $succ; 992b39c5158SmillertAn error occurred while using $type: 993b39c5158Smillert------------------------------------ 994b39c5158Smillert$error 995b39c5158Smillert------------------------------------ 996b39c5158SmillertEND 997b39c5158Smillert 998b39c5158Smillert unless($ok) { 999b39c5158Smillert $self->$unoverload( \$got, \$expect ); 1000b39c5158Smillert 1001b39c5158Smillert if( $type =~ /^(eq|==)$/ ) { 1002b39c5158Smillert $self->_is_diag( $got, $type, $expect ); 1003b39c5158Smillert } 1004b39c5158Smillert elsif( $type =~ /^(ne|!=)$/ ) { 1005e0680481Safresh1 if (defined($got) xor defined($expect)) { 10069f11ffb7Safresh1 $self->_cmp_diag( $got, $type, $expect ); 10079f11ffb7Safresh1 } 10089f11ffb7Safresh1 else { 1009b39c5158Smillert $self->_isnt_diag( $got, $type ); 1010b39c5158Smillert } 10119f11ffb7Safresh1 } 1012b39c5158Smillert else { 1013b39c5158Smillert $self->_cmp_diag( $got, $type, $expect ); 1014b39c5158Smillert } 1015b39c5158Smillert } 10169f11ffb7Safresh1 return release $ctx, $ok; 1017b39c5158Smillert} 1018b39c5158Smillert 1019b39c5158Smillertsub _cmp_diag { 1020b39c5158Smillert my( $self, $got, $type, $expect ) = @_; 1021b39c5158Smillert 1022b39c5158Smillert $got = defined $got ? "'$got'" : 'undef'; 1023b39c5158Smillert $expect = defined $expect ? "'$expect'" : 'undef'; 1024b39c5158Smillert 1025b39c5158Smillert local $Level = $Level + 1; 1026b39c5158Smillert return $self->diag(<<"DIAGNOSTIC"); 1027b39c5158Smillert $got 1028b39c5158Smillert $type 1029b39c5158Smillert $expect 1030b39c5158SmillertDIAGNOSTIC 1031b39c5158Smillert} 1032b39c5158Smillert 1033b39c5158Smillertsub _caller_context { 1034b39c5158Smillert my $self = shift; 1035b39c5158Smillert 1036b39c5158Smillert my( $pack, $file, $line ) = $self->caller(1); 1037b39c5158Smillert 1038b39c5158Smillert my $code = ''; 1039b39c5158Smillert $code .= "#line $line $file\n" if defined $file and defined $line; 1040b39c5158Smillert 1041b39c5158Smillert return $code; 1042b39c5158Smillert} 1043b39c5158Smillert 1044b39c5158Smillert 1045b39c5158Smillertsub BAIL_OUT { 1046b39c5158Smillert my( $self, $reason ) = @_; 1047b39c5158Smillert 10489f11ffb7Safresh1 my $ctx = $self->ctx; 10499f11ffb7Safresh1 1050b39c5158Smillert $self->{Bailed_Out} = 1; 1051e5157e49Safresh1 10529f11ffb7Safresh1 $ctx->bail($reason); 1053e5157e49Safresh1} 1054e5157e49Safresh1 1055b39c5158Smillert 1056b39c5158Smillert{ 1057b39c5158Smillert no warnings 'once'; 1058b39c5158Smillert *BAILOUT = \&BAIL_OUT; 1059b39c5158Smillert} 1060b39c5158Smillert 1061b39c5158Smillertsub skip { 1062b8851fccSafresh1 my( $self, $why, $name ) = @_; 1063b39c5158Smillert $why ||= ''; 1064b8851fccSafresh1 $name = '' unless defined $name; 1065b39c5158Smillert $self->_unoverload_str( \$why ); 1066b39c5158Smillert 10679f11ffb7Safresh1 my $ctx = $self->ctx; 1068b39c5158Smillert 1069eac174f2Safresh1 $name = "$name"; 1070eac174f2Safresh1 $why = "$why"; 1071eac174f2Safresh1 1072eac174f2Safresh1 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness. 1073eac174f2Safresh1 $name =~ s{\n}{\n# }sg; 1074eac174f2Safresh1 $why =~ s{\n}{\n# }sg; 1075eac174f2Safresh1 10769f11ffb7Safresh1 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = { 1077b39c5158Smillert 'ok' => 1, 1078b39c5158Smillert actual_ok => 1, 1079b8851fccSafresh1 name => $name, 1080b39c5158Smillert type => 'skip', 1081b39c5158Smillert reason => $why, 10829f11ffb7Safresh1 } unless $self->{no_log_results}; 1083b39c5158Smillert 10849f11ffb7Safresh1 my $tctx = $ctx->snapshot; 10859f11ffb7Safresh1 $tctx->skip('', $why); 1086b39c5158Smillert 10879f11ffb7Safresh1 return release $ctx, 1; 1088b39c5158Smillert} 1089b39c5158Smillert 1090b39c5158Smillert 1091b39c5158Smillertsub todo_skip { 1092b39c5158Smillert my( $self, $why ) = @_; 1093b39c5158Smillert $why ||= ''; 1094b39c5158Smillert 10959f11ffb7Safresh1 my $ctx = $self->ctx; 1096b39c5158Smillert 10979f11ffb7Safresh1 $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = { 1098b39c5158Smillert 'ok' => 1, 1099b39c5158Smillert actual_ok => 0, 1100b39c5158Smillert name => '', 1101b39c5158Smillert type => 'todo_skip', 1102b39c5158Smillert reason => $why, 11039f11ffb7Safresh1 } unless $self->{no_log_results}; 1104b39c5158Smillert 11059f11ffb7Safresh1 $why =~ s{\n}{\n# }sg; 11069f11ffb7Safresh1 my $tctx = $ctx->snapshot; 11079f11ffb7Safresh1 $tctx->send_event( 'Skip', todo => $why, todo_diag => 1, reason => $why, pass => 0); 1108b39c5158Smillert 11099f11ffb7Safresh1 return release $ctx, 1; 1110b39c5158Smillert} 1111b39c5158Smillert 1112b39c5158Smillert 1113b39c5158Smillertsub maybe_regex { 1114b39c5158Smillert my( $self, $regex ) = @_; 1115b39c5158Smillert my $usable_regex = undef; 1116b39c5158Smillert 1117b39c5158Smillert return $usable_regex unless defined $regex; 1118b39c5158Smillert 1119b39c5158Smillert my( $re, $opts ); 1120b39c5158Smillert 1121b39c5158Smillert # Check for qr/foo/ 1122b39c5158Smillert if( _is_qr($regex) ) { 1123b39c5158Smillert $usable_regex = $regex; 1124b39c5158Smillert } 1125b39c5158Smillert # Check for '/foo/' or 'm,foo,' 1126b39c5158Smillert elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or 1127b39c5158Smillert ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx 1128b39c5158Smillert ) 1129b39c5158Smillert { 1130b39c5158Smillert $usable_regex = length $opts ? "(?$opts)$re" : $re; 1131b39c5158Smillert } 1132b39c5158Smillert 1133b39c5158Smillert return $usable_regex; 1134b39c5158Smillert} 1135b39c5158Smillert 1136b39c5158Smillertsub _is_qr { 1137b39c5158Smillert my $regex = shift; 1138b39c5158Smillert 1139b39c5158Smillert # is_regexp() checks for regexes in a robust manner, say if they're 1140b39c5158Smillert # blessed. 1141b39c5158Smillert return re::is_regexp($regex) if defined &re::is_regexp; 1142b39c5158Smillert return ref $regex eq 'Regexp'; 1143b39c5158Smillert} 1144b39c5158Smillert 1145b39c5158Smillertsub _regex_ok { 1146e5157e49Safresh1 my( $self, $thing, $regex, $cmp, $name ) = @_; 1147b39c5158Smillert 1148b39c5158Smillert my $ok = 0; 1149b39c5158Smillert my $usable_regex = $self->maybe_regex($regex); 1150b39c5158Smillert unless( defined $usable_regex ) { 1151b39c5158Smillert local $Level = $Level + 1; 1152b39c5158Smillert $ok = $self->ok( 0, $name ); 1153b39c5158Smillert $self->diag(" '$regex' doesn't look much like a regex to me."); 1154b39c5158Smillert return $ok; 1155b39c5158Smillert } 1156b39c5158Smillert 1157b39c5158Smillert { 1158b39c5158Smillert my $test; 1159b39c5158Smillert my $context = $self->_caller_context; 1160b39c5158Smillert 1161e5157e49Safresh1 { 1162e5157e49Safresh1 ## no critic (BuiltinFunctions::ProhibitStringyEval) 1163e5157e49Safresh1 1164b39c5158Smillert local( $@, $!, $SIG{__DIE__} ); # isolate eval 1165b39c5158Smillert 1166e5157e49Safresh1 # No point in issuing an uninit warning, they'll see it in the diagnostics 1167e5157e49Safresh1 no warnings 'uninitialized'; 1168e5157e49Safresh1 1169e5157e49Safresh1 $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0}; 1170e5157e49Safresh1 } 1171b39c5158Smillert 1172b39c5158Smillert $test = !$test if $cmp eq '!~'; 1173b39c5158Smillert 1174b39c5158Smillert local $Level = $Level + 1; 1175b39c5158Smillert $ok = $self->ok( $test, $name ); 1176b39c5158Smillert } 1177b39c5158Smillert 1178b39c5158Smillert unless($ok) { 1179e5157e49Safresh1 $thing = defined $thing ? "'$thing'" : 'undef'; 1180b39c5158Smillert my $match = $cmp eq '=~' ? "doesn't match" : "matches"; 1181b39c5158Smillert 1182b39c5158Smillert local $Level = $Level + 1; 1183e5157e49Safresh1 $self->diag( sprintf <<'DIAGNOSTIC', $thing, $match, $regex ); 1184b39c5158Smillert %s 1185b39c5158Smillert %13s '%s' 1186b39c5158SmillertDIAGNOSTIC 1187b39c5158Smillert 1188b39c5158Smillert } 1189b39c5158Smillert 1190b39c5158Smillert return $ok; 1191b39c5158Smillert} 1192b39c5158Smillert 1193b39c5158Smillert 1194b39c5158Smillertsub is_fh { 1195b39c5158Smillert my $self = shift; 1196b39c5158Smillert my $maybe_fh = shift; 1197b39c5158Smillert return 0 unless defined $maybe_fh; 1198b39c5158Smillert 1199b39c5158Smillert return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref 1200b39c5158Smillert return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob 1201b39c5158Smillert 1202b39c5158Smillert return eval { $maybe_fh->isa("IO::Handle") } || 1203b39c5158Smillert eval { tied($maybe_fh)->can('TIEHANDLE') }; 1204b39c5158Smillert} 1205b39c5158Smillert 1206b39c5158Smillert 1207b39c5158Smillertsub level { 1208b39c5158Smillert my( $self, $level ) = @_; 1209b39c5158Smillert 1210b39c5158Smillert if( defined $level ) { 1211b39c5158Smillert $Level = $level; 1212b39c5158Smillert } 1213b39c5158Smillert return $Level; 1214b39c5158Smillert} 1215b39c5158Smillert 1216b39c5158Smillert 1217b39c5158Smillertsub use_numbers { 1218b39c5158Smillert my( $self, $use_nums ) = @_; 1219b39c5158Smillert 12209f11ffb7Safresh1 my $ctx = $self->ctx; 12219f11ffb7Safresh1 my $format = $ctx->hub->format; 12229f11ffb7Safresh1 unless ($format && $format->can('no_numbers') && $format->can('set_no_numbers')) { 12239f11ffb7Safresh1 warn "The current formatter does not support 'use_numbers'" if $format; 12249f11ffb7Safresh1 return release $ctx, 0; 1225b39c5158Smillert } 1226b39c5158Smillert 12279f11ffb7Safresh1 $format->set_no_numbers(!$use_nums) if defined $use_nums; 1228b39c5158Smillert 12299f11ffb7Safresh1 return release $ctx, $format->no_numbers ? 0 : 1; 12309f11ffb7Safresh1} 1231b39c5158Smillert 12329f11ffb7Safresh1BEGIN { 12339f11ffb7Safresh1 for my $method (qw(no_header no_diag)) { 12349f11ffb7Safresh1 my $set = "set_$method"; 1235b39c5158Smillert my $code = sub { 1236b39c5158Smillert my( $self, $no ) = @_; 1237b39c5158Smillert 12389f11ffb7Safresh1 my $ctx = $self->ctx; 12399f11ffb7Safresh1 my $format = $ctx->hub->format; 12409f11ffb7Safresh1 unless ($format && $format->can($set)) { 12419f11ffb7Safresh1 warn "The current formatter does not support '$method'" if $format; 12429f11ffb7Safresh1 $ctx->release; 12439f11ffb7Safresh1 return 1244b39c5158Smillert } 12459f11ffb7Safresh1 12469f11ffb7Safresh1 $format->$set($no) if defined $no; 12479f11ffb7Safresh1 12489f11ffb7Safresh1 return release $ctx, $format->$method ? 1 : 0; 1249b39c5158Smillert }; 1250b39c5158Smillert 1251b39c5158Smillert no strict 'refs'; ## no critic 12529f11ffb7Safresh1 *$method = $code; 12539f11ffb7Safresh1 } 1254b39c5158Smillert} 1255b39c5158Smillert 12569f11ffb7Safresh1sub no_ending { 12579f11ffb7Safresh1 my( $self, $no ) = @_; 1258b39c5158Smillert 12599f11ffb7Safresh1 my $ctx = $self->ctx; 1260b39c5158Smillert 12619f11ffb7Safresh1 $ctx->hub->set_no_ending($no) if defined $no; 1262b39c5158Smillert 12639f11ffb7Safresh1 return release $ctx, $ctx->hub->no_ending; 12649f11ffb7Safresh1} 1265b39c5158Smillert 1266b39c5158Smillertsub diag { 1267b39c5158Smillert my $self = shift; 12689f11ffb7Safresh1 return unless @_; 1269b39c5158Smillert 12709f11ffb7Safresh1 my $text = join '' => map {defined($_) ? $_ : 'undef'} @_; 1271b39c5158Smillert 12729f11ffb7Safresh1 if (Test2::API::test2_in_preload()) { 12739f11ffb7Safresh1 chomp($text); 12749f11ffb7Safresh1 $text =~ s/^/# /msg; 12759f11ffb7Safresh1 print STDERR $text, "\n"; 1276b39c5158Smillert return 0; 1277b39c5158Smillert } 1278b39c5158Smillert 12799f11ffb7Safresh1 my $ctx = $self->ctx; 12809f11ffb7Safresh1 $ctx->diag($text); 12819f11ffb7Safresh1 $ctx->release; 12829f11ffb7Safresh1 return 0; 12839f11ffb7Safresh1} 1284b39c5158Smillert 1285b39c5158Smillert 12869f11ffb7Safresh1sub note { 12879f11ffb7Safresh1 my $self = shift; 12889f11ffb7Safresh1 return unless @_; 1289b39c5158Smillert 12909f11ffb7Safresh1 my $text = join '' => map {defined($_) ? $_ : 'undef'} @_; 1291b39c5158Smillert 12929f11ffb7Safresh1 if (Test2::API::test2_in_preload()) { 12939f11ffb7Safresh1 chomp($text); 12949f11ffb7Safresh1 $text =~ s/^/# /msg; 12959f11ffb7Safresh1 print STDOUT $text, "\n"; 12969f11ffb7Safresh1 return 0; 12979f11ffb7Safresh1 } 1298b39c5158Smillert 12999f11ffb7Safresh1 my $ctx = $self->ctx; 13009f11ffb7Safresh1 $ctx->note($text); 13019f11ffb7Safresh1 $ctx->release; 13029f11ffb7Safresh1 return 0; 13039f11ffb7Safresh1} 1304b39c5158Smillert 1305b39c5158Smillert 1306b39c5158Smillertsub explain { 1307b39c5158Smillert my $self = shift; 1308b39c5158Smillert 13099f11ffb7Safresh1 local ($@, $!); 13109f11ffb7Safresh1 require Data::Dumper; 13119f11ffb7Safresh1 1312b39c5158Smillert return map { 1313b39c5158Smillert ref $_ 1314b39c5158Smillert ? do { 1315b39c5158Smillert my $dumper = Data::Dumper->new( [$_] ); 1316b39c5158Smillert $dumper->Indent(1)->Terse(1); 1317b39c5158Smillert $dumper->Sortkeys(1) if $dumper->can("Sortkeys"); 1318b39c5158Smillert $dumper->Dump; 1319b39c5158Smillert } 1320b39c5158Smillert : $_ 1321b39c5158Smillert } @_; 1322b39c5158Smillert} 1323b39c5158Smillert 1324b39c5158Smillert 1325b39c5158Smillertsub output { 1326b39c5158Smillert my( $self, $fh ) = @_; 1327b39c5158Smillert 13289f11ffb7Safresh1 my $ctx = $self->ctx; 13299f11ffb7Safresh1 my $format = $ctx->hub->format; 13309f11ffb7Safresh1 $ctx->release; 13319f11ffb7Safresh1 return unless $format && $format->isa('Test2::Formatter::TAP'); 13329f11ffb7Safresh1 13339f11ffb7Safresh1 $format->handles->[Test2::Formatter::TAP::OUT_STD()] = $self->_new_fh($fh) 13349f11ffb7Safresh1 if defined $fh; 13359f11ffb7Safresh1 13369f11ffb7Safresh1 return $format->handles->[Test2::Formatter::TAP::OUT_STD()]; 1337b39c5158Smillert} 1338b39c5158Smillert 1339b39c5158Smillertsub failure_output { 1340b39c5158Smillert my( $self, $fh ) = @_; 1341b39c5158Smillert 13429f11ffb7Safresh1 my $ctx = $self->ctx; 13439f11ffb7Safresh1 my $format = $ctx->hub->format; 13449f11ffb7Safresh1 $ctx->release; 13459f11ffb7Safresh1 return unless $format && $format->isa('Test2::Formatter::TAP'); 13469f11ffb7Safresh1 13479f11ffb7Safresh1 $format->handles->[Test2::Formatter::TAP::OUT_ERR()] = $self->_new_fh($fh) 13489f11ffb7Safresh1 if defined $fh; 13499f11ffb7Safresh1 13509f11ffb7Safresh1 return $format->handles->[Test2::Formatter::TAP::OUT_ERR()]; 1351b39c5158Smillert} 1352b39c5158Smillert 1353b39c5158Smillertsub todo_output { 1354b39c5158Smillert my( $self, $fh ) = @_; 1355b39c5158Smillert 13569f11ffb7Safresh1 my $ctx = $self->ctx; 13579f11ffb7Safresh1 my $format = $ctx->hub->format; 13589f11ffb7Safresh1 $ctx->release; 13599f11ffb7Safresh1 return unless $format && $format->isa('Test::Builder::Formatter'); 13609f11ffb7Safresh1 13619f11ffb7Safresh1 $format->handles->[Test::Builder::Formatter::OUT_TODO()] = $self->_new_fh($fh) 13629f11ffb7Safresh1 if defined $fh; 13639f11ffb7Safresh1 13649f11ffb7Safresh1 return $format->handles->[Test::Builder::Formatter::OUT_TODO()]; 1365b39c5158Smillert} 1366b39c5158Smillert 1367b39c5158Smillertsub _new_fh { 1368b39c5158Smillert my $self = shift; 1369b39c5158Smillert my($file_or_fh) = shift; 1370b39c5158Smillert 1371b39c5158Smillert my $fh; 1372b39c5158Smillert if( $self->is_fh($file_or_fh) ) { 1373b39c5158Smillert $fh = $file_or_fh; 1374b39c5158Smillert } 1375b39c5158Smillert elsif( ref $file_or_fh eq 'SCALAR' ) { 1376b39c5158Smillert # Scalar refs as filehandles was added in 5.8. 1377b39c5158Smillert if( $] >= 5.008 ) { 1378b39c5158Smillert open $fh, ">>", $file_or_fh 1379b39c5158Smillert or $self->croak("Can't open scalar ref $file_or_fh: $!"); 1380b39c5158Smillert } 1381b39c5158Smillert # Emulate scalar ref filehandles with a tie. 1382b39c5158Smillert else { 1383b39c5158Smillert $fh = Test::Builder::IO::Scalar->new($file_or_fh) 1384b39c5158Smillert or $self->croak("Can't tie scalar ref $file_or_fh"); 1385b39c5158Smillert } 1386b39c5158Smillert } 1387b39c5158Smillert else { 1388b39c5158Smillert open $fh, ">", $file_or_fh 1389b39c5158Smillert or $self->croak("Can't open test output log $file_or_fh: $!"); 1390b39c5158Smillert _autoflush($fh); 1391b39c5158Smillert } 1392b39c5158Smillert 1393b39c5158Smillert return $fh; 1394b39c5158Smillert} 1395b39c5158Smillert 1396b39c5158Smillertsub _autoflush { 1397b39c5158Smillert my($fh) = shift; 1398b39c5158Smillert my $old_fh = select $fh; 1399b39c5158Smillert $| = 1; 1400b39c5158Smillert select $old_fh; 1401b39c5158Smillert 1402b39c5158Smillert return; 1403b39c5158Smillert} 1404b39c5158Smillert 1405b39c5158Smillert 14069f11ffb7Safresh1sub reset_outputs { 1407b39c5158Smillert my $self = shift; 1408b39c5158Smillert 14099f11ffb7Safresh1 my $ctx = $self->ctx; 14109f11ffb7Safresh1 my $format = $ctx->hub->format; 14119f11ffb7Safresh1 $ctx->release; 14129f11ffb7Safresh1 return unless $format && $format->isa('Test2::Formatter::TAP'); 14139f11ffb7Safresh1 $format->set_handles([@{$self->{Orig_Handles}}]) if $self->{Orig_Handles}; 1414b39c5158Smillert 1415b39c5158Smillert return; 1416b39c5158Smillert} 1417b39c5158Smillert 14189f11ffb7Safresh1 14199f11ffb7Safresh1sub carp { 14209f11ffb7Safresh1 my $self = shift; 14219f11ffb7Safresh1 my $ctx = $self->ctx; 14229f11ffb7Safresh1 $ctx->alert(join "", @_); 14239f11ffb7Safresh1 $ctx->release; 14249f11ffb7Safresh1} 14259f11ffb7Safresh1 14269f11ffb7Safresh1sub croak { 14279f11ffb7Safresh1 my $self = shift; 14289f11ffb7Safresh1 my $ctx = $self->ctx; 14299f11ffb7Safresh1 $ctx->throw(join "", @_); 14309f11ffb7Safresh1 $ctx->release; 14319f11ffb7Safresh1} 14329f11ffb7Safresh1 14339f11ffb7Safresh1 14349f11ffb7Safresh1sub current_test { 14359f11ffb7Safresh1 my( $self, $num ) = @_; 14369f11ffb7Safresh1 14379f11ffb7Safresh1 my $ctx = $self->ctx; 14389f11ffb7Safresh1 my $hub = $ctx->hub; 14399f11ffb7Safresh1 14409f11ffb7Safresh1 if( defined $num ) { 14419f11ffb7Safresh1 $hub->set_count($num); 14429f11ffb7Safresh1 14439f11ffb7Safresh1 unless ($self->{no_log_results}) { 14449f11ffb7Safresh1 # If the test counter is being pushed forward fill in the details. 14459f11ffb7Safresh1 my $test_results = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 14469f11ffb7Safresh1 if ($num > @$test_results) { 14479f11ffb7Safresh1 my $start = @$test_results ? @$test_results : 0; 14489f11ffb7Safresh1 for ($start .. $num - 1) { 14499f11ffb7Safresh1 $test_results->[$_] = { 14509f11ffb7Safresh1 'ok' => 1, 14519f11ffb7Safresh1 actual_ok => undef, 14529f11ffb7Safresh1 reason => 'incrementing test number', 14539f11ffb7Safresh1 type => 'unknown', 14549f11ffb7Safresh1 name => undef 14559f11ffb7Safresh1 }; 14569f11ffb7Safresh1 } 14579f11ffb7Safresh1 } 14589f11ffb7Safresh1 # If backward, wipe history. Its their funeral. 14599f11ffb7Safresh1 elsif ($num < @$test_results) { 14609f11ffb7Safresh1 $#{$test_results} = $num - 1; 14619f11ffb7Safresh1 } 14629f11ffb7Safresh1 } 14639f11ffb7Safresh1 } 14649f11ffb7Safresh1 return release $ctx, $hub->count; 14659f11ffb7Safresh1} 14669f11ffb7Safresh1 14679f11ffb7Safresh1 14689f11ffb7Safresh1sub is_passing { 1469b39c5158Smillert my $self = shift; 1470b39c5158Smillert 14719f11ffb7Safresh1 my $ctx = $self->ctx; 14729f11ffb7Safresh1 my $hub = $ctx->hub; 1473b39c5158Smillert 14749f11ffb7Safresh1 if( @_ ) { 14759f11ffb7Safresh1 my ($bool) = @_; 14769f11ffb7Safresh1 $hub->set_failed(0) if $bool; 14779f11ffb7Safresh1 $hub->is_passing($bool); 14789f11ffb7Safresh1 } 1479b39c5158Smillert 14809f11ffb7Safresh1 return release $ctx, $hub->is_passing; 14819f11ffb7Safresh1} 1482b39c5158Smillert 14839f11ffb7Safresh1 14849f11ffb7Safresh1sub summary { 14859f11ffb7Safresh1 my($self) = shift; 14869f11ffb7Safresh1 14879f11ffb7Safresh1 return if $self->{no_log_results}; 14889f11ffb7Safresh1 14899f11ffb7Safresh1 my $ctx = $self->ctx; 14909f11ffb7Safresh1 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 14919f11ffb7Safresh1 $ctx->release; 14929f11ffb7Safresh1 return map { $_ ? $_->{'ok'} : () } @$data; 14939f11ffb7Safresh1} 14949f11ffb7Safresh1 14959f11ffb7Safresh1 14969f11ffb7Safresh1sub details { 14979f11ffb7Safresh1 my $self = shift; 14989f11ffb7Safresh1 14999f11ffb7Safresh1 return if $self->{no_log_results}; 15009f11ffb7Safresh1 15019f11ffb7Safresh1 my $ctx = $self->ctx; 15029f11ffb7Safresh1 my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}; 15039f11ffb7Safresh1 $ctx->release; 15049f11ffb7Safresh1 return @$data; 15059f11ffb7Safresh1} 15069f11ffb7Safresh1 15079f11ffb7Safresh1 15089f11ffb7Safresh1sub find_TODO { 15099f11ffb7Safresh1 my( $self, $pack, $set, $new_value ) = @_; 15109f11ffb7Safresh1 15119f11ffb7Safresh1 my $ctx = $self->ctx; 15129f11ffb7Safresh1 15139f11ffb7Safresh1 $pack ||= $ctx->trace->package || $self->exported_to; 15149f11ffb7Safresh1 $ctx->release; 15159f11ffb7Safresh1 15169f11ffb7Safresh1 return unless $pack; 15179f11ffb7Safresh1 15189f11ffb7Safresh1 no strict 'refs'; ## no critic 15199f11ffb7Safresh1 no warnings 'once'; 15209f11ffb7Safresh1 my $old_value = ${ $pack . '::TODO' }; 15219f11ffb7Safresh1 $set and ${ $pack . '::TODO' } = $new_value; 15229f11ffb7Safresh1 return $old_value; 15239f11ffb7Safresh1} 15249f11ffb7Safresh1 15259f11ffb7Safresh1sub todo { 15269f11ffb7Safresh1 my( $self, $pack ) = @_; 15279f11ffb7Safresh1 15289f11ffb7Safresh1 local $Level = $Level + 1; 15299f11ffb7Safresh1 my $ctx = $self->ctx; 15309f11ffb7Safresh1 $ctx->release; 15319f11ffb7Safresh1 15329f11ffb7Safresh1 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}; 15339f11ffb7Safresh1 return $meta->[-1]->[1] if $meta && @$meta; 15349f11ffb7Safresh1 15359f11ffb7Safresh1 $pack ||= $ctx->trace->package; 15369f11ffb7Safresh1 15379f11ffb7Safresh1 return unless $pack; 15389f11ffb7Safresh1 15399f11ffb7Safresh1 no strict 'refs'; ## no critic 15409f11ffb7Safresh1 no warnings 'once'; 15419f11ffb7Safresh1 return ${ $pack . '::TODO' }; 15429f11ffb7Safresh1} 15439f11ffb7Safresh1 15449f11ffb7Safresh1sub in_todo { 15459f11ffb7Safresh1 my $self = shift; 15469f11ffb7Safresh1 15479f11ffb7Safresh1 local $Level = $Level + 1; 15489f11ffb7Safresh1 my $ctx = $self->ctx; 15499f11ffb7Safresh1 $ctx->release; 15509f11ffb7Safresh1 15519f11ffb7Safresh1 my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}; 15529f11ffb7Safresh1 return 1 if $meta && @$meta; 15539f11ffb7Safresh1 15549f11ffb7Safresh1 my $pack = $ctx->trace->package || return 0; 15559f11ffb7Safresh1 15569f11ffb7Safresh1 no strict 'refs'; ## no critic 15579f11ffb7Safresh1 no warnings 'once'; 15589f11ffb7Safresh1 my $todo = ${ $pack . '::TODO' }; 15599f11ffb7Safresh1 15609f11ffb7Safresh1 return 0 unless defined $todo; 15619f11ffb7Safresh1 return 0 if "$todo" eq ''; 15629f11ffb7Safresh1 return 1; 15639f11ffb7Safresh1} 15649f11ffb7Safresh1 15659f11ffb7Safresh1sub todo_start { 15669f11ffb7Safresh1 my $self = shift; 15679f11ffb7Safresh1 my $message = @_ ? shift : ''; 15689f11ffb7Safresh1 15699f11ffb7Safresh1 my $ctx = $self->ctx; 15709f11ffb7Safresh1 15719f11ffb7Safresh1 my $hub = $ctx->hub; 15729f11ffb7Safresh1 my $filter = $hub->pre_filter(sub { 15739f11ffb7Safresh1 my ($active_hub, $e) = @_; 15749f11ffb7Safresh1 15759f11ffb7Safresh1 # Turn a diag into a todo diag 15769f11ffb7Safresh1 return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag'; 15779f11ffb7Safresh1 15789f11ffb7Safresh1 # Set todo on ok's 15799f11ffb7Safresh1 if ($hub == $active_hub && $e->isa('Test2::Event::Ok')) { 15809f11ffb7Safresh1 $e->set_todo($message); 15819f11ffb7Safresh1 $e->set_effective_pass(1); 15829f11ffb7Safresh1 15839f11ffb7Safresh1 if (my $result = $e->get_meta(__PACKAGE__)) { 15849f11ffb7Safresh1 $result->{reason} ||= $message; 15859f11ffb7Safresh1 $result->{type} ||= 'todo'; 15869f11ffb7Safresh1 $result->{ok} = 1; 15879f11ffb7Safresh1 } 15889f11ffb7Safresh1 } 15899f11ffb7Safresh1 15909f11ffb7Safresh1 return $e; 15919f11ffb7Safresh1 }, inherit => 1); 15929f11ffb7Safresh1 15939f11ffb7Safresh1 push @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}} => [$filter, $message]; 15949f11ffb7Safresh1 15959f11ffb7Safresh1 $ctx->release; 1596b39c5158Smillert 1597b39c5158Smillert return; 1598b39c5158Smillert} 1599b39c5158Smillert 16009f11ffb7Safresh1sub todo_end { 16019f11ffb7Safresh1 my $self = shift; 1602b39c5158Smillert 16039f11ffb7Safresh1 my $ctx = $self->ctx; 1604b39c5158Smillert 16059f11ffb7Safresh1 my $set = pop @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}}; 16069f11ffb7Safresh1 16079f11ffb7Safresh1 $ctx->throw('todo_end() called without todo_start()') unless $set; 16089f11ffb7Safresh1 16099f11ffb7Safresh1 $ctx->hub->pre_unfilter($set->[0]); 16109f11ffb7Safresh1 16119f11ffb7Safresh1 $ctx->release; 1612b39c5158Smillert 1613b39c5158Smillert return; 1614b39c5158Smillert} 1615b39c5158Smillert 16169f11ffb7Safresh1 16179f11ffb7Safresh1sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms) 16189f11ffb7Safresh1 my( $self ) = @_; 16199f11ffb7Safresh1 16209f11ffb7Safresh1 my $ctx = $self->ctx; 16219f11ffb7Safresh1 16229f11ffb7Safresh1 my $trace = $ctx->trace; 16239f11ffb7Safresh1 $ctx->release; 16249f11ffb7Safresh1 return wantarray ? $trace->call : $trace->package; 16256eddea87Sjasper} 16266eddea87Sjasper 16276eddea87Sjasper 16289f11ffb7Safresh1sub _try { 16299f11ffb7Safresh1 my( $self, $code, %opts ) = @_; 16309f11ffb7Safresh1 16319f11ffb7Safresh1 my $error; 16329f11ffb7Safresh1 my $return; 16339f11ffb7Safresh1 { 16349f11ffb7Safresh1 local $!; # eval can mess up $! 16359f11ffb7Safresh1 local $@; # don't set $@ in the test 16369f11ffb7Safresh1 local $SIG{__DIE__}; # don't trip an outside DIE handler. 16379f11ffb7Safresh1 $return = eval { $code->() }; 16389f11ffb7Safresh1 $error = $@; 16399f11ffb7Safresh1 } 16409f11ffb7Safresh1 16419f11ffb7Safresh1 die $error if $error and $opts{die_on_fail}; 16429f11ffb7Safresh1 16439f11ffb7Safresh1 return wantarray ? ( $return, $error ) : $return; 16449f11ffb7Safresh1} 16459f11ffb7Safresh1 16469f11ffb7Safresh1sub _ending { 16479f11ffb7Safresh1 my $self = shift; 16489f11ffb7Safresh1 my ($ctx, $real_exit_code, $new) = @_; 16499f11ffb7Safresh1 16509f11ffb7Safresh1 unless ($ctx) { 16519f11ffb7Safresh1 my $octx = $self->ctx; 16529f11ffb7Safresh1 $ctx = $octx->snapshot; 16539f11ffb7Safresh1 $octx->release; 16549f11ffb7Safresh1 } 16559f11ffb7Safresh1 16569f11ffb7Safresh1 return if $ctx->hub->no_ending; 16579f11ffb7Safresh1 return if $ctx->hub->meta(__PACKAGE__, {})->{Ending}++; 16589f11ffb7Safresh1 16599f11ffb7Safresh1 # Don't bother with an ending if this is a forked copy. Only the parent 16609f11ffb7Safresh1 # should do the ending. 16619f11ffb7Safresh1 return unless $self->{Original_Pid} == $$; 16629f11ffb7Safresh1 16639f11ffb7Safresh1 my $hub = $ctx->hub; 16649f11ffb7Safresh1 return if $hub->bailed_out; 16659f11ffb7Safresh1 16669f11ffb7Safresh1 my $plan = $hub->plan; 16679f11ffb7Safresh1 my $count = $hub->count; 16689f11ffb7Safresh1 my $failed = $hub->failed; 16699f11ffb7Safresh1 my $passed = $hub->is_passing; 16709f11ffb7Safresh1 return unless $plan || $count || $failed; 16719f11ffb7Safresh1 16729f11ffb7Safresh1 # Ran tests but never declared a plan or hit done_testing 1673*3d61058aSafresh1 if( !defined($hub->plan) and $hub->count ) { 16749f11ffb7Safresh1 $self->diag("Tests were run but no plan was declared and done_testing() was not seen."); 16759f11ffb7Safresh1 16769f11ffb7Safresh1 if($real_exit_code) { 16779f11ffb7Safresh1 $self->diag(<<"FAIL"); 16789f11ffb7Safresh1Looks like your test exited with $real_exit_code just after $count. 16799f11ffb7Safresh1FAIL 16809f11ffb7Safresh1 $$new ||= $real_exit_code; 16819f11ffb7Safresh1 return; 16829f11ffb7Safresh1 } 16839f11ffb7Safresh1 16849f11ffb7Safresh1 # But if the tests ran, handle exit code. 16859f11ffb7Safresh1 if($failed > 0) { 16869f11ffb7Safresh1 my $exit_code = $failed <= 254 ? $failed : 254; 16879f11ffb7Safresh1 $$new ||= $exit_code; 16889f11ffb7Safresh1 return; 16899f11ffb7Safresh1 } 16909f11ffb7Safresh1 16919f11ffb7Safresh1 $$new ||= 254; 16929f11ffb7Safresh1 return; 16939f11ffb7Safresh1 } 16949f11ffb7Safresh1 16959f11ffb7Safresh1 if ($real_exit_code && !$count) { 16969f11ffb7Safresh1 $self->diag("Looks like your test exited with $real_exit_code before it could output anything."); 16979f11ffb7Safresh1 $$new ||= $real_exit_code; 16989f11ffb7Safresh1 return; 16999f11ffb7Safresh1 } 17009f11ffb7Safresh1 17019f11ffb7Safresh1 return if $plan && "$plan" eq 'SKIP'; 17029f11ffb7Safresh1 17039f11ffb7Safresh1 if (!$count) { 17049f11ffb7Safresh1 $self->diag('No tests run!'); 17059f11ffb7Safresh1 $$new ||= 255; 17069f11ffb7Safresh1 return; 17079f11ffb7Safresh1 } 17089f11ffb7Safresh1 17099f11ffb7Safresh1 if ($real_exit_code) { 17109f11ffb7Safresh1 $self->diag(<<"FAIL"); 17119f11ffb7Safresh1Looks like your test exited with $real_exit_code just after $count. 17129f11ffb7Safresh1FAIL 17139f11ffb7Safresh1 $$new ||= $real_exit_code; 17149f11ffb7Safresh1 return; 17159f11ffb7Safresh1 } 17169f11ffb7Safresh1 17179f11ffb7Safresh1 if ($plan eq 'NO PLAN') { 17189f11ffb7Safresh1 $ctx->plan( $count ); 17199f11ffb7Safresh1 $plan = $hub->plan; 17209f11ffb7Safresh1 } 17219f11ffb7Safresh1 17229f11ffb7Safresh1 # Figure out if we passed or failed and print helpful messages. 17239f11ffb7Safresh1 my $num_extra = $count - $plan; 17249f11ffb7Safresh1 17259f11ffb7Safresh1 if ($num_extra != 0) { 17269f11ffb7Safresh1 my $s = $plan == 1 ? '' : 's'; 17279f11ffb7Safresh1 $self->diag(<<"FAIL"); 17289f11ffb7Safresh1Looks like you planned $plan test$s but ran $count. 17299f11ffb7Safresh1FAIL 17309f11ffb7Safresh1 } 17319f11ffb7Safresh1 17329f11ffb7Safresh1 if ($failed) { 17339f11ffb7Safresh1 my $s = $failed == 1 ? '' : 's'; 17349f11ffb7Safresh1 17359f11ffb7Safresh1 my $qualifier = $num_extra == 0 ? '' : ' run'; 17369f11ffb7Safresh1 17379f11ffb7Safresh1 $self->diag(<<"FAIL"); 17389f11ffb7Safresh1Looks like you failed $failed test$s of $count$qualifier. 17399f11ffb7Safresh1FAIL 17409f11ffb7Safresh1 } 17419f11ffb7Safresh1 17429f11ffb7Safresh1 if (!$passed && !$failed && $count && !$num_extra) { 17439f11ffb7Safresh1 $ctx->diag(<<"FAIL"); 17449f11ffb7Safresh1All assertions passed, but errors were encountered. 17459f11ffb7Safresh1FAIL 17469f11ffb7Safresh1 } 17479f11ffb7Safresh1 17489f11ffb7Safresh1 my $exit_code = 0; 17499f11ffb7Safresh1 if ($failed) { 17509f11ffb7Safresh1 $exit_code = $failed <= 254 ? $failed : 254; 17519f11ffb7Safresh1 } 17529f11ffb7Safresh1 elsif ($num_extra != 0) { 17539f11ffb7Safresh1 $exit_code = 255; 17549f11ffb7Safresh1 } 17559f11ffb7Safresh1 elsif (!$passed) { 17569f11ffb7Safresh1 $exit_code = 255; 17579f11ffb7Safresh1 } 17589f11ffb7Safresh1 17599f11ffb7Safresh1 $$new ||= $exit_code; 17609f11ffb7Safresh1 return; 17619f11ffb7Safresh1} 17629f11ffb7Safresh1 17639f11ffb7Safresh1# Some things used this even though it was private... I am looking at you 17649f11ffb7Safresh1# Test::Builder::Prefix... 17659f11ffb7Safresh1sub _print_comment { 17669f11ffb7Safresh1 my( $self, $fh, @msgs ) = @_; 17679f11ffb7Safresh1 17689f11ffb7Safresh1 return if $self->no_diag; 17699f11ffb7Safresh1 return unless @msgs; 17709f11ffb7Safresh1 17719f11ffb7Safresh1 # Prevent printing headers when compiling (i.e. -c) 17729f11ffb7Safresh1 return if $^C; 17739f11ffb7Safresh1 17749f11ffb7Safresh1 # Smash args together like print does. 17759f11ffb7Safresh1 # Convert undef to 'undef' so its readable. 17769f11ffb7Safresh1 my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs; 17779f11ffb7Safresh1 17789f11ffb7Safresh1 # Escape the beginning, _print will take care of the rest. 17799f11ffb7Safresh1 $msg =~ s/^/# /; 17809f11ffb7Safresh1 17819f11ffb7Safresh1 local( $\, $", $, ) = ( undef, ' ', '' ); 17829f11ffb7Safresh1 print $fh $msg; 17839f11ffb7Safresh1 17849f11ffb7Safresh1 return 0; 17859f11ffb7Safresh1} 17869f11ffb7Safresh1 17879f11ffb7Safresh1# This is used by Test::SharedFork to turn on IPC after the fact. Not 17889f11ffb7Safresh1# documenting because I do not want it used. The method name is borrowed from 17899f11ffb7Safresh1# Test::Builder 2 17909f11ffb7Safresh1# Once Test2 stuff goes stable this method will be removed and Test::SharedFork 17919f11ffb7Safresh1# will be made smarter. 17929f11ffb7Safresh1sub coordinate_forks { 17939f11ffb7Safresh1 my $self = shift; 17949f11ffb7Safresh1 17959f11ffb7Safresh1 { 17969f11ffb7Safresh1 local ($@, $!); 17979f11ffb7Safresh1 require Test2::IPC; 17989f11ffb7Safresh1 } 17999f11ffb7Safresh1 Test2::IPC->import; 18009f11ffb7Safresh1 Test2::API::test2_ipc_enable_polling(); 18019f11ffb7Safresh1 Test2::API::test2_load(); 18029f11ffb7Safresh1 my $ipc = Test2::IPC::apply_ipc($self->{Stack}); 18039f11ffb7Safresh1 $ipc->set_no_fatal(1); 18049f11ffb7Safresh1 Test2::API::test2_no_wait(1); 18059f11ffb7Safresh1} 18069f11ffb7Safresh1 18079f11ffb7Safresh1sub no_log_results { $_[0]->{no_log_results} = 1 } 18089f11ffb7Safresh1 18099f11ffb7Safresh11; 18109f11ffb7Safresh1 18119f11ffb7Safresh1__END__ 18129f11ffb7Safresh1 18139f11ffb7Safresh1=head1 NAME 18149f11ffb7Safresh1 18159f11ffb7Safresh1Test::Builder - Backend for building test libraries 18169f11ffb7Safresh1 18179f11ffb7Safresh1=head1 SYNOPSIS 18189f11ffb7Safresh1 18199f11ffb7Safresh1 package My::Test::Module; 18209f11ffb7Safresh1 use base 'Test::Builder::Module'; 18219f11ffb7Safresh1 18229f11ffb7Safresh1 my $CLASS = __PACKAGE__; 18239f11ffb7Safresh1 18249f11ffb7Safresh1 sub ok { 18259f11ffb7Safresh1 my($test, $name) = @_; 18269f11ffb7Safresh1 my $tb = $CLASS->builder; 18279f11ffb7Safresh1 18289f11ffb7Safresh1 $tb->ok($test, $name); 18299f11ffb7Safresh1 } 18309f11ffb7Safresh1 18319f11ffb7Safresh1 18329f11ffb7Safresh1=head1 DESCRIPTION 18339f11ffb7Safresh1 18349f11ffb7Safresh1L<Test::Simple> and L<Test::More> have proven to be popular testing modules, 18359f11ffb7Safresh1but they're not always flexible enough. Test::Builder provides a 18369f11ffb7Safresh1building block upon which to write your own test libraries I<which can 18379f11ffb7Safresh1work together>. 18389f11ffb7Safresh1 18399f11ffb7Safresh1=head2 Construction 18409f11ffb7Safresh1 18419f11ffb7Safresh1=over 4 18429f11ffb7Safresh1 18439f11ffb7Safresh1=item B<new> 18449f11ffb7Safresh1 18459f11ffb7Safresh1 my $Test = Test::Builder->new; 18469f11ffb7Safresh1 18479f11ffb7Safresh1Returns a Test::Builder object representing the current state of the 18489f11ffb7Safresh1test. 18499f11ffb7Safresh1 18509f11ffb7Safresh1Since you only run one test per program C<new> always returns the same 18519f11ffb7Safresh1Test::Builder object. No matter how many times you call C<new()>, you're 18529f11ffb7Safresh1getting the same object. This is called a singleton. This is done so that 18539f11ffb7Safresh1multiple modules share such global information as the test counter and 18549f11ffb7Safresh1where test output is going. 18559f11ffb7Safresh1 18569f11ffb7Safresh1If you want a completely new Test::Builder object different from the 18579f11ffb7Safresh1singleton, use C<create>. 18589f11ffb7Safresh1 18599f11ffb7Safresh1=item B<create> 18609f11ffb7Safresh1 18619f11ffb7Safresh1 my $Test = Test::Builder->create; 18629f11ffb7Safresh1 18639f11ffb7Safresh1Ok, so there can be more than one Test::Builder object and this is how 18649f11ffb7Safresh1you get it. You might use this instead of C<new()> if you're testing 18659f11ffb7Safresh1a Test::Builder based module, but otherwise you probably want C<new>. 18669f11ffb7Safresh1 18679f11ffb7Safresh1B<NOTE>: the implementation is not complete. C<level>, for example, is still 18689f11ffb7Safresh1shared by B<all> Test::Builder objects, even ones created using this method. 18699f11ffb7Safresh1Also, the method name may change in the future. 18709f11ffb7Safresh1 18719f11ffb7Safresh1=item B<subtest> 18729f11ffb7Safresh1 18739f11ffb7Safresh1 $builder->subtest($name, \&subtests, @args); 18749f11ffb7Safresh1 18759f11ffb7Safresh1See documentation of C<subtest> in Test::More. 18769f11ffb7Safresh1 18779f11ffb7Safresh1C<subtest> also, and optionally, accepts arguments which will be passed to the 18789f11ffb7Safresh1subtests reference. 18799f11ffb7Safresh1 18809f11ffb7Safresh1=item B<name> 18819f11ffb7Safresh1 18829f11ffb7Safresh1 diag $builder->name; 18839f11ffb7Safresh1 18849f11ffb7Safresh1Returns the name of the current builder. Top level builders default to C<$0> 18859f11ffb7Safresh1(the name of the executable). Child builders are named via the C<child> 18869f11ffb7Safresh1method. If no name is supplied, will be named "Child of $parent->name". 18879f11ffb7Safresh1 18889f11ffb7Safresh1=item B<reset> 18899f11ffb7Safresh1 18909f11ffb7Safresh1 $Test->reset; 18919f11ffb7Safresh1 18929f11ffb7Safresh1Reinitializes the Test::Builder singleton to its original state. 18939f11ffb7Safresh1Mostly useful for tests run in persistent environments where the same 18949f11ffb7Safresh1test might be run multiple times in the same process. 18959f11ffb7Safresh1 18969f11ffb7Safresh1=back 18979f11ffb7Safresh1 18989f11ffb7Safresh1=head2 Setting up tests 18999f11ffb7Safresh1 19009f11ffb7Safresh1These methods are for setting up tests and declaring how many there 19019f11ffb7Safresh1are. You usually only want to call one of these methods. 19029f11ffb7Safresh1 19039f11ffb7Safresh1=over 4 19049f11ffb7Safresh1 19059f11ffb7Safresh1=item B<plan> 19069f11ffb7Safresh1 19079f11ffb7Safresh1 $Test->plan('no_plan'); 19089f11ffb7Safresh1 $Test->plan( skip_all => $reason ); 19099f11ffb7Safresh1 $Test->plan( tests => $num_tests ); 19109f11ffb7Safresh1 19119f11ffb7Safresh1A convenient way to set up your tests. Call this and Test::Builder 19129f11ffb7Safresh1will print the appropriate headers and take the appropriate actions. 19139f11ffb7Safresh1 19149f11ffb7Safresh1If you call C<plan()>, don't call any of the other methods below. 19159f11ffb7Safresh1 19169f11ffb7Safresh1=item B<expected_tests> 19179f11ffb7Safresh1 19189f11ffb7Safresh1 my $max = $Test->expected_tests; 19199f11ffb7Safresh1 $Test->expected_tests($max); 19209f11ffb7Safresh1 19219f11ffb7Safresh1Gets/sets the number of tests we expect this test to run and prints out 19229f11ffb7Safresh1the appropriate headers. 19239f11ffb7Safresh1 19249f11ffb7Safresh1 19259f11ffb7Safresh1=item B<no_plan> 19269f11ffb7Safresh1 19279f11ffb7Safresh1 $Test->no_plan; 19289f11ffb7Safresh1 19299f11ffb7Safresh1Declares that this test will run an indeterminate number of tests. 19309f11ffb7Safresh1 19319f11ffb7Safresh1 19329f11ffb7Safresh1=item B<done_testing> 19339f11ffb7Safresh1 19349f11ffb7Safresh1 $Test->done_testing(); 19359f11ffb7Safresh1 $Test->done_testing($num_tests); 19369f11ffb7Safresh1 19379f11ffb7Safresh1Declares that you are done testing, no more tests will be run after this point. 19389f11ffb7Safresh1 19399f11ffb7Safresh1If a plan has not yet been output, it will do so. 19409f11ffb7Safresh1 19419f11ffb7Safresh1$num_tests is the number of tests you planned to run. If a numbered 19429f11ffb7Safresh1plan was already declared, and if this contradicts, a failing test 19439f11ffb7Safresh1will be run to reflect the planning mistake. If C<no_plan> was declared, 19449f11ffb7Safresh1this will override. 19459f11ffb7Safresh1 19469f11ffb7Safresh1If C<done_testing()> is called twice, the second call will issue a 19479f11ffb7Safresh1failing test. 19489f11ffb7Safresh1 19499f11ffb7Safresh1If C<$num_tests> is omitted, the number of tests run will be used, like 19509f11ffb7Safresh1no_plan. 19519f11ffb7Safresh1 19529f11ffb7Safresh1C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but 19539f11ffb7Safresh1safer. You'd use it like so: 19549f11ffb7Safresh1 19559f11ffb7Safresh1 $Test->ok($a == $b); 19569f11ffb7Safresh1 $Test->done_testing(); 19579f11ffb7Safresh1 19589f11ffb7Safresh1Or to plan a variable number of tests: 19599f11ffb7Safresh1 19609f11ffb7Safresh1 for my $test (@tests) { 19619f11ffb7Safresh1 $Test->ok($test); 19629f11ffb7Safresh1 } 19639f11ffb7Safresh1 $Test->done_testing(scalar @tests); 19649f11ffb7Safresh1 19659f11ffb7Safresh1 19669f11ffb7Safresh1=item B<has_plan> 19679f11ffb7Safresh1 19689f11ffb7Safresh1 $plan = $Test->has_plan 19699f11ffb7Safresh1 19709f11ffb7Safresh1Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan 19719f11ffb7Safresh1has been set), C<no_plan> (indeterminate # of tests) or an integer (the number 19729f11ffb7Safresh1of expected tests). 19739f11ffb7Safresh1 19749f11ffb7Safresh1=item B<skip_all> 19759f11ffb7Safresh1 19769f11ffb7Safresh1 $Test->skip_all; 19779f11ffb7Safresh1 $Test->skip_all($reason); 19789f11ffb7Safresh1 19799f11ffb7Safresh1Skips all the tests, using the given C<$reason>. Exits immediately with 0. 19809f11ffb7Safresh1 19819f11ffb7Safresh1=item B<exported_to> 19829f11ffb7Safresh1 19839f11ffb7Safresh1 my $pack = $Test->exported_to; 19849f11ffb7Safresh1 $Test->exported_to($pack); 19859f11ffb7Safresh1 19869f11ffb7Safresh1Tells Test::Builder what package you exported your functions to. 19879f11ffb7Safresh1 19889f11ffb7Safresh1This method isn't terribly useful since modules which share the same 19899f11ffb7Safresh1Test::Builder object might get exported to different packages and only 19909f11ffb7Safresh1the last one will be honored. 19919f11ffb7Safresh1 19929f11ffb7Safresh1=back 19939f11ffb7Safresh1 19949f11ffb7Safresh1=head2 Running tests 19959f11ffb7Safresh1 19969f11ffb7Safresh1These actually run the tests, analogous to the functions in Test::More. 19979f11ffb7Safresh1 19989f11ffb7Safresh1They all return true if the test passed, false if the test failed. 19999f11ffb7Safresh1 20009f11ffb7Safresh1C<$name> is always optional. 20019f11ffb7Safresh1 20029f11ffb7Safresh1=over 4 20039f11ffb7Safresh1 20049f11ffb7Safresh1=item B<ok> 20059f11ffb7Safresh1 20069f11ffb7Safresh1 $Test->ok($test, $name); 20079f11ffb7Safresh1 20089f11ffb7Safresh1Your basic test. Pass if C<$test> is true, fail if $test is false. Just 20099f11ffb7Safresh1like Test::Simple's C<ok()>. 20109f11ffb7Safresh1 20119f11ffb7Safresh1=item B<is_eq> 20129f11ffb7Safresh1 20139f11ffb7Safresh1 $Test->is_eq($got, $expected, $name); 20149f11ffb7Safresh1 20159f11ffb7Safresh1Like Test::More's C<is()>. Checks if C<$got eq $expected>. This is the 20169f11ffb7Safresh1string version. 20179f11ffb7Safresh1 20189f11ffb7Safresh1C<undef> only ever matches another C<undef>. 20199f11ffb7Safresh1 20209f11ffb7Safresh1=item B<is_num> 20219f11ffb7Safresh1 20229f11ffb7Safresh1 $Test->is_num($got, $expected, $name); 20239f11ffb7Safresh1 20249f11ffb7Safresh1Like Test::More's C<is()>. Checks if C<$got == $expected>. This is the 20259f11ffb7Safresh1numeric version. 20269f11ffb7Safresh1 20279f11ffb7Safresh1C<undef> only ever matches another C<undef>. 20289f11ffb7Safresh1 20299f11ffb7Safresh1=item B<isnt_eq> 20309f11ffb7Safresh1 20319f11ffb7Safresh1 $Test->isnt_eq($got, $dont_expect, $name); 20329f11ffb7Safresh1 20339f11ffb7Safresh1Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is 20349f11ffb7Safresh1the string version. 20359f11ffb7Safresh1 20369f11ffb7Safresh1=item B<isnt_num> 20379f11ffb7Safresh1 20389f11ffb7Safresh1 $Test->isnt_num($got, $dont_expect, $name); 20399f11ffb7Safresh1 20409f11ffb7Safresh1Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is 20419f11ffb7Safresh1the numeric version. 20429f11ffb7Safresh1 20439f11ffb7Safresh1=item B<like> 20449f11ffb7Safresh1 20459f11ffb7Safresh1 $Test->like($thing, qr/$regex/, $name); 20469f11ffb7Safresh1 $Test->like($thing, '/$regex/', $name); 20479f11ffb7Safresh1 20489f11ffb7Safresh1Like L<Test::More>'s C<like()>. Checks if $thing matches the given C<$regex>. 20499f11ffb7Safresh1 20509f11ffb7Safresh1=item B<unlike> 20519f11ffb7Safresh1 20529f11ffb7Safresh1 $Test->unlike($thing, qr/$regex/, $name); 20539f11ffb7Safresh1 $Test->unlike($thing, '/$regex/', $name); 20549f11ffb7Safresh1 20559f11ffb7Safresh1Like L<Test::More>'s C<unlike()>. Checks if $thing B<does not match> the 20569f11ffb7Safresh1given C<$regex>. 20579f11ffb7Safresh1 20589f11ffb7Safresh1=item B<cmp_ok> 20599f11ffb7Safresh1 20609f11ffb7Safresh1 $Test->cmp_ok($thing, $type, $that, $name); 20619f11ffb7Safresh1 20629f11ffb7Safresh1Works just like L<Test::More>'s C<cmp_ok()>. 20639f11ffb7Safresh1 20649f11ffb7Safresh1 $Test->cmp_ok($big_num, '!=', $other_big_num); 20659f11ffb7Safresh1 20669f11ffb7Safresh1=back 20679f11ffb7Safresh1 20689f11ffb7Safresh1=head2 Other Testing Methods 20699f11ffb7Safresh1 20709f11ffb7Safresh1These are methods which are used in the course of writing a test but are not themselves tests. 20719f11ffb7Safresh1 20729f11ffb7Safresh1=over 4 20739f11ffb7Safresh1 20749f11ffb7Safresh1=item B<BAIL_OUT> 20759f11ffb7Safresh1 20769f11ffb7Safresh1 $Test->BAIL_OUT($reason); 20779f11ffb7Safresh1 20789f11ffb7Safresh1Indicates to the L<Test::Harness> that things are going so badly all 20799f11ffb7Safresh1testing should terminate. This includes running any additional test 20809f11ffb7Safresh1scripts. 20819f11ffb7Safresh1 20829f11ffb7Safresh1It will exit with 255. 20839f11ffb7Safresh1 20849f11ffb7Safresh1=for deprecated 20859f11ffb7Safresh1BAIL_OUT() used to be BAILOUT() 20869f11ffb7Safresh1 20879f11ffb7Safresh1=item B<skip> 20889f11ffb7Safresh1 20899f11ffb7Safresh1 $Test->skip; 20909f11ffb7Safresh1 $Test->skip($why); 20919f11ffb7Safresh1 20929f11ffb7Safresh1Skips the current test, reporting C<$why>. 20939f11ffb7Safresh1 20949f11ffb7Safresh1=item B<todo_skip> 20959f11ffb7Safresh1 20969f11ffb7Safresh1 $Test->todo_skip; 20979f11ffb7Safresh1 $Test->todo_skip($why); 20989f11ffb7Safresh1 20999f11ffb7Safresh1Like C<skip()>, only it will declare the test as failing and TODO. Similar 21009f11ffb7Safresh1to 21019f11ffb7Safresh1 21029f11ffb7Safresh1 print "not ok $tnum # TODO $why\n"; 21039f11ffb7Safresh1 21049f11ffb7Safresh1=begin _unimplemented 21059f11ffb7Safresh1 21069f11ffb7Safresh1=item B<skip_rest> 21079f11ffb7Safresh1 21089f11ffb7Safresh1 $Test->skip_rest; 21099f11ffb7Safresh1 $Test->skip_rest($reason); 21109f11ffb7Safresh1 21119f11ffb7Safresh1Like C<skip()>, only it skips all the rest of the tests you plan to run 21129f11ffb7Safresh1and terminates the test. 21139f11ffb7Safresh1 21149f11ffb7Safresh1If you're running under C<no_plan>, it skips once and terminates the 21159f11ffb7Safresh1test. 21169f11ffb7Safresh1 21179f11ffb7Safresh1=end _unimplemented 21189f11ffb7Safresh1 21199f11ffb7Safresh1=back 21209f11ffb7Safresh1 21219f11ffb7Safresh1 21229f11ffb7Safresh1=head2 Test building utility methods 21239f11ffb7Safresh1 21249f11ffb7Safresh1These methods are useful when writing your own test methods. 21259f11ffb7Safresh1 21269f11ffb7Safresh1=over 4 21279f11ffb7Safresh1 21289f11ffb7Safresh1=item B<maybe_regex> 21299f11ffb7Safresh1 21309f11ffb7Safresh1 $Test->maybe_regex(qr/$regex/); 21319f11ffb7Safresh1 $Test->maybe_regex('/$regex/'); 21329f11ffb7Safresh1 21339f11ffb7Safresh1This method used to be useful back when Test::Builder worked on Perls 21349f11ffb7Safresh1before 5.6 which didn't have qr//. Now its pretty useless. 21359f11ffb7Safresh1 21369f11ffb7Safresh1Convenience method for building testing functions that take regular 21379f11ffb7Safresh1expressions as arguments. 21389f11ffb7Safresh1 21399f11ffb7Safresh1Takes a quoted regular expression produced by C<qr//>, or a string 21409f11ffb7Safresh1representing a regular expression. 21419f11ffb7Safresh1 21429f11ffb7Safresh1Returns a Perl value which may be used instead of the corresponding 21439f11ffb7Safresh1regular expression, or C<undef> if its argument is not recognized. 21449f11ffb7Safresh1 21459f11ffb7Safresh1For example, a version of C<like()>, sans the useful diagnostic messages, 21469f11ffb7Safresh1could be written as: 21479f11ffb7Safresh1 21489f11ffb7Safresh1 sub laconic_like { 21499f11ffb7Safresh1 my ($self, $thing, $regex, $name) = @_; 21509f11ffb7Safresh1 my $usable_regex = $self->maybe_regex($regex); 21519f11ffb7Safresh1 die "expecting regex, found '$regex'\n" 21529f11ffb7Safresh1 unless $usable_regex; 21539f11ffb7Safresh1 $self->ok($thing =~ m/$usable_regex/, $name); 21549f11ffb7Safresh1 } 21559f11ffb7Safresh1 21569f11ffb7Safresh1 21579f11ffb7Safresh1=item B<is_fh> 21589f11ffb7Safresh1 21599f11ffb7Safresh1 my $is_fh = $Test->is_fh($thing); 21609f11ffb7Safresh1 21619f11ffb7Safresh1Determines if the given C<$thing> can be used as a filehandle. 21629f11ffb7Safresh1 21639f11ffb7Safresh1=cut 21649f11ffb7Safresh1 21659f11ffb7Safresh1 21669f11ffb7Safresh1=back 21679f11ffb7Safresh1 21689f11ffb7Safresh1 21699f11ffb7Safresh1=head2 Test style 21709f11ffb7Safresh1 21719f11ffb7Safresh1 21729f11ffb7Safresh1=over 4 21739f11ffb7Safresh1 21749f11ffb7Safresh1=item B<level> 21759f11ffb7Safresh1 21769f11ffb7Safresh1 $Test->level($how_high); 21779f11ffb7Safresh1 21789f11ffb7Safresh1How far up the call stack should C<$Test> look when reporting where the 21799f11ffb7Safresh1test failed. 21809f11ffb7Safresh1 21819f11ffb7Safresh1Defaults to 1. 21829f11ffb7Safresh1 21839f11ffb7Safresh1Setting C<$Test::Builder::Level> overrides. This is typically useful 21849f11ffb7Safresh1localized: 21859f11ffb7Safresh1 21869f11ffb7Safresh1 sub my_ok { 21879f11ffb7Safresh1 my $test = shift; 21889f11ffb7Safresh1 21899f11ffb7Safresh1 local $Test::Builder::Level = $Test::Builder::Level + 1; 21909f11ffb7Safresh1 $TB->ok($test); 21919f11ffb7Safresh1 } 21929f11ffb7Safresh1 21939f11ffb7Safresh1To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant. 21949f11ffb7Safresh1 21959f11ffb7Safresh1=item B<use_numbers> 21969f11ffb7Safresh1 21979f11ffb7Safresh1 $Test->use_numbers($on_or_off); 21989f11ffb7Safresh1 21999f11ffb7Safresh1Whether or not the test should output numbers. That is, this if true: 22009f11ffb7Safresh1 22019f11ffb7Safresh1 ok 1 22029f11ffb7Safresh1 ok 2 22039f11ffb7Safresh1 ok 3 22049f11ffb7Safresh1 22059f11ffb7Safresh1or this if false 22069f11ffb7Safresh1 22079f11ffb7Safresh1 ok 22089f11ffb7Safresh1 ok 22099f11ffb7Safresh1 ok 22109f11ffb7Safresh1 22119f11ffb7Safresh1Most useful when you can't depend on the test output order, such as 22129f11ffb7Safresh1when threads or forking is involved. 22139f11ffb7Safresh1 22149f11ffb7Safresh1Defaults to on. 22159f11ffb7Safresh1 22169f11ffb7Safresh1=item B<no_diag> 22179f11ffb7Safresh1 22189f11ffb7Safresh1 $Test->no_diag($no_diag); 22199f11ffb7Safresh1 22209f11ffb7Safresh1If set true no diagnostics will be printed. This includes calls to 22219f11ffb7Safresh1C<diag()>. 22229f11ffb7Safresh1 22239f11ffb7Safresh1=item B<no_ending> 22249f11ffb7Safresh1 22259f11ffb7Safresh1 $Test->no_ending($no_ending); 22269f11ffb7Safresh1 22279f11ffb7Safresh1Normally, Test::Builder does some extra diagnostics when the test 22289f11ffb7Safresh1ends. It also changes the exit code as described below. 22299f11ffb7Safresh1 22309f11ffb7Safresh1If this is true, none of that will be done. 22319f11ffb7Safresh1 22329f11ffb7Safresh1=item B<no_header> 22339f11ffb7Safresh1 22349f11ffb7Safresh1 $Test->no_header($no_header); 22359f11ffb7Safresh1 22369f11ffb7Safresh1If set to true, no "1..N" header will be printed. 22379f11ffb7Safresh1 22389f11ffb7Safresh1=back 22399f11ffb7Safresh1 22409f11ffb7Safresh1=head2 Output 22419f11ffb7Safresh1 22429f11ffb7Safresh1Controlling where the test output goes. 22439f11ffb7Safresh1 22449f11ffb7Safresh1It's ok for your test to change where STDOUT and STDERR point to, 22459f11ffb7Safresh1Test::Builder's default output settings will not be affected. 22469f11ffb7Safresh1 22479f11ffb7Safresh1=over 4 22489f11ffb7Safresh1 22499f11ffb7Safresh1=item B<diag> 22509f11ffb7Safresh1 22519f11ffb7Safresh1 $Test->diag(@msgs); 22529f11ffb7Safresh1 22539f11ffb7Safresh1Prints out the given C<@msgs>. Like C<print>, arguments are simply 22549f11ffb7Safresh1appended together. 22559f11ffb7Safresh1 22569f11ffb7Safresh1Normally, it uses the C<failure_output()> handle, but if this is for a 22579f11ffb7Safresh1TODO test, the C<todo_output()> handle is used. 22589f11ffb7Safresh1 22599f11ffb7Safresh1Output will be indented and marked with a # so as not to interfere 22609f11ffb7Safresh1with test output. A newline will be put on the end if there isn't one 22619f11ffb7Safresh1already. 22629f11ffb7Safresh1 22639f11ffb7Safresh1We encourage using this rather than calling print directly. 22649f11ffb7Safresh1 22659f11ffb7Safresh1Returns false. Why? Because C<diag()> is often used in conjunction with 22669f11ffb7Safresh1a failing test (C<ok() || diag()>) it "passes through" the failure. 22679f11ffb7Safresh1 22689f11ffb7Safresh1 return ok(...) || diag(...); 22699f11ffb7Safresh1 22709f11ffb7Safresh1=for blame transfer 22719f11ffb7Safresh1Mark Fowler <mark@twoshortplanks.com> 22729f11ffb7Safresh1 22739f11ffb7Safresh1=item B<note> 22749f11ffb7Safresh1 22759f11ffb7Safresh1 $Test->note(@msgs); 22769f11ffb7Safresh1 22779f11ffb7Safresh1Like C<diag()>, but it prints to the C<output()> handle so it will not 22789f11ffb7Safresh1normally be seen by the user except in verbose mode. 22799f11ffb7Safresh1 22809f11ffb7Safresh1=item B<explain> 22819f11ffb7Safresh1 22829f11ffb7Safresh1 my @dump = $Test->explain(@msgs); 22839f11ffb7Safresh1 22849f11ffb7Safresh1Will dump the contents of any references in a human readable format. 22859f11ffb7Safresh1Handy for things like... 22869f11ffb7Safresh1 22879f11ffb7Safresh1 is_deeply($have, $want) || diag explain $have; 22889f11ffb7Safresh1 22899f11ffb7Safresh1or 22909f11ffb7Safresh1 22919f11ffb7Safresh1 is_deeply($have, $want) || note explain $have; 22929f11ffb7Safresh1 22939f11ffb7Safresh1=item B<output> 22949f11ffb7Safresh1 22959f11ffb7Safresh1=item B<failure_output> 22969f11ffb7Safresh1 22979f11ffb7Safresh1=item B<todo_output> 22989f11ffb7Safresh1 22999f11ffb7Safresh1 my $filehandle = $Test->output; 23009f11ffb7Safresh1 $Test->output($filehandle); 23019f11ffb7Safresh1 $Test->output($filename); 23029f11ffb7Safresh1 $Test->output(\$scalar); 23039f11ffb7Safresh1 23049f11ffb7Safresh1These methods control where Test::Builder will print its output. 23059f11ffb7Safresh1They take either an open C<$filehandle>, a C<$filename> to open and write to 23069f11ffb7Safresh1or a C<$scalar> reference to append to. It will always return a C<$filehandle>. 23079f11ffb7Safresh1 23089f11ffb7Safresh1B<output> is where normal "ok/not ok" test output goes. 23099f11ffb7Safresh1 23109f11ffb7Safresh1Defaults to STDOUT. 23119f11ffb7Safresh1 23129f11ffb7Safresh1B<failure_output> is where diagnostic output on test failures and 23139f11ffb7Safresh1C<diag()> goes. It is normally not read by Test::Harness and instead is 23149f11ffb7Safresh1displayed to the user. 23159f11ffb7Safresh1 23169f11ffb7Safresh1Defaults to STDERR. 23179f11ffb7Safresh1 23189f11ffb7Safresh1C<todo_output> is used instead of C<failure_output()> for the 23199f11ffb7Safresh1diagnostics of a failing TODO test. These will not be seen by the 23209f11ffb7Safresh1user. 23219f11ffb7Safresh1 23229f11ffb7Safresh1Defaults to STDOUT. 23239f11ffb7Safresh1 2324b39c5158Smillert=item reset_outputs 2325b39c5158Smillert 2326b39c5158Smillert $tb->reset_outputs; 2327b39c5158Smillert 2328b39c5158SmillertResets all the output filehandles back to their defaults. 2329b39c5158Smillert 2330b39c5158Smillert=item carp 2331b39c5158Smillert 2332b39c5158Smillert $tb->carp(@message); 2333b39c5158Smillert 2334b39c5158SmillertWarns with C<@message> but the message will appear to come from the 2335b39c5158Smillertpoint where the original test function was called (C<< $tb->caller >>). 2336b39c5158Smillert 2337b39c5158Smillert=item croak 2338b39c5158Smillert 2339b39c5158Smillert $tb->croak(@message); 2340b39c5158Smillert 2341b39c5158SmillertDies with C<@message> but the message will appear to come from the 2342b39c5158Smillertpoint where the original test function was called (C<< $tb->caller >>). 2343b39c5158Smillert 2344b39c5158Smillert 2345b39c5158Smillert=back 2346b39c5158Smillert 2347b39c5158Smillert 2348b39c5158Smillert=head2 Test Status and Info 2349b39c5158Smillert 2350b39c5158Smillert=over 4 2351b39c5158Smillert 23529f11ffb7Safresh1=item B<no_log_results> 23539f11ffb7Safresh1 23549f11ffb7Safresh1This will turn off result long-term storage. Calling this method will make 23559f11ffb7Safresh1C<details> and C<summary> useless. You may want to use this if you are running 23569f11ffb7Safresh1enough tests to fill up all available memory. 23579f11ffb7Safresh1 23589f11ffb7Safresh1 Test::Builder->new->no_log_results(); 23599f11ffb7Safresh1 23609f11ffb7Safresh1There is no way to turn it back on. 23619f11ffb7Safresh1 2362b39c5158Smillert=item B<current_test> 2363b39c5158Smillert 2364b39c5158Smillert my $curr_test = $Test->current_test; 2365b39c5158Smillert $Test->current_test($num); 2366b39c5158Smillert 2367b39c5158SmillertGets/sets the current test number we're on. You usually shouldn't 2368b39c5158Smillerthave to set this. 2369b39c5158Smillert 2370b39c5158SmillertIf set forward, the details of the missing tests are filled in as 'unknown'. 2371b39c5158Smillertif set backward, the details of the intervening tests are deleted. You 2372b39c5158Smillertcan erase history if you really want to. 2373b39c5158Smillert 2374b39c5158Smillert 2375b39c5158Smillert=item B<is_passing> 2376b39c5158Smillert 2377b39c5158Smillert my $ok = $builder->is_passing; 2378b39c5158Smillert 2379b39c5158SmillertIndicates if the test suite is currently passing. 2380b39c5158Smillert 2381b39c5158SmillertMore formally, it will be false if anything has happened which makes 2382b39c5158Smillertit impossible for the test suite to pass. True otherwise. 2383b39c5158Smillert 2384b39c5158SmillertFor example, if no tests have run C<is_passing()> will be true because 2385b39c5158Smillerteven though a suite with no tests is a failure you can add a passing 2386b39c5158Smillerttest to it and start passing. 2387b39c5158Smillert 2388b39c5158SmillertDon't think about it too much. 2389b39c5158Smillert 2390b39c5158Smillert 2391b39c5158Smillert=item B<summary> 2392b39c5158Smillert 2393b39c5158Smillert my @tests = $Test->summary; 2394b39c5158Smillert 2395b39c5158SmillertA simple summary of the tests so far. True for pass, false for fail. 2396b39c5158SmillertThis is a logical pass/fail, so todos are passes. 2397b39c5158Smillert 2398b39c5158SmillertOf course, test #1 is $tests[0], etc... 2399b39c5158Smillert 2400b39c5158Smillert 2401b39c5158Smillert=item B<details> 2402b39c5158Smillert 2403b39c5158Smillert my @tests = $Test->details; 2404b39c5158Smillert 2405b39c5158SmillertLike C<summary()>, but with a lot more detail. 2406b39c5158Smillert 2407b39c5158Smillert $tests[$test_num - 1] = 2408b39c5158Smillert { 'ok' => is the test considered a pass? 2409b39c5158Smillert actual_ok => did it literally say 'ok'? 2410b39c5158Smillert name => name of the test (if any) 2411b39c5158Smillert type => type of test (if any, see below). 2412b39c5158Smillert reason => reason for the above (if any) 2413b39c5158Smillert }; 2414b39c5158Smillert 2415b39c5158Smillert'ok' is true if Test::Harness will consider the test to be a pass. 2416b39c5158Smillert 2417b39c5158Smillert'actual_ok' is a reflection of whether or not the test literally 2418b39c5158Smillertprinted 'ok' or 'not ok'. This is for examining the result of 'todo' 2419b39c5158Smillerttests. 2420b39c5158Smillert 2421b39c5158Smillert'name' is the name of the test. 2422b39c5158Smillert 2423b39c5158Smillert'type' indicates if it was a special test. Normal tests have a type 2424b39c5158Smillertof ''. Type can be one of the following: 2425b39c5158Smillert 2426b39c5158Smillert skip see skip() 2427b39c5158Smillert todo see todo() 2428b39c5158Smillert todo_skip see todo_skip() 2429b39c5158Smillert unknown see below 2430b39c5158Smillert 2431b39c5158SmillertSometimes the Test::Builder test counter is incremented without it 2432b39c5158Smillertprinting any test output, for example, when C<current_test()> is changed. 2433b39c5158SmillertIn these cases, Test::Builder doesn't know the result of the test, so 2434b39c5158Smillertits type is 'unknown'. These details for these tests are filled in. 2435b39c5158SmillertThey are considered ok, but the name and actual_ok is left C<undef>. 2436b39c5158Smillert 2437b39c5158SmillertFor example "not ok 23 - hole count # TODO insufficient donuts" would 2438b39c5158Smillertresult in this structure: 2439b39c5158Smillert 2440b39c5158Smillert $tests[22] = # 23 - 1, since arrays start from 0. 2441b39c5158Smillert { ok => 1, # logically, the test passed since its todo 2442b39c5158Smillert actual_ok => 0, # in absolute terms, it failed 2443b39c5158Smillert name => 'hole count', 2444b39c5158Smillert type => 'todo', 2445b39c5158Smillert reason => 'insufficient donuts' 2446b39c5158Smillert }; 2447b39c5158Smillert 2448b39c5158Smillert 2449b39c5158Smillert=item B<todo> 2450b39c5158Smillert 2451b39c5158Smillert my $todo_reason = $Test->todo; 2452b39c5158Smillert my $todo_reason = $Test->todo($pack); 2453b39c5158Smillert 2454b39c5158SmillertIf the current tests are considered "TODO" it will return the reason, 2455b39c5158Smillertif any. This reason can come from a C<$TODO> variable or the last call 2456b39c5158Smillertto C<todo_start()>. 2457b39c5158Smillert 2458b39c5158SmillertSince a TODO test does not need a reason, this function can return an 2459b39c5158Smillertempty string even when inside a TODO block. Use C<< $Test->in_todo >> 2460b39c5158Smillertto determine if you are currently inside a TODO block. 2461b39c5158Smillert 2462b39c5158SmillertC<todo()> is about finding the right package to look for C<$TODO> in. It's 2463b39c5158Smillertpretty good at guessing the right package to look at. It first looks for 2464b39c5158Smillertthe caller based on C<$Level + 1>, since C<todo()> is usually called inside 2465b39c5158Smillerta test function. As a last resort it will use C<exported_to()>. 2466b39c5158Smillert 2467b8851fccSafresh1Sometimes there is some confusion about where C<todo()> should be looking 2468b39c5158Smillertfor the C<$TODO> variable. If you want to be sure, tell it explicitly 2469b39c5158Smillertwhat $pack to use. 2470b39c5158Smillert 2471b39c5158Smillert=item B<find_TODO> 2472b39c5158Smillert 2473b39c5158Smillert my $todo_reason = $Test->find_TODO(); 247465d9bffcSjasper my $todo_reason = $Test->find_TODO($pack); 2475b39c5158Smillert 2476b39c5158SmillertLike C<todo()> but only returns the value of C<$TODO> ignoring 2477b39c5158SmillertC<todo_start()>. 2478b39c5158Smillert 247965d9bffcSjasperCan also be used to set C<$TODO> to a new value while returning the 248065d9bffcSjasperold value: 248165d9bffcSjasper 248265d9bffcSjasper my $old_reason = $Test->find_TODO($pack, 1, $new_reason); 248365d9bffcSjasper 2484b39c5158Smillert=item B<in_todo> 2485b39c5158Smillert 2486b39c5158Smillert my $in_todo = $Test->in_todo; 2487b39c5158Smillert 2488b39c5158SmillertReturns true if the test is currently inside a TODO block. 2489b39c5158Smillert 2490b39c5158Smillert=item B<todo_start> 2491b39c5158Smillert 2492b39c5158Smillert $Test->todo_start(); 2493b39c5158Smillert $Test->todo_start($message); 2494b39c5158Smillert 2495b39c5158SmillertThis method allows you declare all subsequent tests as TODO tests, up until 2496b39c5158Smillertthe C<todo_end> method has been called. 2497b39c5158Smillert 2498b39c5158SmillertThe C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out 2499b39c5158Smillertwhether or not we're in a TODO test. However, often we find that this is not 2500b39c5158Smillertpossible to determine (such as when we want to use C<$TODO> but 2501b39c5158Smillertthe tests are being executed in other packages which can't be inferred 2502b39c5158Smillertbeforehand). 2503b39c5158Smillert 2504b39c5158SmillertNote that you can use this to nest "todo" tests 2505b39c5158Smillert 2506b39c5158Smillert $Test->todo_start('working on this'); 2507b39c5158Smillert # lots of code 2508b39c5158Smillert $Test->todo_start('working on that'); 2509b39c5158Smillert # more code 2510b39c5158Smillert $Test->todo_end; 2511b39c5158Smillert $Test->todo_end; 2512b39c5158Smillert 2513b39c5158SmillertThis is generally not recommended, but large testing systems often have weird 2514b39c5158Smillertinternal needs. 2515b39c5158Smillert 2516b39c5158SmillertWe've tried to make this also work with the TODO: syntax, but it's not 2517b39c5158Smillertguaranteed and its use is also discouraged: 2518b39c5158Smillert 2519b39c5158Smillert TODO: { 2520b39c5158Smillert local $TODO = 'We have work to do!'; 2521b39c5158Smillert $Test->todo_start('working on this'); 2522b39c5158Smillert # lots of code 2523b39c5158Smillert $Test->todo_start('working on that'); 2524b39c5158Smillert # more code 2525b39c5158Smillert $Test->todo_end; 2526b39c5158Smillert $Test->todo_end; 2527b39c5158Smillert } 2528b39c5158Smillert 2529b39c5158SmillertPick one style or another of "TODO" to be on the safe side. 2530b39c5158Smillert 2531b39c5158Smillert 2532b39c5158Smillert=item C<todo_end> 2533b39c5158Smillert 2534b39c5158Smillert $Test->todo_end; 2535b39c5158Smillert 2536b39c5158SmillertStops running tests as "TODO" tests. This method is fatal if called without a 2537b39c5158Smillertpreceding C<todo_start> method call. 2538b39c5158Smillert 2539b39c5158Smillert=item B<caller> 2540b39c5158Smillert 2541b39c5158Smillert my $package = $Test->caller; 2542b39c5158Smillert my($pack, $file, $line) = $Test->caller; 2543b39c5158Smillert my($pack, $file, $line) = $Test->caller($height); 2544b39c5158Smillert 2545b39c5158SmillertLike the normal C<caller()>, except it reports according to your C<level()>. 2546b39c5158Smillert 2547b39c5158SmillertC<$height> will be added to the C<level()>. 2548b39c5158Smillert 2549b39c5158SmillertIf C<caller()> winds up off the top of the stack it report the highest context. 2550b39c5158Smillert 2551b39c5158Smillert=back 2552b39c5158Smillert 2553b39c5158Smillert=head1 EXIT CODES 2554b39c5158Smillert 2555b39c5158SmillertIf all your tests passed, Test::Builder will exit with zero (which is 2556b39c5158Smillertnormal). If anything failed it will exit with how many failed. If 2557b39c5158Smillertyou run less (or more) tests than you planned, the missing (or extras) 2558b39c5158Smillertwill be considered failures. If no tests were ever run Test::Builder 2559b39c5158Smillertwill throw a warning and exit with 255. If the test died, even after 2560b39c5158Smillerthaving successfully completed all its tests, it will still be 2561b39c5158Smillertconsidered a failure and will exit with 255. 2562b39c5158Smillert 2563b39c5158SmillertSo the exit codes are... 2564b39c5158Smillert 2565b39c5158Smillert 0 all tests successful 2566b39c5158Smillert 255 test died or all passed but wrong # of tests run 2567b39c5158Smillert any other number how many failed (including missing or extras) 2568b39c5158Smillert 2569b39c5158SmillertIf you fail more than 254 tests, it will be reported as 254. 2570b39c5158Smillert 2571b39c5158Smillert=head1 THREADS 2572b39c5158Smillert 25739f11ffb7Safresh1In perl 5.8.1 and later, Test::Builder is thread-safe. The test number is 25749f11ffb7Safresh1shared by all threads. This means if one thread sets the test number using 25759f11ffb7Safresh1C<current_test()> they will all be effected. 2576b39c5158Smillert 2577b39c5158SmillertWhile versions earlier than 5.8.1 had threads they contain too many 2578b39c5158Smillertbugs to support. 2579b39c5158Smillert 2580b39c5158SmillertTest::Builder is only thread-aware if threads.pm is loaded I<before> 2581b39c5158SmillertTest::Builder. 2582b39c5158Smillert 25839f11ffb7Safresh1You can directly disable thread support with one of the following: 25849f11ffb7Safresh1 25859f11ffb7Safresh1 $ENV{T2_NO_IPC} = 1 25869f11ffb7Safresh1 25879f11ffb7Safresh1or 25889f11ffb7Safresh1 25899f11ffb7Safresh1 no Test2::IPC; 25909f11ffb7Safresh1 25919f11ffb7Safresh1or 25929f11ffb7Safresh1 25939f11ffb7Safresh1 Test2::API::test2_ipc_disable() 25949f11ffb7Safresh1 2595b39c5158Smillert=head1 MEMORY 2596b39c5158Smillert 2597b8851fccSafresh1An informative hash, accessible via C<details()>, is stored for each 2598b39c5158Smillerttest you perform. So memory usage will scale linearly with each test 2599b39c5158Smillertrun. Although this is not a problem for most test suites, it can 2600b39c5158Smillertbecome an issue if you do large (hundred thousands to million) 2601b39c5158Smillertcombinatorics tests in the same run. 2602b39c5158Smillert 2603b39c5158SmillertIn such cases, you are advised to either split the test file into smaller 2604b39c5158Smillertones, or use a reverse approach, doing "normal" (code) compares and 2605b8851fccSafresh1triggering C<fail()> should anything go unexpected. 2606b39c5158Smillert 2607b39c5158SmillertFuture versions of Test::Builder will have a way to turn history off. 2608b39c5158Smillert 2609b39c5158Smillert 2610b39c5158Smillert=head1 EXAMPLES 2611b39c5158Smillert 2612b8851fccSafresh1CPAN can provide the best examples. L<Test::Simple>, L<Test::More>, 2613b8851fccSafresh1L<Test::Exception> and L<Test::Differences> all use Test::Builder. 2614b39c5158Smillert 2615b39c5158Smillert=head1 SEE ALSO 2616b39c5158Smillert 2617b46d8ef2Safresh1=head2 INTERNALS 2618b46d8ef2Safresh1 2619b46d8ef2Safresh1L<Test2>, L<Test2::API> 2620b46d8ef2Safresh1 2621b46d8ef2Safresh1=head2 LEGACY 2622b46d8ef2Safresh1 2623b46d8ef2Safresh1L<Test::Simple>, L<Test::More> 2624b46d8ef2Safresh1 2625b46d8ef2Safresh1=head2 EXTERNAL 2626b46d8ef2Safresh1 2627b46d8ef2Safresh1L<Test::Harness> 2628b39c5158Smillert 2629b39c5158Smillert=head1 AUTHORS 2630b39c5158Smillert 2631b39c5158SmillertOriginal code by chromatic, maintained by Michael G Schwern 2632b39c5158SmillertE<lt>schwern@pobox.comE<gt> 2633b39c5158Smillert 2634b8851fccSafresh1=head1 MAINTAINERS 2635b8851fccSafresh1 2636b8851fccSafresh1=over 4 2637b8851fccSafresh1 2638b8851fccSafresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt> 2639b8851fccSafresh1 2640b8851fccSafresh1=back 2641b8851fccSafresh1 2642b39c5158Smillert=head1 COPYRIGHT 2643b39c5158Smillert 2644b39c5158SmillertCopyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and 2645b39c5158Smillert Michael G Schwern E<lt>schwern@pobox.comE<gt>. 2646b39c5158Smillert 2647b39c5158SmillertThis program is free software; you can redistribute it and/or 2648b39c5158Smillertmodify it under the same terms as Perl itself. 2649b39c5158Smillert 2650*3d61058aSafresh1See L<https://dev.perl.org/licenses/> 2651