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