X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FScope-Upper.git;a=blobdiff_plain;f=t%2F20-localize-target.t;h=e16fa4c80d7991a83f9d7c401225161fce1ac71e;hp=ba77ea88a62b6f7bf677435057de5b66359501d9;hb=d0000ba43a773bbbdfd7cbf8b4414a99cedb6391;hpb=e4175b0fdbe9a877f01c26f2b2a72aa977e75c98 diff --git a/t/20-localize-target.t b/t/20-localize-target.t index ba77ea8..e16fa4c 100644 --- a/t/20-localize-target.t +++ b/t/20-localize-target.t @@ -3,9 +3,9 @@ use strict; use warnings; -use Test::More tests => 70 + 4; +use Test::More tests => 70 + 2 * 5 + 4; -use Scope::Upper qw/localize UP HERE/; +use Scope::Upper qw; # Scalars @@ -66,7 +66,8 @@ undef *x; SKIP: { - skip 'Can\'t localize through a reference before 5.8.1' => 2 if $] < 5.008001; + skip 'Can\'t localize through a reference before 5.8.1' => 2 + if "$]" < 5.008_001; eval q{ no strict 'refs'; local ${''} = 9; @@ -80,7 +81,8 @@ SKIP: SKIP: { - skip 'Can\'t localize through a reference before 5.8.1' => 2 if $] < 5.008001; + skip 'Can\'t localize through a reference before 5.8.1' => 2 + if "$]" < 5.008_001; eval q{ no strict 'refs'; local ${''} = 10; @@ -331,6 +333,42 @@ sub X::foo { 'X::foo' } is(Y->foo, 'X::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [end]'); } +# Import + +sub is_imported { + my ($pkg, $sig, $val) = @_; + my $exp = $sig eq '$' ? \$val : $val; + my $var = 'daffodil'; # don't use 'x' or eval will capture $main::x + my $spec = $sig . $pkg . '::' . $var; + localize $spec, $val => HERE; + { + my $desc = "localize imported ${sig}${var} to $val"; + my $got = eval "package $pkg; \\${sig}${var}"; + if ($@) { + fail "$desc test did not compile: $@"; + } else { + is_deeply $got, $exp, $desc; + } + } + { + my $desc = "localize defined ${sig}${var} to $val"; + my $got = eval "\\${sig}${pkg}::${var}"; + if ($@) { + fail "$desc test did not compile: $@"; + } else { + is_deeply $got, $exp, $desc; + } + } +} + +{ + is_imported 'Scope::Upper::Test::Mock10', '$', 0; + is_imported 'Scope::Upper::Test::Mock11', '$', \1; + is_imported 'Scope::Upper::Test::Mock12', '@', [ 2, 3 ]; + is_imported 'Scope::Upper::Test::Mock13', '%', { a => 4 }; + is_imported 'Scope::Upper::Test::Mock14', '&', sub { 5 }; +} + # Invalid sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }