4 use Test::More tests => 17;
7 use ExtUtils::CBuilder;
9 my ($source_file, $obj_file, $lib_file);
11 require_ok( 'ExtUtils::ParseXS' );
15 use Carp; $SIG{__WARN__} = \&Carp::cluck;
17 #########################
19 { # first block: try without linenumbers
20 my $pxs = ExtUtils::ParseXS->new;
21 # Try sending to filehandle
23 $pxs->process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 );
24 like tied(*FH)->content, '/is_even/', "Test that output contains some text";
26 $source_file = 'XSTest.c';
29 $pxs->process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
30 ok -e $source_file, "Create an output file";
32 my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
33 my $b = ExtUtils::CBuilder->new(quiet => $quiet);
36 skip "no compiler available", 2
37 if ! $b->have_compiler;
38 $obj_file = $b->compile( source => $source_file );
39 ok $obj_file, "ExtUtils::CBuilder::compile() returned true value";
40 ok -e $obj_file, "Make sure $obj_file exists";
44 skip "no dynamic loading", 5
45 if !$b->have_compiler || !$Config{usedl};
46 my $module = 'XSTest';
47 $lib_file = $b->link( objects => $obj_file, module_name => $module );
48 ok $lib_file, "ExtUtils::CBuilder::link() returned true value";
49 ok -e $lib_file, "Make sure $lib_file exists";
51 eval {require XSTest};
52 is $@, '', "No error message recorded, as expected";
53 ok XSTest::is_even(8),
54 "Function created thru XS returned expected true value";
55 ok !XSTest::is_even(9),
56 "Function created thru XS returned expected false value";
58 # Win32 needs to close the DLL before it can unlink it, but unfortunately
59 # dl_unload_file was missing on Win32 prior to perl change #24679!
60 if ($^O eq 'MSWin32' and defined &DynaLoader::dl_unload_file) {
61 for (my $i = 0; $i < @DynaLoader::dl_modules; $i++) {
62 if ($DynaLoader::dl_modules[$i] eq $module) {
63 DynaLoader::dl_unload_file($DynaLoader::dl_librefs[$i]);
71 open my $IN, '<', $source_file
72 or die "Unable to open $source_file: $!";
73 while (my $l = <$IN>) {
74 $seen++ if $l =~ m/#line\s1\s/;
76 is( $seen, 1, "Line numbers created in output file, as intended" );
78 #rewind .c file and regexp it to look for code generation problems
81 my $filecontents = <$IN>;
83 qr|\QXS_EUPXS(XS_XSTest_T_BOOL)\E
85 #line \d+\Q "XSTest.c"
86 ST(0) = boolSV(RETVAL);
91 like($filecontents, $good_T_BOOL_re, "T_BOOL doesn\'t have an extra sv_newmortal or sv_2mortal");
93 my $good_T_BOOL_2_re =
94 qr|\QXS_EUPXS(XS_XSTest_T_BOOL_2)\E
96 #line \d+\Q "XSTest.c"
97 sv_setsv(ST(0), boolSV(in));
103 like($filecontents, $good_T_BOOL_2_re, 'T_BOOL_2 doesn\'t have an extra sv_newmortal or sv_2mortal');
104 my $good_T_BOOL_OUT_re =
105 qr|\QXS_EUPXS(XS_XSTest_T_BOOL_OUT)\E
107 #line \d+\Q "XSTest.c"
108 sv_setsv(ST(0), boolSV(out));
114 like($filecontents, $good_T_BOOL_OUT_re, 'T_BOOL_OUT doesn\'t have an extra sv_newmortal or sv_2mortal');
117 close $IN or die "Unable to close $source_file: $!";
119 unless ($ENV{PERL_NO_CLEANUP}) {
120 for ( $obj_file, $lib_file, $source_file) {
121 next unless defined $_;
127 #####################################################################
129 { # second block: try with linenumbers
130 my $pxs = ExtUtils::ParseXS->new;
131 # Try sending to filehandle
134 filename => 'XSTest.xs',
139 like tied(*FH)->content, '/is_even/', "Test that output contains some text";
141 $source_file = 'XSTest.c';
143 # Try sending to file
145 filename => 'XSTest.xs',
146 output => $source_file,
150 ok -e $source_file, "Create an output file";
154 open my $IN, '<', $source_file
155 or die "Unable to open $source_file: $!";
156 while (my $l = <$IN>) {
157 $seen++ if $l =~ m/#line\s1\s/;
159 close $IN or die "Unable to close $source_file: $!";
160 is( $seen, 0, "No linenumbers created in output file, as intended" );
162 unless ($ENV{PERL_NO_CLEANUP}) {
163 for ( $obj_file, $lib_file, $source_file) {
164 next unless defined $_;
169 #####################################################################
171 sub Foo::TIEHANDLE { bless {}, 'Foo' }
172 sub Foo::PRINT { shift->{buf} .= join '', @_ }
173 sub Foo::content { shift->{buf} }