Eviscerate README.macos to match the state of the world
[p5sagit/p5-mst-13.2.git] / ext / B / t / showlex.t
1 #!./perl
2
3 BEGIN {
4     unshift @INC, 't';
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     }
10     require 'test.pl';
11 }
12
13 $| = 1;
14 use warnings;
15 use strict;
16 use Config;
17 use B::Showlex ();
18
19 plan tests => 15;
20
21 my $verbose = @ARGV; # set if ANY ARGS
22
23 my $a;
24 my $Is_VMS = $^O eq 'VMS';
25 my $Is_MacOS = $^O eq 'MacOS';
26
27 my $path = join " ", map { qq["-I$_"] } @INC;
28 $path = '"-I../lib" "-Iperl_root:[lib]"' if $Is_VMS;   # gets too long otherwise
29 my $redir = $Is_MacOS ? "" : "2>&1";
30 my $is_thread = $Config{use5005threads} && $Config{use5005threads} eq 'define';
31
32 if ($is_thread) {
33     ok "# use5005threads: test skipped\n";
34 } else {
35     $a = `$^X $path "-MO=Showlex" -e "my \@one" $redir`;
36     like ($a, qr/sv_undef.*PVNV.*\@one.*sv_undef.*AV/s,
37           "canonical usage works");
38 }
39
40 # v1.01 tests
41
42 my ($na,$nb,$nc);       # holds regex-strs
43 my ($out, $newlex);     # output, option-flag
44
45 sub padrep {
46     my ($varname,$newlex) = @_;
47     return ($newlex)
48         ? 'PVNV \(0x[0-9a-fA-F]+\) "\\'.$varname.'" = '
49         : "PVNV \\\(0x[0-9a-fA-F]+\\\) \\$varname\n";
50 }
51
52 for $newlex ('', '-newlex') {
53
54     $out = runperl ( switches => ["-MO=Showlex,$newlex"],
55                      prog => 'my ($a,$b)', stderr => 1 );
56     $na = padrep('$a',$newlex);
57     $nb = padrep('$b',$newlex);
58     like ($out, qr/1: $na/ms, 'found $a in "my ($a,$b)"');
59     like ($out, qr/2: $nb/ms, 'found $b in "my ($a,$b)"');
60
61     print $out if $verbose;
62
63 SKIP: {
64     skip "no perlio in this build", 5
65     unless $Config::Config{useperlio};
66
67     our $buf = 'arb startval';
68     my $ak = B::Showlex::walk_output (\$buf);
69
70     my $walker = B::Showlex::compile( $newlex, sub{my($foo,$bar)} );
71     $walker->();
72     $na = padrep('$foo',$newlex);
73     $nb = padrep('$bar',$newlex);
74     like ($buf, qr/1: $na/ms, 'found $foo in "sub { my ($foo,$bar) }"');
75     like ($buf, qr/2: $nb/ms, 'found $bar in "sub { my ($foo,$bar) }"');
76
77     print $buf if $verbose;
78
79     $ak = B::Showlex::walk_output (\$buf);
80
81     my $src = 'sub { my ($scalar,@arr,%hash) }';
82     my $sub = eval $src;
83     $walker = B::Showlex::compile($sub);
84     $walker->();
85     $na = padrep('$scalar',$newlex);
86     $nb = padrep('@arr',$newlex);
87     $nc = padrep('%hash',$newlex);
88     like ($buf, qr/1: $na/ms, 'found $scalar in "'. $src .'"');
89     like ($buf, qr/2: $nb/ms, 'found @arr    in "'. $src .'"');
90     like ($buf, qr/3: $nc/ms, 'found %hash   in "'. $src .'"');
91
92     print $buf if $verbose;
93
94     # fibonacci function under test
95     my $asub = sub {
96         my ($self,%props)=@_;
97         my $total;
98         { # inner block vars
99             my (@fib)=(1,2);
100             for (my $i=2; $i<10; $i++) {
101                 $fib[$i] = $fib[$i-2] + $fib[$i-1];
102             }
103             for my $i(0..10) {
104                 $total += $i;
105             }
106         }
107     };
108     $walker = B::Showlex::compile($asub, $newlex, -nosp);
109     $walker->();
110     print $buf if $verbose;
111
112     $walker = B::Concise::compile($asub, '-exec');
113     $walker->();
114
115 }
116 }