http://qs321.pair.com?node_id=227879

adrianh has asked for the wisdom of the Perl Monks concerning the following question:

I often find myself doing things like this when testing exception-based code.

use Test::More 'no_plan'; use Test::Exception;

my $result = lives_ok {$o->answer} 'answer worked';
is $result, 42, 'answer returned 42';

Update:
my $result; lives_ok {$result = $o->answer} 'answer worked'; is $result, 42, 'answer returned 42';

This is a bit clumsy since to me it is conceptually one test ($o->answer does not throw an exception and returns 42).

Of course, you can write a custom test subroutine lives_and_is, but you can very quickly end up writing lives_and_isnt, lives_and_is_deeply, etc.

Is there a more generic way?

I'm considering adding a live subroutine to Test::Exception that you can use like this:

is live{$o->answer}, 42, 'answer worked';

Implemented by, when an exception is thrown, returning an object that overloads all the comparison operations to return false.

Documentation:

live Returns the result of the given expression if no exception is thrown. If an exception is thrown an object is returned that evaluates to false when used in any standard boolean test or comparison operation ("<", "<=", ">", ">=", "==", "!=", "lt", "le", "gt", "ge", "eq" and "ne"). This can be used to simplify testing for exceptions. For example: foreach my $n (3,2,1,0) { is live{$n/$n}, 1, "$n/$n == 1"; }; will produce ok 1 - 3/3 == 1 ok 2 - 2/2 == 1 ok 3 - 1/1 == 1 not ok 4 - 0/0 == 1 # Failed test (test.pl at line 37) # got: 'DIED: Illegal division by zero at test.pl line 37 +.' # expected: '1' NOTE: that it is an object not a false value that is returned when an exception occurred. # this will pass even if foo dies ok(refaddr live {$o->foo}, 'foo returns object'; # this will pass even if foo dies unlike live {$o->foo}, '/fribble/', 'no fribble'; Appropriate care must be taken.

Implementation:

use Sub::Uplevel; sub _exception_as_string { my $exception = shift; my $class = ref($exception); $exception = "$class ($exception)" if $class && "$exception" !~ m/^\Q$class/; chomp($exception); return($exception); }; { package Test::Exception::_NULL; use overload q{""} => sub { "${$_[0]}" }, map { $_ => sub { return } } ( qw( < <= > >= == != lt le gt ge eq ne bool ) ), fallback => 1; sub new { bless \$_[1], $_[0] }; }; sub live (&) { my $coderef = shift; my $value = eval { uplevel 2, $coderef }; $value = Test::Exception::_NULL->new( "DIED: ". _exception_as_string($@) ) if $@; $value; };

Sound sane? Or is Test::Exception::_NULL too evil to be allowed out in public?

Alternate suggestions?

Update per ER request - dvergin 2003-01-23