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