This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
grrr.
[perl5.git] / ext / B / Deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 if ($^O eq 'MacOS') {
6 @INC = qw(: ::lib ::macos:lib);
7 } else {
8 @INC = '.';
9 push @INC, '../lib';
10 }
11}
12
13$| = 1;
14use warnings;
15use strict;
16use Config;
17
14a55f98 18print "1..15\n";
87a42246
MS
19
20use B::Deparse;
21my $deparse = B::Deparse->new() or print "not ";
ad46c0be 22my $i=1;
d4a0c6f3 23print "ok " . $i++ . "\n";
ad46c0be 24
87a42246
MS
25
26# Tell B::Deparse about our ambient pragmas
27{ my ($hint_bits, $warning_bits);
28 BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
29 $deparse->ambient_pragmas (
30 hint_bits => $hint_bits,
31 warning_bits => $warning_bits,
32 '$[' => 0 + $[
33 );
34}
35
ad46c0be
RH
36$/ = "\n####\n";
37while (<DATA>) {
38 chomp;
39 s/#.*$//mg;
87a42246 40
ad46c0be
RH
41 my ($input, $expected);
42 if (/(.*)\n>>>>\n(.*)/s) {
43 ($input, $expected) = ($1, $2);
44 }
45 else {
46 ($input, $expected) = ($_, $_);
47 }
87a42246 48
ad46c0be 49 my $coderef = eval "sub {$input}";
87a42246 50
ad46c0be 51 if ($@) {
d4a0c6f3 52 print "not ok " . $i++ . "\n";
ad46c0be
RH
53 print "# $@";
54 }
55 else {
56 my $deparsed = $deparse->coderef2text( $coderef );
57 my $regex = quotemeta($expected);
58 do {
59 no warnings 'misc';
60 $regex =~ s/\s+/\s+/g;
61 };
62
63 my $ok = ($deparsed =~ /^\{\s*$regex\s*\}$/);
d4a0c6f3 64 print (($ok ? "ok " : "not ok ") . $i++ . "\n");
ad46c0be
RH
65 if (!$ok) {
66 print "# EXPECTED:\n";
67 $regex =~ s/^/# /mg;
68 print "$regex\n";
69
70 print "\n# GOT: \n";
71 $deparsed =~ s/^/# /mg;
72 print "$deparsed\n";
73 }
87a42246 74 }
87a42246
MS
75}
76
87a42246
MS
77use constant 'c', 'stuff';
78print "not " if (eval "sub ".$deparse->coderef2text(\&c))->() ne 'stuff';
d4a0c6f3 79print "ok " . $i++ . "\n";
87a42246
MS
80
81$a = 0;
82print "not " if "{\n (-1) ** \$a;\n}"
83 ne $deparse->coderef2text(sub{(-1) ** $a });
d4a0c6f3 84print "ok " . $i++ . "\n";
87a42246
MS
85
86# XXX ToDo - constsub that returns a reference
87#use constant cr => ['hello'];
88#my $string = "sub " . $deparse->coderef2text(\&cr);
89#my $val = (eval $string)->();
90#print "not " if ref($val) ne 'ARRAY' || $val->[0] ne 'hello';
d4a0c6f3 91#print "ok " . $i++ . "\n";
87a42246
MS
92
93my $a;
94my $Is_VMS = $^O eq 'VMS';
95my $Is_MacOS = $^O eq 'MacOS';
96
97my $path = join " ", map { qq["-I$_"] } @INC;
98my $redir = $Is_MacOS ? "" : "2>&1";
99
100$a = `$^X $path "-MO=Deparse" -anle 1 $redir`;
101$a =~ s/-e syntax OK\n//g;
102$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
103$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
104$b = <<'EOF';
87a42246
MS
105LINE: while (defined($_ = <ARGV>)) {
106 chomp $_;
14a55f98 107 our(@F) = split(" ", $_, 0);
87a42246
MS
108 '???';
109}
87a42246 110EOF
f99a63a2 111print "# [$a]\n\# vs expected\n# [$b]\nnot " if $a ne $b;
d4a0c6f3 112print "ok " . $i++ . "\n";
87a42246 113
ad46c0be 114__DATA__
14a55f98 115# 2
ad46c0be
RH
1161;
117####
14a55f98 118# 3
ad46c0be
RH
119{
120 no warnings;
121 '???';
122 2;
123}
124####
14a55f98 125# 4
ad46c0be
RH
126my $test;
127++$test and $test /= 2;
128>>>>
129my $test;
130$test /= 2 if ++$test;
131####
14a55f98 132# 5
ad46c0be
RH
133-((1, 2) x 2);
134####
14a55f98 135# 6
ad46c0be
RH
136{
137 my $test = sub : lvalue {
138 my $x;
139 }
140 ;
141}
142####
14a55f98 143# 7
ad46c0be
RH
144{
145 my $test = sub : method {
146 my $x;
147 }
148 ;
149}
150####
14a55f98 151# 8
ad46c0be
RH
152{
153 my $test = sub : locked method {
154 my $x;
155 }
156 ;
157}
158####
14a55f98 159# 9
87a42246 160{
ad46c0be 161 234;
f99a63a2 162}
ad46c0be
RH
163continue {
164 123;
87a42246 165}
ce4e655d 166####
14a55f98 167# 10
ce4e655d
RH
168my $x;
169print $main::x;
170####
14a55f98 171# 11
ce4e655d
RH
172my @x;
173print $main::x[1];
14a55f98
RH
174####
175# 12
176my %x;
177$x{warn()};