change/correction to 32246
[p5sagit/p5-mst-13.2.git] / ext / B / t / deparse.t
CommitLineData
87a42246 1#!./perl
2
3BEGIN {
5638aaac 4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 if ($^O eq 'MacOS') {
7 @INC = qw(: ::lib ::macos:lib);
8 } else {
9 @INC = '.';
10 push @INC, '../lib';
11 }
87a42246 12 } else {
5638aaac 13 unshift @INC, 't';
87a42246 14 }
9cd8f857 15 require Config;
16 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
17 print "1..0 # Skip -- Perl configured without B module\n";
18 exit 0;
19 }
87a42246 20}
21
87a42246 22use warnings;
23use strict;
e9c69003 24BEGIN {
25 # BEGIN block is acutally a subroutine :-)
26 return unless $] > 5.009;
27 require feature;
28 feature->import(':5.10');
29}
644741fd 30use Test::More tests => 56;
87a42246 31
32use B::Deparse;
09d856fb 33my $deparse = B::Deparse->new();
34ok($deparse);
87a42246 35
36# Tell B::Deparse about our ambient pragmas
0ced6c29 37{ my ($hint_bits, $warning_bits, $hinthash);
38 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
87a42246 39 $deparse->ambient_pragmas (
40 hint_bits => $hint_bits,
41 warning_bits => $warning_bits,
0ced6c29 42 '$[' => 0 + $[,
43 '%^H' => $hinthash,
87a42246 44 );
45}
46
ad46c0be 47$/ = "\n####\n";
48while (<DATA>) {
49 chomp;
e9c69003 50 # This code is pinched from the t/lib/common.pl for TODO.
51 # It's not clear how to avoid duplication
52 my ($skip, $skip_reason);
53 s/^#\s*SKIP\s*(.*)\n//m and $skip_reason = $1;
54 # If the SKIP reason starts ? then it's taken as a code snippet to evaluate
55 # This provides the flexibility to have conditional SKIPs
56 if ($skip_reason && $skip_reason =~ s/^\?//) {
57 my $temp = eval $skip_reason;
58 if ($@) {
59 die "# In SKIP code reason:\n# $skip_reason\n$@";
60 }
61 $skip_reason = $temp;
62 }
63
ec59cdf2 64 s/#\s*(.*)$//mg;
65 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
e9c69003 66
67 if ($skip_reason) {
68 # Like this to avoid needing a label SKIP:
69 Test::More->builder->skip($skip_reason);
70 next;
71 }
72
ad46c0be 73 my ($input, $expected);
74 if (/(.*)\n>>>>\n(.*)/s) {
75 ($input, $expected) = ($1, $2);
76 }
77 else {
78 ($input, $expected) = ($_, $_);
79 }
87a42246 80
ad46c0be 81 my $coderef = eval "sub {$input}";
87a42246 82
ad46c0be 83 if ($@) {
ec59cdf2 84 diag("$num deparsed: $@");
85 ok(0, $testname);
ad46c0be 86 }
87 else {
88 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a 89 my $regex = $expected;
90 $regex =~ s/(\S+)/\Q$1/g;
91 $regex =~ s/\s+/\\s+/g;
92 $regex = '^\{\s*' . $regex . '\s*\}$';
ec59cdf2 93 like($deparsed, qr/$regex/, $testname);
87a42246 94 }
87a42246 95}
96
87a42246 97use constant 'c', 'stuff';
09d856fb 98is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
87a42246 99
09d856fb 100my $a = 0;
101is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
87a42246 102
d989cdac 103use constant cr => ['hello'];
104my $string = "sub " . $deparse->coderef2text(\&cr);
105my $val = (eval $string)->();
09d856fb 106ok( ref($val) eq 'ARRAY' && $val->[0] eq 'hello');
87a42246 107
87a42246 108my $Is_VMS = $^O eq 'VMS';
109my $Is_MacOS = $^O eq 'MacOS';
110
111my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 112$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246 113my $redir = $Is_MacOS ? "" : "2>&1";
114
d2bc402e 115$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`;
e69a2255 116$a =~ s/-e syntax OK\n//g;
d2bc402e 117$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246 118$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
119$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
120$b = <<'EOF';
d2bc402e 121BEGIN { $^I = ".bak"; }
122BEGIN { $^W = 1; }
123BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246 124LINE: while (defined($_ = <ARGV>)) {
125 chomp $_;
f86ea535 126 our(@F) = split(' ', $_, 0);
87a42246 127 '???';
128}
87a42246 129EOF
e69a2255 130$b =~ s/(LINE:)/sub BEGIN {
131 'MacPerl'->bootstrap;
132 'OSA'->bootstrap;
133 'XL'->bootstrap;
134}
135$1/ if $Is_MacOS;
09d856fb 136is($a, $b);
87a42246 137
579a54dc 138#Re: perlbug #35857, patch #24505
b3980c39 139#handle warnings::register-ed packages properly.
140package B::Deparse::Wrapper;
141use strict;
142use warnings;
143use warnings::register;
144sub getcode {
579a54dc 145 my $deparser = B::Deparse->new();
b3980c39 146 return $deparser->coderef2text(shift);
147}
148
149package main;
150use strict;
151use warnings;
152sub test {
579a54dc 153 my $val = shift;
154 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 155 like( $res, qr/use warnings/);
b3980c39 156}
157my ($q,$p);
158my $x=sub { ++$q,++$p };
159test($x);
160eval <<EOFCODE and test($x);
161 package bar;
162 use strict;
163 use warnings;
164 use warnings::register;
165 package main;
166 1
167EOFCODE
168
ad46c0be 169__DATA__
14a55f98 170# 2
ad46c0be 1711;
172####
14a55f98 173# 3
ad46c0be 174{
175 no warnings;
176 '???';
177 2;
178}
179####
14a55f98 180# 4
ad46c0be 181my $test;
182++$test and $test /= 2;
183>>>>
184my $test;
185$test /= 2 if ++$test;
186####
14a55f98 187# 5
ad46c0be 188-((1, 2) x 2);
189####
14a55f98 190# 6
ad46c0be 191{
192 my $test = sub : lvalue {
193 my $x;
194 }
195 ;
196}
197####
14a55f98 198# 7
ad46c0be 199{
200 my $test = sub : method {
201 my $x;
202 }
203 ;
204}
205####
14a55f98 206# 8
ad46c0be 207{
208 my $test = sub : locked method {
209 my $x;
210 }
211 ;
212}
213####
14a55f98 214# 9
87a42246 215{
ad46c0be 216 234;
f99a63a2 217}
ad46c0be 218continue {
219 123;
87a42246 220}
ce4e655d 221####
14a55f98 222# 10
ce4e655d 223my $x;
224print $main::x;
225####
14a55f98 226# 11
ce4e655d 227my @x;
228print $main::x[1];
14a55f98 229####
230# 12
231my %x;
232$x{warn()};
ad8caead 233####
234# 13
235my $foo;
236$_ .= <ARGV> . <$foo>;
cef22867 237####
238# 14
239my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81 240####
241# 15
242s/x/'y';/e;
241416b8 243####
244# 16 - various lypes of loop
245{ my $x; }
246####
247# 17
248while (1) { my $k; }
249####
250# 18
251my ($x,@a);
252$x=1 for @a;
253>>>>
254my($x, @a);
0bb5f065 255$x = 1 foreach (@a);
241416b8 256####
257# 19
258for (my $i = 0; $i < 2;) {
259 my $z = 1;
260}
261####
262# 20
263for (my $i = 0; $i < 2; ++$i) {
264 my $z = 1;
265}
266####
267# 21
268for (my $i = 0; $i < 2; ++$i) {
269 my $z = 1;
270}
271####
272# 22
273my $i;
274while ($i) { my $z = 1; } continue { $i = 99; }
275####
276# 23
09d856fb 277foreach my $i (1, 2) {
241416b8 278 my $z = 1;
279}
280####
281# 24
282my $i;
283foreach $i (1, 2) {
284 my $z = 1;
285}
286####
287# 25
288my $i;
289foreach my $i (1, 2) {
290 my $z = 1;
291}
292####
293# 26
294foreach my $i (1, 2) {
295 my $z = 1;
296}
297####
298# 27
299foreach our $i (1, 2) {
300 my $z = 1;
301}
302####
303# 28
304my $i;
305foreach our $i (1, 2) {
306 my $z = 1;
307}
3ac6e0f9 308####
309# 29
310my @x;
311print reverse sort(@x);
312####
313# 30
314my @x;
315print((sort {$b cmp $a} @x));
316####
317# 31
318my @x;
319print((reverse sort {$b <=> $a} @x));
36d57d93 320####
321# 32
322our @a;
323print $_ foreach (reverse @a);
aae53c41 324####
579a54dc 325# 33
aae53c41 326our @a;
327print $_ foreach (reverse 1, 2..5);
f86ea535 328####
329# 34 (bug #38684)
330our @ary;
331@ary = split(' ', 'foo', 0);
31c6271a 332####
333# 35 (bug #40055)
334do { () };
335####
336# 36 (ibid.)
337do { my $x = 1; $x };
d9002312 338####
339# 37 <20061012113037.GJ25805@c4.convolution.nl>
340my $f = sub {
341 +{[]};
342} ;
8b2d6640 343####
344# 38 (bug #43010)
345'!@$%'->();
346####
347# 39 (ibid.)
348::();
349####
350# 40 (ibid.)
351'::::'->();
352####
353# 41 (ibid.)
354&::::;
09d856fb 355####
356# 42
357my $bar;
358'Foo'->$bar('orz');
359####
360# 43
361'Foo'->bar('orz');
362####
363# 44
364'Foo'->bar;
0ced6c29 365####
e9c69003 366# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01 367# 45 say
368say 'foo';
369####
e9c69003 370# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 371# 46 state vars
0ced6c29 372state $x = 42;
373####
e9c69003 374# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 375# 47 state var assignment
376{
377 my $y = (state $x = 42);
378}
379####
e9c69003 380# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 381# 48 state vars in anoymous subroutines
382$a = sub {
383 state $x;
384 return $x++;
385}
386;
644741fd 387####
388# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
389# 49 each @array;
390each @ARGV;
391each @$a;
392####
393# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
394# 50 keys @array; values @array
395keys @$a if keys @ARGV;
396values @ARGV if values @$a;