1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
use strict;
use warnings;
BEGIN { $Test2::API::DO_DEPTH_CHECK = 1 }
use Test2::Tools::Tiny;
use Test2::API qw/context/;
skip_all("known to fail on $]") if $] le "5.006002";
sub outer {
my $code = shift;
my $ctx = context();
$ctx->note("outer");
my $out = eval { $code->() };
$ctx->release;
return $out;
}
sub dies {
my $ctx = context();
$ctx->note("dies");
die "Foo";
}
sub bad_store {
my $ctx = context();
$ctx->note("bad store");
return $ctx; # Emulate storing it somewhere
}
sub bad_simple {
my $ctx = context();
$ctx->note("bad simple");
return;
}
my @warnings;
{
local $SIG{__WARN__} = sub { push @warnings => @_ };
eval { dies() };
}
ok(!@warnings, "no warnings") || diag @warnings;
@warnings = ();
my $keep = bad_store();
eval { my $x = 1 }; # Ensure an eval changing $@ does not meddle.
{
local $SIG{__WARN__} = sub { push @warnings => @_ };
ok(1, "random event");
}
ok(@warnings, "got warnings");
like(
$warnings[0],
qr/context\(\) was called to retrieve an existing context/,
"got expected warning"
);
$keep = undef;
{
@warnings = ();
local $SIG{__WARN__} = sub { push @warnings => @_ };
bad_simple();
}
ok(@warnings, "got warnings");
like(
$warnings[0],
qr/A context appears to have been destroyed without first calling release/,
"got expected warning"
);
@warnings = ();
outer(\&dies);
{
local $SIG{__WARN__} = sub { push @warnings => @_ };
ok(1, "random event");
}
ok(!@warnings, "no warnings") || diag @warnings;
@warnings = ();
{
local $SIG{__WARN__} = sub { push @warnings => @_ };
outer(\&bad_store);
}
ok(@warnings, "got warnings");
like(
$warnings[0],
qr/A context appears to have been destroyed without first calling release/,
"got expected warning"
);
{
@warnings = ();
local $SIG{__WARN__} = sub { push @warnings => @_ };
outer(\&bad_simple);
}
ok(@warnings, "got warnings") || diag @warnings;
like(
$warnings[0],
qr/A context appears to have been destroyed without first calling release/,
"got expected warning"
);
done_testing;
|