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
1#!./perl
2
3BEGIN {
4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = '.';
7 push @INC, '../lib';
8 } else {
9 unshift @INC, 't';
10 }
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 }
16}
17
18use warnings;
19use strict;
20BEGIN {
21 # BEGIN block is acutally a subroutine :-)
22 return unless $] > 5.009;
23 require feature;
24 feature->import(':5.10');
25}
26use Test::More tests => 77;
27use Config ();
28
29use B::Deparse;
30my $deparse = B::Deparse->new();
31ok($deparse);
32
33# Tell B::Deparse about our ambient pragmas
34{ my ($hint_bits, $warning_bits, $hinthash);
35 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
36 $deparse->ambient_pragmas (
37 hint_bits => $hint_bits,
38 warning_bits => $warning_bits,
39 '$[' => 0 + $[,
40 '%^H' => $hinthash,
41 );
42}
43
44$/ = "\n####\n";
45while (<DATA>) {
46 chomp;
47 # This code is pinched from the t/lib/common.pl for TODO.
48 # It's not clear how to avoid duplication
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;
61 }
62 }
63
64 s/^\s*#\s*(.*)$//mg;
65 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
66
67 if ($reason{skip}) {
68 # Like this to avoid needing a label SKIP:
69 Test::More->builder->skip($reason{skip});
70 next;
71 }
72
73 my ($input, $expected);
74 if (/(.*)\n>>>>\n(.*)/s) {
75 ($input, $expected) = ($1, $2);
76 }
77 else {
78 ($input, $expected) = ($_, $_);
79 }
80
81 my $coderef = eval "sub {$input}";
82
83 if ($@) {
84 diag("$num deparsed: $@");
85 ok(0, $testname);
86 }
87 else {
88 my $deparsed = $deparse->coderef2text( $coderef );
89 my $regex = $expected;
90 $regex =~ s/(\S+)/\Q$1/g;
91 $regex =~ s/\s+/\\s+/g;
92 $regex = '^\{\s*' . $regex . '\s*\}$';
93
94 local $::TODO = $reason{todo};
95 like($deparsed, qr/$regex/, $testname);
96 }
97}
98
99use constant 'c', 'stuff';
100is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
101
102my $a = 0;
103is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
104
105use constant cr => ['hello'];
106my $string = "sub " . $deparse->coderef2text(\&cr);
107my $val = (eval $string)->() or diag $string;
108is(ref($val), 'ARRAY');
109is($val->[0], 'hello');
110
111my $path = join " ", map { qq["-I$_"] } @INC;
112
113$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
114$a =~ s/-e syntax OK\n//g;
115$a =~ s/.*possible typo.*\n//; # Remove warning line
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';
119BEGIN { $^I = ".bak"; }
120BEGIN { $^W = 1; }
121BEGIN { $/ = "\n"; $\ = "\n"; }
122LINE: while (defined($_ = <ARGV>)) {
123 chomp $_;
124 our(@F) = split(' ', $_, 0);
125 '???';
126}
127EOF
128is($a, $b);
129
130#Re: perlbug #35857, patch #24505
131#handle warnings::register-ed packages properly.
132package B::Deparse::Wrapper;
133use strict;
134use warnings;
135use warnings::register;
136sub getcode {
137 my $deparser = B::Deparse->new();
138 return $deparser->coderef2text(shift);
139}
140
141package Moo;
142use overload '0+' => sub { 42 };
143
144package main;
145use strict;
146use warnings;
147use constant GLIPP => 'glipp';
148use constant PI => 4;
149use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
150use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
151BEGIN { delete $::Fcntl::{O_APPEND}; }
152use POSIX qw/O_CREAT/;
153sub test {
154 my $val = shift;
155 my $res = B::Deparse::Wrapper::getcode($val);
156 like( $res, qr/use warnings/);
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
170__DATA__
171# 2
1721;
173####
174# 3
175{
176 no warnings;
177 '???';
178 2;
179}
180####
181# 4
182my $test;
183++$test and $test /= 2;
184>>>>
185my $test;
186$test /= 2 if ++$test;
187####
188# 5
189-((1, 2) x 2);
190####
191# 6
192{
193 my $test = sub : lvalue {
194 my $x;
195 }
196 ;
197}
198####
199# 7
200{
201 my $test = sub : method {
202 my $x;
203 }
204 ;
205}
206####
207# 8
208# Was sub : locked method { ... }
209# This number could be re-used.
210####
211# 9
212{
213 234;
214}
215continue {
216 123;
217}
218####
219# 10
220my $x;
221print $main::x;
222####
223# 11
224my @x;
225print $main::x[1];
226####
227# 12
228my %x;
229$x{warn()};
230####
231# 13
232my $foo;
233$_ .= <ARGV> . <$foo>;
234####
235# 14
236my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
237####
238# 15
239s/x/'y';/e;
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);
252$x = 1 foreach (@a);
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
274foreach my $i (1, 2) {
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}
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));
317####
318# 32
319our @a;
320print $_ foreach (reverse @a);
321####
322# 33
323our @a;
324print $_ foreach (reverse 1, 2..5);
325####
326# 34 (bug #38684)
327our @ary;
328@ary = split(' ', 'foo', 0);
329####
330# 35 (bug #40055)
331do { () };
332####
333# 36 (ibid.)
334do { my $x = 1; $x };
335####
336# 37 <20061012113037.GJ25805@c4.convolution.nl>
337my $f = sub {
338 +{[]};
339} ;
340####
341# 38 (bug #43010)
342'!@$%'->();
343####
344# 39 (ibid.)
345::();
346####
347# 40 (ibid.)
348'::::'->();
349####
350# 41 (ibid.)
351&::::;
352####
353# 42
354my $bar;
355'Foo'->$bar('orz');
356####
357# 43
358'Foo'->bar('orz');
359####
360# 44
361'Foo'->bar;
362####
363# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
364# 45 say
365say 'foo';
366####
367# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
368# 46 state vars
369state $x = 42;
370####
371# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
372# 47 state var assignment
373{
374 my $y = (state $x = 42);
375}
376####
377# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
378# 48 state vars in anoymous subroutines
379$a = sub {
380 state $x;
381 return $x++;
382}
383;
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;
394####
395# 51 Anonymous arrays and hashes, and references to them
396my $a = {};
397my $b = \{};
398my $c = [];
399my $d = \[];
400####
401# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
402# 52 implicit smartmatch in given/when
403given ('foo') {
404 when ('bar') { continue; }
405 when ($_ ~~ 'quux') { continue; }
406 default { 0; }
407}
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(); }
415####
416# 54 interpolation in regexps
417my($y, $t);
418/x${y}z$t/;
419####
420# TODO new undocumented cpan-bug #33708
421# 55 (cpan-bug #33708)
422%{$_ || {}}
423####
424# TODO hash constants not yet fixed
425# 56 (cpan-bug #33708)
426use constant H => { "#" => 1 }; H->{"#"}
427####
428# TODO optimized away 0 not yet fixed
429# 57 (cpan-bug #33708)
430foreach my $i (@_) { 0 }
431####
432# 58 tests with not, not optimized
433my $c;
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;
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;
451####
452# 59 tests with not, optimized
453my $c;
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;
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;
469>>>>
470my $c;
471x() unless $a;
472x() if $a;
473x() unless $a or $b;
474x() if $a or $b;
475x() unless $a and $b;
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;
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####
538# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
539# 61 tests that shouldn't be constant folded
540# It might be fundamentally impossible to make this work on ithreads, in which
541# case the TODO should become a SKIP
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(); }
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
556warn O_TRUNC;
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
562warn O_APPEND;
563####
564# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
565# 66 tests for deparsing constants which got turned into full typeglobs
566# It might be fundamentally impossible to make this work on ithreads, in which
567# case the TODO should become a SKIP
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;
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}