This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add Rainer Tammer and Torsten Schönfeld to AUTHORS.
[perl5.git] / ext / B / t / deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
5638aaac
SM
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
NC
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
MS
20}
21
87a42246
MS
22use warnings;
23use strict;
e9c69003
NC
24BEGIN {
25 # BEGIN block is acutally a subroutine :-)
26 return unless $] > 5.009;
27 require feature;
28 feature->import(':5.10');
29}
2990415a 30use Test::More tests => 74;
1bb3cfc5 31use Config ();
87a42246
MS
32
33use B::Deparse;
09d856fb
CK
34my $deparse = B::Deparse->new();
35ok($deparse);
87a42246
MS
36
37# Tell B::Deparse about our ambient pragmas
0ced6c29
RGS
38{ my ($hint_bits, $warning_bits, $hinthash);
39 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
87a42246
MS
40 $deparse->ambient_pragmas (
41 hint_bits => $hint_bits,
42 warning_bits => $warning_bits,
0ced6c29
RGS
43 '$[' => 0 + $[,
44 '%^H' => $hinthash,
87a42246
MS
45 );
46}
47
ad46c0be
RH
48$/ = "\n####\n";
49while (<DATA>) {
50 chomp;
e9c69003
NC
51 # This code is pinched from the t/lib/common.pl for TODO.
52 # It's not clear how to avoid duplication
53 my ($skip, $skip_reason);
54 s/^#\s*SKIP\s*(.*)\n//m and $skip_reason = $1;
55 # If the SKIP reason starts ? then it's taken as a code snippet to evaluate
56 # This provides the flexibility to have conditional SKIPs
57 if ($skip_reason && $skip_reason =~ s/^\?//) {
58 my $temp = eval $skip_reason;
59 if ($@) {
60 die "# In SKIP code reason:\n# $skip_reason\n$@";
61 }
62 $skip_reason = $temp;
63 }
64
ec59cdf2
RGS
65 s/#\s*(.*)$//mg;
66 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
e9c69003
NC
67
68 if ($skip_reason) {
69 # Like this to avoid needing a label SKIP:
70 Test::More->builder->skip($skip_reason);
71 next;
72 }
73
ad46c0be
RH
74 my ($input, $expected);
75 if (/(.*)\n>>>>\n(.*)/s) {
76 ($input, $expected) = ($1, $2);
77 }
78 else {
79 ($input, $expected) = ($_, $_);
80 }
87a42246 81
ad46c0be 82 my $coderef = eval "sub {$input}";
87a42246 83
ad46c0be 84 if ($@) {
ec59cdf2
RGS
85 diag("$num deparsed: $@");
86 ok(0, $testname);
ad46c0be
RH
87 }
88 else {
89 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a
RD
90 my $regex = $expected;
91 $regex =~ s/(\S+)/\Q$1/g;
92 $regex =~ s/\s+/\\s+/g;
93 $regex = '^\{\s*' . $regex . '\s*\}$';
ec59cdf2 94 like($deparsed, qr/$regex/, $testname);
87a42246 95 }
87a42246
MS
96}
97
87a42246 98use constant 'c', 'stuff';
09d856fb 99is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
87a42246 100
09d856fb
CK
101my $a = 0;
102is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
87a42246 103
d989cdac
SM
104use constant cr => ['hello'];
105my $string = "sub " . $deparse->coderef2text(\&cr);
0707d6cc
NC
106my $val = (eval $string)->() or diag $string;
107is(ref($val), 'ARRAY');
108is($val->[0], 'hello');
87a42246 109
87a42246
MS
110my $Is_VMS = $^O eq 'VMS';
111my $Is_MacOS = $^O eq 'MacOS';
112
113my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 114$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246
MS
115my $redir = $Is_MacOS ? "" : "2>&1";
116
d2bc402e 117$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 $redir`;
e69a2255 118$a =~ s/-e syntax OK\n//g;
d2bc402e 119$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246
MS
120$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
121$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
122$b = <<'EOF';
d2bc402e
RGS
123BEGIN { $^I = ".bak"; }
124BEGIN { $^W = 1; }
125BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246
MS
126LINE: while (defined($_ = <ARGV>)) {
127 chomp $_;
f86ea535 128 our(@F) = split(' ', $_, 0);
87a42246
MS
129 '???';
130}
87a42246 131EOF
e69a2255
JH
132$b =~ s/(LINE:)/sub BEGIN {
133 'MacPerl'->bootstrap;
134 'OSA'->bootstrap;
135 'XL'->bootstrap;
136}
137$1/ if $Is_MacOS;
09d856fb 138is($a, $b);
87a42246 139
579a54dc 140#Re: perlbug #35857, patch #24505
b3980c39
YO
141#handle warnings::register-ed packages properly.
142package B::Deparse::Wrapper;
143use strict;
144use warnings;
145use warnings::register;
146sub getcode {
579a54dc 147 my $deparser = B::Deparse->new();
b3980c39
YO
148 return $deparser->coderef2text(shift);
149}
150
2990415a
FR
151package Moo;
152use overload '0+' => sub { 42 };
153
b3980c39
YO
154package main;
155use strict;
156use warnings;
71c4dbc3 157use constant GLIPP => 'glipp';
2990415a
FR
158use constant PI => 4;
159use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
aaf9c2b2
NC
160use Fcntl qw/O_EXCL O_APPEND O_EXCL/;
161BEGIN { delete $::Fcntl::{O_APPEND}; }
2990415a 162use POSIX qw/O_CREAT/;
b3980c39 163sub test {
579a54dc
RGS
164 my $val = shift;
165 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 166 like( $res, qr/use warnings/);
b3980c39
YO
167}
168my ($q,$p);
169my $x=sub { ++$q,++$p };
170test($x);
171eval <<EOFCODE and test($x);
172 package bar;
173 use strict;
174 use warnings;
175 use warnings::register;
176 package main;
177 1
178EOFCODE
179
ad46c0be 180__DATA__
14a55f98 181# 2
ad46c0be
RH
1821;
183####
14a55f98 184# 3
ad46c0be
RH
185{
186 no warnings;
187 '???';
188 2;
189}
190####
14a55f98 191# 4
ad46c0be
RH
192my $test;
193++$test and $test /= 2;
194>>>>
195my $test;
196$test /= 2 if ++$test;
197####
14a55f98 198# 5
ad46c0be
RH
199-((1, 2) x 2);
200####
14a55f98 201# 6
ad46c0be
RH
202{
203 my $test = sub : lvalue {
204 my $x;
205 }
206 ;
207}
208####
14a55f98 209# 7
ad46c0be
RH
210{
211 my $test = sub : method {
212 my $x;
213 }
214 ;
215}
216####
14a55f98 217# 8
ad46c0be
RH
218{
219 my $test = sub : locked method {
220 my $x;
221 }
222 ;
223}
224####
14a55f98 225# 9
87a42246 226{
ad46c0be 227 234;
f99a63a2 228}
ad46c0be
RH
229continue {
230 123;
87a42246 231}
ce4e655d 232####
14a55f98 233# 10
ce4e655d
RH
234my $x;
235print $main::x;
236####
14a55f98 237# 11
ce4e655d
RH
238my @x;
239print $main::x[1];
14a55f98
RH
240####
241# 12
242my %x;
243$x{warn()};
ad8caead
RGS
244####
245# 13
246my $foo;
247$_ .= <ARGV> . <$foo>;
cef22867
JH
248####
249# 14
250my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81
RGS
251####
252# 15
253s/x/'y';/e;
241416b8
DM
254####
255# 16 - various lypes of loop
256{ my $x; }
257####
258# 17
259while (1) { my $k; }
260####
261# 18
262my ($x,@a);
263$x=1 for @a;
264>>>>
265my($x, @a);
0bb5f065 266$x = 1 foreach (@a);
241416b8
DM
267####
268# 19
269for (my $i = 0; $i < 2;) {
270 my $z = 1;
271}
272####
273# 20
274for (my $i = 0; $i < 2; ++$i) {
275 my $z = 1;
276}
277####
278# 21
279for (my $i = 0; $i < 2; ++$i) {
280 my $z = 1;
281}
282####
283# 22
284my $i;
285while ($i) { my $z = 1; } continue { $i = 99; }
286####
287# 23
09d856fb 288foreach my $i (1, 2) {
241416b8
DM
289 my $z = 1;
290}
291####
292# 24
293my $i;
294foreach $i (1, 2) {
295 my $z = 1;
296}
297####
298# 25
299my $i;
300foreach my $i (1, 2) {
301 my $z = 1;
302}
303####
304# 26
305foreach my $i (1, 2) {
306 my $z = 1;
307}
308####
309# 27
310foreach our $i (1, 2) {
311 my $z = 1;
312}
313####
314# 28
315my $i;
316foreach our $i (1, 2) {
317 my $z = 1;
318}
3ac6e0f9
RGS
319####
320# 29
321my @x;
322print reverse sort(@x);
323####
324# 30
325my @x;
326print((sort {$b cmp $a} @x));
327####
328# 31
329my @x;
330print((reverse sort {$b <=> $a} @x));
36d57d93
RGS
331####
332# 32
333our @a;
334print $_ foreach (reverse @a);
aae53c41 335####
579a54dc 336# 33
aae53c41
RGS
337our @a;
338print $_ foreach (reverse 1, 2..5);
f86ea535
SM
339####
340# 34 (bug #38684)
341our @ary;
342@ary = split(' ', 'foo', 0);
31c6271a
RD
343####
344# 35 (bug #40055)
345do { () };
346####
347# 36 (ibid.)
348do { my $x = 1; $x };
d9002312
SM
349####
350# 37 <20061012113037.GJ25805@c4.convolution.nl>
351my $f = sub {
352 +{[]};
353} ;
8b2d6640
FC
354####
355# 38 (bug #43010)
356'!@$%'->();
357####
358# 39 (ibid.)
359::();
360####
361# 40 (ibid.)
362'::::'->();
363####
364# 41 (ibid.)
365&::::;
09d856fb
CK
366####
367# 42
368my $bar;
369'Foo'->$bar('orz');
370####
371# 43
372'Foo'->bar('orz');
373####
374# 44
375'Foo'->bar;
0ced6c29 376####
e9c69003 377# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01
NC
378# 45 say
379say 'foo';
380####
e9c69003 381# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 382# 46 state vars
0ced6c29
RGS
383state $x = 42;
384####
e9c69003 385# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
386# 47 state var assignment
387{
388 my $y = (state $x = 42);
389}
390####
e9c69003 391# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
392# 48 state vars in anoymous subroutines
393$a = sub {
394 state $x;
395 return $x++;
396}
397;
644741fd
NC
398####
399# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
400# 49 each @array;
401each @ARGV;
402each @$a;
403####
404# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
405# 50 keys @array; values @array
406keys @$a if keys @ARGV;
407values @ARGV if values @$a;
35925e80 408####
43b09ad7 409# 51 Anonymous arrays and hashes, and references to them
35925e80
RGS
410my $a = {};
411my $b = \{};
412my $c = [];
413my $d = \[];
9210de83
FR
414####
415# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
43b09ad7 416# 52 implicit smartmatch in given/when
9210de83
FR
417given ('foo') {
418 when ('bar') { continue; }
419 when ($_ ~~ 'quux') { continue; }
420 default { 0; }
421}
7ecdd211
PJ
422####
423# 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
424if ($a) { x(); }
425elsif ($b) { x(); }
426elsif ($a and $b) { x(); }
427elsif ($a or $b) { x(); }
428else { x(); }
03b22f1b
RGS
429####
430# 54 interpolation in regexps
431my($y, $t);
432/x${y}z$t/;
227375e1 433####
2990415a 434# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO new undocumented cpan-bug #33708"
227375e1
RU
435# 55 (cpan-bug #33708)
436%{$_ || {}}
437####
2990415a 438# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO hash constants not yet fixed"
227375e1
RU
439# 56 (cpan-bug #33708)
440use constant H => { "#" => 1 }; H->{"#"}
441####
2990415a 442# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO optimized away 0 not yet fixed"
227375e1
RU
443# 57 (cpan-bug #33708)
444foreach my $i (@_) { 0 }
edbe35ea
VP
445####
446# 58 tests with not, not optimized
07f3cdf5 447my $c;
edbe35ea
VP
448x() unless $a;
449x() if not $a and $b;
450x() if $a and not $b;
451x() unless not $a and $b;
452x() unless $a and not $b;
453x() if not $a or $b;
454x() if $a or not $b;
455x() unless not $a or $b;
456x() unless $a or not $b;
07f3cdf5
VP
457x() if $a and not $b and $c;
458x() if not $a and $b and not $c;
459x() unless $a and not $b and $c;
460x() unless not $a and $b and not $c;
461x() if $a or not $b or $c;
462x() if not $a or $b or not $c;
463x() unless $a or not $b or $c;
464x() unless not $a or $b or not $c;
edbe35ea
VP
465####
466# 59 tests with not, optimized
07f3cdf5 467my $c;
edbe35ea
VP
468x() if not $a;
469x() unless not $a;
470x() if not $a and not $b;
471x() unless not $a and not $b;
472x() if not $a or not $b;
473x() unless not $a or not $b;
07f3cdf5
VP
474x() if not $a and not $b and $c;
475x() unless not $a and not $b and $c;
476x() if not $a or not $b or $c;
477x() unless not $a or not $b or $c;
478x() if not $a and not $b and not $c;
479x() unless not $a and not $b and not $c;
480x() if not $a or not $b or not $c;
481x() unless not $a or not $b or not $c;
482x() unless not $a or not $b or not $c;
edbe35ea 483>>>>
07f3cdf5 484my $c;
edbe35ea
VP
485x() unless $a;
486x() if $a;
487x() unless $a or $b;
488x() if $a or $b;
489x() unless $a and $b;
07f3cdf5
VP
490x() if $a and $b;
491x() if not $a || $b and $c;
492x() unless not $a || $b and $c;
493x() if not $a && $b or $c;
494x() unless not $a && $b or $c;
495x() unless $a or $b or $c;
496x() if $a or $b or $c;
497x() unless $a and $b and $c;
498x() if $a and $b and $c;
499x() unless not $a && $b && $c;
71c4dbc3
VP
500####
501# 60 tests that should be constant folded
502x() if 1;
503x() if GLIPP;
504x() if !GLIPP;
505x() if GLIPP && GLIPP;
506x() if !GLIPP || GLIPP;
507x() if do { GLIPP };
508x() if do { no warnings 'void'; 5; GLIPP };
509x() if do { !GLIPP };
510if (GLIPP) { x() } else { z() }
511if (!GLIPP) { x() } else { z() }
512if (GLIPP) { x() } elsif (GLIPP) { z() }
513if (!GLIPP) { x() } elsif (GLIPP) { z() }
514if (GLIPP) { x() } elsif (!GLIPP) { z() }
515if (!GLIPP) { x() } elsif (!GLIPP) { z() }
516if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
517if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
518if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
519>>>>
520x();
521x();
522'???';
523x();
524x();
525x();
526x();
527do {
528 '???'
529};
530do {
531 x()
532};
533do {
534 z()
535};
536do {
537 x()
538};
539do {
540 z()
541};
542do {
543 x()
544};
545'???';
546do {
547 t()
548};
549'???';
550!1;
551####
1bb3cfc5 552# SKIP ? $Config::Config{useithreads} && "TODO doesn't work with threads"
71c4dbc3
VP
553# 61 tests that shouldn't be constant folded
554x() if $a;
555if ($a == 1) { x() } elsif ($b == 2) { z() }
556if (do { foo(); GLIPP }) { x() }
557if (do { $a++; GLIPP }) { x() }
558>>>>
559x() if $a;
560if ($a == 1) { x(); } elsif ($b == 2) { z(); }
2990415a
FR
561if (do { foo(); GLIPP }) { x(); }
562if (do { ++$a; GLIPP }) { x(); }
563####
564# 62 tests for deparsing constants
565warn PI;
566####
567# 63 tests for deparsing imported constants
aaf9c2b2 568warn O_EXCL;
2990415a
FR
569####
570# 64 tests for deparsing re-exported constants
571warn O_CREAT;
572####
573# 65 tests for deparsing imported constants that got deleted from the original namespace
aaf9c2b2 574warn O_APPEND;
2990415a 575####
1bb3cfc5 576# SKIP ? $Config::Config{useithreads} && "TODO doesn't work with threads"
2990415a
FR
577# 66 tests for deparsing constants which got turned into full typeglobs
578warn O_EXCL;
579eval '@Fcntl::O_EXCL = qw/affe tiger/;';
580warn O_EXCL;
581####
582# 67 tests for deparsing of blessed constant with overloaded numification
583warn OVERLOADED_NUMIFICATION;