Attached patch works-for-me to add dies_ok / lives_ok.
--
Paul Evans
=== modified file 'lib/Test/Fatal.pm'
--- lib/Test/Fatal.pm 2011-05-26 13:36:59 +0000
+++ lib/Test/Fatal.pm 2011-05-26 14:04:03 +0000
@@ -13,7 +13,7 @@
use Exporter 5.59 'import';
our @EXPORT = qw(exception);
-our @EXPORT_OK = qw(exception success);
+our @EXPORT_OK = qw(exception success dies_ok lives_ok);
sub exception (&;@) {
@@ -39,6 +39,34 @@
}, @_ );
}
+
+my $Tester;
+
+# Signature should match that of Test::Exception
+sub dies_ok (&;$) {
+ my $code = shift;
+ my $name = shift;
+
+ require Test::Builder;
+ $Tester ||= Test::Builder->new;
+
+ my $ok = $Tester->ok( exception( \&$code ), $name );
+ $ok or $Tester->diag( "expected an exception but none was raised" );
+ return $ok;
+}
+
+sub lives_ok (&;$) {
+ my $code = shift;
+ my $name = shift;
+
+ require Test::Builder;
+ $Tester ||= Test::Builder->new;
+
+ my $ok = $Tester->ok( !exception( \&$code ), $name );
+ $ok or $Tester->diag( "expected return but an exception was raised" );
+ return $ok;
+}
+
1;
__END__
@@ -103,7 +131,8 @@
Note that there is no TAP assert being performed. In other words, no "ok" or
"not ok" line is emitted. It's up to you to use the rest of C<exception> in an
-existing test like C<ok>, C<isa_ok>, C<is>, et cetera.
+existing test like C<ok>, C<isa_ok>, C<is>, et cetera. Or you may wish to use
+the C<dies_ok> and C<lives_ok> wrappers, which do provide TAP output.
C<exception> does I<not> alter the stack presented to the called block, meaning
that if the exception returned has a stack trace, it will include some frames
@@ -128,10 +157,28 @@
Although almost any needed exception tests can be performed with C<exception>,
success blocks may sometimes help organize complex testing.
+=head2 dies_ok
+
+=head2 lives_ok
+
+Exported only by request, these two functions run a given block of code, and
+provide TAP output indicating if it did, or did not throw an exception.
+These provide an easy upgrade path for replacing existing unit tests based on
+C<Test::Exception>.
+
+ use Test::More tests => 2;
+ use Test::Fatal qw(dies_ok lives_ok);
+
+ dies_ok { die "I failed" } 'code that fails';
+
+ lives_ok { return "I'm still alive" } 'code that does not fail';
+
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
+C<dies_ok> and C<lives_ok> by Paul Evans <leonerd@leonerd.org.uk>
+
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Ricardo Signes.
=== added file 't/like-exception.t'
--- t/like-exception.t 1970-01-01 00:00:00 +0000
+++ t/like-exception.t 2011-05-26 14:04:03 +0000
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use strict;
+
+use Test::Builder::Tester tests => 4;
+
+use Test::Fatal qw( dies_ok lives_ok );
+
+test_out( "ok 1 - died" );
+dies_ok { die "FAIL" } 'died';
+test_test( "die dies" );
+
+test_out( "not ok 1 - returned" );
+test_fail( +2 );
+test_err( "# expected an exception but none was raised" );
+dies_ok { return 1 } 'returned';
+test_test( "return doesn't die" );
+
+test_out( "ok 1 - returned" );
+lives_ok { return 1 } 'returned';
+test_test( "return lived" );
+
+test_out( "not ok 1 - died" );
+test_fail( +2 );
+test_err( "# expected return but an exception was raised" );
+lives_ok { die "FAIL" } 'died';
+test_test( "die doesn't live" );