in reply to Re: Test::Exception extension with (evil?) overloading
in thread Test::Exception extension with (evil?) overloading

Good option. Only downside is that you lose the prototyping of the test functions, and the calling syntax is a little more complex (in my opinion).

You would also need to have it as:

wrapper (\&is, sub {$o->answer}, 42, 'answer returned 42')

(we can only prototype the sub away if the coderef is the first argument)

One other problem would be in figuring out the test name to output when an exception is thrown. Since it's optional for test subs it may, or may not, be the last argument.

Replies are listed 'Best First'.
Re^3: Test::Exception extension with (evil?) overloading
by Aristotle (Chancellor) on Jan 18, 2003 at 20:40 UTC

    In my other post I stopped just short of proposing

    sub lives_and_tests_ok (&&;$) { my ($case, $test, $name) = @_; local $@; my $result = eval { $case->() }; $@ ? fail($name) : $test->($result, $name); }

    because it’s somewhat confusing to read the following:

    lives_and_tests_ok ( sub { $o->answer }, sub { is shift, 42, shift }, "answer is 42" );

    I briefly pondered

    sub lives_and_tests_ok (&&;$) { my ($case, $test, $name) = @_; local $@; local ($b, $a) = ($name, eval { $case->() }); $@ ? fail($name) : $test->($result, $name); }

    but I don’t think it’s any more readable than

    lives_and_tests_ok ( sub { $o->answer }, sub { is $a, 42, $b }, "answer is 42" );

    Makeshifts last the longest.

      lives_and_tests_ok ( sub { $o->answer }, sub { is $a, 42, $b }, "answer is 42" );

      I quite like this one actually :-) However, is there some problem with:

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

      that I'm missing?

        None syntactically, but the amount of required magic feels like a kludge.

        But I just thought of something - how about:

        sub lives_and (&$) { my ( $test, $name ) = @_; local $@; eval { $test->() } and return 1; fail $name; diag "Died: $@"; return; } lives_and { is $o->answer(), 42, shift } 'answer is 42';

        Makeshifts last the longest.