Commit | Line | Data |
49d42823 |
1 | #!./perl |
2 | |
d87ebaca |
3 | # Add new tests to the end with format: |
4 | # ######## |
5 | # |
6 | # # test description |
7 | # Test code |
8 | # EXPECT |
9 | # Warn or die msgs (if any) at - line 1234 |
10 | # |
49d42823 |
11 | |
12 | chdir 't' if -d 't'; |
20822f61 |
13 | @INC = '../lib'; |
49d42823 |
14 | $ENV{PERL5LIB} = "../lib"; |
15 | |
16 | $|=1; |
17 | |
18 | undef $/; |
d87ebaca |
19 | @prgs = split /^########\n/m, <DATA>; |
49d42823 |
20 | |
d87ebaca |
21 | require './test.pl'; |
22 | plan(tests => scalar @prgs); |
49d42823 |
23 | for (@prgs){ |
d87ebaca |
24 | ++$i; |
25 | my($prog,$expected) = split(/\nEXPECT\n/, $_, 2); |
26 | print("not ok $i # bad test format\n"), next |
27 | unless defined $expected; |
28 | my ($testname) = $prog =~ /^# (.*)\n/m; |
29 | $testname ||= ''; |
30 | $TODO = $testname =~ s/^TODO //; |
49d42823 |
31 | $results =~ s/\n+$//; |
32 | $expected =~ s/\n+$//; |
d87ebaca |
33 | |
34 | fresh_perl_is($prog, $expected, {}, $testname); |
49d42823 |
35 | } |
36 | |
37 | __END__ |
38 | |
39 | # standard behaviour, without any extra references |
40 | use Tie::Hash ; |
41 | tie %h, Tie::StdHash; |
42 | untie %h; |
43 | EXPECT |
44 | ######## |
45 | |
a29a5827 |
46 | # standard behaviour, without any extra references |
47 | use Tie::Hash ; |
48 | {package Tie::HashUntie; |
49 | use base 'Tie::StdHash'; |
50 | sub UNTIE |
51 | { |
52 | warn "Untied\n"; |
53 | } |
54 | } |
55 | tie %h, Tie::HashUntie; |
56 | untie %h; |
57 | EXPECT |
58 | Untied |
59 | ######## |
60 | |
49d42823 |
61 | # standard behaviour, with 1 extra reference |
62 | use Tie::Hash ; |
63 | $a = tie %h, Tie::StdHash; |
64 | untie %h; |
65 | EXPECT |
66 | ######## |
67 | |
68 | # standard behaviour, with 1 extra reference via tied |
69 | use Tie::Hash ; |
70 | tie %h, Tie::StdHash; |
71 | $a = tied %h; |
72 | untie %h; |
73 | EXPECT |
74 | ######## |
75 | |
76 | # standard behaviour, with 1 extra reference which is destroyed |
77 | use Tie::Hash ; |
78 | $a = tie %h, Tie::StdHash; |
79 | $a = 0 ; |
80 | untie %h; |
81 | EXPECT |
82 | ######## |
83 | |
84 | # standard behaviour, with 1 extra reference via tied which is destroyed |
85 | use Tie::Hash ; |
86 | tie %h, Tie::StdHash; |
87 | $a = tied %h; |
88 | $a = 0 ; |
89 | untie %h; |
90 | EXPECT |
91 | ######## |
92 | |
93 | # strict behaviour, without any extra references |
4438c4b7 |
94 | use warnings 'untie'; |
49d42823 |
95 | use Tie::Hash ; |
96 | tie %h, Tie::StdHash; |
97 | untie %h; |
98 | EXPECT |
99 | ######## |
100 | |
101 | # strict behaviour, with 1 extra references generating an error |
4438c4b7 |
102 | use warnings 'untie'; |
49d42823 |
103 | use Tie::Hash ; |
104 | $a = tie %h, Tie::StdHash; |
105 | untie %h; |
106 | EXPECT |
d87ebaca |
107 | untie attempted while 1 inner references still exist at - line 6. |
49d42823 |
108 | ######## |
109 | |
110 | # strict behaviour, with 1 extra references via tied generating an error |
4438c4b7 |
111 | use warnings 'untie'; |
49d42823 |
112 | use Tie::Hash ; |
113 | tie %h, Tie::StdHash; |
114 | $a = tied %h; |
115 | untie %h; |
116 | EXPECT |
d87ebaca |
117 | untie attempted while 1 inner references still exist at - line 7. |
49d42823 |
118 | ######## |
119 | |
120 | # strict behaviour, with 1 extra references which are destroyed |
4438c4b7 |
121 | use warnings 'untie'; |
49d42823 |
122 | use Tie::Hash ; |
123 | $a = tie %h, Tie::StdHash; |
124 | $a = 0 ; |
125 | untie %h; |
126 | EXPECT |
127 | ######## |
128 | |
129 | # strict behaviour, with extra 1 references via tied which are destroyed |
4438c4b7 |
130 | use warnings 'untie'; |
49d42823 |
131 | use Tie::Hash ; |
132 | tie %h, Tie::StdHash; |
133 | $a = tied %h; |
134 | $a = 0 ; |
135 | untie %h; |
136 | EXPECT |
137 | ######## |
138 | |
87f0b213 |
139 | # strict error behaviour, with 2 extra references |
4438c4b7 |
140 | use warnings 'untie'; |
49d42823 |
141 | use Tie::Hash ; |
142 | $a = tie %h, Tie::StdHash; |
143 | $b = tied %h ; |
144 | untie %h; |
145 | EXPECT |
d87ebaca |
146 | untie attempted while 2 inner references still exist at - line 7. |
49d42823 |
147 | ######## |
148 | |
149 | # strict behaviour, check scope of strictness. |
4438c4b7 |
150 | no warnings 'untie'; |
49d42823 |
151 | use Tie::Hash ; |
152 | $A = tie %H, Tie::StdHash; |
153 | $C = $B = tied %H ; |
154 | { |
4438c4b7 |
155 | use warnings 'untie'; |
49d42823 |
156 | use Tie::Hash ; |
157 | tie %h, Tie::StdHash; |
158 | untie %h; |
159 | } |
160 | untie %H; |
161 | EXPECT |
33c27489 |
162 | ######## |
d87ebaca |
163 | |
ae21d580 |
164 | # Forbidden aggregate self-ties |
33c27489 |
165 | sub Self::TIEHASH { bless $_[1], $_[0] } |
ae21d580 |
166 | { |
d87ebaca |
167 | my %c; |
ae21d580 |
168 | tie %c, 'Self', \%c; |
169 | } |
170 | EXPECT |
d87ebaca |
171 | Self-ties of arrays and hashes are not supported at - line 6. |
ae21d580 |
172 | ######## |
d87ebaca |
173 | |
ae21d580 |
174 | # Allowed scalar self-ties |
d87ebaca |
175 | my $destroyed = 0; |
ae21d580 |
176 | sub Self::TIESCALAR { bless $_[1], $_[0] } |
d87ebaca |
177 | sub Self::DESTROY { $destroyed = 1; } |
33c27489 |
178 | { |
ae21d580 |
179 | my $c = 42; |
ae21d580 |
180 | tie $c, 'Self', \$c; |
33c27489 |
181 | } |
d87ebaca |
182 | die "self-tied scalar not DESTROYed" unless $destroyed == 1; |
7bb043c3 |
183 | EXPECT |
83f527ec |
184 | ######## |
3ca7705e |
185 | |
b5ccf5f2 |
186 | # Allowed glob self-ties |
87f0b213 |
187 | my $destroyed = 0; |
188 | my $printed = 0; |
189 | sub Self2::TIEHANDLE { bless $_[1], $_[0] } |
190 | sub Self2::DESTROY { $destroyed = 1; } |
191 | sub Self2::PRINT { $printed = 1; } |
192 | { |
193 | use Symbol; |
194 | my $c = gensym; |
195 | tie *$c, 'Self2', $c; |
196 | print $c 'Hello'; |
197 | } |
198 | die "self-tied glob not PRINTed" unless $printed == 1; |
43bb546a |
199 | die "self-tied glob not DESTROYed" unless $destroyed == 1; |
87f0b213 |
200 | EXPECT |
201 | ######## |
202 | |
203 | # Allowed IO self-ties |
204 | my $destroyed = 0; |
205 | sub Self3::TIEHANDLE { bless $_[1], $_[0] } |
206 | sub Self3::DESTROY { $destroyed = 1; } |
b5ccf5f2 |
207 | sub Self3::PRINT { $printed = 1; } |
87f0b213 |
208 | { |
209 | use Symbol 'geniosym'; |
210 | my $c = geniosym; |
211 | tie *$c, 'Self3', $c; |
b5ccf5f2 |
212 | print $c 'Hello'; |
87f0b213 |
213 | } |
b5ccf5f2 |
214 | die "self-tied IO not PRINTed" unless $printed == 1; |
43bb546a |
215 | die "self-tied IO not DESTROYed" unless $destroyed == 1; |
87f0b213 |
216 | EXPECT |
217 | ######## |
0b2c215a |
218 | |
b5ccf5f2 |
219 | # TODO IO "self-tie" via TEMP glob |
220 | my $destroyed = 0; |
221 | sub Self3::TIEHANDLE { bless $_[1], $_[0] } |
222 | sub Self3::DESTROY { $destroyed = 1; } |
223 | sub Self3::PRINT { $printed = 1; } |
224 | { |
225 | use Symbol 'geniosym'; |
226 | my $c = geniosym; |
227 | tie *$c, 'Self3', \*$c; |
228 | print $c 'Hello'; |
229 | } |
230 | die "IO tied to TEMP glob not PRINTed" unless $printed == 1; |
231 | die "IO tied to TEMP glob not DESTROYed" unless $destroyed == 1; |
232 | EXPECT |
233 | ######## |
234 | |
d87ebaca |
235 | # Interaction of tie and vec |
236 | |
237 | my ($a, $b); |
238 | use Tie::Scalar; |
239 | tie $a,Tie::StdScalar or die; |
240 | vec($b,1,1)=1; |
241 | $a = $b; |
242 | vec($a,1,1)=0; |
243 | vec($b,1,1)=0; |
244 | die unless $a eq $b; |
245 | EXPECT |
246 | ######## |
247 | |
248 | # correct unlocalisation of tied hashes (patch #16431) |
249 | use Tie::Hash ; |
250 | tie %tied, Tie::StdHash; |
251 | { local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'}; |
252 | { local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'}; |
253 | { local $ENV{'foo'} } warn "%ENV bad unlocalize" if exists $ENV{'foo'}; |
254 | EXPECT |
255 | ######## |
256 | |
257 | # An attempt at lvalueable barewords broke this |
258 | tie FH, 'main'; |
259 | EXPECT |
260 | Can't modify constant item in tie at - line 3, near "'main';" |
261 | Execution of - aborted due to compilation errors. |
eb85dfd3 |
262 | ######## |
263 | |
264 | # localizing tied hash slices |
265 | $ENV{FooA} = 1; |
266 | $ENV{FooB} = 2; |
267 | print exists $ENV{FooA} ? 1 : 0, "\n"; |
268 | print exists $ENV{FooB} ? 2 : 0, "\n"; |
269 | print exists $ENV{FooC} ? 3 : 0, "\n"; |
270 | { |
271 | local @ENV{qw(FooA FooC)}; |
272 | print exists $ENV{FooA} ? 4 : 0, "\n"; |
273 | print exists $ENV{FooB} ? 5 : 0, "\n"; |
274 | print exists $ENV{FooC} ? 6 : 0, "\n"; |
275 | } |
276 | print exists $ENV{FooA} ? 7 : 0, "\n"; |
277 | print exists $ENV{FooB} ? 8 : 0, "\n"; |
278 | print exists $ENV{FooC} ? 9 : 0, "\n"; # this should not exist |
279 | EXPECT |
280 | 1 |
281 | 2 |
282 | 0 |
283 | 4 |
284 | 5 |
285 | 6 |
286 | 7 |
287 | 8 |
288 | 0 |
b77f7d40 |
289 | ######## |
290 | # |
291 | # FETCH freeing tie'd SV |
292 | sub TIESCALAR { bless [] } |
293 | sub FETCH { *a = \1; 1 } |
294 | tie $a, 'main'; |
295 | print $a; |
296 | EXPECT |
297 | Tied variable freed while still in use at - line 6. |
dd28f7bb |
298 | ######## |
299 | |
300 | # [20020716.007] - nested FETCHES |
301 | |
302 | sub F1::TIEARRAY { bless [], 'F1' } |
303 | sub F1::FETCH { 1 } |
304 | my @f1; |
305 | tie @f1, 'F1'; |
306 | |
307 | sub F2::TIEARRAY { bless [2], 'F2' } |
308 | sub F2::FETCH { my $self = shift; my $x = $f1[3]; $self } |
309 | my @f2; |
310 | tie @f2, 'F2'; |
311 | |
312 | print $f2[4][0],"\n"; |
313 | |
314 | sub F3::TIEHASH { bless [], 'F3' } |
315 | sub F3::FETCH { 1 } |
316 | my %f3; |
317 | tie %f3, 'F3'; |
318 | |
319 | sub F4::TIEHASH { bless [3], 'F4' } |
320 | sub F4::FETCH { my $self = shift; my $x = $f3{3}; $self } |
321 | my %f4; |
322 | tie %f4, 'F4'; |
323 | |
324 | print $f4{'foo'}[0],"\n"; |
325 | |
326 | EXPECT |
327 | 2 |
328 | 3 |
38193a09 |
329 | ######## |
330 | # test untie() from within FETCH |
331 | package Foo; |
332 | sub TIESCALAR { my $pkg = shift; return bless [@_], $pkg; } |
333 | sub FETCH { |
334 | my $self = shift; |
335 | my ($obj, $field) = @$self; |
336 | untie $obj->{$field}; |
337 | $obj->{$field} = "Bar"; |
338 | } |
339 | package main; |
340 | tie $a->{foo}, "Foo", $a, "foo"; |
341 | $a->{foo}; # access once |
342 | # the hash element should not be tied anymore |
343 | print defined tied $a->{foo} ? "not ok" : "ok"; |
344 | EXPECT |
345 | ok |
be65207d |
346 | ######## |
347 | # the tmps returned by FETCH should appear to be SCALAR |
348 | # (even though they are now implemented using PVLVs.) |
349 | package X; |
350 | sub TIEHASH { bless {} } |
351 | sub TIEARRAY { bless {} } |
352 | sub FETCH {1} |
353 | my (%h, @a); |
354 | tie %h, 'X'; |
355 | tie @a, 'X'; |
356 | my $r1 = \$h{1}; |
357 | my $r2 = \$a[0]; |
358 | my $s = "$r1 ". ref($r1) . " $r2 " . ref($r2); |
359 | $s=~ s/\(0x\w+\)//g; |
360 | print $s, "\n"; |
361 | EXPECT |
362 | SCALAR SCALAR SCALAR SCALAR |
b7056d9c |
363 | ######## |
364 | # [perl #23287] segfault in untie |
365 | sub TIESCALAR { bless $_[1], $_[0] } |
366 | my $var; |
367 | tie $var, 'main', \$var; |
368 | untie $var; |
369 | EXPECT |
16e0ce55 |
370 | ######## |
371 | # Test case from perlmonks by runrig |
372 | # http://www.perlmonks.org/index.pl?node_id=273490 |
373 | # "Here is what I tried. I think its similar to what you've tried |
374 | # above. Its odd but convienient that after untie'ing you are left with |
375 | # a variable that has the same value as was last returned from |
376 | # FETCH. (At least on my perl v5.6.1). So you don't need to pass a |
377 | # reference to the variable in order to set it after the untie (here it |
378 | # is accessed through a closure)." |
379 | use strict; |
380 | use warnings; |
381 | package MyTied; |
382 | sub TIESCALAR { |
383 | my ($class,$code) = @_; |
384 | bless $code, $class; |
385 | } |
386 | sub FETCH { |
387 | my $self = shift; |
388 | print "Untie\n"; |
389 | $self->(); |
390 | } |
391 | package main; |
392 | my $var; |
393 | tie $var, 'MyTied', sub { untie $var; 4 }; |
394 | print "One\n"; |
395 | print "$var\n"; |
396 | print "Two\n"; |
397 | print "$var\n"; |
398 | print "Three\n"; |
399 | print "$var\n"; |
400 | EXPECT |
401 | One |
402 | Untie |
403 | 4 |
404 | Two |
405 | 4 |
406 | Three |
407 | 4 |