Add Tom Wyant to AUTHORS.
[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}
71c4dbc3 30use Test::More tests => 68;
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);
0707d6cc 105my $val = (eval $string)->() or diag $string;
106is(ref($val), 'ARRAY');
107is($val->[0], 'hello');
87a42246 108
87a42246 109my $Is_VMS = $^O eq 'VMS';
110my $Is_MacOS = $^O eq 'MacOS';
111
112my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 113$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246 114my $redir = $Is_MacOS ? "" : "2>&1";
115
d2bc402e 116$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`;
e69a2255 117$a =~ s/-e syntax OK\n//g;
d2bc402e 118$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246 119$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
120$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
121$b = <<'EOF';
d2bc402e 122BEGIN { $^I = ".bak"; }
123BEGIN { $^W = 1; }
124BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246 125LINE: while (defined($_ = <ARGV>)) {
126 chomp $_;
f86ea535 127 our(@F) = split(' ', $_, 0);
87a42246 128 '???';
129}
87a42246 130EOF
e69a2255 131$b =~ s/(LINE:)/sub BEGIN {
132 'MacPerl'->bootstrap;
133 'OSA'->bootstrap;
134 'XL'->bootstrap;
135}
136$1/ if $Is_MacOS;
09d856fb 137is($a, $b);
87a42246 138
579a54dc 139#Re: perlbug #35857, patch #24505
b3980c39 140#handle warnings::register-ed packages properly.
141package B::Deparse::Wrapper;
142use strict;
143use warnings;
144use warnings::register;
145sub getcode {
579a54dc 146 my $deparser = B::Deparse->new();
b3980c39 147 return $deparser->coderef2text(shift);
148}
149
150package main;
151use strict;
152use warnings;
71c4dbc3 153use constant GLIPP => 'glipp';
b3980c39 154sub test {
579a54dc 155 my $val = shift;
156 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 157 like( $res, qr/use warnings/);
b3980c39 158}
159my ($q,$p);
160my $x=sub { ++$q,++$p };
161test($x);
162eval <<EOFCODE and test($x);
163 package bar;
164 use strict;
165 use warnings;
166 use warnings::register;
167 package main;
168 1
169EOFCODE
170
ad46c0be 171__DATA__
14a55f98 172# 2
ad46c0be 1731;
174####
14a55f98 175# 3
ad46c0be 176{
177 no warnings;
178 '???';
179 2;
180}
181####
14a55f98 182# 4
ad46c0be 183my $test;
184++$test and $test /= 2;
185>>>>
186my $test;
187$test /= 2 if ++$test;
188####
14a55f98 189# 5
ad46c0be 190-((1, 2) x 2);
191####
14a55f98 192# 6
ad46c0be 193{
194 my $test = sub : lvalue {
195 my $x;
196 }
197 ;
198}
199####
14a55f98 200# 7
ad46c0be 201{
202 my $test = sub : method {
203 my $x;
204 }
205 ;
206}
207####
14a55f98 208# 8
ad46c0be 209{
210 my $test = sub : locked method {
211 my $x;
212 }
213 ;
214}
215####
14a55f98 216# 9
87a42246 217{
ad46c0be 218 234;
f99a63a2 219}
ad46c0be 220continue {
221 123;
87a42246 222}
ce4e655d 223####
14a55f98 224# 10
ce4e655d 225my $x;
226print $main::x;
227####
14a55f98 228# 11
ce4e655d 229my @x;
230print $main::x[1];
14a55f98 231####
232# 12
233my %x;
234$x{warn()};
ad8caead 235####
236# 13
237my $foo;
238$_ .= <ARGV> . <$foo>;
cef22867 239####
240# 14
241my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81 242####
243# 15
244s/x/'y';/e;
241416b8 245####
246# 16 - various lypes of loop
247{ my $x; }
248####
249# 17
250while (1) { my $k; }
251####
252# 18
253my ($x,@a);
254$x=1 for @a;
255>>>>
256my($x, @a);
0bb5f065 257$x = 1 foreach (@a);
241416b8 258####
259# 19
260for (my $i = 0; $i < 2;) {
261 my $z = 1;
262}
263####
264# 20
265for (my $i = 0; $i < 2; ++$i) {
266 my $z = 1;
267}
268####
269# 21
270for (my $i = 0; $i < 2; ++$i) {
271 my $z = 1;
272}
273####
274# 22
275my $i;
276while ($i) { my $z = 1; } continue { $i = 99; }
277####
278# 23
09d856fb 279foreach my $i (1, 2) {
241416b8 280 my $z = 1;
281}
282####
283# 24
284my $i;
285foreach $i (1, 2) {
286 my $z = 1;
287}
288####
289# 25
290my $i;
291foreach my $i (1, 2) {
292 my $z = 1;
293}
294####
295# 26
296foreach my $i (1, 2) {
297 my $z = 1;
298}
299####
300# 27
301foreach our $i (1, 2) {
302 my $z = 1;
303}
304####
305# 28
306my $i;
307foreach our $i (1, 2) {
308 my $z = 1;
309}
3ac6e0f9 310####
311# 29
312my @x;
313print reverse sort(@x);
314####
315# 30
316my @x;
317print((sort {$b cmp $a} @x));
318####
319# 31
320my @x;
321print((reverse sort {$b <=> $a} @x));
36d57d93 322####
323# 32
324our @a;
325print $_ foreach (reverse @a);
aae53c41 326####
579a54dc 327# 33
aae53c41 328our @a;
329print $_ foreach (reverse 1, 2..5);
f86ea535 330####
331# 34 (bug #38684)
332our @ary;
333@ary = split(' ', 'foo', 0);
31c6271a 334####
335# 35 (bug #40055)
336do { () };
337####
338# 36 (ibid.)
339do { my $x = 1; $x };
d9002312 340####
341# 37 <20061012113037.GJ25805@c4.convolution.nl>
342my $f = sub {
343 +{[]};
344} ;
8b2d6640 345####
346# 38 (bug #43010)
347'!@$%'->();
348####
349# 39 (ibid.)
350::();
351####
352# 40 (ibid.)
353'::::'->();
354####
355# 41 (ibid.)
356&::::;
09d856fb 357####
358# 42
359my $bar;
360'Foo'->$bar('orz');
361####
362# 43
363'Foo'->bar('orz');
364####
365# 44
366'Foo'->bar;
0ced6c29 367####
e9c69003 368# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01 369# 45 say
370say 'foo';
371####
e9c69003 372# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 373# 46 state vars
0ced6c29 374state $x = 42;
375####
e9c69003 376# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 377# 47 state var assignment
378{
379 my $y = (state $x = 42);
380}
381####
e9c69003 382# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 383# 48 state vars in anoymous subroutines
384$a = sub {
385 state $x;
386 return $x++;
387}
388;
644741fd 389####
390# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
391# 49 each @array;
392each @ARGV;
393each @$a;
394####
395# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
396# 50 keys @array; values @array
397keys @$a if keys @ARGV;
398values @ARGV if values @$a;
35925e80 399####
43b09ad7 400# 51 Anonymous arrays and hashes, and references to them
35925e80 401my $a = {};
402my $b = \{};
403my $c = [];
404my $d = \[];
9210de83 405####
406# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
43b09ad7 407# 52 implicit smartmatch in given/when
9210de83 408given ('foo') {
409 when ('bar') { continue; }
410 when ($_ ~~ 'quux') { continue; }
411 default { 0; }
412}
7ecdd211 413####
414# 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
415if ($a) { x(); }
416elsif ($b) { x(); }
417elsif ($a and $b) { x(); }
418elsif ($a or $b) { x(); }
419else { x(); }
03b22f1b 420####
421# 54 interpolation in regexps
422my($y, $t);
423/x${y}z$t/;
227375e1 424####
425# SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO new undocumented cpan-bug #33708"
426# 55 (cpan-bug #33708)
427%{$_ || {}}
428####
429# SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO hash constants not yet fixed"
430# 56 (cpan-bug #33708)
431use constant H => { "#" => 1 }; H->{"#"}
432####
433# SKIP ?$B::Deparse::VERSION <= 0.87 && "TODO optimized away 0 not yet fixed"
434# 57 (cpan-bug #33708)
435foreach my $i (@_) { 0 }
edbe35ea 436####
437# 58 tests with not, not optimized
07f3cdf5 438my $c;
edbe35ea 439x() unless $a;
440x() if not $a and $b;
441x() if $a and not $b;
442x() unless not $a and $b;
443x() unless $a and not $b;
444x() if not $a or $b;
445x() if $a or not $b;
446x() unless not $a or $b;
447x() unless $a or not $b;
07f3cdf5 448x() if $a and not $b and $c;
449x() if not $a and $b and not $c;
450x() unless $a and not $b and $c;
451x() unless not $a and $b and not $c;
452x() if $a or not $b or $c;
453x() if not $a or $b or not $c;
454x() unless $a or not $b or $c;
455x() unless not $a or $b or not $c;
edbe35ea 456####
457# 59 tests with not, optimized
07f3cdf5 458my $c;
edbe35ea 459x() if not $a;
460x() unless not $a;
461x() if not $a and not $b;
462x() unless not $a and not $b;
463x() if not $a or not $b;
464x() unless not $a or not $b;
07f3cdf5 465x() if not $a and not $b and $c;
466x() unless not $a and not $b and $c;
467x() if not $a or not $b or $c;
468x() unless not $a or not $b or $c;
469x() if not $a and not $b and not $c;
470x() unless not $a and not $b and not $c;
471x() if not $a or not $b or not $c;
472x() unless not $a or not $b or not $c;
473x() unless not $a or not $b or not $c;
edbe35ea 474>>>>
07f3cdf5 475my $c;
edbe35ea 476x() unless $a;
477x() if $a;
478x() unless $a or $b;
479x() if $a or $b;
480x() unless $a and $b;
07f3cdf5 481x() if $a and $b;
482x() if not $a || $b and $c;
483x() unless not $a || $b and $c;
484x() if not $a && $b or $c;
485x() unless not $a && $b or $c;
486x() unless $a or $b or $c;
487x() if $a or $b or $c;
488x() unless $a and $b and $c;
489x() if $a and $b and $c;
490x() unless not $a && $b && $c;
71c4dbc3 491####
492# 60 tests that should be constant folded
493x() if 1;
494x() if GLIPP;
495x() if !GLIPP;
496x() if GLIPP && GLIPP;
497x() if !GLIPP || GLIPP;
498x() if do { GLIPP };
499x() if do { no warnings 'void'; 5; GLIPP };
500x() if do { !GLIPP };
501if (GLIPP) { x() } else { z() }
502if (!GLIPP) { x() } else { z() }
503if (GLIPP) { x() } elsif (GLIPP) { z() }
504if (!GLIPP) { x() } elsif (GLIPP) { z() }
505if (GLIPP) { x() } elsif (!GLIPP) { z() }
506if (!GLIPP) { x() } elsif (!GLIPP) { z() }
507if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
508if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
509if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
510>>>>
511x();
512x();
513'???';
514x();
515x();
516x();
517x();
518do {
519 '???'
520};
521do {
522 x()
523};
524do {
525 z()
526};
527do {
528 x()
529};
530do {
531 z()
532};
533do {
534 x()
535};
536'???';
537do {
538 t()
539};
540'???';
541!1;
542####
543# 61 tests that shouldn't be constant folded
544x() if $a;
545if ($a == 1) { x() } elsif ($b == 2) { z() }
546if (do { foo(); GLIPP }) { x() }
547if (do { $a++; GLIPP }) { x() }
548>>>>
549x() if $a;
550if ($a == 1) { x(); } elsif ($b == 2) { z(); }
551if (do { foo(); 'glipp' }) { x(); }
552if (do { ++$a; 'glipp' }) { x(); }