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

I am trying to get comfortable with XS modules, as the ability to convert C++ code to a perl module would be invaluable in the current problem I'm working on.

I have made it through some of the perlxstut examples, and my question is in regards to testing the XS module using 'dmake test'...

The idea was to make a test script to run 4 test cases on a simple XS module with a method called 'is_even' that takes an int and returns true if it is even.

My current "Mytest.t" looks like this:

use Test::More tests => 4; BEGIN {use_ok('Mytest')} print &Mytest::is_even(0) == 1 ? "ok 2" : "not ok 2", "\n"; print &Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n"; print &Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n";

now when I run 'dmake test', I get the error message:

Looks like you planned 4 tests but only ran 1

The perlxstut says to modify the BEGIN block to "print 1..4", but anything I change in the BEGIN block leads to syntax errors.

I'm sure I am missing something very easy here, what am I doing wrong with the BEGIN block of my test code?

Replies are listed 'Best First'.
Re: Testing a XS module
by chargrill (Parson) on Aug 29, 2007 at 16:31 UTC

    You're not using Test::More's tests, even though when you use it, you tell it you will (via tests => 4). I would be inclined to rewrite your tests as follows:

    use Test::More tests => 4; BEGIN {use_ok('Mytest')} is( Mytest::is_even(0), 1, 'is_even says 0 is even' ); is( Mytest::is_even(1), 0, 'is_even says 1 is not even' ); is( Mytest::is_even(2), 1, 'is_even says 2 is even' );

    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Testing a XS module
by ikegami (Patriarch) on Aug 29, 2007 at 16:36 UTC

    Don't mix and match Test::More with print "ok".

    use Test::More tests => 4; BEGIN { use_ok('Mytest') } ok( Mytest::is_even(0) == 1 ); ok( Mytest::is_even(1) == 0 ); ok( Mytest::is_even(2) == 1 );

    In detail:

      Thank you very much for the help. I had to re-run the Makefile.pl to get the same output as you, but otherwise it works perfect...
Re: Testing a XS module
by andyford (Curate) on Aug 29, 2007 at 16:37 UTC

    I'm curious to see your syntax errors from adding to the BEGIN block. I created a simple Mytest.pm and could add to BEGIN without errors.

    package Mytest; sub is_even { return 1; } return 1;

    non-Perl: Andy Ford