]> git.vpit.fr Git - perl/modules/Variable-Temp.git/blob - t/20-lvalue.t
This is 0.04
[perl/modules/Variable-Temp.git] / t / 20-lvalue.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Variable::Temp 'temp';
7
8 use Test::More;
9
10 BEGIN {
11  if ("$]" < 5.014) {
12   plan skip_all => 'perl 5.14 required to use lvalue temp()';
13  } else {
14   plan tests => (9 + 2 * 19) * 2 + 6 * 3;
15  }
16 }
17
18 sub describe {
19  my $h = $_[0];
20  return join ', ', map "$_:$h->{$_}", sort keys %$h;
21 }
22
23 my $aelem_delete_msg = 'Localized extraneous array elements do not reset array length at scope end before perl 5.12';
24 my $aelem_delete_ok  = ("$]" >= 5.012);
25
26 # Lexicals
27
28 {
29  my $x = 1;
30  is $x, 1;
31  {
32   temp $x = 2;
33   is $x, 2;
34   $x = 3;
35   is $x, 3;
36  }
37  is $x, 1;
38  {
39   temp $x = 4;
40   is $x, 4;
41   temp $x = 5;
42   is $x, 5;
43  }
44  is $x, 1;
45  {
46   temp $x;
47   is $x, undef;
48  }
49  is $x, 1;
50 }
51
52 {
53  my @y = (1, 2);
54  is "@y", "1 2";
55  {
56   temp @y = [ 3 ];
57   is "@y", '3';
58   @y = (4, 5, 6);
59   is "@y", '4 5 6';
60   $y[3] = 7;
61   is "@y", '4 5 6 7';
62  }
63  is "@y", "1 2";
64  {
65   temp @y = [ 8, 9, 10 ];
66   is "@y", '8 9 10';
67   $y[1] = 11;
68   is "@y", '8 11 10';
69  }
70  is "@y", "1 2";
71  {
72   temp @y = [ 12, 13, 14 ];
73   is "@y", '12 13 14';
74   temp @y = [ 15, 16];
75   is "@y", '15 16';
76  }
77  is "@y", '1 2';
78  {
79   temp @y;
80   is "@y", '';
81  }
82  is "@y", '1 2';
83  {
84   temp @y = [ qw<a b c> ];
85   is "@y", 'a b c';
86   SKIP: {
87    skip $aelem_delete_msg => 3 unless $aelem_delete_ok;
88    local $y[1] = 'd';
89    is "@y", 'a d c';
90    {
91     local @y[2, 3] = qw<e f>;
92     is "@y", 'a d e f';
93    }
94    is "@y", 'a d c';
95   }
96   is "@y", 'a b c';
97  }
98  is "@y", '1 2';
99 }
100
101 {
102  my %z = (a => 1);
103  is describe(\%z), 'a:1';
104  {
105   temp %z = { b => 2 };
106   is describe(\%z), 'b:2';
107   %z = (c => 3);
108   is describe(\%z), 'c:3';
109   $z{d} = 4;
110   is describe(\%z), 'c:3, d:4';
111  }
112  is describe(\%z), 'a:1';
113  {
114   temp %z = { a => 5 };
115   is describe(\%z), 'a:5';
116   $z{a} = 6;
117   is describe(\%z), 'a:6';
118  }
119  is describe(\%z), 'a:1';
120  {
121   temp %z = { a => 7, d => 8 };
122   is describe(\%z), 'a:7, d:8';
123   temp %z = { d => 9, e => 10 };
124   is describe(\%z), 'd:9, e:10';
125  }
126  is describe(\%z), 'a:1';
127  {
128   temp %z;
129   is describe(\%z), '';
130  }
131  is describe(\%z), 'a:1';
132  {
133   temp %z = { a => 11, f => 12 };
134   is describe(\%z), 'a:11, f:12';
135   {
136    local $z{a} = 13;
137    is describe(\%z), 'a:13, f:12';
138    {
139     local @z{qw<f g>} = (14, 15);
140     is describe(\%z), 'a:13, f:14, g:15';
141    }
142    is describe(\%z), 'a:13, f:12';
143   }
144   is describe(\%z), 'a:11, f:12';
145  }
146  is describe(\%z), 'a:1';
147 }
148
149 # Globals
150
151 {
152  our $X = 1;
153  is $X, 1;
154  {
155   temp $X = 2;
156   is $X, 2;
157   $X = 3;
158   is $X, 3;
159  }
160  is $X, 1;
161  {
162   temp $X = 4;
163   is $X, 4;
164   temp $X = 5;
165   is $X, 5;
166  }
167  is $X, 1;
168  {
169   temp $X;
170   is $X, undef;
171  }
172  is $X, 1;
173  {
174   local $X = 6;
175   is $X, 6;
176  }
177  is $X, 1;
178  {
179   local $X = 7;
180   temp $X = 8;
181   is $X, 8;
182  }
183  is $X, 1;
184  {
185   temp $X = 9;
186   local $X = 10;
187   is $X, 10;
188  }
189  is $X, 1;
190 }
191
192 {
193  our @Y = (1, 2);
194  is "@Y", "1 2";
195  {
196   temp @Y = [ 3 ];
197   is "@Y", '3';
198   @Y = (4, 5, 6);
199   is "@Y", '4 5 6';
200   $Y[3] = 7;
201   is "@Y", '4 5 6 7';
202  }
203  is "@Y", "1 2";
204  {
205   temp @Y = [ 8, 9, 10 ];
206   is "@Y", '8 9 10';
207   $Y[1] = 11;
208   is "@Y", '8 11 10';
209  }
210  is "@Y", "1 2";
211  {
212   temp @Y = [ 12, 13, 14 ];
213   is "@Y", '12 13 14';
214   temp @Y = [ 15, 16];
215   is "@Y", '15 16';
216  }
217  is "@Y", '1 2';
218  {
219   temp @Y;
220   is "@Y", '';
221  }
222  is "@Y", '1 2';
223  {
224   temp @Y = [ qw<a b c> ];
225   is "@Y", 'a b c';
226   SKIP: {
227    skip $aelem_delete_msg => 3 unless $aelem_delete_ok;
228    local $Y[1] = 'd';
229    is "@Y", 'a d c';
230    {
231     local @Y[2, 3] = qw<e f>;
232     is "@Y", 'a d e f';
233    }
234    is "@Y", 'a d c';
235   }
236   is "@Y", 'a b c';
237  }
238  is "@Y", '1 2';
239  {
240   local @Y = qw<A B>;
241   is "@Y", 'A B';
242  }
243  is "@Y", '1 2';
244  {
245   local @Y = qw<C D E>;
246   temp @Y = [ qw<F> ];
247   is "@Y", 'F';
248  }
249  is "@Y", '1 2';
250  {
251   temp @Y = [ qw<G H I> ];
252   local @Y = qw<J>;
253   is "@Y", 'J';
254  }
255  is "@Y", '1 2';
256 }
257
258 {
259  our %Z = (a => 1);
260  is describe(\%Z), 'a:1';
261  {
262   temp %Z = { b => 2 };
263   is describe(\%Z), 'b:2';
264   %Z = (c => 3);
265   is describe(\%Z), 'c:3';
266   $Z{d} = 4;
267   is describe(\%Z), 'c:3, d:4';
268  }
269  is describe(\%Z), 'a:1';
270  {
271   temp %Z = { a => 5 };
272   is describe(\%Z), 'a:5';
273   $Z{a} = 6;
274   is describe(\%Z), 'a:6';
275  }
276  is describe(\%Z), 'a:1';
277  {
278   temp %Z = { a => 7, d => 8 };
279   is describe(\%Z), 'a:7, d:8';
280   temp %Z = { d => 9, e => 10 };
281   is describe(\%Z), 'd:9, e:10';
282  }
283  is describe(\%Z), 'a:1';
284  {
285   temp %Z;
286   is describe(\%Z), '';
287  }
288  is describe(\%Z), 'a:1';
289  {
290   temp %Z = { a => 11, f => 12 };
291   is describe(\%Z), 'a:11, f:12';
292   {
293    local $Z{a} = 13;
294    is describe(\%Z), 'a:13, f:12';
295    {
296     local @Z{qw<f g>} = (14, 15);
297     is describe(\%Z), 'a:13, f:14, g:15';
298    }
299    is describe(\%Z), 'a:13, f:12';
300   }
301   is describe(\%Z), 'a:11, f:12';
302  }
303  is describe(\%Z), 'a:1';
304  {
305   local %Z = (A => 1, B => 2);
306   is describe(\%Z), 'A:1, B:2';
307  }
308  is describe(\%Z), 'a:1';
309  {
310   local %Z = (A => 3, C => 4);
311   temp %Z = { A => 5, D => 6 };
312   is describe(\%Z), 'A:5, D:6';
313  }
314  is describe(\%Z), 'a:1';
315  {
316   temp %Z = { A => 7, E => 8 };
317   local %Z = (A => 9, F => 10);
318   is describe(\%Z), 'A:9, F:10';
319  }
320  is describe(\%Z), 'a:1';
321 }