<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test::Builder;

use 5.006;
use strict;
use warnings;

our $VERSION = '1.302133';

BEGIN {
    if( $] &lt; 5.008 ) {
        require Test::Builder::IO::Scalar;
    }
}

use Scalar::Util qw/blessed reftype weaken/;

use Test2::Util qw/USE_THREADS try get_tid/;
use Test2::API qw/context release/;
# Make Test::Builder thread-safe for ithreads.
BEGIN {
    warn "Test::Builder was loaded after Test2 initialization, this is not recommended."
        if Test2::API::test2_init_done() || Test2::API::test2_load_done();

    if (USE_THREADS &amp;&amp; ! Test2::API::test2_ipc_disabled()) {
        require Test2::IPC;
        require Test2::IPC::Driver::Files;
        Test2::IPC::Driver::Files-&gt;import;
        Test2::API::test2_ipc_enable_polling();
        Test2::API::test2_no_wait(1);
        Test2::API::test2_ipc_enable_shm();
    }
}

use Test2::Event::Subtest;
use Test2::Hub::Subtest;

use Test::Builder::Formatter;
use Test::Builder::TodoDiag;

our $Level = 1;
our $Test = $ENV{TB_NO_EARLY_INIT} ? undef : Test::Builder-&gt;new;

sub _add_ts_hooks {
    my $self = shift;

    my $hub = $self-&gt;{Stack}-&gt;top;

    # Take a reference to the hash key, we do this to avoid closing over $self
    # which is the singleton. We use a reference because the value could change
    # in rare cases.
    my $epkgr = \$self-&gt;{Exported_To};

    #$hub-&gt;add_context_aquire(sub {$_[0]-&gt;{level} += $Level - 1});

    $hub-&gt;pre_filter(sub {
        my ($active_hub, $e) = @_;

        my $epkg = $$epkgr;
        my $cpkg = $e-&gt;{trace} ? $e-&gt;{trace}-&gt;{frame}-&gt;[0] : undef;

        no strict 'refs';
        no warnings 'once';
        my $todo;
        $todo = ${"$cpkg\::TODO"} if $cpkg;
        $todo = ${"$epkg\::TODO"} if $epkg &amp;&amp; !$todo;

        return $e unless $todo;

        # Turn a diag into a todo diag
        return Test::Builder::TodoDiag-&gt;new(%$e) if ref($e) eq 'Test2::Event::Diag';

        if ($active_hub == $hub) {
            $e-&gt;set_todo($todo) if $e-&gt;can('set_todo');
            $e-&gt;add_amnesty({tag =&gt; 'TODO', details =&gt; $todo});
        }
        else {
            $e-&gt;add_amnesty({tag =&gt; 'TODO', details =&gt; $todo, inherited =&gt; 1});
        }

        # Set todo on ok's
        if ($e-&gt;isa('Test2::Event::Ok')) {
            $e-&gt;set_effective_pass(1);

            if (my $result = $e-&gt;get_meta(__PACKAGE__)) {
                $result-&gt;{reason} ||= $todo;
                $result-&gt;{type}   ||= 'todo';
                $result-&gt;{ok}       = 1;
            }
        }

        return $e;
    }, inherit =&gt; 1);
}

{
    no warnings;
    INIT {
        use warnings;
        Test2::API::test2_load() unless Test2::API::test2_in_preload();
    }
}

sub new {
    my($class) = shift;
    unless($Test) {
        $Test = $class-&gt;create(singleton =&gt; 1);

        Test2::API::test2_add_callback_post_load(
            sub {
                $Test-&gt;{Original_Pid} = $$ if !$Test-&gt;{Original_Pid} || $Test-&gt;{Original_Pid} == 0;
                $Test-&gt;reset(singleton =&gt; 1);
                $Test-&gt;_add_ts_hooks;
            }
        );

        # Non-TB tools normally expect 0 added to the level. $Level is normally 1. So
        # we only want the level to change if $Level != 1.
        # TB-&gt;ctx compensates for this later.
        Test2::API::test2_add_callback_context_aquire(sub { $_[0]-&gt;{level} += $Level - 1 });

        Test2::API::test2_add_callback_exit(sub { $Test-&gt;_ending(@_) });

        Test2::API::test2_ipc()-&gt;set_no_fatal(1) if Test2::API::test2_has_ipc();
    }
    return $Test;
}

sub create {
    my $class = shift;
    my %params = @_;

    my $self = bless {}, $class;
    if ($params{singleton}) {
        $self-&gt;{Stack} = Test2::API::test2_stack();
    }
    else {
        $self-&gt;{Stack} = Test2::API::Stack-&gt;new;
        $self-&gt;{Stack}-&gt;new_hub(
            formatter =&gt; Test::Builder::Formatter-&gt;new,
            ipc       =&gt; Test2::API::test2_ipc(),
        );

        $self-&gt;reset(%params);
        $self-&gt;_add_ts_hooks;
    }

    return $self;
}

sub ctx {
    my $self = shift;
    context(
        # 1 for our frame, another for the -1 off of $Level in our hook at the top.
        level   =&gt; 2,
        fudge   =&gt; 1,
        stack   =&gt; $self-&gt;{Stack},
        hub     =&gt; $self-&gt;{Hub},
        wrapped =&gt; 1,
        @_
    );
}

sub parent {
    my $self = shift;
    my $ctx = $self-&gt;ctx;
    my $chub = $self-&gt;{Hub} || $ctx-&gt;hub;
    $ctx-&gt;release;

    my $meta = $chub-&gt;meta(__PACKAGE__, {});
    my $parent = $meta-&gt;{parent};

    return undef unless $parent;

    return bless {
        Original_Pid =&gt; $$,
        Stack =&gt; $self-&gt;{Stack},
        Hub =&gt; $parent,
    }, blessed($self);
}

sub child {
    my( $self, $name ) = @_;

    $name ||= "Child of " . $self-&gt;name;
    my $ctx = $self-&gt;ctx;

    my $parent = $ctx-&gt;hub;
    my $pmeta = $parent-&gt;meta(__PACKAGE__, {});
    $self-&gt;croak("You already have a child named ($pmeta-&gt;{child}) running")
        if $pmeta-&gt;{child};

    $pmeta-&gt;{child} = $name;

    # Clear $TODO for the child.
    my $orig_TODO = $self-&gt;find_TODO(undef, 1, undef);

    my $subevents = [];

    my $hub = $ctx-&gt;stack-&gt;new_hub(
        class =&gt; 'Test2::Hub::Subtest',
    );

    $hub-&gt;pre_filter(sub {
        my ($active_hub, $e) = @_;

        # Turn a diag into a todo diag
        return Test::Builder::TodoDiag-&gt;new(%$e) if ref($e) eq 'Test2::Event::Diag';

        return $e;
    }, inherit =&gt; 1) if $orig_TODO;

    $hub-&gt;listen(sub { push @$subevents =&gt; $_[1] });

    $hub-&gt;set_nested( $parent-&gt;nested + 1 );

    my $meta = $hub-&gt;meta(__PACKAGE__, {});
    $meta-&gt;{Name} = $name;
    $meta-&gt;{TODO} = $orig_TODO;
    $meta-&gt;{TODO_PKG} = $ctx-&gt;trace-&gt;package;
    $meta-&gt;{parent} = $parent;
    $meta-&gt;{Test_Results} = [];
    $meta-&gt;{subevents} = $subevents;
    $meta-&gt;{subtest_id} = $hub-&gt;id;
    $meta-&gt;{subtest_uuid} = $hub-&gt;uuid;
    $meta-&gt;{subtest_buffered} = $parent-&gt;format ? 0 : 1;

    $self-&gt;_add_ts_hooks;

    $ctx-&gt;release;
    return bless { Original_Pid =&gt; $$, Stack =&gt; $self-&gt;{Stack}, Hub =&gt; $hub, no_log_results =&gt; $self-&gt;{no_log_results} }, blessed($self);
}

sub finalize {
    my $self = shift;
    my $ok = 1;
    ($ok) = @_ if @_;

    my $st_ctx = $self-&gt;ctx;
    my $chub = $self-&gt;{Hub} || return $st_ctx-&gt;release;

    my $meta = $chub-&gt;meta(__PACKAGE__, {});
    if ($meta-&gt;{child}) {
        $self-&gt;croak("Can't call finalize() with child ($meta-&gt;{child}) active");
    }

    local $? = 0;     # don't fail if $subtests happened to set $? nonzero

    $self-&gt;{Stack}-&gt;pop($chub);

    $self-&gt;find_TODO($meta-&gt;{TODO_PKG}, 1, $meta-&gt;{TODO});

    my $parent = $self-&gt;parent;
    my $ctx = $parent-&gt;ctx;
    my $trace = $ctx-&gt;trace;
    delete $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{child};

    $chub-&gt;finalize($trace-&gt;snapshot(hid =&gt; $chub-&gt;hid, nested =&gt; $chub-&gt;nested), 1)
        if $ok
        &amp;&amp; $chub-&gt;count
        &amp;&amp; !$chub-&gt;no_ending
        &amp;&amp; !$chub-&gt;ended;

    my $plan   = $chub-&gt;plan || 0;
    my $count  = $chub-&gt;count;
    my $failed = $chub-&gt;failed;
    my $passed = $chub-&gt;is_passing;

    my $num_extra = $plan =~ m/\D/ ? 0 : $count - $plan;
    if ($count &amp;&amp; $num_extra != 0) {
        my $s = $plan == 1 ? '' : 's';
        $st_ctx-&gt;diag(&lt;&lt;"FAIL");
Looks like you planned $plan test$s but ran $count.
FAIL
    }

    if ($failed) {
        my $s = $failed == 1 ? '' : 's';

        my $qualifier = $num_extra == 0 ? '' : ' run';

        $st_ctx-&gt;diag(&lt;&lt;"FAIL");
Looks like you failed $failed test$s of $count$qualifier.
FAIL
    }

    if (!$passed &amp;&amp; !$failed &amp;&amp; $count &amp;&amp; !$num_extra) {
        $st_ctx-&gt;diag(&lt;&lt;"FAIL");
All assertions inside the subtest passed, but errors were encountered.
FAIL
    }

    $st_ctx-&gt;release;

    unless ($chub-&gt;bailed_out) {
        my $plan = $chub-&gt;plan;
        if ( $plan &amp;&amp; $plan eq 'SKIP' ) {
            $parent-&gt;skip($chub-&gt;skip_reason, $meta-&gt;{Name});
        }
        elsif ( !$chub-&gt;count ) {
            $parent-&gt;ok( 0, sprintf q[No tests run for subtest "%s"], $meta-&gt;{Name} );
        }
        else {
            $parent-&gt;{subevents}  = $meta-&gt;{subevents};
            $parent-&gt;{subtest_id} = $meta-&gt;{subtest_id};
            $parent-&gt;{subtest_uuid} = $meta-&gt;{subtest_uuid};
            $parent-&gt;{subtest_buffered} = $meta-&gt;{subtest_buffered};
            $parent-&gt;ok( $chub-&gt;is_passing, $meta-&gt;{Name} );
        }
    }

    $ctx-&gt;release;
    return $chub-&gt;is_passing;
}

sub subtest {
    my $self = shift;
    my ($name, $code, @args) = @_;
    my $ctx = $self-&gt;ctx;
    $ctx-&gt;throw("subtest()'s second argument must be a code ref")
        unless $code &amp;&amp; reftype($code) eq 'CODE';

    $name ||= "Child of " . $self-&gt;name;


    $_-&gt;($name,$code,@args)
        for Test2::API::test2_list_pre_subtest_callbacks();

    $ctx-&gt;note("Subtest: $name");

    my $child = $self-&gt;child($name);

    my $start_pid = $$;
    my $st_ctx;
    my ($ok, $err, $finished, $child_error);
    T2_SUBTEST_WRAPPER: {
        my $ctx = $self-&gt;ctx;
        $st_ctx = $ctx-&gt;snapshot;
        $ctx-&gt;release;
        $ok = eval { local $Level = 1; $code-&gt;(@args); 1 };
        ($err, $child_error) = ($@, $?);

        # They might have done 'BEGIN { skip_all =&gt; "whatever" }'
        if (!$ok &amp;&amp; $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) &amp;&amp; blessed($err) eq 'Test::Builder::Exception')) {
            $ok  = undef;
            $err = undef;
        }
        else {
            $finished = 1;
        }
    }

    if ($start_pid != $$ &amp;&amp; !$INC{'Test2/IPC.pm'}) {
        warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err;
        exit 255;
    }

    my $trace = $ctx-&gt;trace;

    if (!$finished) {
        if(my $bailed = $st_ctx-&gt;hub-&gt;bailed_out) {
            my $chub = $child-&gt;{Hub};
            $self-&gt;{Stack}-&gt;pop($chub);
            $ctx-&gt;bail($bailed-&gt;reason);
        }
        my $code = $st_ctx-&gt;hub-&gt;exit_code;
        $ok = !$code;
        $err = "Subtest ended with exit code $code" if $code;
    }

    my $st_hub  = $st_ctx-&gt;hub;
    my $plan  = $st_hub-&gt;plan;
    my $count = $st_hub-&gt;count;

    if (!$count &amp;&amp; (!defined($plan) || "$plan" ne 'SKIP')) {
        $st_ctx-&gt;plan(0) unless defined $plan;
        $st_ctx-&gt;diag('No tests run!');
    }

    $child-&gt;finalize($st_ctx-&gt;trace);

    $ctx-&gt;release;

    die $err unless $ok;

    $? = $child_error if defined $child_error;

    return $st_hub-&gt;is_passing;
}

sub name {
    my $self = shift;
    my $ctx = $self-&gt;ctx;
    release $ctx, $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Name};
}

sub reset {    ## no critic (Subroutines::ProhibitBuiltinHomonyms)
    my ($self, %params) = @_;

    Test2::API::test2_set_is_end(0);

    # We leave this a global because it has to be localized and localizing
    # hash keys is just asking for pain.  Also, it was documented.
    $Level = 1;

    $self-&gt;{no_log_results} = $ENV{TEST_NO_LOG_RESULTS} ? 1 : 0
        unless $params{singleton};

    $self-&gt;{Original_Pid} = Test2::API::test2_in_preload() ? -1 : $$;

    my $ctx = $self-&gt;ctx;
    my $hub = $ctx-&gt;hub;
    $ctx-&gt;release;
    unless ($params{singleton}) {
        $hub-&gt;reset_state();
        $hub-&gt;_tb_reset();
    }

    $ctx = $self-&gt;ctx;

    my $meta = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {});
    %$meta = (
        Name         =&gt; $0,
        Ending       =&gt; 0,
        Done_Testing =&gt; undef,
        Skip_All     =&gt; 0,
        Test_Results =&gt; [],
        parent       =&gt; $meta-&gt;{parent},
    );

    $self-&gt;{Exported_To} = undef unless $params{singleton};

    $self-&gt;{Orig_Handles} ||= do {
        my $format = $ctx-&gt;hub-&gt;format;
        my $out;
        if ($format &amp;&amp; $format-&gt;isa('Test2::Formatter::TAP')) {
            $out = $format-&gt;handles;
        }
        $out ? [@$out] : [];
    };

    $self-&gt;use_numbers(1);
    $self-&gt;no_header(0) unless $params{singleton};
    $self-&gt;no_ending(0) unless $params{singleton};
    $self-&gt;reset_outputs;

    $ctx-&gt;release;

    return;
}


my %plan_cmds = (
    no_plan  =&gt; \&amp;no_plan,
    skip_all =&gt; \&amp;skip_all,
    tests    =&gt; \&amp;_plan_tests,
);

sub plan {
    my( $self, $cmd, $arg ) = @_;

    return unless $cmd;

    my $ctx = $self-&gt;ctx;
    my $hub = $ctx-&gt;hub;

    $ctx-&gt;throw("You tried to plan twice") if $hub-&gt;plan;

    local $Level = $Level + 1;

    if( my $method = $plan_cmds{$cmd} ) {
        local $Level = $Level + 1;
        $self-&gt;$method($arg);
    }
    else {
        my @args = grep { defined } ( $cmd, $arg );
        $ctx-&gt;throw("plan() doesn't understand @args");
    }

    release $ctx, 1;
}


sub _plan_tests {
    my($self, $arg) = @_;

    my $ctx = $self-&gt;ctx;

    if($arg) {
        local $Level = $Level + 1;
        $self-&gt;expected_tests($arg);
    }
    elsif( !defined $arg ) {
        $ctx-&gt;throw("Got an undefined number of tests");
    }
    else {
        $ctx-&gt;throw("You said to run 0 tests");
    }

    $ctx-&gt;release;
}


sub expected_tests {
    my $self = shift;
    my($max) = @_;

    my $ctx = $self-&gt;ctx;

    if(@_) {
        $self-&gt;croak("Number of tests must be a positive integer.  You gave it '$max'")
          unless $max =~ /^\+?\d+$/;

        $ctx-&gt;plan($max);
    }

    my $hub = $ctx-&gt;hub;

    $ctx-&gt;release;

    my $plan = $hub-&gt;plan;
    return 0 unless $plan;
    return 0 if $plan =~ m/\D/;
    return $plan;
}


sub no_plan {
    my($self, $arg) = @_;

    my $ctx = $self-&gt;ctx;

    if (defined $ctx-&gt;hub-&gt;plan) {
        warn "Plan already set, no_plan() is a no-op, this will change to a hard failure in the future.";
        $ctx-&gt;release;
        return;
    }

    $ctx-&gt;alert("no_plan takes no arguments") if $arg;

    $ctx-&gt;hub-&gt;plan('NO PLAN');

    release $ctx, 1;
}


sub done_testing {
    my($self, $num_tests) = @_;

    my $ctx = $self-&gt;ctx;

    my $meta = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {});

    if ($meta-&gt;{Done_Testing}) {
        my ($file, $line) = @{$meta-&gt;{Done_Testing}}[1,2];
        local $ctx-&gt;hub-&gt;{ended}; # OMG This is awful.
        $self-&gt;ok(0, "done_testing() was already called at $file line $line");
        $ctx-&gt;release;
        return;
    }
    $meta-&gt;{Done_Testing} = [$ctx-&gt;trace-&gt;call];

    my $plan = $ctx-&gt;hub-&gt;plan;
    my $count = $ctx-&gt;hub-&gt;count;

    # If done_testing() specified the number of tests, shut off no_plan
    if( defined $num_tests ) {
        $ctx-&gt;plan($num_tests) if !$plan || $plan eq 'NO PLAN';
    }
    elsif ($count &amp;&amp; defined $num_tests &amp;&amp; $count != $num_tests) {
        $self-&gt;ok(0, "planned to run @{[ $self-&gt;expected_tests ]} but done_testing() expects $num_tests");
    }
    else {
        $num_tests = $self-&gt;current_test;
    }

    if( $self-&gt;expected_tests &amp;&amp; $num_tests != $self-&gt;expected_tests ) {
        $self-&gt;ok(0, "planned to run @{[ $self-&gt;expected_tests ]} ".
                     "but done_testing() expects $num_tests");
    }

    $ctx-&gt;plan($num_tests) if $ctx-&gt;hub-&gt;plan &amp;&amp; $ctx-&gt;hub-&gt;plan eq 'NO PLAN';

    $ctx-&gt;hub-&gt;finalize($ctx-&gt;trace, 1);

    release $ctx, 1;
}


sub has_plan {
    my $self = shift;

    my $ctx = $self-&gt;ctx;
    my $plan = $ctx-&gt;hub-&gt;plan;
    $ctx-&gt;release;

    return( $plan ) if $plan &amp;&amp; $plan !~ m/\D/;
    return('no_plan') if $plan &amp;&amp; $plan eq 'NO PLAN';
    return(undef);
}


sub skip_all {
    my( $self, $reason ) = @_;

    my $ctx = $self-&gt;ctx;

    $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Skip_All} = $reason || 1;

    # Work around old perl bug
    if ($] &lt; 5.020000) {
        my $begin = 0;
        my $level = 0;
        while (my @call = caller($level++)) {
            last unless @call &amp;&amp; $call[0];
            next unless $call[3] =~ m/::BEGIN$/;
            $begin++;
            last;
        }
        # HACK!
        die 'Label not found for "last T2_SUBTEST_WRAPPER"' if $begin &amp;&amp; $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{parent};
    }

    $ctx-&gt;plan(0, SKIP =&gt; $reason);
}


sub exported_to {
    my( $self, $pack ) = @_;

    if( defined $pack ) {
        $self-&gt;{Exported_To} = $pack;
    }
    return $self-&gt;{Exported_To};
}


sub ok {
    my( $self, $test, $name ) = @_;

    my $ctx = $self-&gt;ctx;

    # $test might contain an object which we don't want to accidentally
    # store, so we turn it into a boolean.
    $test = $test ? 1 : 0;

    # In case $name is a string overloaded object, force it to stringify.
    no  warnings qw/uninitialized numeric/;
    $name = "$name" if defined $name;

    # Profiling showed that the regex here was a huge time waster, doing the
    # numeric addition first cuts our profile time from ~300ms to ~50ms
    $self-&gt;diag(&lt;&lt;"    ERR") if 0 + $name &amp;&amp; $name =~ /^[\d\s]+$/;
    You named your test '$name'.  You shouldn't use numbers for your test names.
    Very confusing.
    ERR
    use warnings qw/uninitialized numeric/;

    my $trace = $ctx-&gt;{trace};
    my $hub   = $ctx-&gt;{hub};

    my $result = {
        ok =&gt; $test,
        actual_ok =&gt; $test,
        reason =&gt; '',
        type =&gt; '',
        (name =&gt; defined($name) ? $name : ''),
    };

    $hub-&gt;{_meta}-&gt;{+__PACKAGE__}-&gt;{Test_Results}[ $hub-&gt;{count} ] = $result unless $self-&gt;{no_log_results};

    my $orig_name = $name;

    my @attrs;
    my $subevents  = delete $self-&gt;{subevents};
    my $subtest_id = delete $self-&gt;{subtest_id};
    my $subtest_uuid = delete $self-&gt;{subtest_uuid};
    my $subtest_buffered = delete $self-&gt;{subtest_buffered};
    my $epkg = 'Test2::Event::Ok';
    if ($subevents) {
        $epkg = 'Test2::Event::Subtest';
        push @attrs =&gt; (subevents =&gt; $subevents, subtest_id =&gt; $subtest_id, subtest_uuid =&gt; $subtest_uuid, buffered =&gt; $subtest_buffered);
    }

    my $e = bless {
        trace =&gt; bless( {%$trace}, 'Test2::EventFacet::Trace'),
        pass  =&gt; $test,
        name  =&gt; $name,
        _meta =&gt; {'Test::Builder' =&gt; $result},
        effective_pass =&gt; $test,
        @attrs,
    }, $epkg;
    $hub-&gt;send($e);

    $self-&gt;_ok_debug($trace, $orig_name) unless($test);

    $ctx-&gt;release;
    return $test;
}

sub _ok_debug {
    my $self = shift;
    my ($trace, $orig_name) = @_;

    my $is_todo = defined($self-&gt;todo);

    my $msg = $is_todo ? "Failed (TODO)" : "Failed";

    my (undef, $file, $line) = $trace-&gt;call;
    if (defined $orig_name) {
        $self-&gt;diag(qq[  $msg test '$orig_name'\n  at $file line $line.\n]);
    }
    else {
        $self-&gt;diag(qq[  $msg test at $file line $line.\n]);
    }
}

sub _diag_fh {
    my $self = shift;
    local $Level = $Level + 1;
    return $self-&gt;in_todo ? $self-&gt;todo_output : $self-&gt;failure_output;
}

sub _unoverload {
    my ($self, $type, $thing) = @_;

    return unless ref $$thing;
    return unless blessed($$thing) || scalar $self-&gt;_try(sub{ $$thing-&gt;isa('UNIVERSAL') });
    {
        local ($!, $@);
        require overload;
    }
    my $string_meth = overload::Method( $$thing, $type ) || return;
    $$thing = $$thing-&gt;$string_meth();
}

sub _unoverload_str {
    my $self = shift;

    $self-&gt;_unoverload( q[""], $_ ) for @_;
}

sub _unoverload_num {
    my $self = shift;

    $self-&gt;_unoverload( '0+', $_ ) for @_;

    for my $val (@_) {
        next unless $self-&gt;_is_dualvar($$val);
        $$val = $$val + 0;
    }
}

# This is a hack to detect a dualvar such as $!
sub _is_dualvar {
    my( $self, $val ) = @_;

    # Objects are not dualvars.
    return 0 if ref $val;

    no warnings 'numeric';
    my $numval = $val + 0;
    return ($numval != 0 and $numval ne $val ? 1 : 0);
}


sub is_eq {
    my( $self, $got, $expect, $name ) = @_;

    my $ctx = $self-&gt;ctx;

    local $Level = $Level + 1;

    if( !defined $got || !defined $expect ) {
        # undef only matches undef and nothing else
        my $test = !defined $got &amp;&amp; !defined $expect;

        $self-&gt;ok( $test, $name );
        $self-&gt;_is_diag( $got, 'eq', $expect ) unless $test;
        $ctx-&gt;release;
        return $test;
    }

    release $ctx, $self-&gt;cmp_ok( $got, 'eq', $expect, $name );
}


sub is_num {
    my( $self, $got, $expect, $name ) = @_;
    my $ctx = $self-&gt;ctx;
    local $Level = $Level + 1;

    if( !defined $got || !defined $expect ) {
        # undef only matches undef and nothing else
        my $test = !defined $got &amp;&amp; !defined $expect;

        $self-&gt;ok( $test, $name );
        $self-&gt;_is_diag( $got, '==', $expect ) unless $test;
        $ctx-&gt;release;
        return $test;
    }

    release $ctx, $self-&gt;cmp_ok( $got, '==', $expect, $name );
}


sub _diag_fmt {
    my( $self, $type, $val ) = @_;

    if( defined $$val ) {
        if( $type eq 'eq' or $type eq 'ne' ) {
            # quote and force string context
            $$val = "'$$val'";
        }
        else {
            # force numeric context
            $self-&gt;_unoverload_num($val);
        }
    }
    else {
        $$val = 'undef';
    }

    return;
}


sub _is_diag {
    my( $self, $got, $type, $expect ) = @_;

    $self-&gt;_diag_fmt( $type, $_ ) for \$got, \$expect;

    local $Level = $Level + 1;
    return $self-&gt;diag(&lt;&lt;"DIAGNOSTIC");
         got: $got
    expected: $expect
DIAGNOSTIC

}

sub _isnt_diag {
    my( $self, $got, $type ) = @_;

    $self-&gt;_diag_fmt( $type, \$got );

    local $Level = $Level + 1;
    return $self-&gt;diag(&lt;&lt;"DIAGNOSTIC");
         got: $got
    expected: anything else
DIAGNOSTIC
}


sub isnt_eq {
    my( $self, $got, $dont_expect, $name ) = @_;
    my $ctx = $self-&gt;ctx;
    local $Level = $Level + 1;

    if( !defined $got || !defined $dont_expect ) {
        # undef only matches undef and nothing else
        my $test = defined $got || defined $dont_expect;

        $self-&gt;ok( $test, $name );
        $self-&gt;_isnt_diag( $got, 'ne' ) unless $test;
        $ctx-&gt;release;
        return $test;
    }

    release $ctx, $self-&gt;cmp_ok( $got, 'ne', $dont_expect, $name );
}

sub isnt_num {
    my( $self, $got, $dont_expect, $name ) = @_;
    my $ctx = $self-&gt;ctx;
    local $Level = $Level + 1;

    if( !defined $got || !defined $dont_expect ) {
        # undef only matches undef and nothing else
        my $test = defined $got || defined $dont_expect;

        $self-&gt;ok( $test, $name );
        $self-&gt;_isnt_diag( $got, '!=' ) unless $test;
        $ctx-&gt;release;
        return $test;
    }

    release $ctx, $self-&gt;cmp_ok( $got, '!=', $dont_expect, $name );
}


sub like {
    my( $self, $thing, $regex, $name ) = @_;
    my $ctx = $self-&gt;ctx;

    local $Level = $Level + 1;

    release $ctx, $self-&gt;_regex_ok( $thing, $regex, '=~', $name );
}

sub unlike {
    my( $self, $thing, $regex, $name ) = @_;
    my $ctx = $self-&gt;ctx;

    local $Level = $Level + 1;

    release $ctx, $self-&gt;_regex_ok( $thing, $regex, '!~', $name );
}


my %numeric_cmps = map { ( $_, 1 ) } ( "&lt;", "&lt;=", "&gt;", "&gt;=", "==", "!=", "&lt;=&gt;" );

# Bad, these are not comparison operators. Should we include more?
my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&amp;&amp;=", "...");

sub cmp_ok {
    my( $self, $got, $type, $expect, $name ) = @_;
    my $ctx = $self-&gt;ctx;

    if ($cmp_ok_bl{$type}) {
        $ctx-&gt;throw("$type is not a valid comparison operator in cmp_ok()");
    }

    my ($test, $succ);
    my $error;
    {
        ## no critic (BuiltinFunctions::ProhibitStringyEval)

        local( $@, $!, $SIG{__DIE__} );    # isolate eval

        my($pack, $file, $line) = $ctx-&gt;trace-&gt;call();

        # This is so that warnings come out at the caller's level
        $succ = eval qq[
#line $line "(eval in cmp_ok) $file"
\$test = (\$got $type \$expect);
1;
];
        $error = $@;
    }
    local $Level = $Level + 1;
    my $ok = $self-&gt;ok( $test, $name );

    # Treat overloaded objects as numbers if we're asked to do a
    # numeric comparison.
    my $unoverload
      = $numeric_cmps{$type}
      ? '_unoverload_num'
      : '_unoverload_str';

    $self-&gt;diag(&lt;&lt;"END") unless $succ;
An error occurred while using $type:
------------------------------------
$error
------------------------------------
END

    unless($ok) {
        $self-&gt;$unoverload( \$got, \$expect );

        if( $type =~ /^(eq|==)$/ ) {
            $self-&gt;_is_diag( $got, $type, $expect );
        }
        elsif( $type =~ /^(ne|!=)$/ ) {
            no warnings;
            my $eq = ($got eq $expect || $got == $expect)
                &amp;&amp; (
                    (defined($got) xor defined($expect))
                 || (length($got)  !=  length($expect))
                );
            use warnings;

            if ($eq) {
                $self-&gt;_cmp_diag( $got, $type, $expect );
            }
            else {
                $self-&gt;_isnt_diag( $got, $type );
            }
        }
        else {
            $self-&gt;_cmp_diag( $got, $type, $expect );
        }
    }
    return release $ctx, $ok;
}

sub _cmp_diag {
    my( $self, $got, $type, $expect ) = @_;

    $got    = defined $got    ? "'$got'"    : 'undef';
    $expect = defined $expect ? "'$expect'" : 'undef';

    local $Level = $Level + 1;
    return $self-&gt;diag(&lt;&lt;"DIAGNOSTIC");
    $got
        $type
    $expect
DIAGNOSTIC
}

sub _caller_context {
    my $self = shift;

    my( $pack, $file, $line ) = $self-&gt;caller(1);

    my $code = '';
    $code .= "#line $line $file\n" if defined $file and defined $line;

    return $code;
}


sub BAIL_OUT {
    my( $self, $reason ) = @_;

    my $ctx = $self-&gt;ctx;

    $self-&gt;{Bailed_Out} = 1;

    $ctx-&gt;bail($reason);
}


{
    no warnings 'once';
    *BAILOUT = \&amp;BAIL_OUT;
}

sub skip {
    my( $self, $why, $name ) = @_;
    $why ||= '';
    $name = '' unless defined $name;
    $self-&gt;_unoverload_str( \$why );

    my $ctx = $self-&gt;ctx;

    $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Test_Results}[ $ctx-&gt;hub-&gt;count ] = {
        'ok'      =&gt; 1,
        actual_ok =&gt; 1,
        name      =&gt; $name,
        type      =&gt; 'skip',
        reason    =&gt; $why,
    } unless $self-&gt;{no_log_results};

    $name =~ s|#|\\#|g;    # # in a name can confuse Test::Harness.
    $name =~ s{\n}{\n# }sg;
    $why =~ s{\n}{\n# }sg;

    my $tctx = $ctx-&gt;snapshot;
    $tctx-&gt;skip('', $why);

    return release $ctx, 1;
}


sub todo_skip {
    my( $self, $why ) = @_;
    $why ||= '';

    my $ctx = $self-&gt;ctx;

    $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Test_Results}[ $ctx-&gt;hub-&gt;count ] = {
        'ok'      =&gt; 1,
        actual_ok =&gt; 0,
        name      =&gt; '',
        type      =&gt; 'todo_skip',
        reason    =&gt; $why,
    } unless $self-&gt;{no_log_results};

    $why =~ s{\n}{\n# }sg;
    my $tctx = $ctx-&gt;snapshot;
    $tctx-&gt;send_event( 'Skip', todo =&gt; $why, todo_diag =&gt; 1, reason =&gt; $why, pass =&gt; 0);

    return release $ctx, 1;
}


sub maybe_regex {
    my( $self, $regex ) = @_;
    my $usable_regex = undef;

    return $usable_regex unless defined $regex;

    my( $re, $opts );

    # Check for qr/foo/
    if( _is_qr($regex) ) {
        $usable_regex = $regex;
    }
    # Check for '/foo/' or 'm,foo,'
    elsif(( $re, $opts )        = $regex =~ m{^ /(.*)/ (\w*) $ }sx              or
          ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
    )
    {
        $usable_regex = length $opts ? "(?$opts)$re" : $re;
    }

    return $usable_regex;
}

sub _is_qr {
    my $regex = shift;

    # is_regexp() checks for regexes in a robust manner, say if they're
    # blessed.
    return re::is_regexp($regex) if defined &amp;re::is_regexp;
    return ref $regex eq 'Regexp';
}

sub _regex_ok {
    my( $self, $thing, $regex, $cmp, $name ) = @_;

    my $ok           = 0;
    my $usable_regex = $self-&gt;maybe_regex($regex);
    unless( defined $usable_regex ) {
        local $Level = $Level + 1;
        $ok = $self-&gt;ok( 0, $name );
        $self-&gt;diag("    '$regex' doesn't look much like a regex to me.");
        return $ok;
    }

    {
        my $test;
        my $context = $self-&gt;_caller_context;

        {
            ## no critic (BuiltinFunctions::ProhibitStringyEval)

            local( $@, $!, $SIG{__DIE__} );    # isolate eval

            # No point in issuing an uninit warning, they'll see it in the diagnostics
            no warnings 'uninitialized';

            $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0};
        }

        $test = !$test if $cmp eq '!~';

        local $Level = $Level + 1;
        $ok = $self-&gt;ok( $test, $name );
    }

    unless($ok) {
        $thing = defined $thing ? "'$thing'" : 'undef';
        my $match = $cmp eq '=~' ? "doesn't match" : "matches";

        local $Level = $Level + 1;
        $self-&gt;diag( sprintf &lt;&lt;'DIAGNOSTIC', $thing, $match, $regex );
                  %s
    %13s '%s'
DIAGNOSTIC

    }

    return $ok;
}


sub is_fh {
    my $self     = shift;
    my $maybe_fh = shift;
    return 0 unless defined $maybe_fh;

    return 1 if ref $maybe_fh  eq 'GLOB';    # its a glob ref
    return 1 if ref \$maybe_fh eq 'GLOB';    # its a glob

    return eval { $maybe_fh-&gt;isa("IO::Handle") } ||
           eval { tied($maybe_fh)-&gt;can('TIEHANDLE') };
}


sub level {
    my( $self, $level ) = @_;

    if( defined $level ) {
        $Level = $level;
    }
    return $Level;
}


sub use_numbers {
    my( $self, $use_nums ) = @_;

    my $ctx = $self-&gt;ctx;
    my $format = $ctx-&gt;hub-&gt;format;
    unless ($format &amp;&amp; $format-&gt;can('no_numbers') &amp;&amp; $format-&gt;can('set_no_numbers')) {
        warn "The current formatter does not support 'use_numbers'" if $format;
        return release $ctx, 0;
    }

    $format-&gt;set_no_numbers(!$use_nums) if defined $use_nums;

    return release $ctx, $format-&gt;no_numbers ? 0 : 1;
}

BEGIN {
    for my $method (qw(no_header no_diag)) {
        my $set = "set_$method";
        my $code = sub {
            my( $self, $no ) = @_;

            my $ctx = $self-&gt;ctx;
            my $format = $ctx-&gt;hub-&gt;format;
            unless ($format &amp;&amp; $format-&gt;can($set)) {
                warn "The current formatter does not support '$method'" if $format;
                $ctx-&gt;release;
                return
            }

            $format-&gt;$set($no) if defined $no;

            return release $ctx, $format-&gt;$method ? 1 : 0;
        };

        no strict 'refs';    ## no critic
        *$method = $code;
    }
}

sub no_ending {
    my( $self, $no ) = @_;

    my $ctx = $self-&gt;ctx;

    $ctx-&gt;hub-&gt;set_no_ending($no) if defined $no;

    return release $ctx, $ctx-&gt;hub-&gt;no_ending;
}

sub diag {
    my $self = shift;
    return unless @_;

    my $text = join '' =&gt; map {defined($_) ? $_ : 'undef'} @_;

    if (Test2::API::test2_in_preload()) {
        chomp($text);
        $text =~ s/^/# /msg;
        print STDERR $text, "\n";
        return 0;
    }

    my $ctx = $self-&gt;ctx;
    $ctx-&gt;diag($text);
    $ctx-&gt;release;
    return 0;
}


sub note {
    my $self = shift;
    return unless @_;

    my $text = join '' =&gt; map {defined($_) ? $_ : 'undef'} @_;

    if (Test2::API::test2_in_preload()) {
        chomp($text);
        $text =~ s/^/# /msg;
        print STDOUT $text, "\n";
        return 0;
    }

    my $ctx = $self-&gt;ctx;
    $ctx-&gt;note($text);
    $ctx-&gt;release;
    return 0;
}


sub explain {
    my $self = shift;

    local ($@, $!);
    require Data::Dumper;

    return map {
        ref $_
          ? do {
            my $dumper = Data::Dumper-&gt;new( [$_] );
            $dumper-&gt;Indent(1)-&gt;Terse(1);
            $dumper-&gt;Sortkeys(1) if $dumper-&gt;can("Sortkeys");
            $dumper-&gt;Dump;
          }
          : $_
    } @_;
}


sub output {
    my( $self, $fh ) = @_;

    my $ctx = $self-&gt;ctx;
    my $format = $ctx-&gt;hub-&gt;format;
    $ctx-&gt;release;
    return unless $format &amp;&amp; $format-&gt;isa('Test2::Formatter::TAP');

    $format-&gt;handles-&gt;[Test2::Formatter::TAP::OUT_STD()] = $self-&gt;_new_fh($fh)
        if defined $fh;

    return $format-&gt;handles-&gt;[Test2::Formatter::TAP::OUT_STD()];
}

sub failure_output {
    my( $self, $fh ) = @_;

    my $ctx = $self-&gt;ctx;
    my $format = $ctx-&gt;hub-&gt;format;
    $ctx-&gt;release;
    return unless $format &amp;&amp; $format-&gt;isa('Test2::Formatter::TAP');

    $format-&gt;handles-&gt;[Test2::Formatter::TAP::OUT_ERR()] = $self-&gt;_new_fh($fh)
        if defined $fh;

    return $format-&gt;handles-&gt;[Test2::Formatter::TAP::OUT_ERR()];
}

sub todo_output {
    my( $self, $fh ) = @_;

    my $ctx = $self-&gt;ctx;
    my $format = $ctx-&gt;hub-&gt;format;
    $ctx-&gt;release;
    return unless $format &amp;&amp; $format-&gt;isa('Test::Builder::Formatter');

    $format-&gt;handles-&gt;[Test::Builder::Formatter::OUT_TODO()] = $self-&gt;_new_fh($fh)
        if defined $fh;

    return $format-&gt;handles-&gt;[Test::Builder::Formatter::OUT_TODO()];
}

sub _new_fh {
    my $self = shift;
    my($file_or_fh) = shift;

    my $fh;
    if( $self-&gt;is_fh($file_or_fh) ) {
        $fh = $file_or_fh;
    }
    elsif( ref $file_or_fh eq 'SCALAR' ) {
        # Scalar refs as filehandles was added in 5.8.
        if( $] &gt;= 5.008 ) {
            open $fh, "&gt;&gt;", $file_or_fh
              or $self-&gt;croak("Can't open scalar ref $file_or_fh: $!");
        }
        # Emulate scalar ref filehandles with a tie.
        else {
            $fh = Test::Builder::IO::Scalar-&gt;new($file_or_fh)
              or $self-&gt;croak("Can't tie scalar ref $file_or_fh");
        }
    }
    else {
        open $fh, "&gt;", $file_or_fh
          or $self-&gt;croak("Can't open test output log $file_or_fh: $!");
        _autoflush($fh);
    }

    return $fh;
}

sub _autoflush {
    my($fh) = shift;
    my $old_fh = select $fh;
    $| = 1;
    select $old_fh;

    return;
}


sub reset_outputs {
    my $self = shift;

    my $ctx = $self-&gt;ctx;
    my $format = $ctx-&gt;hub-&gt;format;
    $ctx-&gt;release;
    return unless $format &amp;&amp; $format-&gt;isa('Test2::Formatter::TAP');
    $format-&gt;set_handles([@{$self-&gt;{Orig_Handles}}]) if $self-&gt;{Orig_Handles};

    return;
}


sub carp {
    my $self = shift;
    my $ctx = $self-&gt;ctx;
    $ctx-&gt;alert(join "", @_);
    $ctx-&gt;release;
}

sub croak {
    my $self = shift;
    my $ctx = $self-&gt;ctx;
    $ctx-&gt;throw(join "", @_);
    $ctx-&gt;release;
}


sub current_test {
    my( $self, $num ) = @_;

    my $ctx = $self-&gt;ctx;
    my $hub = $ctx-&gt;hub;

    if( defined $num ) {
        $hub-&gt;set_count($num);

        unless ($self-&gt;{no_log_results}) {
            # If the test counter is being pushed forward fill in the details.
            my $test_results = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Test_Results};
            if ($num &gt; @$test_results) {
                my $start = @$test_results ? @$test_results : 0;
                for ($start .. $num - 1) {
                    $test_results-&gt;[$_] = {
                        'ok'      =&gt; 1,
                        actual_ok =&gt; undef,
                        reason    =&gt; 'incrementing test number',
                        type      =&gt; 'unknown',
                        name      =&gt; undef
                    };
                }
            }
            # If backward, wipe history.  Its their funeral.
            elsif ($num &lt; @$test_results) {
                $#{$test_results} = $num - 1;
            }
        }
    }
    return release $ctx, $hub-&gt;count;
}


sub is_passing {
    my $self = shift;

    my $ctx = $self-&gt;ctx;
    my $hub = $ctx-&gt;hub;

    if( @_ ) {
        my ($bool) = @_;
        $hub-&gt;set_failed(0) if $bool;
        $hub-&gt;is_passing($bool);
    }

    return release $ctx, $hub-&gt;is_passing;
}


sub summary {
    my($self) = shift;

    return if $self-&gt;{no_log_results};

    my $ctx = $self-&gt;ctx;
    my $data = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Test_Results};
    $ctx-&gt;release;
    return map { $_ ? $_-&gt;{'ok'} : () } @$data;
}


sub details {
    my $self = shift;

    return if $self-&gt;{no_log_results};

    my $ctx = $self-&gt;ctx;
    my $data = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Test_Results};
    $ctx-&gt;release;
    return @$data;
}


sub find_TODO {
    my( $self, $pack, $set, $new_value ) = @_;

    my $ctx = $self-&gt;ctx;

    $pack ||= $ctx-&gt;trace-&gt;package || $self-&gt;exported_to;
    $ctx-&gt;release;

    return unless $pack;

    no strict 'refs';    ## no critic
    no warnings 'once';
    my $old_value = ${ $pack . '::TODO' };
    $set and ${ $pack . '::TODO' } = $new_value;
    return $old_value;
}

sub todo {
    my( $self, $pack ) = @_;

    local $Level = $Level + 1;
    my $ctx = $self-&gt;ctx;
    $ctx-&gt;release;

    my $meta = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {todo =&gt; []})-&gt;{todo};
    return $meta-&gt;[-1]-&gt;[1] if $meta &amp;&amp; @$meta;

    $pack ||= $ctx-&gt;trace-&gt;package;

    return unless $pack;

    no strict 'refs';    ## no critic
    no warnings 'once';
    return ${ $pack . '::TODO' };
}

sub in_todo {
    my $self = shift;

    local $Level = $Level + 1;
    my $ctx = $self-&gt;ctx;
    $ctx-&gt;release;

    my $meta = $ctx-&gt;hub-&gt;meta(__PACKAGE__, {todo =&gt; []})-&gt;{todo};
    return 1 if $meta &amp;&amp; @$meta;

    my $pack = $ctx-&gt;trace-&gt;package || return 0;

    no strict 'refs';    ## no critic
    no warnings 'once';
    my $todo = ${ $pack . '::TODO' };

    return 0 unless defined $todo;
    return 0 if "$todo" eq '';
    return 1;
}

sub todo_start {
    my $self = shift;
    my $message = @_ ? shift : '';

    my $ctx = $self-&gt;ctx;

    my $hub = $ctx-&gt;hub;
    my $filter = $hub-&gt;pre_filter(sub {
        my ($active_hub, $e) = @_;

        # Turn a diag into a todo diag
        return Test::Builder::TodoDiag-&gt;new(%$e) if ref($e) eq 'Test2::Event::Diag';

        # Set todo on ok's
        if ($hub == $active_hub &amp;&amp; $e-&gt;isa('Test2::Event::Ok')) {
            $e-&gt;set_todo($message);
            $e-&gt;set_effective_pass(1);

            if (my $result = $e-&gt;get_meta(__PACKAGE__)) {
                $result-&gt;{reason} ||= $message;
                $result-&gt;{type}   ||= 'todo';
                $result-&gt;{ok}       = 1;
            }
        }

        return $e;
    }, inherit =&gt; 1);

    push @{$ctx-&gt;hub-&gt;meta(__PACKAGE__, {todo =&gt; []})-&gt;{todo}} =&gt; [$filter, $message];

    $ctx-&gt;release;

    return;
}

sub todo_end {
    my $self = shift;

    my $ctx = $self-&gt;ctx;

    my $set = pop @{$ctx-&gt;hub-&gt;meta(__PACKAGE__, {todo =&gt; []})-&gt;{todo}};

    $ctx-&gt;throw('todo_end() called without todo_start()') unless $set;

    $ctx-&gt;hub-&gt;pre_unfilter($set-&gt;[0]);

    $ctx-&gt;release;

    return;
}


sub caller {    ## no critic (Subroutines::ProhibitBuiltinHomonyms)
    my( $self ) = @_;

    my $ctx = $self-&gt;ctx;

    my $trace = $ctx-&gt;trace;
    $ctx-&gt;release;
    return wantarray ? $trace-&gt;call : $trace-&gt;package;
}


sub _try {
    my( $self, $code, %opts ) = @_;

    my $error;
    my $return;
    {
        local $!;               # eval can mess up $!
        local $@;               # don't set $@ in the test
        local $SIG{__DIE__};    # don't trip an outside DIE handler.
        $return = eval { $code-&gt;() };
        $error = $@;
    }

    die $error if $error and $opts{die_on_fail};

    return wantarray ? ( $return, $error ) : $return;
}

sub _ending {
    my $self = shift;
    my ($ctx, $real_exit_code, $new) = @_;

    unless ($ctx) {
        my $octx = $self-&gt;ctx;
        $ctx = $octx-&gt;snapshot;
        $octx-&gt;release;
    }

    return if $ctx-&gt;hub-&gt;no_ending;
    return if $ctx-&gt;hub-&gt;meta(__PACKAGE__, {})-&gt;{Ending}++;

    # Don't bother with an ending if this is a forked copy.  Only the parent
    # should do the ending.
    return unless $self-&gt;{Original_Pid} == $$;

    my $hub = $ctx-&gt;hub;
    return if $hub-&gt;bailed_out;

    my $plan  = $hub-&gt;plan;
    my $count = $hub-&gt;count;
    my $failed = $hub-&gt;failed;
    my $passed = $hub-&gt;is_passing;
    return unless $plan || $count || $failed;

    # Ran tests but never declared a plan or hit done_testing
    if( !$hub-&gt;plan and $hub-&gt;count ) {
        $self-&gt;diag("Tests were run but no plan was declared and done_testing() was not seen.");

        if($real_exit_code) {
            $self-&gt;diag(&lt;&lt;"FAIL");
Looks like your test exited with $real_exit_code just after $count.
FAIL
            $$new ||= $real_exit_code;
            return;
        }

        # But if the tests ran, handle exit code.
        if($failed &gt; 0) {
            my $exit_code = $failed &lt;= 254 ? $failed : 254;
            $$new ||= $exit_code;
            return;
        }

        $$new ||= 254;
        return;
    }

    if ($real_exit_code &amp;&amp; !$count) {
        $self-&gt;diag("Looks like your test exited with $real_exit_code before it could output anything.");
        $$new ||= $real_exit_code;
        return;
    }

    return if $plan &amp;&amp; "$plan" eq 'SKIP';

    if (!$count) {
        $self-&gt;diag('No tests run!');
        $$new ||= 255;
        return;
    }

    if ($real_exit_code) {
        $self-&gt;diag(&lt;&lt;"FAIL");
Looks like your test exited with $real_exit_code just after $count.
FAIL
        $$new ||= $real_exit_code;
        return;
    }

    if ($plan eq 'NO PLAN') {
        $ctx-&gt;plan( $count );
        $plan = $hub-&gt;plan;
    }

    # Figure out if we passed or failed and print helpful messages.
    my $num_extra = $count - $plan;

    if ($num_extra != 0) {
        my $s = $plan == 1 ? '' : 's';
        $self-&gt;diag(&lt;&lt;"FAIL");
Looks like you planned $plan test$s but ran $count.
FAIL
    }

    if ($failed) {
        my $s = $failed == 1 ? '' : 's';

        my $qualifier = $num_extra == 0 ? '' : ' run';

        $self-&gt;diag(&lt;&lt;"FAIL");
Looks like you failed $failed test$s of $count$qualifier.
FAIL
    }

    if (!$passed &amp;&amp; !$failed &amp;&amp; $count &amp;&amp; !$num_extra) {
        $ctx-&gt;diag(&lt;&lt;"FAIL");
All assertions passed, but errors were encountered.
FAIL
    }

    my $exit_code = 0;
    if ($failed) {
        $exit_code = $failed &lt;= 254 ? $failed : 254;
    }
    elsif ($num_extra != 0) {
        $exit_code = 255;
    }
    elsif (!$passed) {
        $exit_code = 255;
    }

    $$new ||= $exit_code;
    return;
}

# Some things used this even though it was private... I am looking at you
# Test::Builder::Prefix...
sub _print_comment {
    my( $self, $fh, @msgs ) = @_;

    return if $self-&gt;no_diag;
    return unless @msgs;

    # Prevent printing headers when compiling (i.e. -c)
    return if $^C;

    # Smash args together like print does.
    # Convert undef to 'undef' so its readable.
    my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;

    # Escape the beginning, _print will take care of the rest.
    $msg =~ s/^/# /;

    local( $\, $", $, ) = ( undef, ' ', '' );
    print $fh $msg;

    return 0;
}

# This is used by Test::SharedFork to turn on IPC after the fact. Not
# documenting because I do not want it used. The method name is borrowed from
# Test::Builder 2
# Once Test2 stuff goes stable this method will be removed and Test::SharedFork
# will be made smarter.
sub coordinate_forks {
    my $self = shift;

    {
        local ($@, $!);
        require Test2::IPC;
    }
    Test2::IPC-&gt;import;
    Test2::API::test2_ipc_enable_polling();
    Test2::API::test2_load();
    my $ipc = Test2::IPC::apply_ipc($self-&gt;{Stack});
    $ipc-&gt;set_no_fatal(1);
    Test2::API::test2_no_wait(1);
    Test2::API::test2_ipc_enable_shm();
}

sub no_log_results { $_[0]-&gt;{no_log_results} = 1 }

1;

__END__

=head1 NAME

Test::Builder - Backend for building test libraries

=head1 SYNOPSIS

  package My::Test::Module;
  use base 'Test::Builder::Module';

  my $CLASS = __PACKAGE__;

  sub ok {
      my($test, $name) = @_;
      my $tb = $CLASS-&gt;builder;

      $tb-&gt;ok($test, $name);
  }


=head1 DESCRIPTION

L&lt;Test::Simple&gt; and L&lt;Test::More&gt; have proven to be popular testing modules,
but they're not always flexible enough.  Test::Builder provides a
building block upon which to write your own test libraries I&lt;which can
work together&gt;.

=head2 Construction

=over 4

=item B&lt;new&gt;

  my $Test = Test::Builder-&gt;new;

Returns a Test::Builder object representing the current state of the
test.

Since you only run one test per program C&lt;new&gt; always returns the same
Test::Builder object.  No matter how many times you call C&lt;new()&gt;, you're
getting the same object.  This is called a singleton.  This is done so that
multiple modules share such global information as the test counter and
where test output is going.

If you want a completely new Test::Builder object different from the
singleton, use C&lt;create&gt;.

=item B&lt;create&gt;

  my $Test = Test::Builder-&gt;create;

Ok, so there can be more than one Test::Builder object and this is how
you get it.  You might use this instead of C&lt;new()&gt; if you're testing
a Test::Builder based module, but otherwise you probably want C&lt;new&gt;.

B&lt;NOTE&gt;: the implementation is not complete.  C&lt;level&gt;, for example, is still
shared by B&lt;all&gt; Test::Builder objects, even ones created using this method.
Also, the method name may change in the future.

=item B&lt;subtest&gt;

    $builder-&gt;subtest($name, \&amp;subtests, @args);

See documentation of C&lt;subtest&gt; in Test::More.

C&lt;subtest&gt; also, and optionally, accepts arguments which will be passed to the
subtests reference.

=item B&lt;name&gt;

 diag $builder-&gt;name;

Returns the name of the current builder.  Top level builders default to C&lt;$0&gt;
(the name of the executable).  Child builders are named via the C&lt;child&gt;
method.  If no name is supplied, will be named "Child of $parent-&gt;name".

=item B&lt;reset&gt;

  $Test-&gt;reset;

Reinitializes the Test::Builder singleton to its original state.
Mostly useful for tests run in persistent environments where the same
test might be run multiple times in the same process.

=back

=head2 Setting up tests

These methods are for setting up tests and declaring how many there
are.  You usually only want to call one of these methods.

=over 4

=item B&lt;plan&gt;

  $Test-&gt;plan('no_plan');
  $Test-&gt;plan( skip_all =&gt; $reason );
  $Test-&gt;plan( tests =&gt; $num_tests );

A convenient way to set up your tests.  Call this and Test::Builder
will print the appropriate headers and take the appropriate actions.

If you call C&lt;plan()&gt;, don't call any of the other methods below.

=item B&lt;expected_tests&gt;

    my $max = $Test-&gt;expected_tests;
    $Test-&gt;expected_tests($max);

Gets/sets the number of tests we expect this test to run and prints out
the appropriate headers.


=item B&lt;no_plan&gt;

  $Test-&gt;no_plan;

Declares that this test will run an indeterminate number of tests.


=item B&lt;done_testing&gt;

  $Test-&gt;done_testing();
  $Test-&gt;done_testing($num_tests);

Declares that you are done testing, no more tests will be run after this point.

If a plan has not yet been output, it will do so.

$num_tests is the number of tests you planned to run.  If a numbered
plan was already declared, and if this contradicts, a failing test
will be run to reflect the planning mistake.  If C&lt;no_plan&gt; was declared,
this will override.

If C&lt;done_testing()&gt; is called twice, the second call will issue a
failing test.

If C&lt;$num_tests&gt; is omitted, the number of tests run will be used, like
no_plan.

C&lt;done_testing()&gt; is, in effect, used when you'd want to use C&lt;no_plan&gt;, but
safer. You'd use it like so:

    $Test-&gt;ok($a == $b);
    $Test-&gt;done_testing();

Or to plan a variable number of tests:

    for my $test (@tests) {
        $Test-&gt;ok($test);
    }
    $Test-&gt;done_testing(scalar @tests);


=item B&lt;has_plan&gt;

  $plan = $Test-&gt;has_plan

Find out whether a plan has been defined. C&lt;$plan&gt; is either C&lt;undef&gt; (no plan
has been set), C&lt;no_plan&gt; (indeterminate # of tests) or an integer (the number
of expected tests).

=item B&lt;skip_all&gt;

  $Test-&gt;skip_all;
  $Test-&gt;skip_all($reason);

Skips all the tests, using the given C&lt;$reason&gt;.  Exits immediately with 0.

=item B&lt;exported_to&gt;

  my $pack = $Test-&gt;exported_to;
  $Test-&gt;exported_to($pack);

Tells Test::Builder what package you exported your functions to.

This method isn't terribly useful since modules which share the same
Test::Builder object might get exported to different packages and only
the last one will be honored.

=back

=head2 Running tests

These actually run the tests, analogous to the functions in Test::More.

They all return true if the test passed, false if the test failed.

C&lt;$name&gt; is always optional.

=over 4

=item B&lt;ok&gt;

  $Test-&gt;ok($test, $name);

Your basic test.  Pass if C&lt;$test&gt; is true, fail if $test is false.  Just
like Test::Simple's C&lt;ok()&gt;.

=item B&lt;is_eq&gt;

  $Test-&gt;is_eq($got, $expected, $name);

Like Test::More's C&lt;is()&gt;.  Checks if C&lt;$got eq $expected&gt;.  This is the
string version.

C&lt;undef&gt; only ever matches another C&lt;undef&gt;.

=item B&lt;is_num&gt;

  $Test-&gt;is_num($got, $expected, $name);

Like Test::More's C&lt;is()&gt;.  Checks if C&lt;$got == $expected&gt;.  This is the
numeric version.

C&lt;undef&gt; only ever matches another C&lt;undef&gt;.

=item B&lt;isnt_eq&gt;

  $Test-&gt;isnt_eq($got, $dont_expect, $name);

Like L&lt;Test::More&gt;'s C&lt;isnt()&gt;.  Checks if C&lt;$got ne $dont_expect&gt;.  This is
the string version.

=item B&lt;isnt_num&gt;

  $Test-&gt;isnt_num($got, $dont_expect, $name);

Like L&lt;Test::More&gt;'s C&lt;isnt()&gt;.  Checks if C&lt;$got ne $dont_expect&gt;.  This is
the numeric version.

=item B&lt;like&gt;

  $Test-&gt;like($thing, qr/$regex/, $name);
  $Test-&gt;like($thing, '/$regex/', $name);

Like L&lt;Test::More&gt;'s C&lt;like()&gt;.  Checks if $thing matches the given C&lt;$regex&gt;.

=item B&lt;unlike&gt;

  $Test-&gt;unlike($thing, qr/$regex/, $name);
  $Test-&gt;unlike($thing, '/$regex/', $name);

Like L&lt;Test::More&gt;'s C&lt;unlike()&gt;.  Checks if $thing B&lt;does not match&gt; the
given C&lt;$regex&gt;.

=item B&lt;cmp_ok&gt;

  $Test-&gt;cmp_ok($thing, $type, $that, $name);

Works just like L&lt;Test::More&gt;'s C&lt;cmp_ok()&gt;.

    $Test-&gt;cmp_ok($big_num, '!=', $other_big_num);

=back

=head2 Other Testing Methods

These are methods which are used in the course of writing a test but are not themselves tests.

=over 4

=item B&lt;BAIL_OUT&gt;

    $Test-&gt;BAIL_OUT($reason);

Indicates to the L&lt;Test::Harness&gt; that things are going so badly all
testing should terminate.  This includes running any additional test
scripts.

It will exit with 255.

=for deprecated
BAIL_OUT() used to be BAILOUT()

=item B&lt;skip&gt;

    $Test-&gt;skip;
    $Test-&gt;skip($why);

Skips the current test, reporting C&lt;$why&gt;.

=item B&lt;todo_skip&gt;

  $Test-&gt;todo_skip;
  $Test-&gt;todo_skip($why);

Like C&lt;skip()&gt;, only it will declare the test as failing and TODO.  Similar
to

    print "not ok $tnum # TODO $why\n";

=begin _unimplemented

=item B&lt;skip_rest&gt;

  $Test-&gt;skip_rest;
  $Test-&gt;skip_rest($reason);

Like C&lt;skip()&gt;, only it skips all the rest of the tests you plan to run
and terminates the test.

If you're running under C&lt;no_plan&gt;, it skips once and terminates the
test.

=end _unimplemented

=back


=head2 Test building utility methods

These methods are useful when writing your own test methods.

=over 4

=item B&lt;maybe_regex&gt;

  $Test-&gt;maybe_regex(qr/$regex/);
  $Test-&gt;maybe_regex('/$regex/');

This method used to be useful back when Test::Builder worked on Perls
before 5.6 which didn't have qr//.  Now its pretty useless.

Convenience method for building testing functions that take regular
expressions as arguments.

Takes a quoted regular expression produced by C&lt;qr//&gt;, or a string
representing a regular expression.

Returns a Perl value which may be used instead of the corresponding
regular expression, or C&lt;undef&gt; if its argument is not recognized.

For example, a version of C&lt;like()&gt;, sans the useful diagnostic messages,
could be written as:

  sub laconic_like {
      my ($self, $thing, $regex, $name) = @_;
      my $usable_regex = $self-&gt;maybe_regex($regex);
      die "expecting regex, found '$regex'\n"
          unless $usable_regex;
      $self-&gt;ok($thing =~ m/$usable_regex/, $name);
  }


=item B&lt;is_fh&gt;

    my $is_fh = $Test-&gt;is_fh($thing);

Determines if the given C&lt;$thing&gt; can be used as a filehandle.

=cut


=back


=head2 Test style


=over 4

=item B&lt;level&gt;

    $Test-&gt;level($how_high);

How far up the call stack should C&lt;$Test&gt; look when reporting where the
test failed.

Defaults to 1.

Setting C&lt;$Test::Builder::Level&gt; overrides.  This is typically useful
localized:

    sub my_ok {
        my $test = shift;

        local $Test::Builder::Level = $Test::Builder::Level + 1;
        $TB-&gt;ok($test);
    }

To be polite to other functions wrapping your own you usually want to increment C&lt;$Level&gt; rather than set it to a constant.

=item B&lt;use_numbers&gt;

    $Test-&gt;use_numbers($on_or_off);

Whether or not the test should output numbers.  That is, this if true:

  ok 1
  ok 2
  ok 3

or this if false

  ok
  ok
  ok

Most useful when you can't depend on the test output order, such as
when threads or forking is involved.

Defaults to on.

=item B&lt;no_diag&gt;

    $Test-&gt;no_diag($no_diag);

If set true no diagnostics will be printed.  This includes calls to
C&lt;diag()&gt;.

=item B&lt;no_ending&gt;

    $Test-&gt;no_ending($no_ending);

Normally, Test::Builder does some extra diagnostics when the test
ends.  It also changes the exit code as described below.

If this is true, none of that will be done.

=item B&lt;no_header&gt;

    $Test-&gt;no_header($no_header);

If set to true, no "1..N" header will be printed.

=back

=head2 Output

Controlling where the test output goes.

It's ok for your test to change where STDOUT and STDERR point to,
Test::Builder's default output settings will not be affected.

=over 4

=item B&lt;diag&gt;

    $Test-&gt;diag(@msgs);

Prints out the given C&lt;@msgs&gt;.  Like C&lt;print&gt;, arguments are simply
appended together.

Normally, it uses the C&lt;failure_output()&gt; handle, but if this is for a
TODO test, the C&lt;todo_output()&gt; handle is used.

Output will be indented and marked with a # so as not to interfere
with test output.  A newline will be put on the end if there isn't one
already.

We encourage using this rather than calling print directly.

Returns false.  Why?  Because C&lt;diag()&gt; is often used in conjunction with
a failing test (C&lt;ok() || diag()&gt;) it "passes through" the failure.

    return ok(...) || diag(...);

=for blame transfer
Mark Fowler &lt;mark@twoshortplanks.com&gt;

=item B&lt;note&gt;

    $Test-&gt;note(@msgs);

Like C&lt;diag()&gt;, but it prints to the C&lt;output()&gt; handle so it will not
normally be seen by the user except in verbose mode.

=item B&lt;explain&gt;

    my @dump = $Test-&gt;explain(@msgs);

Will dump the contents of any references in a human readable format.
Handy for things like...

    is_deeply($have, $want) || diag explain $have;

or

    is_deeply($have, $want) || note explain $have;

=item B&lt;output&gt;

=item B&lt;failure_output&gt;

=item B&lt;todo_output&gt;

    my $filehandle = $Test-&gt;output;
    $Test-&gt;output($filehandle);
    $Test-&gt;output($filename);
    $Test-&gt;output(\$scalar);

These methods control where Test::Builder will print its output.
They take either an open C&lt;$filehandle&gt;, a C&lt;$filename&gt; to open and write to
or a C&lt;$scalar&gt; reference to append to.  It will always return a C&lt;$filehandle&gt;.

B&lt;output&gt; is where normal "ok/not ok" test output goes.

Defaults to STDOUT.

B&lt;failure_output&gt; is where diagnostic output on test failures and
C&lt;diag()&gt; goes.  It is normally not read by Test::Harness and instead is
displayed to the user.

Defaults to STDERR.

C&lt;todo_output&gt; is used instead of C&lt;failure_output()&gt; for the
diagnostics of a failing TODO test.  These will not be seen by the
user.

Defaults to STDOUT.

=item reset_outputs

  $tb-&gt;reset_outputs;

Resets all the output filehandles back to their defaults.

=item carp

  $tb-&gt;carp(@message);

Warns with C&lt;@message&gt; but the message will appear to come from the
point where the original test function was called (C&lt;&lt; $tb-&gt;caller &gt;&gt;).

=item croak

  $tb-&gt;croak(@message);

Dies with C&lt;@message&gt; but the message will appear to come from the
point where the original test function was called (C&lt;&lt; $tb-&gt;caller &gt;&gt;).


=back


=head2 Test Status and Info

=over 4

=item B&lt;no_log_results&gt;

This will turn off result long-term storage. Calling this method will make
C&lt;details&gt; and C&lt;summary&gt; useless. You may want to use this if you are running
enough tests to fill up all available memory.

    Test::Builder-&gt;new-&gt;no_log_results();

There is no way to turn it back on.

=item B&lt;current_test&gt;

    my $curr_test = $Test-&gt;current_test;
    $Test-&gt;current_test($num);

Gets/sets the current test number we're on.  You usually shouldn't
have to set this.

If set forward, the details of the missing tests are filled in as 'unknown'.
if set backward, the details of the intervening tests are deleted.  You
can erase history if you really want to.


=item B&lt;is_passing&gt;

   my $ok = $builder-&gt;is_passing;

Indicates if the test suite is currently passing.

More formally, it will be false if anything has happened which makes
it impossible for the test suite to pass.  True otherwise.

For example, if no tests have run C&lt;is_passing()&gt; will be true because
even though a suite with no tests is a failure you can add a passing
test to it and start passing.

Don't think about it too much.


=item B&lt;summary&gt;

    my @tests = $Test-&gt;summary;

A simple summary of the tests so far.  True for pass, false for fail.
This is a logical pass/fail, so todos are passes.

Of course, test #1 is $tests[0], etc...


=item B&lt;details&gt;

    my @tests = $Test-&gt;details;

Like C&lt;summary()&gt;, but with a lot more detail.

    $tests[$test_num - 1] =
            { 'ok'       =&gt; is the test considered a pass?
              actual_ok  =&gt; did it literally say 'ok'?
              name       =&gt; name of the test (if any)
              type       =&gt; type of test (if any, see below).
              reason     =&gt; reason for the above (if any)
            };

'ok' is true if Test::Harness will consider the test to be a pass.

'actual_ok' is a reflection of whether or not the test literally
printed 'ok' or 'not ok'.  This is for examining the result of 'todo'
tests.

'name' is the name of the test.

'type' indicates if it was a special test.  Normal tests have a type
of ''.  Type can be one of the following:

    skip        see skip()
    todo        see todo()
    todo_skip   see todo_skip()
    unknown     see below

Sometimes the Test::Builder test counter is incremented without it
printing any test output, for example, when C&lt;current_test()&gt; is changed.
In these cases, Test::Builder doesn't know the result of the test, so
its type is 'unknown'.  These details for these tests are filled in.
They are considered ok, but the name and actual_ok is left C&lt;undef&gt;.

For example "not ok 23 - hole count # TODO insufficient donuts" would
result in this structure:

    $tests[22] =    # 23 - 1, since arrays start from 0.
      { ok        =&gt; 1,   # logically, the test passed since its todo
        actual_ok =&gt; 0,   # in absolute terms, it failed
        name      =&gt; 'hole count',
        type      =&gt; 'todo',
        reason    =&gt; 'insufficient donuts'
      };


=item B&lt;todo&gt;

    my $todo_reason = $Test-&gt;todo;
    my $todo_reason = $Test-&gt;todo($pack);

If the current tests are considered "TODO" it will return the reason,
if any.  This reason can come from a C&lt;$TODO&gt; variable or the last call
to C&lt;todo_start()&gt;.

Since a TODO test does not need a reason, this function can return an
empty string even when inside a TODO block.  Use C&lt;&lt; $Test-&gt;in_todo &gt;&gt;
to determine if you are currently inside a TODO block.

C&lt;todo()&gt; is about finding the right package to look for C&lt;$TODO&gt; in.  It's
pretty good at guessing the right package to look at.  It first looks for
the caller based on C&lt;$Level + 1&gt;, since C&lt;todo()&gt; is usually called inside
a test function.  As a last resort it will use C&lt;exported_to()&gt;.

Sometimes there is some confusion about where C&lt;todo()&gt; should be looking
for the C&lt;$TODO&gt; variable.  If you want to be sure, tell it explicitly
what $pack to use.

=item B&lt;find_TODO&gt;

    my $todo_reason = $Test-&gt;find_TODO();
    my $todo_reason = $Test-&gt;find_TODO($pack);

Like C&lt;todo()&gt; but only returns the value of C&lt;$TODO&gt; ignoring
C&lt;todo_start()&gt;.

Can also be used to set C&lt;$TODO&gt; to a new value while returning the
old value:

    my $old_reason = $Test-&gt;find_TODO($pack, 1, $new_reason);

=item B&lt;in_todo&gt;

    my $in_todo = $Test-&gt;in_todo;

Returns true if the test is currently inside a TODO block.

=item B&lt;todo_start&gt;

    $Test-&gt;todo_start();
    $Test-&gt;todo_start($message);

This method allows you declare all subsequent tests as TODO tests, up until
the C&lt;todo_end&gt; method has been called.

The C&lt;TODO:&gt; and C&lt;$TODO&gt; syntax is generally pretty good about figuring out
whether or not we're in a TODO test.  However, often we find that this is not
possible to determine (such as when we want to use C&lt;$TODO&gt; but
the tests are being executed in other packages which can't be inferred
beforehand).

Note that you can use this to nest "todo" tests

 $Test-&gt;todo_start('working on this');
 # lots of code
 $Test-&gt;todo_start('working on that');
 # more code
 $Test-&gt;todo_end;
 $Test-&gt;todo_end;

This is generally not recommended, but large testing systems often have weird
internal needs.

We've tried to make this also work with the TODO: syntax, but it's not
guaranteed and its use is also discouraged:

 TODO: {
     local $TODO = 'We have work to do!';
     $Test-&gt;todo_start('working on this');
     # lots of code
     $Test-&gt;todo_start('working on that');
     # more code
     $Test-&gt;todo_end;
     $Test-&gt;todo_end;
 }

Pick one style or another of "TODO" to be on the safe side.


=item C&lt;todo_end&gt;

 $Test-&gt;todo_end;

Stops running tests as "TODO" tests.  This method is fatal if called without a
preceding C&lt;todo_start&gt; method call.

=item B&lt;caller&gt;

    my $package = $Test-&gt;caller;
    my($pack, $file, $line) = $Test-&gt;caller;
    my($pack, $file, $line) = $Test-&gt;caller($height);

Like the normal C&lt;caller()&gt;, except it reports according to your C&lt;level()&gt;.

C&lt;$height&gt; will be added to the C&lt;level()&gt;.

If C&lt;caller()&gt; winds up off the top of the stack it report the highest context.

=back

=head1 EXIT CODES

If all your tests passed, Test::Builder will exit with zero (which is
normal).  If anything failed it will exit with how many failed.  If
you run less (or more) tests than you planned, the missing (or extras)
will be considered failures.  If no tests were ever run Test::Builder
will throw a warning and exit with 255.  If the test died, even after
having successfully completed all its tests, it will still be
considered a failure and will exit with 255.

So the exit codes are...

    0                   all tests successful
    255                 test died or all passed but wrong # of tests run
    any other number    how many failed (including missing or extras)

If you fail more than 254 tests, it will be reported as 254.

=head1 THREADS

In perl 5.8.1 and later, Test::Builder is thread-safe.  The test number is
shared by all threads.  This means if one thread sets the test number using
C&lt;current_test()&gt; they will all be effected.

While versions earlier than 5.8.1 had threads they contain too many
bugs to support.

Test::Builder is only thread-aware if threads.pm is loaded I&lt;before&gt;
Test::Builder.

You can directly disable thread support with one of the following:

    $ENV{T2_NO_IPC} = 1

or

    no Test2::IPC;

or

    Test2::API::test2_ipc_disable()

=head1 MEMORY

An informative hash, accessible via C&lt;details()&gt;, is stored for each
test you perform.  So memory usage will scale linearly with each test
run. Although this is not a problem for most test suites, it can
become an issue if you do large (hundred thousands to million)
combinatorics tests in the same run.

In such cases, you are advised to either split the test file into smaller
ones, or use a reverse approach, doing "normal" (code) compares and
triggering C&lt;fail()&gt; should anything go unexpected.

Future versions of Test::Builder will have a way to turn history off.


=head1 EXAMPLES

CPAN can provide the best examples.  L&lt;Test::Simple&gt;, L&lt;Test::More&gt;,
L&lt;Test::Exception&gt; and L&lt;Test::Differences&gt; all use Test::Builder.

=head1 SEE ALSO

L&lt;Test::Simple&gt;, L&lt;Test::More&gt;, L&lt;Test::Harness&gt;

=head1 AUTHORS

Original code by chromatic, maintained by Michael G Schwern
E&lt;lt&gt;schwern@pobox.comE&lt;gt&gt;

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2002-2008 by chromatic E&lt;lt&gt;chromatic@wgz.orgE&lt;gt&gt; and
                       Michael G Schwern E&lt;lt&gt;schwern@pobox.comE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://www.perl.com/perl/misc/Artistic.html&gt;
</pre></body></html>