Commit | Line | Data |
adfe19db |
1 | ################################################################################ |
2 | # |
3 | # !!!!! Do NOT edit this file directly! !!!!! |
4 | # |
5 | # Edit mktests.PL and/or parts/inc/ppphtest instead. |
6 | # |
7 | ################################################################################ |
8 | |
9 | BEGIN { |
10 | if ($ENV{'PERL_CORE'}) { |
11 | chdir 't' if -d 't'; |
12 | @INC = ('../lib', '../ext/Devel/PPPort/t') if -d '../lib' && -d '../ext'; |
13 | require Config; import Config; |
14 | use vars '%Config'; |
15 | if (" $Config{'extensions'} " !~ m[ Devel/PPPort ] ) { |
16 | print "1..0 # Skip -- Perl configured without Devel::PPPort module\n"; |
17 | exit 0; |
18 | } |
19 | } |
20 | else { |
21 | unshift @INC, 't'; |
22 | } |
23 | |
24 | eval "use Test"; |
25 | if ($@) { |
26 | require 'testutil.pl'; |
27 | print "1..131\n"; |
28 | } |
29 | else { |
30 | plan(tests => 131); |
31 | } |
32 | } |
33 | |
34 | use Devel::PPPort; |
35 | use strict; |
36 | $^W = 1; |
37 | |
38 | use File::Path qw/rmtree mkpath/; |
39 | |
40 | my $tmp = 'ppptmp'; |
41 | |
42 | rmtree($tmp) if -d $tmp; |
43 | mkpath($tmp) or die "mkpath $tmp: $!\n"; |
44 | chdir($tmp) or die "chdir $tmp: $!\n"; |
45 | |
46 | my $inc = ''; |
47 | if ($ENV{'PERL_CORE'}) { |
48 | $inc = '-I../../lib' if -d '../../lib'; |
49 | } |
50 | |
51 | END { |
52 | chdir("..") if !-d $tmp && -d "../$tmp"; |
53 | rmtree($tmp); |
54 | } |
55 | |
56 | ok(&Devel::PPPort::WriteFile("ppport.h")); |
57 | |
58 | sub ppport |
59 | { |
60 | my @args = @_; |
61 | print "# *** running $^X $inc ppport.h @args ***\n"; |
62 | my $out = join '', `$^X $inc ppport.h @args`; |
63 | my $copy = $out; |
64 | $copy =~ s/^/# | /mg; |
65 | print "$copy\n"; |
66 | return $out; |
67 | } |
68 | |
69 | sub matches |
70 | { |
71 | my($str, $re, $mod) = @_; |
72 | my @n; |
73 | eval "\@n = \$str =~ /$re/g$mod;"; |
74 | if ($@) { |
75 | my $err = $@; |
76 | $err =~ s/^/# *** /mg; |
77 | print "# *** ERROR ***\n$err\n"; |
78 | } |
79 | return $@ ? -42 : scalar @n; |
80 | } |
81 | |
82 | sub eq_files |
83 | { |
84 | my($f1, $f2) = @_; |
85 | return 0 unless -e $f1 && -e $f2; |
86 | local *F; |
87 | for ($f1, $f2) { |
88 | print "# File: $_\n"; |
89 | unless (open F, $_) { |
90 | print "# couldn't open $_: $!\n"; |
91 | return 0; |
92 | } |
93 | $_ = do { local $/; <F> }; |
94 | close F; |
95 | my $copy = $_; |
96 | $copy =~ s/^/# | /mg; |
97 | print "$copy\n"; |
98 | } |
99 | return $f1 eq $f2; |
100 | } |
101 | |
102 | my @tests; |
103 | |
104 | for (split /\s*={70,}\s*/, do { local $/; <DATA> }) { |
105 | s/^\s+//; s/\s+$//; |
106 | my($c, %f); |
107 | ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/; |
108 | push @tests, { code => $c, files => \%f }; |
109 | } |
110 | |
111 | my $t; |
112 | for $t (@tests) { |
113 | my $f; |
114 | for $f (keys %{$t->{files}}) { |
115 | my @f = split /\//, $f; |
116 | if (@f > 1) { |
117 | pop @f; |
118 | my $path = join '/', @f; |
119 | mkpath($path) or die "mkpath('$path'): $!\n"; |
120 | } |
121 | my $txt = $t->{files}{$f}; |
122 | local *F; |
123 | open F, ">$f" or die "open $f: $!\n"; |
124 | print F "$txt\n"; |
125 | close F; |
126 | $txt =~ s/^/# | /mg; |
127 | print "# *** writing $f ***\n$txt\n"; |
128 | } |
129 | |
130 | eval $t->{code}; |
131 | if ($@) { |
132 | my $err = $@; |
133 | $err =~ s/^/# *** /mg; |
134 | print "# *** ERROR ***\n$err\n"; |
135 | } |
136 | ok($@, ''); |
137 | |
138 | for (keys %{$t->{files}}) { |
139 | unlink $_ or die "unlink('$_'): $!\n"; |
140 | } |
141 | } |
142 | |
143 | __DATA__ |
144 | |
145 | my $o = ppport(qw(--help)); |
146 | ok($o =~ /^Usage:.*ppport\.h/m); |
147 | ok($o =~ /--help/m); |
148 | |
149 | $o = ppport(qw(--nochanges)); |
150 | ok($o =~ /^scanning.*test\.xs/mi); |
151 | ok($o =~ /analyzing.*test\.xs/mi); |
152 | ok(matches($o, '^scanning', 'mi'), 1); |
153 | ok(matches($o, 'analyzing', 'mi'), 1); |
154 | ok($o =~ /Uses Perl_newSViv instead of newSViv/); |
155 | |
156 | $o = ppport(qw(--quiet --nochanges)); |
157 | ok($o =~ /^\s*$/); |
158 | |
159 | ---------------------------- test.xs ------------------------------------------ |
160 | |
161 | Perl_newSViv(); |
162 | |
163 | =============================================================================== |
164 | |
165 | # check if C and C++ comments are filtered correctly |
166 | |
167 | my $o = ppport(qw(--copy=a)); |
168 | ok($o =~ /^scanning.*MyExt\.xs/mi); |
169 | ok($o =~ /analyzing.*MyExt\.xs/mi); |
170 | ok(matches($o, '^scanning', 'mi'), 1); |
171 | ok($o =~ /^Needs to include.*ppport\.h/m); |
172 | ok($o !~ /^Uses grok_bin/m); |
173 | ok($o !~ /^Uses newSVpv/m); |
174 | ok($o =~ /Uses 1 C\+\+ style comment/m); |
175 | ok(eq_files('MyExt.xsa', 'MyExt.ra')); |
176 | |
177 | # check if C++ are left untouched with --cplusplus |
178 | |
179 | $o = ppport(qw(--copy=b --cplusplus)); |
180 | ok($o =~ /^scanning.*MyExt\.xs/mi); |
181 | ok($o =~ /analyzing.*MyExt\.xs/mi); |
182 | ok(matches($o, '^scanning', 'mi'), 1); |
183 | ok($o =~ /^Needs to include.*ppport\.h/m); |
184 | ok($o !~ /^Uses grok_bin/m); |
185 | ok($o !~ /^Uses newSVpv/m); |
186 | ok($o !~ /Uses \d+ C\+\+ style comment/m); |
187 | ok(eq_files('MyExt.xsb', 'MyExt.rb')); |
188 | |
189 | unlink qw(MyExt.xsa MyExt.xsb); |
190 | |
191 | ---------------------------- MyExt.xs ----------------------------------------- |
192 | |
193 | newSVuv(); |
194 | // newSVpv(); |
195 | XPUSHs(foo); |
196 | /* grok_bin(); */ |
197 | |
198 | ---------------------------- MyExt.ra ----------------------------------------- |
199 | |
200 | #include "ppport.h" |
201 | newSVuv(); |
202 | /* newSVpv(); */ |
203 | XPUSHs(foo); |
204 | /* grok_bin(); */ |
205 | |
206 | ---------------------------- MyExt.rb ----------------------------------------- |
207 | |
208 | #include "ppport.h" |
209 | newSVuv(); |
210 | // newSVpv(); |
211 | XPUSHs(foo); |
212 | /* grok_bin(); */ |
213 | |
214 | =============================================================================== |
215 | |
216 | my $o = ppport(qw(--nochanges file1.xs)); |
217 | ok($o =~ /^scanning.*file1\.xs/mi); |
218 | ok($o =~ /analyzing.*file1\.xs/mi); |
219 | ok($o !~ /^scanning.*file2\.xs/mi); |
220 | ok($o =~ /^Uses newCONSTSUB/m); |
221 | ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m); |
222 | ok($o =~ /hint for newCONSTSUB/m); |
223 | ok($o !~ /hint for sv_2pv_nolen/m); |
224 | ok($o =~ /^Looks good/m); |
225 | |
226 | $o = ppport(qw(--nochanges --nohints file1.xs)); |
227 | ok($o =~ /^scanning.*file1\.xs/mi); |
228 | ok($o =~ /analyzing.*file1\.xs/mi); |
229 | ok($o !~ /^scanning.*file2\.xs/mi); |
230 | ok($o =~ /^Uses newCONSTSUB/m); |
231 | ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m); |
232 | ok($o !~ /hint for newCONSTSUB/m); |
233 | ok($o !~ /hint for sv_2pv_nolen/m); |
234 | ok($o =~ /^Looks good/m); |
235 | |
236 | $o = ppport(qw(--nochanges --nohints --nodiag file1.xs)); |
237 | ok($o =~ /^scanning.*file1\.xs/mi); |
238 | ok($o =~ /analyzing.*file1\.xs/mi); |
239 | ok($o !~ /^scanning.*file2\.xs/mi); |
240 | ok($o !~ /^Uses newCONSTSUB/m); |
241 | ok($o !~ /^Uses SvPV_nolen/m); |
242 | ok($o !~ /hint for newCONSTSUB/m); |
243 | ok($o !~ /hint for sv_2pv_nolen/m); |
244 | ok($o =~ /^Looks good/m); |
245 | |
246 | $o = ppport(qw(--nochanges --quiet file1.xs)); |
247 | ok($o =~ /^\s*$/); |
248 | |
249 | $o = ppport(qw(--nochanges file2.xs)); |
250 | ok($o =~ /^scanning.*file2\.xs/mi); |
251 | ok($o =~ /analyzing.*file2\.xs/mi); |
252 | ok($o !~ /^scanning.*file1\.xs/mi); |
253 | ok($o =~ /^Uses mXPUSHp/m); |
254 | ok($o =~ /^Needs to include.*ppport\.h/m); |
255 | ok($o !~ /^Looks good/m); |
256 | ok($o =~ /^1 potentially required change detected/m); |
257 | |
258 | $o = ppport(qw(--nochanges --nohints file2.xs)); |
259 | ok($o =~ /^scanning.*file2\.xs/mi); |
260 | ok($o =~ /analyzing.*file2\.xs/mi); |
261 | ok($o !~ /^scanning.*file1\.xs/mi); |
262 | ok($o =~ /^Uses mXPUSHp/m); |
263 | ok($o =~ /^Needs to include.*ppport\.h/m); |
264 | ok($o !~ /^Looks good/m); |
265 | ok($o =~ /^1 potentially required change detected/m); |
266 | |
267 | $o = ppport(qw(--nochanges --nohints --nodiag file2.xs)); |
268 | ok($o =~ /^scanning.*file2\.xs/mi); |
269 | ok($o =~ /analyzing.*file2\.xs/mi); |
270 | ok($o !~ /^scanning.*file1\.xs/mi); |
271 | ok($o !~ /^Uses mXPUSHp/m); |
272 | ok($o !~ /^Needs to include.*ppport\.h/m); |
273 | ok($o !~ /^Looks good/m); |
274 | ok($o =~ /^1 potentially required change detected/m); |
275 | |
276 | $o = ppport(qw(--nochanges --quiet file2.xs)); |
277 | ok($o =~ /^\s*$/); |
278 | |
279 | ---------------------------- file1.xs ----------------------------------------- |
280 | |
281 | #define NEED_newCONSTSUB |
282 | #define NEED_sv_2pv_nolen |
283 | #include "ppport.h" |
284 | |
285 | newCONSTSUB(); |
286 | SvPV_nolen(); |
287 | |
288 | ---------------------------- file2.xs ----------------------------------------- |
289 | |
290 | mXPUSHp(foo); |
291 | |
292 | =============================================================================== |
293 | |
294 | my $o = ppport(qw(--nochanges)); |
295 | ok($o =~ /^scanning.*FooBar\.xs/mi); |
296 | ok($o =~ /analyzing.*FooBar\.xs/mi); |
297 | ok(matches($o, '^scanning', 'mi'), 1); |
298 | ok($o !~ /^Looks good/m); |
299 | ok($o =~ /^Uses grok_bin/m); |
300 | |
301 | ---------------------------- FooBar.xs ---------------------------------------- |
302 | |
303 | newSViv(); |
304 | XPUSHs(foo); |
305 | grok_bin(); |
306 | |
307 | =============================================================================== |
308 | |
309 | my $o = ppport(qw(--nochanges)); |
310 | ok($o =~ /^scanning.*First\.xs/mi); |
311 | ok($o =~ /analyzing.*First\.xs/mi); |
312 | ok($o =~ /^scanning.*second\.h/mi); |
313 | ok($o =~ /analyzing.*second\.h/mi); |
314 | ok($o =~ /^scanning.*sub.*third\.c/mi); |
315 | ok($o =~ /analyzing.*sub.*third\.c/mi); |
316 | ok($o !~ /^scanning.*foobar/mi); |
317 | ok(matches($o, '^scanning', 'mi'), 3); |
318 | |
319 | ---------------------------- First.xs ----------------------------------------- |
320 | |
321 | one |
322 | |
323 | ---------------------------- foobar.xyz --------------------------------------- |
324 | |
325 | two |
326 | |
327 | ---------------------------- second.h ----------------------------------------- |
328 | |
329 | three |
330 | |
331 | ---------------------------- sub/third.c -------------------------------------- |
332 | |
333 | four |
334 | |
335 | =============================================================================== |
336 | |
337 | my $o = ppport(qw(--nochanges)); |
338 | ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i); |
339 | |
340 | ---------------------------- test.xs ------------------------------------------ |
341 | |
342 | #define NEED_foobar |
343 | |
344 | =============================================================================== |
345 | |
346 | # And now some complex "real-world" example |
347 | |
348 | my $o = ppport(qw(--copy=f)); |
349 | for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) { |
350 | ok($o =~ /^scanning.*\Q$_\E/mi); |
351 | ok($o =~ /analyzing.*\Q$_\E/i); |
352 | } |
353 | ok(matches($o, '^scanning', 'mi'), 6); |
354 | |
355 | ok(matches($o, '^Writing copy of', 'mi'), 5); |
356 | ok(!-e "mod5.cf"); |
357 | |
358 | for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) { |
359 | ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi); |
360 | ok(-e "${_}f"); |
361 | ok(eq_files("${_}f", "${_}r")); |
362 | unlink "${_}f"; |
363 | } |
364 | |
365 | ---------------------------- main.xs ------------------------------------------ |
366 | |
367 | #include "EXTERN.h" |
368 | #include "perl.h" |
369 | #include "XSUB.h" |
370 | |
371 | #define NEED_newCONSTSUB |
372 | #define NEED_grok_hex_GLOBAL |
373 | #include "ppport.h" |
374 | |
375 | newCONSTSUB(); |
376 | grok_hex(); |
377 | Perl_grok_bin(aTHX_ foo, bar); |
378 | |
379 | /* some comment */ |
380 | |
381 | perl_eval_pv(); |
382 | grok_bin(); |
383 | Perl_grok_bin(bar, sv_no); |
384 | |
385 | ---------------------------- mod1.c ------------------------------------------- |
386 | |
387 | #include "EXTERN.h" |
388 | #include "perl.h" |
389 | #include "XSUB.h" |
390 | |
391 | #define NEED_grok_bin_GLOBAL |
392 | #define NEED_newCONSTSUB |
393 | #include "ppport.h" |
394 | |
395 | newCONSTSUB(); |
396 | grok_bin(); |
397 | { |
398 | Perl_croak ("foo"); |
399 | Perl_sv_catpvf(); /* I know it's wrong ;-) */ |
400 | } |
401 | |
402 | ---------------------------- mod2.c ------------------------------------------- |
403 | |
404 | #include "EXTERN.h" |
405 | #include "perl.h" |
406 | #include "XSUB.h" |
407 | |
408 | #define NEED_eval_pv |
409 | #include "ppport.h" |
410 | |
411 | newSViv(); |
412 | |
413 | /* |
414 | eval_pv(); |
415 | */ |
416 | |
417 | ---------------------------- mod3.c ------------------------------------------- |
418 | |
419 | #include "EXTERN.h" |
420 | #include "perl.h" |
421 | #include "XSUB.h" |
422 | |
423 | grok_oct(); |
424 | eval_pv(); |
425 | |
426 | ---------------------------- mod4.c ------------------------------------------- |
427 | |
428 | #include "EXTERN.h" |
429 | #include "perl.h" |
430 | #include "XSUB.h" |
431 | |
432 | START_MY_CXT; |
433 | |
434 | ---------------------------- mod5.c ------------------------------------------- |
435 | |
436 | #include "EXTERN.h" |
437 | #include "perl.h" |
438 | #include "XSUB.h" |
439 | |
440 | #include "ppport.h" |
441 | call_pv(); |
442 | |
443 | ---------------------------- main.xsr ----------------------------------------- |
444 | |
445 | #include "EXTERN.h" |
446 | #include "perl.h" |
447 | #include "XSUB.h" |
448 | |
449 | #define NEED_eval_pv_GLOBAL |
450 | #define NEED_grok_hex |
451 | #define NEED_newCONSTSUB_GLOBAL |
452 | #include "ppport.h" |
453 | |
454 | newCONSTSUB(); |
455 | grok_hex(); |
456 | grok_bin(foo, bar); |
457 | |
458 | /* some comment */ |
459 | |
460 | eval_pv(); |
461 | grok_bin(); |
462 | grok_bin(bar, PL_sv_no); |
463 | |
464 | ---------------------------- mod1.cr ------------------------------------------ |
465 | |
466 | #include "EXTERN.h" |
467 | #include "perl.h" |
468 | #include "XSUB.h" |
469 | |
470 | #define NEED_grok_bin_GLOBAL |
471 | #include "ppport.h" |
472 | |
473 | newCONSTSUB(); |
474 | grok_bin(); |
475 | { |
476 | Perl_croak (aTHX_ "foo"); |
477 | Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */ |
478 | } |
479 | |
480 | ---------------------------- mod2.cr ------------------------------------------ |
481 | |
482 | #include "EXTERN.h" |
483 | #include "perl.h" |
484 | #include "XSUB.h" |
485 | |
486 | |
487 | newSViv(); |
488 | |
489 | /* |
490 | eval_pv(); |
491 | */ |
492 | |
493 | ---------------------------- mod3.cr ------------------------------------------ |
494 | |
495 | #include "EXTERN.h" |
496 | #include "perl.h" |
497 | #include "XSUB.h" |
498 | #define NEED_grok_oct |
499 | #include "ppport.h" |
500 | |
501 | grok_oct(); |
502 | eval_pv(); |
503 | |
504 | ---------------------------- mod4.cr ------------------------------------------ |
505 | |
506 | #include "EXTERN.h" |
507 | #include "perl.h" |
508 | #include "XSUB.h" |
509 | #include "ppport.h" |
510 | |
511 | START_MY_CXT; |
512 | |
513 | =============================================================================== |
514 | |
515 | my $o = ppport(qw(--nochanges)); |
516 | ok($o =~ /Uses grok_hex/m); |
517 | ok($o !~ /Looks good/m); |
518 | |
519 | $o = ppport(qw(--nochanges --compat-version=5.8.0)); |
520 | ok($o !~ /Uses grok_hex/m); |
521 | ok($o =~ /Looks good/m); |
522 | |
523 | ---------------------------- FooBar.xs ---------------------------------------- |
524 | |
525 | grok_hex(); |
526 | |
527 | =============================================================================== |
528 | |
529 | my $o = ppport(qw(--nochanges)); |
530 | ok($o =~ /Uses SvPVutf8_force, which may not be portable/m); |
531 | |
532 | $o = ppport(qw(--nochanges --compat-version=5.6.0)); |
533 | ok($o !~ /Uses SvPVutf8_force/m); |
534 | |
535 | ---------------------------- FooBar.xs ---------------------------------------- |
536 | |
537 | SvPVutf8_force(); |
538 | |