6 use Test::More tests => 70 + 4;
8 use Scope::Upper qw<localize UP HERE>;
17 localize *x, \1 => HERE;
18 is $x, 1, 'localize *x, \1 => HERE [ok]';
20 is $x, 2, 'localize *x, \1 => HERE [end]';
28 localize *x, \bless({ t => 1 }, 'main') => HERE;
29 is ref($x), 'main', 'localize *x, obj => HERE [ref]';
30 is $x->_t, 1, 'localize *x, obj => HERE [meth]';
32 is $x, undef, 'localize *x, obj => HERE [end]';
42 localize *x, 'y' => HERE;
43 is $x, 3, "localize *x, 'y' => HERE [ok]";
45 is $x, 1, "localize *x, 'y' => HERE [end]";
52 localize '$x', 2 => HERE;
53 is $x, 2, 'localize "$x", 2 => HERE [ok]';
55 is $x, 7, 'localize "$x", 2 => HERE [end]';
61 localize ' $x', 3 => HERE;
62 is $x, 3, 'localize " $x", 3 => HERE [ok]';
64 is $x, 8, 'localize " $x", 3 => HERE [end]';
69 skip 'Can\'t localize through a reference before 5.8.1' => 2
75 localize '$', 4 => HERE;
76 is ${''}, 4, 'localize "$", 4 => HERE [ok]';
78 is ${''}, 9, 'localize "$", 4 => HERE [end]';
84 skip 'Can\'t localize through a reference before 5.8.1' => 2
90 localize '', 5 => HERE;
91 is ${''}, 5, 'localize "", 4 => HERE [ok]';
93 is ${''}, 10, 'localize "", 4 => HERE [end]';
100 localize 'x', \1 => HERE;
101 is $x, 1, 'localize "x", \1 => HERE [ok]';
103 is $x, 2, 'localize "x", \1 => HERE [end]';
109 localize 'x', 3 => HERE;
110 is $x, 3, 'localize "x", 3 => HERE [ok]';
112 is $x, 4, 'localize "x", 3 => HERE [end]';
118 localize 'x', bless({ t => 2 }, 'main') => HERE;
119 is ref($x), 'main', 'localize "x", obj => HERE [ref]';
120 is $x->_t, 2, 'localize "x", obj => HERE [meth]';
122 is $x, undef, 'localize "x", obj => HERE [end]';
125 sub callthrough (*$) {
126 my ($what, $val) = @_;
129 $val = eval "\\$val";
132 localize $what, $val => UP;
133 is $x, 'x', 'localize callthrough [not yet]';
137 package Scope::Upper::Test::Mock1;
140 main::callthrough(*x, 4);
141 Test::More::is($x, 4, 'localize glob [ok - SUTM1]');
142 Test::More::is($main::x, undef, 'localize glob [ok - main]');
147 package Scope::Upper::Test::Mock2;
150 main::callthrough(*main::x, 5);
151 Test::More::is($x, undef, 'localize qualified glob [ok - SUTM2]');
152 Test::More::is($main::x, 5, 'localize qualified glob [ok - main]');
157 package Scope::Upper::Test::Mock3;
160 main::callthrough('$main::x', 6);
161 Test::More::is($x, undef, 'localize fully qualified name [ok - SUTM3]');
162 Test::More::is($main::x, 6, 'localize fully qualified name [ok - main]');
167 package Scope::Upper::Test::Mock4;
170 main::callthrough('$x', 7);
171 Test::More::is($x, 7, 'localize unqualified name [ok - SUTM4]');
172 Test::More::is($main::x, undef, 'localize unqualified name [ok - main]');
178 package Scope::Upper::Test::Mock5;
180 main::callthrough('$_', 'bar');
181 Test::More::ok(/bar/, 'localize $_ [ok]');
194 localize *a, $xa => HERE;
195 is_deeply \@a, $xa, 'localize *a, [ ] => HERE [ok]';
197 is_deeply \@a, [ 4 .. 6 ], 'localize *a, [ ] => HERE [end]';
205 localize *a, $xa => UP;
206 is_deeply \@a, [ 5 .. 7 ], 'localize *a, [ ] => UP [not yet]';
208 is_deeply \@a, $xa, 'localize *a, [ ] => UP [ok]';
210 is_deeply \@a, [ 4 .. 6 ], 'localize *a, [ ] => UP [end]';
216 my $xh = { a => 5, c => 7 };
219 local %h = (a => 1, b => 2);
221 localize *h, $xh => HERE;
222 is_deeply \%h, $xh, 'localize *h, { } => HERE [ok]';
224 is_deeply \%h, { a => 1, b => 2 }, 'localize *h, { } => HERE [end]';
228 local %h = (a => 1, b => 2);
230 local %h = (b => 3, c => 4);
232 localize *h, $xh => UP;
233 is_deeply \%h, { b => 3, c => 4 }, 'localize *h, { } => UP [not yet]';
235 is_deeply \%h, $xh, 'localize *h, { } => UP [ok]';
237 is_deeply \%h, { a => 1, b => 2 }, 'localize *h, { } => UP [end]';
243 local *foo = sub { 7 };
245 localize *foo, sub { 6 } => UP;
246 is foo(), 7, 'localize *foo, sub { 6 } => UP [not yet]';
248 is foo(), 6, 'localize *foo, sub { 6 } => UP [ok]';
252 local *foo = sub { 9 };
254 localize '&foo', sub { 8 } => UP;
255 is foo(), 9, 'localize "&foo", sub { 8 } => UP [not yet]';
257 is foo(), 8, 'localize "&foo", sub { 8 } => UP [ok]';
261 local *foo = sub { 'a' };
264 localize *foo, sub { 'b' } => UP;
265 is foo(), 'a', 'localize *foo, sub { "b" } => UP [not yet 1]';
267 no warnings 'redefine';
268 local *foo = sub { 'c' };
269 is foo(), 'c', 'localize *foo, sub { "b" } => UP [localized 1]';
271 is foo(), 'a', 'localize *foo, sub { "b" } => UP [not yet 2]';
273 is foo(), 'b', 'localize *foo, sub { "b" } => UP [ok 1]';
275 no warnings 'redefine';
276 local *foo = sub { 'd' };
277 is foo(), 'd', 'localize *foo, sub { "b" } => UP [localized 2]';
279 is foo(), 'b', 'localize *foo, sub { "b" } => UP [ok 2]';
281 is foo(), 'a', 'localize *foo, sub { "b" } => UP [end]';
285 local *foo = sub { 'x' };
288 localize *foo, sub { 'y' } => UP;
289 is foo(), 'x', 'localize *foo, sub { "y" } => UP [not yet]';
291 is foo(), 'y', 'localize *foo, sub { "y" } => UP [ok]';
292 no warnings 'redefine';
294 is foo(), 'z', 'localize *foo, sub { "y" } => UP [replaced]';
296 is foo(), 'x', 'localize *foo, sub { "y" } => UP [end]';
299 sub X::foo { 'X::foo' }
304 localize 'X::foo', sub { 'X::foo 2' } => UP;
305 is(X->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [not yet]')
307 is(X->foo, 'X::foo 2', 'localize "X::foo", sub { "X::foo 2" } => UP [ok]');
309 is(X->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [end]');
317 localize 'X::foo', sub { 'X::foo 3' } => UP;
318 is(Y->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 3" } => UP [not yet]')
320 is(Y->foo, 'X::foo 3', 'localize "X::foo", sub { "X::foo 3" } => UP [ok]');
322 is(Y->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [end]');
328 localize 'Y::foo', sub { 'Y::foo' } => UP;
329 is(Y->foo, 'X::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [not yet]');
331 is(Y->foo, 'Y::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [ok]');
333 is(Y->foo, 'X::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [end]');
338 sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
341 eval { localize \1, 0 => HERE };
342 like $@, invalid_ref('SCALAR'), 'invalid localize \1, 0 => HERE';
346 eval { localize [ ], 0 => HERE };
347 like $@, invalid_ref('ARRAY'), 'invalid localize [ ], 0 => HERE';
351 eval { localize { }, 0 => HERE };
352 like $@, invalid_ref('HASH'), 'invalid localize { }, 0 => HERE';
356 eval { localize sub { }, 0 => HERE };
357 like $@, invalid_ref('CODE'), 'invalid localize sub { }, 0 => HERE';