Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

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


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

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.


Comment on Re^6: Test::Exception extension with (evil?) overloading
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2014-12-26 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls