Skip to content

Commit 504aaf8

Browse files
vovkasmmohawk2
authored andcommitted
Add C++ XS tests
1 parent 616fc57 commit 504aaf8

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

t/04-xscpp.t

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/perl -w
2+
3+
use strict;
4+
use warnings;
5+
use Config;
6+
BEGIN {
7+
chdir 't' or die "chdir(t): $!\n";
8+
unshift @INC, 'lib/';
9+
}
10+
use MakeMaker::Test::Utils;
11+
use MakeMaker::Test::Setup::XS;
12+
use Test::More;
13+
14+
plan skip_all => "ExtUtils::CBuilder not installed or no C++ compiler"
15+
unless have_cplusplus();
16+
plan skip_all => 'Dynaloading not enabled' if $Config{usedl} ne 'define';
17+
my @tests = list_cpp();
18+
plan skip_all => "No tests" unless @tests;
19+
plan tests => 6 * @tests;
20+
my $perl = which_perl();
21+
perl_lib;
22+
$| = 1;
23+
run_tests($perl, @$_) for @tests;

t/lib/MakeMaker/Test/Setup/XS.pm

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package MakeMaker::Test::Setup::XS;
22

33
@ISA = qw(Exporter);
44
require Exporter;
5-
@EXPORT = qw(run_tests list_dynamic list_static);
5+
@EXPORT = qw(run_tests list_dynamic list_static list_cpp);
66

77
use strict;
88
use File::Path;
@@ -292,6 +292,80 @@ END
292292

293293
};
294294

295+
$label2files{cppbasic} = +{
296+
297+
'lib/XSCPP/Test.pm' => <<'END',
298+
package XSCPP::Test;
299+
require Exporter;
300+
require DynaLoader;
301+
$VERSION = 1.02;
302+
@ISA = qw(Exporter DynaLoader);
303+
@EXPORT = qw(is_even);
304+
bootstrap XSCPP::Test $VERSION;
305+
1;
306+
END
307+
308+
'Makefile.PL' => <<'END',
309+
use ExtUtils::MakeMaker;
310+
use ExtUtils::CppGuess;
311+
my $guess = ExtUtils::CppGuess->new;
312+
WriteMakefile(
313+
NAME => 'XSCPP::Test',
314+
VERSION_FROM => 'lib/XSCPP/Test.pm',
315+
XSMULTI => 1,
316+
$guess->makemaker_options,
317+
);
318+
END
319+
320+
'lib/XSCPP/Test.xscc' => <<END,
321+
extern "C" {
322+
#include "EXTERN.h"
323+
#include "perl.h"
324+
}
325+
#include "XSUB.h"
326+
class CPPTest {
327+
public:
328+
CPPTest() { }
329+
~CPPTest() { }
330+
int is_even(int num) { return (num % 2) == 0; }
331+
};
332+
\nMODULE = XSCPP::Test PACKAGE = XSCPP::Test
333+
\nPROTOTYPES: DISABLE
334+
CPPTest*
335+
CPPTest::new();
336+
\nint
337+
CPPTest::is_even(int input);
338+
\nvoid
339+
CPPTest::DESTROY();
340+
END
341+
342+
'typemap' => <<'END',
343+
TYPEMAP
344+
CPPTest * O_OBJECT
345+
OUTPUT
346+
O_OBJECT
347+
sv_setref_pv( $arg, CLASS, (void*)$var );
348+
INPUT
349+
O_OBJECT
350+
if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
351+
$var = ($type)SvIV((SV*)SvRV( $arg ));
352+
else{
353+
warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
354+
XSRETURN_UNDEF;
355+
}
356+
END
357+
358+
't/is_even.t' => <<'END',
359+
#!/usr/bin/perl -w
360+
use Test::More tests => 3;
361+
use_ok "XSCPP::Test";
362+
my $o = XSCPP::Test->new;
363+
ok !$o->is_even(1);
364+
ok $o->is_even(2);
365+
END
366+
367+
};
368+
295369
sub virtual_rename {
296370
my ($label, $oldfile, $newfile) = @_;
297371
$label2files{$label}->{$newfile} = delete $label2files{$label}->{$oldfile};
@@ -338,6 +412,11 @@ sub list_dynamic {
338412
);
339413
}
340414

415+
sub list_cpp {
416+
(
417+
);
418+
}
419+
341420
sub run_tests {
342421
my ($perl, $label, $add_target, $add_testtarget) = @_;
343422
my $sublabel = $add_target;

0 commit comments

Comments
 (0)