Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Test::Exception extension with (evil?) overloading

by John M. Dlugosz (Monsignor)
on Jan 18, 2003 at 02:06 UTC ( #227897=note: print w/replies, xml ) Need Help??

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

I've only just started looking into Test::More and its kin, but the only other idea I have off hand is to pass a closure to a test function, to defer calling it until after the reusable code can throw an eval around it. That's what you mean by lives_and_is, right? Your complaint is that you have to duplicate all the tests to have a "lives" version.

So, you want to wrap the sub-ref argument so that it can orthogonally apply to all the tests.

Another way to have a modifier that applies orthogonally to all tests would be to pass the underlying test as well:

wrapper (\&innertest, \&subref, @args)
This would evaluate subref in an eval, failing if it dies. Then, take the result and pass it, along with the remaining @args, to the innertest function, whose result is returned.

So you could pass anything you want as the inner test.

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

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

    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.

      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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://227897]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-01-21 20:51 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (186 votes). Check out past polls.