Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

by adrianh (Chancellor)
on Jan 18, 2003 at 18:51 UTC ( #228026=note: print w/replies, xml ) Need Help??

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2018-03-19 17:17 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (245 votes). Check out past polls.