From: Vincent Pit Date: Wed, 1 Apr 2015 17:12:42 +0000 (-0300) Subject: Generate t/20-lvalue.t from t/10-base.t X-Git-Tag: v0.02~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Temp.git;a=commitdiff_plain;h=74f10ab295009c4fb93ccf989d4ed442d15479d3 Generate t/20-lvalue.t from t/10-base.t --- diff --git a/MANIFEST b/MANIFEST index c3416f9..8def295 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ META.yml Makefile.PL README lib/Variable/Temp.pm +samples/gen_lvalue_test t/00-load.t t/01-import.t t/10-base.t diff --git a/samples/gen_lvalue_test b/samples/gen_lvalue_test new file mode 100644 index 0000000..a921f51 --- /dev/null +++ b/samples/gen_lvalue_test @@ -0,0 +1,33 @@ +#!perl + +use strict; +use warnings; +use autodie; + +open my $in, '<', 't/10-base.t'; +open my $out, '>', 't/20-lvalue.t'; + +while (<$in>) { + if (/use Variable::Temp/) { + print $out "use Variable::Temp 'temp';\n"; + } elsif (/use +Test::More +(tests *=>.*); *$/) { + print $out <<" HEAD"; +use Test::More; + +BEGIN { + if ("\$]" < 5.014) { + plan skip_all => 'perl 5.14 required to use lvalue temp()'; + } else { + plan $1; + } +} + HEAD + } else { + s/set_temp(.*?)=>/temp$1=/g; + s/set_temp(.*?);/temp$1;/g; + print $out $_; + } +} + +close $out; +close $in; diff --git a/t/20-lvalue.t b/t/20-lvalue.t index e9a3850..226eccc 100644 --- a/t/20-lvalue.t +++ b/t/20-lvalue.t @@ -8,70 +8,309 @@ use Variable::Temp 'temp'; use Test::More; BEGIN { - if ("$]" >= 5.014) { - plan tests => 9 + 15; + if ("$]" < 5.014) { + plan skip_all => 'perl 5.14 required to use lvalue temp()'; } else { - plan skip_all => 'perl 5.14 required to lvalue assignment with prototype \[$@%]'; + plan tests => (9 + 2 * 19) * 2 + 6 * 3; } } -my $x = 1; -is $x, 1; +sub describe { + my $h = $_[0]; + return join ', ', map "$_:$h->{$_}", sort keys %$h; +} + +# Lexicals + { - temp $x = 2; - is $x, 2; - $x = 3; - is $x, 3; + my $x = 1; + is $x, 1; + { + temp $x = 2; + is $x, 2; + $x = 3; + is $x, 3; + } + is $x, 1; { temp $x = 4; is $x, 4; temp $x = 5; is $x, 5; } - is $x, 3; + is $x, 1; + { + temp $x; + is $x, undef; + } + is $x, 1; +} + +{ + my @y = (1, 2); + is "@y", "1 2"; + { + temp @y = [ 3 ]; + is "@y", '3'; + @y = (4, 5, 6); + is "@y", '4 5 6'; + $y[3] = 7; + is "@y", '4 5 6 7'; + } + is "@y", "1 2"; + { + temp @y = [ 8, 9, 10 ]; + is "@y", '8 9 10'; + $y[1] = 11; + is "@y", '8 11 10'; + } + is "@y", "1 2"; + { + temp @y = [ 12, 13, 14 ]; + is "@y", '12 13 14'; + temp @y = [ 15, 16]; + is "@y", '15 16'; + } + is "@y", '1 2'; + { + temp @y; + is "@y", ''; + } + is "@y", '1 2'; + { + temp @y = [ qw ]; + is "@y", 'a b c'; + { + local $y[1] = 'd'; + is "@y", 'a d c'; + { + local @y[2, 3] = qw; + is "@y", 'a d e f'; + } + is "@y", 'a d c'; + } + is "@y", 'a b c'; + } + is "@y", '1 2'; +} + +{ + my %z = (a => 1); + is describe(\%z), 'a:1'; + { + temp %z = { b => 2 }; + is describe(\%z), 'b:2'; + %z = (c => 3); + is describe(\%z), 'c:3'; + $z{d} = 4; + is describe(\%z), 'c:3, d:4'; + } + is describe(\%z), 'a:1'; + { + temp %z = { a => 5 }; + is describe(\%z), 'a:5'; + $z{a} = 6; + is describe(\%z), 'a:6'; + } + is describe(\%z), 'a:1'; + { + temp %z = { a => 7, d => 8 }; + is describe(\%z), 'a:7, d:8'; + temp %z = { d => 9, e => 10 }; + is describe(\%z), 'd:9, e:10'; + } + is describe(\%z), 'a:1'; + { + temp %z; + is describe(\%z), ''; + } + is describe(\%z), 'a:1'; + { + temp %z = { a => 11, f => 12 }; + is describe(\%z), 'a:11, f:12'; + { + local $z{a} = 13; + is describe(\%z), 'a:13, f:12'; + { + local @z{qw} = (14, 15); + is describe(\%z), 'a:13, f:14, g:15'; + } + is describe(\%z), 'a:13, f:12'; + } + is describe(\%z), 'a:11, f:12'; + } + is describe(\%z), 'a:1'; } -is $x, 1; + +# Globals + { - temp $x; - is $x, undef; + our $X = 1; + is $X, 1; + { + temp $X = 2; + is $X, 2; + $X = 3; + is $X, 3; + } + is $X, 1; + { + temp $X = 4; + is $X, 4; + temp $X = 5; + is $X, 5; + } + is $X, 1; + { + temp $X; + is $X, undef; + } + is $X, 1; + { + local $X = 6; + is $X, 6; + } + is $X, 1; + { + local $X = 7; + temp $X = 8; + is $X, 8; + } + is $X, 1; + { + temp $X = 9; + local $X = 10; + is $X, 10; + } + is $X, 1; } -is $x, 1; -our $y = 1; -is $y, 1; { - temp $y = 2; - is $y, 2; - $y = 3; - is $y, 3; + our @Y = (1, 2); + is "@Y", "1 2"; + { + temp @Y = [ 3 ]; + is "@Y", '3'; + @Y = (4, 5, 6); + is "@Y", '4 5 6'; + $Y[3] = 7; + is "@Y", '4 5 6 7'; + } + is "@Y", "1 2"; + { + temp @Y = [ 8, 9, 10 ]; + is "@Y", '8 9 10'; + $Y[1] = 11; + is "@Y", '8 11 10'; + } + is "@Y", "1 2"; + { + temp @Y = [ 12, 13, 14 ]; + is "@Y", '12 13 14'; + temp @Y = [ 15, 16]; + is "@Y", '15 16'; + } + is "@Y", '1 2'; + { + temp @Y; + is "@Y", ''; + } + is "@Y", '1 2'; { - temp $y = 4; - is $y, 4; - temp $y = 5; - is $y, 5; + temp @Y = [ qw ]; + is "@Y", 'a b c'; + { + local $Y[1] = 'd'; + is "@Y", 'a d c'; + { + local @Y[2, 3] = qw; + is "@Y", 'a d e f'; + } + is "@Y", 'a d c'; + } + is "@Y", 'a b c'; } - is $y, 3; + is "@Y", '1 2'; { - local $y = 6; - is $y, 6; + local @Y = qw; + is "@Y", 'A B'; } - is $y, 3; + is "@Y", '1 2'; { - local $y = 7; - temp $y = 8; - is $y, 8; + local @Y = qw; + temp @Y = [ qw ]; + is "@Y", 'F'; } - is $y, 3; + is "@Y", '1 2'; { - temp $y = 9; - local $y = 10; - is $y, 10; + temp @Y = [ qw ]; + local @Y = qw; + is "@Y", 'J'; } - is $y, 3; + is "@Y", '1 2'; } -is $y, 1; + { - temp $y; - is $y, undef; + our %Z = (a => 1); + is describe(\%Z), 'a:1'; + { + temp %Z = { b => 2 }; + is describe(\%Z), 'b:2'; + %Z = (c => 3); + is describe(\%Z), 'c:3'; + $Z{d} = 4; + is describe(\%Z), 'c:3, d:4'; + } + is describe(\%Z), 'a:1'; + { + temp %Z = { a => 5 }; + is describe(\%Z), 'a:5'; + $Z{a} = 6; + is describe(\%Z), 'a:6'; + } + is describe(\%Z), 'a:1'; + { + temp %Z = { a => 7, d => 8 }; + is describe(\%Z), 'a:7, d:8'; + temp %Z = { d => 9, e => 10 }; + is describe(\%Z), 'd:9, e:10'; + } + is describe(\%Z), 'a:1'; + { + temp %Z; + is describe(\%Z), ''; + } + is describe(\%Z), 'a:1'; + { + temp %Z = { a => 11, f => 12 }; + is describe(\%Z), 'a:11, f:12'; + { + local $Z{a} = 13; + is describe(\%Z), 'a:13, f:12'; + { + local @Z{qw} = (14, 15); + is describe(\%Z), 'a:13, f:14, g:15'; + } + is describe(\%Z), 'a:13, f:12'; + } + is describe(\%Z), 'a:11, f:12'; + } + is describe(\%Z), 'a:1'; + { + local %Z = (A => 1, B => 2); + is describe(\%Z), 'A:1, B:2'; + } + is describe(\%Z), 'a:1'; + { + local %Z = (A => 3, C => 4); + temp %Z = { A => 5, D => 6 }; + is describe(\%Z), 'A:5, D:6'; + } + is describe(\%Z), 'a:1'; + { + temp %Z = { A => 7, E => 8 }; + local %Z = (A => 9, F => 10); + is describe(\%Z), 'A:9, F:10'; + } + is describe(\%Z), 'a:1'; } -is $y, 1;