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]
[Corion]: 1nickt: I bought a 2015 XPS 13, which is a very nice and shiney (and somewhat overpriced) machine, but I think it's still better priced than an Apple machine. Now it just has to hold up 10 years like the old one did ;)
[1nickt]: Ah, you got Linus' fave? With Ubuntu? 4k display?
[1nickt]: ( The 17" monster I just bought is for my wife, who doesn;t leave the house with it...)
[Corion]: 1nickt: No, I'm using it with Windows 10, but with the 4k display, yes. It's a very nice build IMO, and relatively sturdy. Also, it got 8 or 9 on the ifixit repair scale, which I consider good
[Corion]: ... especially as I expect to have to replace the battery set in two or three years' time

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2017-03-27 13:09 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.