SYN SYN
[p5sagit/p5-mst-13.2.git] / t / lib / b.t
CommitLineData
ee8c7f54 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
22d4bb9c 5 @INC = '../lib';
ee8c7f54 6}
7
8$| = 1;
9use warnings;
10use strict;
11use Config;
12
0e06870b 13print "1..17\n";
ee8c7f54 14
15my $test = 1;
16
17sub ok { print "ok $test\n"; $test++ }
18
19use B::Deparse;
20my $deparse = B::Deparse->new() or print "not ";
21ok;
22
23print "not " if "{\n 1;\n}" ne $deparse->coderef2text(sub {1});
24ok;
25
26print "not " if "{\n '???';\n 2;\n}" ne
27 $deparse->coderef2text(sub {1;2});
28ok;
29
30print "not " if "{\n \$test /= 2 if ++\$test;\n}" ne
31 $deparse->coderef2text(sub {++$test and $test/=2;});
32ok;
22d4bb9c 33{
34my $a = <<'EOF';
35{
36 $test = sub : lvalue {
847a5fae 37 my $x;
22d4bb9c 38 }
39 ;
40}
41EOF
42chomp $a;
847a5fae 43print "not " if $deparse->coderef2text(sub{$test = sub : lvalue{my $x}}) ne $a;
22d4bb9c 44ok;
45
46$a =~ s/lvalue/method/;
847a5fae 47print "not " if $deparse->coderef2text(sub{$test = sub : method{my $x}}) ne $a;
22d4bb9c 48ok;
49
50$a =~ s/method/locked method/;
847a5fae 51print "not " if $deparse->coderef2text(sub{$test = sub : method locked {my $x}})
22d4bb9c 52 ne $a;
53ok;
54}
ee8c7f54 55
0e06870b 56print "not " if (eval "sub ".$deparse->coderef2text(sub () { 42 }))->() != 42;
57ok;
58
59use constant 'c', 'stuff';
60print "not " if (eval "sub ".$deparse->coderef2text(\&c))->() ne 'stuff';
61ok;
62
63# XXX ToDo - constsub that returns a reference
64#use constant cr => ['hello'];
65#my $string = "sub " . $deparse->coderef2text(\&cr);
66#my $val = (eval $string)->();
67#print "not " if ref($val) ne 'ARRAY' || $val->[0] ne 'hello';
68#ok;
69
4b19af01 70my $a;
71my $Is_VMS = $^O eq 'VMS';
22d4bb9c 72$a = `$^X "-I../lib" "-MO=Deparse" -anle 1 2>&1`;
ee8c7f54 73$a =~ s/-e syntax OK\n//g;
b82d478d 74$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
75$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
ee8c7f54 76$b = <<'EOF';
77
78LINE: while (defined($_ = <ARGV>)) {
79 chomp $_;
80 @F = split(/\s+/, $_, 0);
0e06870b 81 '???';
ee8c7f54 82}
83
84EOF
85print "# [$a]\n\# vs\n# [$b]\nnot " if $a ne $b;
86ok;
87
22d4bb9c 88$a = `$^X "-I../lib" "-MO=Debug" -e 1 2>&1`;
ee8c7f54 89print "not " unless $a =~
90/\bLISTOP\b.*\bOP\b.*\bCOP\b.*\bOP\b/s;
91ok;
92
22d4bb9c 93$a = `$^X "-I../lib" "-MO=Terse" -e 1 2>&1`;
ee8c7f54 94print "not " unless $a =~
0e06870b 95/\bLISTOP\b.*leave.*\n OP\b.*enter.*\n COP\b.*nextstate.*\n OP\b.*null/s;
ee8c7f54 96ok;
97
22d4bb9c 98$a = `$^X "-I../lib" "-MO=Terse" -ane "s/foo/bar/" 2>&1`;
ee8c7f54 99$a =~ s/\(0x[^)]+\)//g;
100$a =~ s/\[[^\]]+\]//g;
101$a =~ s/-e syntax OK//;
102$a =~ s/[^a-z ]+//g;
103$a =~ s/\s+/ /g;
104$a =~ s/\b(s|foo|bar|ullsv)\b\s?//g;
105$a =~ s/^\s+//;
106$a =~ s/\s+$//;
22d4bb9c 107my $is_thread = $Config{use5005threads} && $Config{use5005threads} eq 'define';
108if ($is_thread) {
109 $b=<<EOF;
110leave enter nextstate label leaveloop enterloop null and defined null
111threadsv readline gv lineseq nextstate aassign null pushmark split pushre
112threadsv const null pushmark rvav gv nextstate subst const unstack nextstate
113EOF
114} else {
115 $b=<<EOF;
ee8c7f54 116leave enter nextstate label leaveloop enterloop null and defined null
117null gvsv readline gv lineseq nextstate aassign null pushmark split pushre
22d4bb9c 118null gvsv const null pushmark rvav gv nextstate subst const unstack nextstate
ee8c7f54 119EOF
22d4bb9c 120}
ee8c7f54 121$b=~s/\n/ /g;$b=~s/\s+/ /g;
122$b =~ s/\s+$//;
22d4bb9c 123print "# [$a]\n# vs\n# [$b]\nnot " if $a ne $b;
ee8c7f54 124ok;
125
22d4bb9c 126chomp($a = `$^X "-I../lib" "-MB::Stash" "-Mwarnings" -e1`);
ee8c7f54 127$a = join ',', sort split /,/, $a;
0e06870b 128$a =~ s/-u(perlio|open)(?:::\w+)?,//g if defined $Config{'useperlio'} and $Config{'useperlio'} eq 'define';
4b19af01 129$a =~ s/-uWin32,// if $^O eq 'MSWin32';
130$a =~ s/-u(Cwd|File|File::Copy|OS2),//g if $^O eq 'os2';
22d4bb9c 131$a =~ s/-uCwd,// if $^O eq 'cygwin';
4b19af01 132if ($Config{static_ext} eq ' ') {
133 $b = '-uCarp,-uCarp::Heavy,-uDB,-uExporter,-uExporter::Heavy,-uattributes,'
0e06870b 134 . '-umain,-ustrict,-uwarnings';
b82d478d 135 if (ord('A') == 193) { # EBCDIC sort order is qw(a A) not qw(A a)
136 $b = join ',', sort split /,/, $b;
137 }
4b19af01 138 print "# [$a] vs [$b]\nnot " if $a ne $b;
139 ok;
140} else {
141 print "ok $test # skipped: one or more static extensions\n"; $test++;
142}
ee8c7f54 143
22d4bb9c 144if ($is_thread) {
145 print "# use5005threads: test $test skipped\n";
146} else {
147 $a = `$^X "-I../lib" "-MO=Showlex" -e "my %one" 2>&1`;
b82d478d 148 if (ord('A') != 193) { # ASCIIish
149 print "# [$a]\nnot " unless $a =~ /sv_undef.*PVNV.*%one.*sv_undef.*HV/s;
150 }
151 else { # EBCDICish C<1: PVNV (0x1a7ede34) "%\226\225\205">
152 print "# [$a]\nnot " unless $a =~ /sv_undef.*PVNV.*%\\[0-9].*sv_undef.*HV/s;
153 }
4b19af01 154}
ee8c7f54 155ok;
0e06870b 156
157# Bug 20001204.07
158{
159my $foo = $deparse->coderef2text(sub { { 234; }});
160# Constants don't get optimised here.
161print "not " unless $foo =~ /{.*{.*234;.*}.*}/sm;
162ok;
163$foo = $deparse->coderef2text(sub { { 234; } continue { 123; } });
164print "not " unless $foo =~ /{.*{.*234;.*}.*continue.*{.*123.*}/sm;
165ok;
166}