################################################################################ ## ## $Revision: 19 $ ## $Author: mhx $ ## $Date: 2004/08/17 22:04:17 +0200 $ ## ################################################################################ ## ## Version 3.x, Copyright (C) 2004, Marcus Holland-Moritz. ## Version 2.x, Copyright (C) 2001, Paul Marquess. ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski. ## ## This program is free software; you can redistribute it and/or ## modify it under the same terms as Perl itself. ## ################################################################################ =tests plan => 134 use File::Path qw/rmtree mkpath/; use Config; my $tmp = 'ppptmp'; my $inc = ''; my $perl = find_perl(); rmtree($tmp) if -d $tmp; mkpath($tmp) or die "mkpath $tmp: $!\n"; chdir($tmp) or die "chdir $tmp: $!\n"; if ($ENV{'PERL_CORE'}) { $inc = '-I../../lib' if -d '../../lib'; } if ($perl =~ m!^\./!) { $perl = ".$perl"; } END { chdir("..") if !-d $tmp && -d "../$tmp"; rmtree($tmp); } ok(&Devel::PPPort::WriteFile("ppport.h")); sub ppport { my @args = @_; print "# *** running $perl $inc ppport.h @args ***\n"; my $out = join '', `$perl $inc ppport.h @args`; my $copy = $out; $copy =~ s/^/# | /mg; print "$copy\n"; return $out; } sub matches { my($str, $re, $mod) = @_; my @n; eval "\@n = \$str =~ /$re/g$mod;"; if ($@) { my $err = $@; $err =~ s/^/# *** /mg; print "# *** ERROR ***\n$err\n"; } return $@ ? -42 : scalar @n; } sub eq_files { my($f1, $f2) = @_; return 0 unless -e $f1 && -e $f2; local *F; for ($f1, $f2) { print "# File: $_\n"; unless (open F, $_) { print "# couldn't open $_: $!\n"; return 0; } $_ = do { local $/; }; close F; my $copy = $_; $copy =~ s/^/# | /mg; print "$copy\n"; } return $f1 eq $f2; } my @tests; for (split /\s*={70,}\s*/, do { local $/; }) { s/^\s+//; s/\s+$//; my($c, %f); ($c, @f{m/-{20,}\s+(\S+)\s+-{20,}/g}) = split /\s*-{20,}\s+\S+\s+-{20,}\s*/; push @tests, { code => $c, files => \%f }; } my $t; for $t (@tests) { my $f; for $f (keys %{$t->{files}}) { my @f = split /\//, $f; if (@f > 1) { pop @f; my $path = join '/', @f; mkpath($path) or die "mkpath('$path'): $!\n"; } my $txt = $t->{files}{$f}; local *F; open F, ">$f" or die "open $f: $!\n"; print F "$txt\n"; close F; $txt =~ s/^/# | /mg; print "# *** writing $f ***\n$txt\n"; } eval $t->{code}; if ($@) { my $err = $@; $err =~ s/^/# *** /mg; print "# *** ERROR ***\n$err\n"; } ok($@, ''); for (keys %{$t->{files}}) { unlink $_ or die "unlink('$_'): $!\n"; } } sub find_perl { my $perl = $^X; return $perl if $^O eq 'VMS'; my $exe = $Config{'_exe'} || ''; if ($perl =~ /^perl\Q$exe\E$/i) { $perl = "perl$exe"; eval "require File::Spec"; if ($@) { $perl = "./$perl"; } else { $perl = File::Spec->catfile(File::Spec->curdir(), $perl); } } if ($perl !~ /\Q$exe\E$/i) { $perl .= $exe; } warn "find_perl: cannot find $perl from $^X" unless -f $perl; return $perl; } __DATA__ my $o = ppport(qw(--help)); ok($o =~ /^Usage:.*ppport\.h/m); ok($o =~ /--help/m); $o = ppport(qw(--nochanges)); ok($o =~ /^scanning.*test\.xs/mi); ok($o =~ /analyzing.*test\.xs/mi); ok(matches($o, '^scanning', 'mi'), 1); ok(matches($o, 'analyzing', 'mi'), 1); ok($o =~ /Uses Perl_newSViv instead of newSViv/); $o = ppport(qw(--quiet --nochanges)); ok($o =~ /^\s*$/); ---------------------------- test.xs ------------------------------------------ Perl_newSViv(); =============================================================================== # check if C and C++ comments are filtered correctly my $o = ppport(qw(--copy=a)); ok($o =~ /^scanning.*MyExt\.xs/mi); ok($o =~ /analyzing.*MyExt\.xs/mi); ok(matches($o, '^scanning', 'mi'), 1); ok($o =~ /^Needs to include.*ppport\.h/m); ok($o !~ /^Uses grok_bin/m); ok($o !~ /^Uses newSVpv/m); ok($o =~ /Uses 1 C\+\+ style comment/m); ok(eq_files('MyExt.xsa', 'MyExt.ra')); # check if C++ are left untouched with --cplusplus $o = ppport(qw(--copy=b --cplusplus)); ok($o =~ /^scanning.*MyExt\.xs/mi); ok($o =~ /analyzing.*MyExt\.xs/mi); ok(matches($o, '^scanning', 'mi'), 1); ok($o =~ /^Needs to include.*ppport\.h/m); ok($o !~ /^Uses grok_bin/m); ok($o !~ /^Uses newSVpv/m); ok($o !~ /Uses \d+ C\+\+ style comment/m); ok(eq_files('MyExt.xsb', 'MyExt.rb')); unlink qw(MyExt.xsa MyExt.xsb); ---------------------------- MyExt.xs ----------------------------------------- newSVuv(); // newSVpv(); XPUSHs(foo); /* grok_bin(); */ ---------------------------- MyExt.ra ----------------------------------------- #include "ppport.h" newSVuv(); /* newSVpv(); */ XPUSHs(foo); /* grok_bin(); */ ---------------------------- MyExt.rb ----------------------------------------- #include "ppport.h" newSVuv(); // newSVpv(); XPUSHs(foo); /* grok_bin(); */ =============================================================================== my $o = ppport(qw(--nochanges file1.xs)); ok($o =~ /^scanning.*file1\.xs/mi); ok($o =~ /analyzing.*file1\.xs/mi); ok($o !~ /^scanning.*file2\.xs/mi); ok($o =~ /^Uses newCONSTSUB/m); ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m); ok($o =~ /hint for newCONSTSUB/m); ok($o !~ /hint for sv_2pv_nolen/m); ok($o =~ /^Looks good/m); $o = ppport(qw(--nochanges --nohints file1.xs)); ok($o =~ /^scanning.*file1\.xs/mi); ok($o =~ /analyzing.*file1\.xs/mi); ok($o !~ /^scanning.*file2\.xs/mi); ok($o =~ /^Uses newCONSTSUB/m); ok($o =~ /^Uses SvPV_nolen.*depends.*sv_2pv_nolen/m); ok($o !~ /hint for newCONSTSUB/m); ok($o !~ /hint for sv_2pv_nolen/m); ok($o =~ /^Looks good/m); $o = ppport(qw(--nochanges --nohints --nodiag file1.xs)); ok($o =~ /^scanning.*file1\.xs/mi); ok($o =~ /analyzing.*file1\.xs/mi); ok($o !~ /^scanning.*file2\.xs/mi); ok($o !~ /^Uses newCONSTSUB/m); ok($o !~ /^Uses SvPV_nolen/m); ok($o !~ /hint for newCONSTSUB/m); ok($o !~ /hint for sv_2pv_nolen/m); ok($o =~ /^Looks good/m); $o = ppport(qw(--nochanges --quiet file1.xs)); ok($o =~ /^\s*$/); $o = ppport(qw(--nochanges file2.xs)); ok($o =~ /^scanning.*file2\.xs/mi); ok($o =~ /analyzing.*file2\.xs/mi); ok($o !~ /^scanning.*file1\.xs/mi); ok($o =~ /^Uses mXPUSHp/m); ok($o =~ /^Needs to include.*ppport\.h/m); ok($o !~ /^Looks good/m); ok($o =~ /^1 potentially required change detected/m); $o = ppport(qw(--nochanges --nohints file2.xs)); ok($o =~ /^scanning.*file2\.xs/mi); ok($o =~ /analyzing.*file2\.xs/mi); ok($o !~ /^scanning.*file1\.xs/mi); ok($o =~ /^Uses mXPUSHp/m); ok($o =~ /^Needs to include.*ppport\.h/m); ok($o !~ /^Looks good/m); ok($o =~ /^1 potentially required change detected/m); $o = ppport(qw(--nochanges --nohints --nodiag file2.xs)); ok($o =~ /^scanning.*file2\.xs/mi); ok($o =~ /analyzing.*file2\.xs/mi); ok($o !~ /^scanning.*file1\.xs/mi); ok($o !~ /^Uses mXPUSHp/m); ok($o !~ /^Needs to include.*ppport\.h/m); ok($o !~ /^Looks good/m); ok($o =~ /^1 potentially required change detected/m); $o = ppport(qw(--nochanges --quiet file2.xs)); ok($o =~ /^\s*$/); ---------------------------- file1.xs ----------------------------------------- #define NEED_newCONSTSUB #define NEED_sv_2pv_nolen #include "ppport.h" newCONSTSUB(); SvPV_nolen(); ---------------------------- file2.xs ----------------------------------------- mXPUSHp(foo); =============================================================================== my $o = ppport(qw(--nochanges)); ok($o =~ /^scanning.*FooBar\.xs/mi); ok($o =~ /analyzing.*FooBar\.xs/mi); ok(matches($o, '^scanning', 'mi'), 1); ok($o !~ /^Looks good/m); ok($o =~ /^Uses grok_bin/m); ---------------------------- FooBar.xs ---------------------------------------- newSViv(); XPUSHs(foo); grok_bin(); =============================================================================== my $o = ppport(qw(--nochanges)); ok($o =~ /^scanning.*First\.xs/mi); ok($o =~ /analyzing.*First\.xs/mi); ok($o =~ /^scanning.*second\.h/mi); ok($o =~ /analyzing.*second\.h/mi); ok($o =~ /^scanning.*sub.*third\.c/mi); ok($o =~ /analyzing.*sub.*third\.c/mi); ok($o !~ /^scanning.*foobar/mi); ok(matches($o, '^scanning', 'mi'), 3); ---------------------------- First.xs ----------------------------------------- one ---------------------------- foobar.xyz --------------------------------------- two ---------------------------- second.h ----------------------------------------- three ---------------------------- sub/third.c -------------------------------------- four =============================================================================== my $o = ppport(qw(--nochanges)); ok($o =~ /Possibly wrong #define NEED_foobar in.*test.xs/i); ---------------------------- test.xs ------------------------------------------ #define NEED_foobar =============================================================================== # And now some complex "real-world" example my $o = ppport(qw(--copy=f)); for (qw(main.xs mod1.c mod2.c mod3.c mod4.c mod5.c)) { ok($o =~ /^scanning.*\Q$_\E/mi); ok($o =~ /analyzing.*\Q$_\E/i); } ok(matches($o, '^scanning', 'mi'), 6); ok(matches($o, '^Writing copy of', 'mi'), 5); ok(!-e "mod5.cf"); for (qw(main.xs mod1.c mod2.c mod3.c mod4.c)) { ok($o =~ /^Writing copy of.*\Q$_\E.*with changes/mi); ok(-e "${_}f"); ok(eq_files("${_}f", "${_}r")); unlink "${_}f"; } ---------------------------- main.xs ------------------------------------------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_newCONSTSUB #define NEED_grok_hex_GLOBAL #include "ppport.h" newCONSTSUB(); grok_hex(); Perl_grok_bin(aTHX_ foo, bar); /* some comment */ perl_eval_pv(); grok_bin(); Perl_grok_bin(bar, sv_no); ---------------------------- mod1.c ------------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_grok_bin_GLOBAL #define NEED_newCONSTSUB #include "ppport.h" newCONSTSUB(); grok_bin(); { Perl_croak ("foo"); Perl_sv_catpvf(); /* I know it's wrong ;-) */ } ---------------------------- mod2.c ------------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_eval_pv #include "ppport.h" newSViv(); /* eval_pv(); */ ---------------------------- mod3.c ------------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" grok_oct(); eval_pv(); ---------------------------- mod4.c ------------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" START_MY_CXT; ---------------------------- mod5.c ------------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" call_pv(); ---------------------------- main.xsr ----------------------------------------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_eval_pv_GLOBAL #define NEED_grok_hex #define NEED_newCONSTSUB_GLOBAL #include "ppport.h" newCONSTSUB(); grok_hex(); grok_bin(foo, bar); /* some comment */ eval_pv(); grok_bin(); grok_bin(bar, PL_sv_no); ---------------------------- mod1.cr ------------------------------------------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_grok_bin_GLOBAL #include "ppport.h" newCONSTSUB(); grok_bin(); { Perl_croak (aTHX_ "foo"); Perl_sv_catpvf(aTHX); /* I know it's wrong ;-) */ } ---------------------------- mod2.cr ------------------------------------------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" newSViv(); /* eval_pv(); */ ---------------------------- mod3.cr ------------------------------------------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #define NEED_grok_oct #include "ppport.h" grok_oct(); eval_pv(); ---------------------------- mod4.cr ------------------------------------------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" START_MY_CXT; =============================================================================== my $o = ppport(qw(--nochanges)); ok($o =~ /Uses grok_hex/m); ok($o !~ /Looks good/m); $o = ppport(qw(--nochanges --compat-version=5.8.0)); ok($o !~ /Uses grok_hex/m); ok($o =~ /Looks good/m); ---------------------------- FooBar.xs ---------------------------------------- grok_hex(); =============================================================================== my $o = ppport(qw(--nochanges)); ok($o =~ /Uses SvPVutf8_force, which may not be portable/m); $o = ppport(qw(--nochanges --compat-version=5.6.0)); ok($o !~ /Uses SvPVutf8_force/m); ---------------------------- FooBar.xs ---------------------------------------- SvPVutf8_force(); =============================================================================== my $o = ppport(qw(--nochanges)); ok($o !~ /potentially required change/); ok(matches($o, '^Looks good', 'mi'), 2); ---------------------------- FooBar.xs ---------------------------------------- #define NEED_grok_numeric_radix #define NEED_grok_number #include "ppport.h" GROK_NUMERIC_RADIX(); grok_number(); ---------------------------- foo.c -------------------------------------------- #include "ppport.h" call_pv();