]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - t/20-localize-target.t
Tie the array after localizing it in t/{34,44}-*-magic.t
[perl/modules/Scope-Upper.git] / t / 20-localize-target.t
index 56ebd978df5f747f571b6585808ddbb1dda2eb76..9bd7a3885fcc6376122bd87515a4ef96dda79f6c 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 50;
+use Test::More tests => 50 + 4;
 
 use Scope::Upper qw/localize UP HERE/;
 
@@ -32,14 +32,17 @@ sub _t { shift->{t} }
  is $x, undef, 'localize *x, obj => HERE [end]';
 }
 
+our $y;
+
 {
- local $x = 2;
+ local $x = 1;
+ local $y = 2;
  {
-  local $x = 3;
-  localize *x, 1 => HERE;
-  is $x, undef, 'localize *x, 1 => HERE [ok]';
+  local $y = 3;
+  localize *x, 'y' => HERE;
+  is $x, 3, "localize *x, 'y' => HERE [ok]";
  }
- is $x, $] < 5.008009 ? undef : 2, 'localize *x, 1 => HERE [end]';
+ is $x, 1, "localize *x, 'y' => HERE [end]";
 }
 undef *x;
 
@@ -63,7 +66,7 @@ undef *x;
 
 SKIP:
 {
- skip 'Can\'t localize through a reference in 5.6' => 2 if $] < 5.008;
+ skip 'Can\'t localize through a reference before 5.8.1' => 2 if $] < 5.008001;
  eval q{
   no strict 'refs';
   local ${''} = 9;
@@ -77,7 +80,7 @@ SKIP:
 
 SKIP:
 {
- skip 'Can\'t localize through a reference in 5.6' => 2 if $] < 5.008;
+ skip 'Can\'t localize through a reference before 5.8.1' => 2 if $] < 5.008001;
  eval q{
   no strict 'refs';
   local ${''} = 10;
@@ -251,3 +254,27 @@ my $xh = { a => 5, c => 7 };
  }
  is foo(), 8, 'localize "&foo", sub { 8 } => UP [ok]';
 }
+
+# Invalid
+
+sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
+
+{
+ eval { localize \1, 0 => HERE };
+ like $@, invalid_ref('SCALAR'), 'invalid localize \1, 0 => HERE';
+}
+
+{
+ eval { localize [ ], 0 => HERE };
+ like $@, invalid_ref('ARRAY'),  'invalid localize [ ], 0 => HERE';
+}
+
+{
+ eval { localize { }, 0 => HERE };
+ like $@, invalid_ref('HASH'),   'invalid localize { }, 0 => HERE';
+}
+
+{
+ eval { localize sub { }, 0 => HERE };
+ like $@, invalid_ref('CODE'),   'invalid localize sub { }, 0 => HERE';
+}