X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F20-localize-target.t;h=e16fa4c80d7991a83f9d7c401225161fce1ac71e;hb=refs%2Ftags%2Frt125931;hp=c68a1a3428e2857b47b5f72b523de154d77aa5a7;hpb=7b2e2b1be5f954f73d3438f1ed7ae0140083ea71;p=perl%2Fmodules%2FScope-Upper.git diff --git a/t/20-localize-target.t b/t/20-localize-target.t index c68a1a3..e16fa4c 100644 --- a/t/20-localize-target.t +++ b/t/20-localize-target.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 70 + 4; +use Test::More tests => 70 + 2 * 5 + 4; use Scope::Upper qw; @@ -333,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/ }