Commit | Line | Data |
---|---|---|
6b09c160 YST |
1 | #!/usr/bin/perl |
2 | ||
6b09c160 | 3 | use strict; |
00590f3b | 4 | use Test::More tests => 17; |
6986f47f | 5 | use Config; |
da2b6f33 | 6 | use DynaLoader; |
6b09c160 | 7 | use ExtUtils::CBuilder; |
6986f47f | 8 | |
6986f47f DG |
9 | my ($source_file, $obj_file, $lib_file); |
10 | ||
11 | require_ok( 'ExtUtils::ParseXS' ); | |
6b09c160 | 12 | |
9219b114 | 13 | chdir('t') if -d 't'; |
6b09c160 YST |
14 | |
15 | use Carp; $SIG{__WARN__} = \&Carp::cluck; | |
16 | ||
17 | ######################### | |
18 | ||
21ae0dea | 19 | { # first block: try without linenumbers |
9f8d2499 | 20 | my $pxs = ExtUtils::ParseXS->new; |
6b09c160 YST |
21 | # Try sending to filehandle |
22 | tie *FH, 'Foo'; | |
9f8d2499 | 23 | $pxs->process_file( filename => 'XSTest.xs', output => \*FH, prototypes => 1 ); |
6986f47f | 24 | like 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 | 30 | ok -e $source_file, "Create an output file"; |
6b09c160 | 31 | |
6b09c160 | 32 | my $quiet = $ENV{PERL_CORE} && !$ENV{HARNESS_ACTIVE}; |
6b09c160 | 33 | my $b = ExtUtils::CBuilder->new(quiet => $quiet); |
6b09c160 | 34 | |
6986f47f DG |
35 | SKIP: { |
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 |
43 | SKIP: { |
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 |
70 | my $seen = 0; |
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/; | |
75 | } | |
fc577107 DD |
76 | is( $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 = | |
83 | qr|\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 = | |
94 | qr|\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 = | |
105 | qr|\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 | 117 | close $IN or die "Unable to close $source_file: $!"; |
84ce4b0a | 118 | |
708f9ca6 | 119 | unless ($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 | |
130 | my $pxs = ExtUtils::ParseXS->new; | |
131 | # Try sending to filehandle | |
132 | tie *FH, 'Foo'; | |
133 | $pxs->process_file( | |
134 | filename => 'XSTest.xs', | |
135 | output => \*FH, | |
136 | prototypes => 1, | |
137 | linenumbers => 0, | |
138 | ); | |
139 | like 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 | ); | |
150 | ok -e $source_file, "Create an output file"; | |
151 | ||
21ae0dea JK |
152 | |
153 | my $seen = 0; | |
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/; | |
158 | } | |
159 | close $IN or die "Unable to close $source_file: $!"; | |
160 | is( $seen, 0, "No linenumbers created in output file, as intended" ); | |
161 | ||
21ae0dea JK |
162 | unless ($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 | ||
171 | sub Foo::TIEHANDLE { bless {}, 'Foo' } | |
172 | sub Foo::PRINT { shift->{buf} .= join '', @_ } | |
173 | sub Foo::content { shift->{buf} } |