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 if $] < 5.008001;
74 localize '$', 4 => HERE;
75 is ${''}, 4, 'localize "$", 4 => HERE [ok]';
77 is ${''}, 9, 'localize "$", 4 => HERE [end]';
83 skip 'Can\'t localize through a reference before 5.8.1' => 2 if $] < 5.008001;
88 localize '', 5 => HERE;
89 is ${''}, 5, 'localize "", 4 => HERE [ok]';
91 is ${''}, 10, 'localize "", 4 => HERE [end]';
98 localize 'x', \1 => HERE;
99 is $x, 1, 'localize "x", \1 => HERE [ok]';
101 is $x, 2, 'localize "x", \1 => HERE [end]';
107 localize 'x', 3 => HERE;
108 is $x, 3, 'localize "x", 3 => HERE [ok]';
110 is $x, 4, 'localize "x", 3 => HERE [end]';
116 localize 'x', bless({ t => 2 }, 'main') => HERE;
117 is ref($x), 'main', 'localize "x", obj => HERE [ref]';
118 is $x->_t, 2, 'localize "x", obj => HERE [meth]';
120 is $x, undef, 'localize "x", obj => HERE [end]';
123 sub callthrough (*$) {
124 my ($what, $val) = @_;
127 $val = eval "\\$val";
130 localize $what, $val => UP;
131 is $x, 'x', 'localize callthrough [not yet]';
135 package Scope::Upper::Test::Mock1;
138 main::callthrough(*x, 4);
139 Test::More::is($x, 4, 'localize glob [ok - SUTM1]');
140 Test::More::is($main::x, undef, 'localize glob [ok - main]');
145 package Scope::Upper::Test::Mock2;
148 main::callthrough(*main::x, 5);
149 Test::More::is($x, undef, 'localize qualified glob [ok - SUTM2]');
150 Test::More::is($main::x, 5, 'localize qualified glob [ok - main]');
155 package Scope::Upper::Test::Mock3;
158 main::callthrough('$main::x', 6);
159 Test::More::is($x, undef, 'localize fully qualified name [ok - SUTM3]');
160 Test::More::is($main::x, 6, 'localize fully qualified name [ok - main]');
165 package Scope::Upper::Test::Mock4;
168 main::callthrough('$x', 7);
169 Test::More::is($x, 7, 'localize unqualified name [ok - SUTM4]');
170 Test::More::is($main::x, undef, 'localize unqualified name [ok - main]');
176 package Scope::Upper::Test::Mock5;
178 main::callthrough('$_', 'bar');
179 Test::More::ok(/bar/, 'localize $_ [ok]');
192 localize *a, $xa => HERE;
193 is_deeply \@a, $xa, 'localize *a, [ ] => HERE [ok]';
195 is_deeply \@a, [ 4 .. 6 ], 'localize *a, [ ] => HERE [end]';
203 localize *a, $xa => UP;
204 is_deeply \@a, [ 5 .. 7 ], 'localize *a, [ ] => UP [not yet]';
206 is_deeply \@a, $xa, 'localize *a, [ ] => UP [ok]';
208 is_deeply \@a, [ 4 .. 6 ], 'localize *a, [ ] => UP [end]';
214 my $xh = { a => 5, c => 7 };
217 local %h = (a => 1, b => 2);
219 localize *h, $xh => HERE;
220 is_deeply \%h, $xh, 'localize *h, { } => HERE [ok]';
222 is_deeply \%h, { a => 1, b => 2 }, 'localize *h, { } => HERE [end]';
226 local %h = (a => 1, b => 2);
228 local %h = (b => 3, c => 4);
230 localize *h, $xh => UP;
231 is_deeply \%h, { b => 3, c => 4 }, 'localize *h, { } => UP [not yet]';
233 is_deeply \%h, $xh, 'localize *h, { } => UP [ok]';
235 is_deeply \%h, { a => 1, b => 2 }, 'localize *h, { } => UP [end]';
241 local *foo = sub { 7 };
243 localize *foo, sub { 6 } => UP;
244 is foo(), 7, 'localize *foo, sub { 6 } => UP [not yet]';
246 is foo(), 6, 'localize *foo, sub { 6 } => UP [ok]';
250 local *foo = sub { 9 };
252 localize '&foo', sub { 8 } => UP;
253 is foo(), 9, 'localize "&foo", sub { 8 } => UP [not yet]';
255 is foo(), 8, 'localize "&foo", sub { 8 } => UP [ok]';
259 local *foo = sub { 'a' };
262 localize *foo, sub { 'b' } => UP;
263 is foo(), 'a', 'localize *foo, sub { "b" } => UP [not yet 1]';
265 no warnings 'redefine';
266 local *foo = sub { 'c' };
267 is foo(), 'c', 'localize *foo, sub { "b" } => UP [localized 1]';
269 is foo(), 'a', 'localize *foo, sub { "b" } => UP [not yet 2]';
271 is foo(), 'b', 'localize *foo, sub { "b" } => UP [ok 1]';
273 no warnings 'redefine';
274 local *foo = sub { 'd' };
275 is foo(), 'd', 'localize *foo, sub { "b" } => UP [localized 2]';
277 is foo(), 'b', 'localize *foo, sub { "b" } => UP [ok 2]';
279 is foo(), 'a', 'localize *foo, sub { "b" } => UP [end]';
283 local *foo = sub { 'x' };
286 localize *foo, sub { 'y' } => UP;
287 is foo(), 'x', 'localize *foo, sub { "y" } => UP [not yet]';
289 is foo(), 'y', 'localize *foo, sub { "y" } => UP [ok]';
290 no warnings 'redefine';
292 is foo(), 'z', 'localize *foo, sub { "y" } => UP [replaced]';
294 is foo(), 'x', 'localize *foo, sub { "y" } => UP [end]';
297 sub X::foo { 'X::foo' }
302 localize 'X::foo', sub { 'X::foo 2' } => UP;
303 is(X->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [not yet]')
305 is(X->foo, 'X::foo 2', 'localize "X::foo", sub { "X::foo 2" } => UP [ok]');
307 is(X->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [end]');
315 localize 'X::foo', sub { 'X::foo 3' } => UP;
316 is(Y->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 3" } => UP [not yet]')
318 is(Y->foo, 'X::foo 3', 'localize "X::foo", sub { "X::foo 3" } => UP [ok]');
320 is(Y->foo, 'X::foo', 'localize "X::foo", sub { "X::foo 2" } => UP [end]');
326 localize 'Y::foo', sub { 'Y::foo' } => UP;
327 is(Y->foo, 'X::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [not yet]');
329 is(Y->foo, 'Y::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [ok]');
331 is(Y->foo, 'X::foo', 'localize "Y::foo", sub { "Y::foo" } => UP [end]');
336 sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
339 eval { localize \1, 0 => HERE };
340 like $@, invalid_ref('SCALAR'), 'invalid localize \1, 0 => HERE';
344 eval { localize [ ], 0 => HERE };
345 like $@, invalid_ref('ARRAY'), 'invalid localize [ ], 0 => HERE';
349 eval { localize { }, 0 => HERE };
350 like $@, invalid_ref('HASH'), 'invalid localize { }, 0 => HERE';
354 eval { localize sub { }, 0 => HERE };
355 like $@, invalid_ref('CODE'), 'invalid localize sub { }, 0 => HERE';