Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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.


Comment on Re^2: Test::Exception extension with (evil?) overloading
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://228026]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-10-31 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (219 votes), past polls