Re: [PATCH] CwdXS, Take 2
[p5sagit/p5-mst-13.2.git] / t / lib / b.t
CommitLineData
ccc418af 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
1b026014 5 if ($^O eq 'MacOS') {
6 @INC = qw(: ::lib ::macos:lib);
7 } else {
8 @INC = '.';
9 push @INC, '../lib';
db5fd395 10 }
ccc418af 11}
12
13$| = 1;
14use warnings;
15use strict;
16use Config;
17
fc674faa 18print "1..19\n";
ccc418af 19
20my $test = 1;
21
22sub ok { print "ok $test\n"; $test++ }
23
24use B::Deparse;
25my $deparse = B::Deparse->new() or print "not ";
26ok;
27
28print "not " if "{\n 1;\n}" ne $deparse->coderef2text(sub {1});
29ok;
30
31print "not " if "{\n '???';\n 2;\n}" ne
32 $deparse->coderef2text(sub {1;2});
33ok;
34
35print "not " if "{\n \$test /= 2 if ++\$test;\n}" ne
36 $deparse->coderef2text(sub {++$test and $test/=2;});
37ok;
f70490b9 38
39print "not " if "{\n -((1, 2) x 2);\n}" ne
40 $deparse->coderef2text(sub {-((1,2)x2)});
41ok;
42
9b86dfa2 43{
44my $a = <<'EOF';
45{
46 $test = sub : lvalue {
78f9721b 47 my $x;
9b86dfa2 48 }
49 ;
50}
51EOF
52chomp $a;
78f9721b 53print "not " if $deparse->coderef2text(sub{$test = sub : lvalue{my $x}}) ne $a;
9b86dfa2 54ok;
55
56$a =~ s/lvalue/method/;
78f9721b 57print "not " if $deparse->coderef2text(sub{$test = sub : method{my $x}}) ne $a;
9b86dfa2 58ok;
59
60$a =~ s/method/locked method/;
78f9721b 61print "not " if $deparse->coderef2text(sub{$test = sub : method locked {my $x}})
9b86dfa2 62 ne $a;
63ok;
64}
ccc418af 65
de3f1649 66print "not " if (eval "sub ".$deparse->coderef2text(sub () { 42 }))->() != 42;
67ok;
68
69use constant 'c', 'stuff';
70print "not " if (eval "sub ".$deparse->coderef2text(\&c))->() ne 'stuff';
71ok;
72
fc674faa 73$a = 0;
74print "not " if "{\n (-1) ** \$a;\n}"
75 ne $deparse->coderef2text(sub{(-1) ** $a });
76ok;
77
de3f1649 78# XXX ToDo - constsub that returns a reference
79#use constant cr => ['hello'];
80#my $string = "sub " . $deparse->coderef2text(\&cr);
81#my $val = (eval $string)->();
82#print "not " if ref($val) ne 'ARRAY' || $val->[0] ne 'hello';
83#ok;
84
885a8af1 85my $a;
86my $Is_VMS = $^O eq 'VMS';
db5fd395 87my $Is_MacOS = $^O eq 'MacOS';
88
89my $path = join " ", map { qq["-I$_"] } @INC;
90my $redir = $Is_MacOS ? "" : "2>&1";
91
92$a = `$^X $path "-MO=Deparse" -anle 1 $redir`;
5fb4d820 93$a =~ s/-e syntax OK\n//g;
bd145f00 94$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
95$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
ccc418af 96$b = <<'EOF';
ccc418af 97
98LINE: while (defined($_ = <ARGV>)) {
99 chomp $_;
100 @F = split(/\s+/, $_, 0);
58cccf98 101 '???';
ccc418af 102}
ccc418af 103
104EOF
5fb4d820 105print "# [$a]\n\# vs\n# [$b]\nnot " if $a ne $b;
ccc418af 106ok;
107
db5fd395 108$a = `$^X $path "-MO=Debug" -e 1 $redir`;
ccc418af 109print "not " unless $a =~
110/\bLISTOP\b.*\bOP\b.*\bCOP\b.*\bOP\b/s;
111ok;
112
db5fd395 113$a = `$^X $path "-MO=Terse" -e 1 $redir`;
ccc418af 114print "not " unless $a =~
f72d64f0 115/\bLISTOP\b.*leave.*\n OP\b.*enter.*\n COP\b.*nextstate.*\n OP\b.*null/s;
ccc418af 116ok;
117
db5fd395 118$a = `$^X $path "-MO=Terse" -ane "s/foo/bar/" $redir`;
ccc418af 119$a =~ s/\(0x[^)]+\)//g;
120$a =~ s/\[[^\]]+\]//g;
121$a =~ s/-e syntax OK//;
122$a =~ s/[^a-z ]+//g;
123$a =~ s/\s+/ /g;
b2ec7025 124$a =~ s/\b(s|foo|bar|ullsv)\b\s?//g;
ccc418af 125$a =~ s/^\s+//;
126$a =~ s/\s+$//;
208edb77 127my $is_thread = $Config{use5005threads} && $Config{use5005threads} eq 'define';
128if ($is_thread) {
cfe9256d 129 $b=<<EOF;
130leave enter nextstate label leaveloop enterloop null and defined null
131threadsv readline gv lineseq nextstate aassign null pushmark split pushre
132threadsv const null pushmark rvav gv nextstate subst const unstack nextstate
133EOF
134} else {
135 $b=<<EOF;
ccc418af 136leave enter nextstate label leaveloop enterloop null and defined null
137null gvsv readline gv lineseq nextstate aassign null pushmark split pushre
cfe9256d 138null gvsv const null pushmark rvav gv nextstate subst const unstack nextstate
ccc418af 139EOF
cfe9256d 140}
ccc418af 141$b=~s/\n/ /g;$b=~s/\s+/ /g;
142$b =~ s/\s+$//;
cfe9256d 143print "# [$a]\n# vs\n# [$b]\nnot " if $a ne $b;
ccc418af 144ok;
145
db5fd395 146chomp($a = `$^X $path "-MB::Stash" "-Mwarnings" -e1`);
ccc418af 147$a = join ',', sort split /,/, $a;
7d3b96bb 148$a =~ s/-u(PerlIO|open)(?:::\w+)?,//g if defined $Config{'useperlio'} and $Config{'useperlio'} eq 'define';
754a99e1 149$a =~ s/-uWin32,// if $^O eq 'MSWin32';
150$a =~ s/-u(Cwd|File|File::Copy|OS2),//g if $^O eq 'os2';
5d129265 151$a =~ s/-uCwd,// if $^O eq 'cygwin';
f3ff050f 152if ($Config{static_ext} eq ' ') {
153 $b = '-uCarp,-uCarp::Heavy,-uDB,-uExporter,-uExporter::Heavy,-uattributes,'
1b026014 154 . '-umain,-ustrict,-uutf8,-uwarnings';
e5befd65 155 if (ord('A') == 193) { # EBCDIC sort order is qw(a A) not qw(A a)
156 $b = join ',', sort split /,/, $b;
157 }
f3ff050f 158 print "# [$a] vs [$b]\nnot " if $a ne $b;
159 ok;
160} else {
161 print "ok $test # skipped: one or more static extensions\n"; $test++;
162}
ccc418af 163
208edb77 164if ($is_thread) {
cfe9256d 165 print "# use5005threads: test $test skipped\n";
166} else {
db5fd395 167 $a = `$^X $path "-MO=Showlex" -e "my %one" $redir`;
bd145f00 168 if (ord('A') != 193) { # ASCIIish
169 print "# [$a]\nnot " unless $a =~ /sv_undef.*PVNV.*%one.*sv_undef.*HV/s;
1b026014 170 }
bd145f00 171 else { # EBCDICish C<1: PVNV (0x1a7ede34) "%\226\225\205">
172 print "# [$a]\nnot " unless $a =~ /sv_undef.*PVNV.*%\\[0-9].*sv_undef.*HV/s;
173 }
885a8af1 174}
ccc418af 175ok;
1e1dbab6 176
177# Bug 20001204.07
178{
179my $foo = $deparse->coderef2text(sub { { 234; }});
180# Constants don't get optimised here.
181print "not " unless $foo =~ /{.*{.*234;.*}.*}/sm;
182ok;
183$foo = $deparse->coderef2text(sub { { 234; } continue { 123; } });
1b026014 184print "not " unless $foo =~ /{.*{.*234;.*}.*continue.*{.*123.*}/sm;
1e1dbab6 185ok;
186}