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

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

by Aristotle (Chancellor)
on Jan 18, 2003 at 20:40 UTC ( #228055=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: Test::Exception extension with (evil?) overloading
Select or Download Code
Re^4: Test::Exception extension with (evil?) overloading
by adrianh (Chancellor) on Jan 18, 2003 at 21:50 UTC
    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.

        Ohhhh! Much better. With a tiny piece of magic we can get rid of that shift too.

        sub lives_and (&$) { my ($test, $name) = @_; local $Test::Builder::Level = $Test::Builder::Level+1; { my $ok = \&Test::Builder::ok; no warnings; local *Test::Builder::ok = sub { $_[2] = $name unless defined $_[2]; $ok->(@_); }; use warnings; eval { $test->() } and return 1; }; if ($@) { fail($name); diag("Died: $@"); }; return; }

        Which allows us to do:

        lives_and { is 42, 42 } 'answer is 42'; lives_and { is 24, 42 } 'answer is 42'; lives_and { is sub { die 'oops' }->(), 42 } 'answer is 42';

        giving us

        ok 1 - answer is 42 + not ok 2 - answer is 42 + # Failed test (foo.pl at line 29) + # got: '24' + # expected: '42' + not ok 3 - answer is 42 + # Failed test (foo.pl at line 31) + # Died: oops at foo.pl line 31.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-07-05 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls