Posted on 2005-08-14 17:19:04-07 by maddingue
Test::Exception and XS code
Hello, While trying to rewrite 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. Here is an example:
use strict; use Test::More tests => 1; use Net::Pcap; eval "use Test::Exception"; my $has_test_exception = !$@; my ($dev,$err) = ('',''); SKIP: { skip "Test::Exception not available", 1 unless $has_test_exception; dies_ok { $dev = Net::Pcap::lookupdev(undef) } '/^arg1 not a reference/', "calling lookupdev() with no reference" }
When executed, this script gives the following:
$ perl 02-lookupdev.t 1..1 arg1 not a hash ref at 02-lookupdev.t line 15. # Looks like your test died before it could output anything.
I checked that the exception can caught using eval{} or eval""
$ perl -MNet::Pcap -le 'eval{Net::Pcap::lookupdev(undef)}; print "after"' after
But if I try to do the same thing with Test::Exception, wrapping the lookupdev() call within an eval{}, it throws me a new kind of error:
$ perl 02-lookupdev.t 1..1 Can't call method "dies_ok" without a package or object reference at 02-lookupdev.t line 14. # Looks like your test died before it could output anything.
Any idea?
Direct Responses: 893 | Write a response
Posted on 2005-08-15 16:37:47-07 by maddingue in response to 888
Re: Test::Exception and XS code
See also here for a shorter test case and here for the reason.
Direct Responses: 894 | Write a response
Posted on 2005-08-15 19:37:48-07 by adrianh in response to 893
Re: Test::Exception and XS code

As Yitzchak Scott-Thoennes correctly points out this is nothing to do with XS, but with Test::Exception not being used at compile time. You can get around this by either wrapping the conditional loading up in a BEGIN block:

BEGIN { eval "use Test::Exception"; plan skip_all => "Test::Exception needed" if $@; }

or by calling the Test::Exception functions without prototypes. I've added a note to make this clearer in the documentation.

Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.