Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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';

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.


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 ( at line 37) # got: 'DIED: Illegal division by zero at 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.


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

In reply to Test::Exception extension with (evil?) overloading by adrianh

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the fog begins to lift...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (6)
    As of 2017-12-13 06:15 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (345 votes). Check out past polls.