This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Trim second block (nolinenumbers) to avert crashes on Win32.
[perl5.git] / dist / ExtUtils-ParseXS / t / 001-basic.t
CommitLineData
6b09c160
YST
1#!/usr/bin/perl
2
6b09c160 3use strict;
00590f3b 4use Test::More tests => 17;
6986f47f 5use Config;
da2b6f33 6use DynaLoader;
6b09c160 7use ExtUtils::CBuilder;
6986f47f 8
6986f47f
DG
9my ($source_file, $obj_file, $lib_file);
10
11require_ok( 'ExtUtils::ParseXS' );
6b09c160 12
9219b114 13chdir('t') if -d 't';
6b09c160
YST
14
15use Carp; $SIG{__WARN__} = \&Carp::cluck;
16
17#########################
18
21ae0dea 19{ # first block: try without linenumbers
9f8d2499 20my $pxs = ExtUtils::ParseXS->new;
6b09c160
YST
21# Try sending to filehandle
22tie *FH, 'Foo';
9f8d2499 23$pxs->process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 );
6986f47f 24like tied(*FH)->content, '/is_even/', "Test that output contains some text";
6b09c160 25
6986f47f 26$source_file = 'XSTest.c';
da2b6f33 27
6b09c160 28# Try sending to file
9f8d2499 29$pxs->process_file(filename => 'XSTest.xs', output => $source_file, prototypes => 0);
6986f47f 30ok -e $source_file, "Create an output file";
6b09c160 31
6b09c160 32my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE};
6b09c160 33my $b = ExtUtils::CBuilder->new(quiet => $quiet);
6b09c160 34
6986f47f
DG
35SKIP: {
36 skip "no compiler available", 2
37 if ! $b->have_compiler;
38 $obj_file = $b->compile( source => $source_file );
84ce4b0a 39 ok $obj_file, "ExtUtils::CBuilder::compile() returned true value";
6986f47f
DG
40 ok -e $obj_file, "Make sure $obj_file exists";
41}
6b09c160 42
6986f47f
DG
43SKIP: {
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 );
84ce4b0a 48 ok $lib_file, "ExtUtils::CBuilder::link() returned true value";
6986f47f 49 ok -e $lib_file, "Make sure $lib_file exists";
6b09c160
YST
50
51 eval {require XSTest};
84ce4b0a
JK
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";
6b09c160 57
da2b6f33
SH
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]);
64 last;
65 }
66 }
67 }
6b09c160
YST
68}
69
84ce4b0a
JK
70my $seen = 0;
71open my $IN, '<', $source_file
72 or die "Unable to open $source_file: $!";
73while (my $l = <$IN>) {
74 $seen++ if $l =~ m/#line\s1\s/;
75}
fc577107
DD
76is( $seen, 1, "Line numbers created in output file, as intended" );
77{
78 #rewind .c file and regexp it to look for code generation problems
79 local $/ = undef;
80 seek($IN, 0, 0);
81 my $filecontents = <$IN>;
82 my $good_T_BOOL_re =
83qr|\QXS_EUPXS(XS_XSTest_T_BOOL)\E
84.+?
85#line \d+\Q "XSTest.c"
86 ST(0) = boolSV(RETVAL);
87 }
88 XSRETURN(1);
89}
90\E|s;
91 like($filecontents, $good_T_BOOL_re, "T_BOOL doesn\'t have an extra sv_newmortal or sv_2mortal");
92
93 my $good_T_BOOL_2_re =
94qr|\QXS_EUPXS(XS_XSTest_T_BOOL_2)\E
95.+?
96#line \d+\Q "XSTest.c"
97 sv_setsv(ST(0), boolSV(in));
98 SvSETMAGIC(ST(0));
99 }
100 XSRETURN(1);
101}
102\E|s;
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 =
105qr|\QXS_EUPXS(XS_XSTest_T_BOOL_OUT)\E
106.+?
107#line \d+\Q "XSTest.c"
108 sv_setsv(ST(0), boolSV(out));
109 SvSETMAGIC(ST(0));
110 }
111 XSRETURN_EMPTY;
112}
113\E|s;
114 like($filecontents, $good_T_BOOL_OUT_re, 'T_BOOL_OUT doesn\'t have an extra sv_newmortal or sv_2mortal');
115
116}
84ce4b0a 117close $IN or die "Unable to close $source_file: $!";
84ce4b0a 118
708f9ca6 119unless ($ENV{PERL_NO_CLEANUP}) {
6986f47f
DG
120 for ( $obj_file, $lib_file, $source_file) {
121 next unless defined $_;
122 1 while unlink $_;
123 }
708f9ca6 124}
21ae0dea
JK
125}
126
127#####################################################################
128
129{ # second block: try with linenumbers
130my $pxs = ExtUtils::ParseXS->new;
131# Try sending to filehandle
132tie *FH, 'Foo';
133$pxs->process_file(
134 filename => 'XSTest.xs',
135 output => \*FH,
136 prototypes => 1,
137 linenumbers => 0,
138);
139like tied(*FH)->content, '/is_even/', "Test that output contains some text";
140
141$source_file = 'XSTest.c';
142
143# Try sending to file
144$pxs->process_file(
145 filename => 'XSTest.xs',
146 output => $source_file,
147 prototypes => 0,
148 linenumbers => 0,
149);
150ok -e $source_file, "Create an output file";
151
21ae0dea
JK
152
153my $seen = 0;
154open my $IN, '<', $source_file
155 or die "Unable to open $source_file: $!";
156while (my $l = <$IN>) {
157 $seen++ if $l =~ m/#line\s1\s/;
158}
159close $IN or die "Unable to close $source_file: $!";
160is( $seen, 0, "No linenumbers created in output file, as intended" );
161
21ae0dea
JK
162unless ($ENV{PERL_NO_CLEANUP}) {
163 for ( $obj_file, $lib_file, $source_file) {
164 next unless defined $_;
165 1 while unlink $_;
166 }
167}
168}
6b09c160
YST
169#####################################################################
170
171sub Foo::TIEHANDLE { bless {}, 'Foo' }
172sub Foo::PRINT { shift->{buf} .= join '', @_ }
173sub Foo::content { shift->{buf} }