Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Testing error handling that calls "die"

by davies (Prior)
on Dec 26, 2011 at 18:19 UTC ( [id://945118]=perlquestion: print w/replies, xml ) Need Help??

davies has asked for the wisdom of the Perl Monks concerning the following question:

When users are allowed to provide data to code, it can be taken for granted that they will louse up at some point. I'm trying to write code that will respond with an error message telling users that I can't proceed with the nonsense they have provided. The actual code looks something like if ($silly) {die "Stop messing around."}. This works fine, although the error message is more explicit. However, I want to test it, and the problem is that when I send the invalid parameters from the test module, it causes the test module to die. This isn't fine. I thought that I could trap the death with

my $errmsg = eval(SubCall($silly)); ok ("Stop messing around." eq $errmsg);

While I get the correct error message sent to the console, processing stops. PerlDoc talks about skipping tests, but that's not what I want to do - I actually want to send dud parameters and check that my code degrades gracefully. Test::Most has a lot of information on die, but the docs are very terse and seem to me to be about getting Test::Most to die, rather than about handling expected deaths. Is what I am trying to do possible?

Regards,

John Davies

Update: I tried the two solutions and found the same problem with the eval/$@/like solution as I had with my original code, namely that the test module died. But I didn't look very far as the Test::Exception solution worked fine. It didn't allow me to check that I was getting the right error message, but that's a minor detail. Tests pass and fail when I expect them to. Thank you both.

Replies are listed 'Best First'.
Re: Testing error handling that calls "die"
by moritz (Cardinal) on Dec 26, 2011 at 19:37 UTC
    my $errmsg = eval(SubCall($silly));

    There are two use cases of eval: eval BLOCK catches exceptions, and eval EXPR takes a string as the source of a Perl program, and executes it.

    You've accidentally used the latter, but you should be using the former. If you write eval(SubCall($silly)), the call to SubCall is not influenced by the eval at all, but rather the return value of the SubCall will be used as a string to be interpreted as Perl code.

    The correct solution is to just use the BLOCK form of eval:

    my $success = eval { SubCall($silly); 1 }; ok !$success && $@ eq $errmsg, 'died with the right error message';

    In Perl 6, the eval BLOCK form has been renamed to try, so there's less potential for confusion. It also works as a statement prefix without any block, so you could write

    my $success = try SubCall($silly); ok $!.defined && $! eq $errmsg, 'died with the right error message';

      I get it now. I didn't know about the two forms of eval, and that was why I couldn't get my original code or toolic's explanation to work. I will admit that I struggled for a while despite your explanation, as $success did not behave as I expected. I was not mentally prepared for it to return undef on an error and spent quite a while trying to work out what had gone wrong. Trying to debug a non-existent bug is not recommended. :-)

      One other thing surprised me slightly. Every use of ok I had seen previously uses brackets, but I notice that your examples do not. I have put brackets in for consistency, but is this just cargo cult? If not, what is the effect of using the brackets?

      Regards,

      John Davies

        One other thing surprised me slightly. Every use of ok I had seen previously uses brackets, but I notice that your examples do not. I have put brackets in for consistency, but is this just cargo cult? If not, what is the effect of using the brackets?

        The parenthesis after a subroutine name are another peculiarity of Perl 5 :-)

        When perl enounters an identifier like ok in the source code, it can interpret it in two ways: either as a "barword" (a string literal), or as the name of a subroutine.

        If perl knows about a subroutine of that name, it decides in favor of the subroutine interpretation, otherwise it chooses the bareword. If you put parenthesis after the identifier, it always chooses the subroutine.

        So, the parens are only needed if the subroutine is declared later in the file. Since you use Test::More; (or something similar) at the start of your test file, all the testing routines are immediately imported, and thus known as subroutines.

        Here are a few examples of the parsing differences:

        $ perl -le 'print STDOUT foo' # bareword foo $ perl -le 'use strict; print STDOUT foo' # strict disables barewords Bareword "foo" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. $ perl -e 'use strict; sub foo { }; foo' # works fine $ perl -e 'use strict; foo; sub foo { };' # error, foo is a bareword, +because it's not declared yet Bareword "foo" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. $ perl -e 'use strict; foo(); sub foo { };' # that's where you actuall +y need the parens; no error
Re: Testing error handling that calls "die"
by toolic (Bishop) on Dec 26, 2011 at 18:41 UTC
    I use eval, $@ and Test::More::like() in my CPAN modules. Here is a snippet from one of my tests:
    # Check error messages $@ = ''; eval { my $str = format_eng() }; like($@, qr/requires numeric input/, 'die if no input'); $@ = ''; eval { my $str = format_eng(' ') }; like($@, qr/not numeric/, 'die if input only has whitespace');
Re: Testing error handling that calls "die"
by kennethk (Abbot) on Dec 26, 2011 at 18:49 UTC
    In addition to toolic's suggestion, I've seen Test::Exception used pretty extensively. For example, it's in the test suite for Moose and Mouse. I haven't worked with it extensively and it's not in CORE, so I don't know if it has any gotchas.
Re: Testing error handling that calls "die"
by TomDLux (Vicar) on Dec 26, 2011 at 19:59 UTC

    Ovid's 3-part article on Test::Class at the Modern Perl website taught me the basics of testing exceptions:

    That should be pretty clear. Look at the new test now. Use the th +rows_ok test from Test::Exception to test the Carp::croak(). Using Test:: +Most instead of Test::More makes this test function available without +explicitly using Test::Exception. sub full_name : Tests(no_plan) { my $test = shift; my $person = $test->class->new; can_ok $person, 'full_name'; throws_ok { $person->full_name } qr/^Both first and last names must be set/, '... and full_name() should croak() if the either name is + not set';

    Don't forget the first arg to throws_ok is a code block or anon subroutine

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://945118]
Approved by toolic
Front-paged by cavac
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found