This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove CVf_LOCKED and CvLOCKED*(), now that nothing sets that flag bit.
[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');
3779476a 160use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
aaf9c2b2 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
8e5dadda
NC
218# Was sub : locked method { ... }
219# This number could be re-used.
ad46c0be 220####
14a55f98 221# 9
87a42246 222{
ad46c0be 223 234;
f99a63a2 224}
ad46c0be
RH
225continue {
226 123;
87a42246 227}
ce4e655d 228####
14a55f98 229# 10
ce4e655d
RH
230my $x;
231print $main::x;
232####
14a55f98 233# 11
ce4e655d
RH
234my @x;
235print $main::x[1];
14a55f98
RH
236####
237# 12
238my %x;
239$x{warn()};
ad8caead
RGS
240####
241# 13
242my $foo;
243$_ .= <ARGV> . <$foo>;
cef22867
JH
244####
245# 14
246my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81
RGS
247####
248# 15
249s/x/'y';/e;
241416b8
DM
250####
251# 16 - various lypes of loop
252{ my $x; }
253####
254# 17
255while (1) { my $k; }
256####
257# 18
258my ($x,@a);
259$x=1 for @a;
260>>>>
261my($x, @a);
0bb5f065 262$x = 1 foreach (@a);
241416b8
DM
263####
264# 19
265for (my $i = 0; $i < 2;) {
266 my $z = 1;
267}
268####
269# 20
270for (my $i = 0; $i < 2; ++$i) {
271 my $z = 1;
272}
273####
274# 21
275for (my $i = 0; $i < 2; ++$i) {
276 my $z = 1;
277}
278####
279# 22
280my $i;
281while ($i) { my $z = 1; } continue { $i = 99; }
282####
283# 23
09d856fb 284foreach my $i (1, 2) {
241416b8
DM
285 my $z = 1;
286}
287####
288# 24
289my $i;
290foreach $i (1, 2) {
291 my $z = 1;
292}
293####
294# 25
295my $i;
296foreach my $i (1, 2) {
297 my $z = 1;
298}
299####
300# 26
301foreach my $i (1, 2) {
302 my $z = 1;
303}
304####
305# 27
306foreach our $i (1, 2) {
307 my $z = 1;
308}
309####
310# 28
311my $i;
312foreach our $i (1, 2) {
313 my $z = 1;
314}
3ac6e0f9
RGS
315####
316# 29
317my @x;
318print reverse sort(@x);
319####
320# 30
321my @x;
322print((sort {$b cmp $a} @x));
323####
324# 31
325my @x;
326print((reverse sort {$b <=> $a} @x));
36d57d93
RGS
327####
328# 32
329our @a;
330print $_ foreach (reverse @a);
aae53c41 331####
579a54dc 332# 33
aae53c41
RGS
333our @a;
334print $_ foreach (reverse 1, 2..5);
f86ea535
SM
335####
336# 34 (bug #38684)
337our @ary;
338@ary = split(' ', 'foo', 0);
31c6271a
RD
339####
340# 35 (bug #40055)
341do { () };
342####
343# 36 (ibid.)
344do { my $x = 1; $x };
d9002312
SM
345####
346# 37 <20061012113037.GJ25805@c4.convolution.nl>
347my $f = sub {
348 +{[]};
349} ;
8b2d6640
FC
350####
351# 38 (bug #43010)
352'!@$%'->();
353####
354# 39 (ibid.)
355::();
356####
357# 40 (ibid.)
358'::::'->();
359####
360# 41 (ibid.)
361&::::;
09d856fb
CK
362####
363# 42
364my $bar;
365'Foo'->$bar('orz');
366####
367# 43
368'Foo'->bar('orz');
369####
370# 44
371'Foo'->bar;
0ced6c29 372####
e9c69003 373# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01
NC
374# 45 say
375say 'foo';
376####
e9c69003 377# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 378# 46 state vars
0ced6c29
RGS
379state $x = 42;
380####
e9c69003 381# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
382# 47 state var assignment
383{
384 my $y = (state $x = 42);
385}
386####
e9c69003 387# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
388# 48 state vars in anoymous subroutines
389$a = sub {
390 state $x;
391 return $x++;
392}
393;
644741fd
NC
394####
395# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
396# 49 each @array;
397each @ARGV;
398each @$a;
399####
400# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
401# 50 keys @array; values @array
402keys @$a if keys @ARGV;
403values @ARGV if values @$a;
35925e80 404####
43b09ad7 405# 51 Anonymous arrays and hashes, and references to them
35925e80
RGS
406my $a = {};
407my $b = \{};
408my $c = [];
409my $d = \[];
9210de83
FR
410####
411# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
43b09ad7 412# 52 implicit smartmatch in given/when
9210de83
FR
413given ('foo') {
414 when ('bar') { continue; }
415 when ($_ ~~ 'quux') { continue; }
416 default { 0; }
417}
7ecdd211
PJ
418####
419# 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
420if ($a) { x(); }
421elsif ($b) { x(); }
422elsif ($a and $b) { x(); }
423elsif ($a or $b) { x(); }
424else { x(); }
03b22f1b
RGS
425####
426# 54 interpolation in regexps
427my($y, $t);
428/x${y}z$t/;
227375e1 429####
2990415a 430# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO new undocumented cpan-bug #33708"
227375e1
RU
431# 55 (cpan-bug #33708)
432%{$_ || {}}
433####
2990415a 434# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO hash constants not yet fixed"
227375e1
RU
435# 56 (cpan-bug #33708)
436use constant H => { "#" => 1 }; H->{"#"}
437####
2990415a 438# SKIP ?$B::Deparse::VERSION <= 0.88 && "TODO optimized away 0 not yet fixed"
227375e1
RU
439# 57 (cpan-bug #33708)
440foreach my $i (@_) { 0 }
edbe35ea
VP
441####
442# 58 tests with not, not optimized
07f3cdf5 443my $c;
edbe35ea
VP
444x() unless $a;
445x() if not $a and $b;
446x() if $a and not $b;
447x() unless not $a and $b;
448x() unless $a and not $b;
449x() if not $a or $b;
450x() if $a or not $b;
451x() unless not $a or $b;
452x() unless $a or not $b;
07f3cdf5
VP
453x() if $a and not $b and $c;
454x() if not $a and $b and not $c;
455x() unless $a and not $b and $c;
456x() unless not $a and $b and not $c;
457x() if $a or not $b or $c;
458x() if not $a or $b or not $c;
459x() unless $a or not $b or $c;
460x() unless not $a or $b or not $c;
edbe35ea
VP
461####
462# 59 tests with not, optimized
07f3cdf5 463my $c;
edbe35ea
VP
464x() if not $a;
465x() unless not $a;
466x() if not $a and not $b;
467x() unless not $a and not $b;
468x() if not $a or not $b;
469x() unless not $a or not $b;
07f3cdf5
VP
470x() if not $a and not $b and $c;
471x() unless not $a and not $b and $c;
472x() if not $a or not $b or $c;
473x() unless not $a or not $b or $c;
474x() if not $a and not $b and not $c;
475x() unless not $a and not $b and not $c;
476x() if not $a or not $b or not $c;
477x() unless not $a or not $b or not $c;
478x() unless not $a or not $b or not $c;
edbe35ea 479>>>>
07f3cdf5 480my $c;
edbe35ea
VP
481x() unless $a;
482x() if $a;
483x() unless $a or $b;
484x() if $a or $b;
485x() unless $a and $b;
07f3cdf5
VP
486x() if $a and $b;
487x() if not $a || $b and $c;
488x() unless not $a || $b and $c;
489x() if not $a && $b or $c;
490x() unless not $a && $b or $c;
491x() unless $a or $b or $c;
492x() if $a or $b or $c;
493x() unless $a and $b and $c;
494x() if $a and $b and $c;
495x() unless not $a && $b && $c;
71c4dbc3
VP
496####
497# 60 tests that should be constant folded
498x() if 1;
499x() if GLIPP;
500x() if !GLIPP;
501x() if GLIPP && GLIPP;
502x() if !GLIPP || GLIPP;
503x() if do { GLIPP };
504x() if do { no warnings 'void'; 5; GLIPP };
505x() if do { !GLIPP };
506if (GLIPP) { x() } else { z() }
507if (!GLIPP) { x() } else { z() }
508if (GLIPP) { x() } elsif (GLIPP) { z() }
509if (!GLIPP) { x() } elsif (GLIPP) { z() }
510if (GLIPP) { x() } elsif (!GLIPP) { z() }
511if (!GLIPP) { x() } elsif (!GLIPP) { z() }
512if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
513if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
514if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
515>>>>
516x();
517x();
518'???';
519x();
520x();
521x();
522x();
523do {
524 '???'
525};
526do {
527 x()
528};
529do {
530 z()
531};
532do {
533 x()
534};
535do {
536 z()
537};
538do {
539 x()
540};
541'???';
542do {
543 t()
544};
545'???';
546!1;
547####
1bb3cfc5 548# SKIP ? $Config::Config{useithreads} && "TODO doesn't work with threads"
71c4dbc3
VP
549# 61 tests that shouldn't be constant folded
550x() if $a;
551if ($a == 1) { x() } elsif ($b == 2) { z() }
552if (do { foo(); GLIPP }) { x() }
553if (do { $a++; GLIPP }) { x() }
554>>>>
555x() if $a;
556if ($a == 1) { x(); } elsif ($b == 2) { z(); }
2990415a
FR
557if (do { foo(); GLIPP }) { x(); }
558if (do { ++$a; GLIPP }) { x(); }
559####
560# 62 tests for deparsing constants
561warn PI;
562####
563# 63 tests for deparsing imported constants
3779476a 564warn O_TRUNC;
2990415a
FR
565####
566# 64 tests for deparsing re-exported constants
567warn O_CREAT;
568####
569# 65 tests for deparsing imported constants that got deleted from the original namespace
aaf9c2b2 570warn O_APPEND;
2990415a 571####
1bb3cfc5 572# SKIP ? $Config::Config{useithreads} && "TODO doesn't work with threads"
2990415a
FR
573# 66 tests for deparsing constants which got turned into full typeglobs
574warn O_EXCL;
575eval '@Fcntl::O_EXCL = qw/affe tiger/;';
576warn O_EXCL;
577####
578# 67 tests for deparsing of blessed constant with overloaded numification
579warn OVERLOADED_NUMIFICATION;