]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - t/20-localize-target.t
Always apply localizations at the glob level
[perl/modules/Scope-Upper.git] / t / 20-localize-target.t
index c352456f15ef5fb1043622f48d332f3c5fd83446..e16fa4c80d7991a83f9d7c401225161fce1ac71e 100644 (file)
@@ -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<localize UP HERE>;
 
@@ -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/ }