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