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