Skip to content
Open
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
15 changes: 10 additions & 5 deletions lib/Test/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ sub throws_ok (&$;$) {
unless defined $description;
my $exception = _try_as_caller( $coderef );
my $regex = $Tester->maybe_regex( $expecting );
my $ok = $regex
? ( $exception =~ m/$regex/ )
: eval {
$exception->isa( ref $expecting ? ref $expecting : $expecting )
};
my $ok;
if ($exception) {
$ok = $regex
? ( $exception =~ m/$regex/ )
: eval {
$exception->isa( ref $expecting ? ref $expecting : $expecting )
};
} else {
$ok = 0;
}
$Tester->ok( $ok, $description );
unless ( $ok ) {
$Tester->diag( _exception_as_string( "expecting:", $expecting ) );
Expand Down
19 changes: 17 additions & 2 deletions t/throws_ok.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

use strict;
use warnings;
use Test::More tests => 2;
use Test::Builder;
use Test::Harness;
use Test::Builder::Tester tests => 4;
use Test::More;

BEGIN { use_ok( 'Test::Exception' ) };

eval { throws_ok {} undef };
like( $@, '/^throws_ok/', 'cannot pass undef to throws_ok' );
like( $@, '/^throws_ok/', 'cannot pass undef to throws_ok' );

{
test_out('not ok 1 - threw Regexp ((?^:Correct output from not throwing in a throws_ok))');
test_fail( +1 );
my $ok = throws_ok { 1 } qr/Correct output from not throwing in a throws_ok/;
test_test(name => 'Correct output from not throwing in a throws_ok', skip_err => 1);

ok(!$ok, 'Fail if you don\'t throw in a throws_ok');
}

done_testing;