Sébastien Aperghis-Tramoni wrote:

While rewriting Net::Pcap test suite, I quickly stumbled upon this problem: it looks like Test::Exception can't catch croak() or die() thrown from XS code.
[...]

Ok, I corrected a mistake (I was using dies_ok() instead of throws_ok()), and reduced the problem to this test case:

    use strict;
    use Test::More tests => 2;
    use Test::Exception;
    use Net::Pcap;

    throws_ok(
        sub { Net::Pcap::lookupdev() },
        '/^Usage: Net::Pcap::lookupdev\(err\)/',
        "calling lookupdev() with no argument"
    );

    throws_ok
        { Net::Pcap::lookupdev() }
        '/^Usage: Net::Pcap::lookupdev\(err\)/',
        "calling lookupdev() with no argument"
    ;

Executing this script works as expected:

    $ perl -W exception.pl
    1..1
    ok 1 - calling lookupdev() with no argument
    ok 2 - calling lookupdev() with no argument
    # Looks like you planned 1 test but ran 1 extra.

Now, if I move the "use Test::Exception" inside an eval-string and execute the new script:

    $ perl -W exception.pl
    1..2
    ok 1 - calling lookupdev() with no argument
    Usage: Net::Pcap::lookupdev(err) at exception.pl line 13.
    # Looks like you planned 2 tests but only ran 1.
    # Looks like your test died just after 1.

Aha! The first test, which uses the normal form of throws_ok() passes, but the second one, which uses the grep-like form, fails.


Sébastien Aperghis-Tramoni
 -- - --- -- - -- - --- -- - --- -- - --[ http://maddingue.org ]
Close the world, txEn eht nepO

Reply via email to