develooper Front page | perl.qa | Postings from August 2005

Test::Exception and XS code

Thread Next
From:
Sébastien Aperghis-Tramoni
Date:
August 15, 2005 08:25
Subject:
Test::Exception and XS code
Message ID:
D04D5FED-0DA0-11DA-ACBC-000502F3279F@free.fr
Hello all,

I have posted this question on CPAN Forum but nobody answered, so I 
also post it here in case someone has an idea.

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. Here is an example:

     use strict;
     use Test::More tests => 1;
     use Net::Pcap;

     eval "use Test::Exception";
     my $has_test_exception = !$@;
     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 be 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.

Using the parens form of dies_ok() resolves the syntax error but then 
the test always successes.

Am I doing something wrong?


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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About