Re: more B::Concise stuff (PATCH - updated)
[p5sagit/p5-mst-13.2.git] / ext / B / t / showlex.t
index afff12e..9e3240f 100755 (executable)
@@ -7,18 +7,18 @@ BEGIN {
     } else {
        @INC = '../lib';
     }
+    require './test.pl';
 }
 
-$|  = 1;
+$| = 1;
 use warnings;
 use strict;
 use Config;
+use B::Showlex ();
 
-print "1..1\n";
+plan tests => 8;
 
-my $test = 1;
-
-sub ok { print "ok $test\n"; $test++ }
+my $verbose = @ARGV; # set if ANY ARGS
 
 my $a;
 my $Is_VMS = $^O eq 'VMS';
@@ -30,9 +30,73 @@ my $redir = $Is_MacOS ? "" : "2>&1";
 my $is_thread = $Config{use5005threads} && $Config{use5005threads} eq 'define';
 
 if ($is_thread) {
-    print "# use5005threads: test $test skipped\n";
+    ok "# use5005threads: test skipped\n";
 } else {
     $a = `$^X $path "-MO=Showlex" -e "my \@one" $redir`;
-    print "# [$a]\nnot " unless $a =~ /sv_undef.*PVNV.*\@one.*sv_undef.*AV/s;
+    like ($a, qr/sv_undef.*PVNV.*\@one.*sv_undef.*AV/s,
+         "canonical usage works");
 }
-ok;
+
+# v1.01 tests
+
+my ($na,$nb,$nc); # holds regex-strs
+sub padrep {
+    my $varname = shift;
+    return "PVNV \\\(0x[0-9a-fA-F]+\\\) \\$varname\n";
+}
+
+my $out = runperl ( switches => ["-MO=Showlex"], 
+                  prog => 'my ($a,$b)', stderr => 1 );
+$na = padrep('$a');
+$nb = padrep('$b');
+like ($out, qr/1: $na/ms, 'found $a in "my ($a,$b)"');
+like ($out, qr/2: $nb/ms, 'found $b in "my ($a,$b)"');
+
+print $out if $verbose;
+
+our $buf = 'arb startval';
+my $ak = B::Showlex::walk_output (\$buf);
+
+my $walker = B::Showlex::compile(sub { my ($foo,$bar) });
+$walker->();
+$na = padrep('$foo');
+$nb = padrep('$bar');
+like ($buf, qr/1: $na/ms, 'found $foo in "sub { my ($foo,$bar) }"');
+like ($buf, qr/2: $nb/ms, 'found $bar in "sub { my ($foo,$bar) }"');
+
+print $buf if $verbose;
+
+$ak = B::Showlex::walk_output (\$buf);
+
+$walker = B::Showlex::compile(sub { my ($scalar,@arr,%hash) });
+$walker->();
+$na = padrep('$scalar');
+$nb = padrep('@arr');
+$nc = padrep('%hash');
+like ($buf, qr/1: $na/ms, 'found $scalar in "sub { my ($scalar,@arr,%hash) }"');
+like ($buf, qr/2: $nb/ms, 'found @arr    in "sub { my ($scalar,@arr,%hash) }"');
+like ($buf, qr/3: $nc/ms, 'found %hash   in "sub { my ($scalar,@arr,%hash) }"');
+
+print $buf if $verbose;
+
+my $asub = sub {
+    my ($self,%props)=@_;
+    my $total;
+    { # inner block vars
+       my (@fib)=(1,2);
+       for (my $i=2; $i<10; $i++) {
+           $fib[$i] = $fib[$i-2] + $fib[$i-1];
+       }
+       for my $i(0..10) {
+           $total += $i;
+       }
+    }
+};
+$walker = B::Showlex::compile($asub, '-newlex');
+$walker->();
+
+$walker = B::Concise::compile($asub, '-exec');
+$walker->();
+
+
+print $buf if $verbose;