Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Test2/Tools/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sub dies(&) {

return undef if $ok;

unless ($err) {
unless (ref($err) or $err) {
my $ctx = context();
$ctx->alert("Got exception as expected, but exception is falsy (undef, '', or 0)...");
$ctx->release;
Expand Down Expand Up @@ -139,6 +139,12 @@ disagree with this, and think the actual line of the failing test is
more important. Ultimately, though L<Test::Fatal> cannot be changed, people
probably already depend on that behavior.

L<Test::Fatal> will die if the exception is a false value (C<undef>, C<''>, or
C<0>). C<dies()> will instead warn and return the false exception. Additionally,
C<dies()> will B<not> warn when the exception is a blessed object that evaluates
to false (e.g. via overloaded boolification), since this is a legitimate use
case for objects that use boolean overloading to indicate success or failure.

=head1 SOURCE

The source code repository for Test2-Suite can be found at
Expand Down
13 changes: 13 additions & 0 deletions t/modules/Tools/Exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ is(

like($err, qr/abc/, '$@ has the exception');

# Test that blessed objects overloading to false do not trigger the falsy warning
{
package FalseException;
use overload bool => sub { 0 }, '""' => sub { "false exception object" }, fallback => 1;
sub new { bless {}, shift }
}

my $false_obj = FalseException->new();

my $got;
is(warning { $got = dies { die $false_obj } }, undef, "no warning for blessed false exception");
isa_ok($got, ['FalseException'], "dies() returns the blessed false exception object");

like(
warning { dies { 1 } },
qr/Useless use of dies\(\) in void context/,
Expand Down