From: Vincent Pit Date: Sun, 9 Sep 2012 09:16:30 +0000 (+0200) Subject: Rework how Scope::Upper::TestGenerator generates its 'local' tests X-Git-Tag: v0.20~21 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FScope-Upper.git;a=commitdiff_plain;h=c51f72ece23a7afb5590272f6aeca327b91a3945 Rework how Scope::Upper::TestGenerator generates its 'local' tests --- diff --git a/t/12-reap-block.t b/t/12-reap-block.t index a4e3d17..fda9aca 100644 --- a/t/12-reap-block.t +++ b/t/12-reap-block.t @@ -22,12 +22,12 @@ local $Scope::Upper::TestGenerator::test = sub { return "is(\$x, $j, 'x h=$height, l=$level, i=$i');\n"; }; -local $Scope::Upper::TestGenerator::local = sub { +local $Scope::Upper::TestGenerator::local_decl = sub { my ($height, $level, $i, $x) = @_; return $i == $height - $level ? "\$x = $x;\n" : "local \$x = $x;\n"; }; -local $Scope::Upper::TestGenerator::testlocal = sub { '' }; +local $Scope::Upper::TestGenerator::local_test = sub { '' }; local $Scope::Upper::TestGenerator::allblocks = 1; diff --git a/t/22-localize-block.t b/t/22-localize-block.t index ea189d8..b32d251 100644 --- a/t/22-localize-block.t +++ b/t/22-localize-block.t @@ -22,7 +22,7 @@ local $Scope::Upper::TestGenerator::test = sub { return "is(\$x, $j, 'x h=$height, l=$level, i=$i');\n"; }; -local $Scope::Upper::TestGenerator::testlocal = sub { '' }; +local $Scope::Upper::TestGenerator::local_test = sub { '' }; local $Scope::Upper::TestGenerator::allblocks = 1; diff --git a/t/32-localize_elem-block.t b/t/32-localize_elem-block.t index a1f4d47..7760831 100644 --- a/t/32-localize_elem-block.t +++ b/t/32-localize_elem-block.t @@ -10,11 +10,11 @@ use Scope::Upper qw; use Scope::Upper::TestGenerator; -local $Scope::Upper::TestGenerator::testlocal = sub { '' }; +our $testcase; -local $Scope::Upper::TestGenerator::allblocks = 1; +local $Scope::Upper::TestGenerator::local_test = sub { '' }; -our $testcase; +local $Scope::Upper::TestGenerator::allblocks = 1; local $Scope::Upper::TestGenerator::call = sub { my ($height, $level, $i) = @_; @@ -28,10 +28,7 @@ local $Scope::Upper::TestGenerator::test = sub { return "is(\$a[1], $j, 'x h=$height, l=$level, i=$i');\n"; }; -local $Scope::Upper::TestGenerator::local = sub { - my $x = $_[3]; - return "local \$a[1] = $x;\n"; -}; +local $Scope::Upper::TestGenerator::local_var = '$a[1]'; our @a; @@ -58,10 +55,7 @@ local $Scope::Upper::TestGenerator::test = sub { return "is(\$h{a}, $j, 'x h=$height, l=$level, i=$i');\n"; }; -local $Scope::Upper::TestGenerator::local = sub { - my $x = $_[3]; - return "local \$h{a} = $x;\n"; -}; +local $Scope::Upper::TestGenerator::local_var = '$h{a}'; our %h; diff --git a/t/lib/Scope/Upper/TestGenerator.pm b/t/lib/Scope/Upper/TestGenerator.pm index 9ef65b8..0b3a8e6 100644 --- a/t/lib/Scope/Upper/TestGenerator.pm +++ b/t/lib/Scope/Upper/TestGenerator.pm @@ -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 = ( @@ -51,7 +58,7 @@ sub gen { my $up = gen($height, $level, $i + 1, $x); for my $base (@$up) { for my $blk (@blks) { - push @res, $blk->[0] . $base . $test->(@_) . $testlocal->(@_) . $blk->[1]; + push @res, $blk->[0] . $base . $test->(@_) . $local_test->(@_) . $blk->[1]; } } $_[3] = $i + 1; @@ -59,7 +66,7 @@ sub gen { for my $base (@$up) { for my $blk (@blks) { push @res, $blk->[0] . - $local->(@_) . $base . $test->(@_) . $testlocal->(@_) + $local_decl->(@_) . $base . $test->(@_) . $local_test->(@_) . $blk->[1]; } }