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