Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Testing a XS module

by technojosh (Priest)
on Aug 29, 2007 at 16:19 UTC ( [id://635840]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-24 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found