Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-Fatal CPAN distribution.

Report information
The Basics
Id: 67598
Status: resolved
Priority: 0/
Queue: Test-Fatal

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Please provide Test::Exception-like primatives
I find most of my uses of Test::Exception were simply calls that look like: dies_ok( sub { foo() }, 'foo dies' ); Rather than replace them with ok( exception { foo() }, 'foo dies' ); would it be possible for use Test::Fatal; to provide a dies_ok() function like Test::Exception did? This would simplify the migration path from Exception to Fatal, because most callsites would not need to change. -- Paul Evans
Subject: [PATCH] Provide Test::Exception-like primatives
Attached patch works-for-me to add dies_ok / lives_ok. -- Paul Evans
Subject: rt67598.patch
=== 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" );
merged -- rjbs