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