]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - t/lib/Scope/Upper/TestGenerator.pm
Do less function calls in Scope::Upper::TestGenerator::gen()
[perl/modules/Scope-Upper.git] / t / lib / Scope / Upper / TestGenerator.pm
index 99764d5618f3fad75c3198f9206d31d0c065b36c..8415632cc349a3a6859d53572f2f0ba3fe7f9d27 100644 (file)
@@ -3,17 +3,24 @@ package Scope::Upper::TestGenerator;
 use strict;
 use warnings;
 
-our ($call, $test, $local, $testlocal, $allblocks);
+our ($call, $test, $allblocks);
 
-$local = sub {
+our $local_var = '$x';
+
+our $local_decl = sub {
+ my $x = $_[3];
+ return "local $local_var = $x;\n";
+};
+
+our $local_cond = sub {
  my $x = $_[3];
- return "local \$x = $x;\n";
+ return defined $x ? "($local_var eq $x)" : "(!defined($local_var))";
 };
 
-$testlocal = sub {
+our $local_test = sub {
  my ($height, $level, $i, $x) = @_;
- my $j = defined $x ? $x : 'undef';
- return "is(\$x, $j, 'x h=$height, l=$level, i=$i');\n";
+ my $cond = $local_cond->(@_);
+ return "ok($cond, 'local h=$height, l=$level, i=$i');\n";
 };
 
 my @blocks = (
@@ -26,8 +33,8 @@ my @blocks = (
 );
 
 sub import {
- if ($] >= 5.010001) {
-  push @blocks, [ 'given (1) { my $_;', '}' ];
+ if ("$]" >= 5.010_001) {
+  push @blocks, [ 'given (1) {', '}' ];
   require feature;
   feature->import('switch');
  }
@@ -44,25 +51,37 @@ sub _block {
 
 sub gen {
  my ($height, $level, $i, $x) = @_;
- push @_, $i = 0 if @_ == 2;
+
+ if (@_ == 2) {
+  $i = 0;
+  push @_, $i;
+ }
+
  return $call->(@_) if $height < $i;
+
  my @res;
  my @blks = $allblocks ? @blocks : _block(@_);
+
  my $up   = gen($height, $level, $i + 1, $x);
+ my $t    = $test->(@_);
+ my $loct = $local_test->(@_);
  for my $base (@$up) {
   for my $blk (@blks) {
-   push @res, $blk->[0] . $base . $test->(@_) . $testlocal->(@_) . $blk->[1];
+   push @res, join '', $blk->[0], $base, $t, $loct, $blk->[1];
   }
  }
- $_[3] = $i + 1;
- $up = gen($height, $level, $i + 1, $i + 1);
+
+ $_[3]    = $x = $i + 1;
+ $up      = gen($height, $level, $i + 1, $x);
+ $t       = $test->(@_);
+ my $locd = $local_decl->(@_);
+ $loct    = $local_test->(@_);
  for my $base (@$up) {
   for my $blk (@blks) {
-   push @res, $blk->[0] .
-               $local->(@_) . $base . $test->(@_) . $testlocal->(@_)
-              . $blk->[1];
+   push @res, join '', $blk->[0], $locd, $base, $t, $loct, $blk->[1];
   }
  }
+
  return \@res;
 }