Commit | Line | Data |
---|---|---|
49d42823 | 1 | #!./perl |
2 | ||
498ea806 CN |
3 | # This test harness will (eventually) test the "tie" functionality |
4 | # without the need for a *DBM* implementation. | |
5 | ||
6 | # Currently it only tests the untie warning | |
49d42823 | 7 | |
8 | chdir 't' if -d 't'; | |
20822f61 | 9 | @INC = '../lib'; |
49d42823 | 10 | $ENV{PERL5LIB} = "../lib"; |
11 | ||
12 | $|=1; | |
13 | ||
498ea806 CN |
14 | # catch warnings into fatal errors |
15 | $SIG{__WARN__} = sub { die "WARNING: @_" } ; | |
16 | $SIG{__DIE__} = sub { die @_ }; | |
17 | ||
49d42823 | 18 | undef $/; |
498ea806 CN |
19 | @prgs = split "\n########\n", <DATA>; |
20 | print "1..", scalar @prgs, "\n"; | |
49d42823 | 21 | |
22 | for (@prgs){ | |
498ea806 CN |
23 | my($prog,$expected) = split(/\nEXPECT\n/, $_); |
24 | eval "$prog" ; | |
25 | $status = $?; | |
26 | $results = $@ ; | |
49d42823 | 27 | $results =~ s/\n+$//; |
28 | $expected =~ s/\n+$//; | |
498ea806 CN |
29 | if ( $status or $results and $results !~ /^(WARNING: )?$expected/){ |
30 | print STDERR "STATUS: $status\n"; | |
31 | print STDERR "PROG: $prog\n"; | |
32 | print STDERR "EXPECTED:\n$expected\n"; | |
33 | print STDERR "GOT:\n$results\n"; | |
34 | print "not "; | |
35 | } | |
36 | print "ok ", ++$i, "\n"; | |
49d42823 | 37 | } |
38 | ||
39 | __END__ | |
40 | ||
41 | # standard behaviour, without any extra references | |
42 | use Tie::Hash ; | |
43 | tie %h, Tie::StdHash; | |
44 | untie %h; | |
45 | EXPECT | |
46 | ######## | |
47 | ||
a29a5827 NIS |
48 | # standard behaviour, without any extra references |
49 | use Tie::Hash ; | |
50 | {package Tie::HashUntie; | |
51 | use base 'Tie::StdHash'; | |
52 | sub UNTIE | |
53 | { | |
54 | warn "Untied\n"; | |
55 | } | |
56 | } | |
57 | tie %h, Tie::HashUntie; | |
58 | untie %h; | |
59 | EXPECT | |
60 | Untied | |
61 | ######## | |
62 | ||
49d42823 | 63 | # standard behaviour, with 1 extra reference |
64 | use Tie::Hash ; | |
65 | $a = tie %h, Tie::StdHash; | |
66 | untie %h; | |
67 | EXPECT | |
68 | ######## | |
69 | ||
70 | # standard behaviour, with 1 extra reference via tied | |
71 | use Tie::Hash ; | |
72 | tie %h, Tie::StdHash; | |
73 | $a = tied %h; | |
74 | untie %h; | |
75 | EXPECT | |
76 | ######## | |
77 | ||
78 | # standard behaviour, with 1 extra reference which is destroyed | |
79 | use Tie::Hash ; | |
80 | $a = tie %h, Tie::StdHash; | |
81 | $a = 0 ; | |
82 | untie %h; | |
83 | EXPECT | |
84 | ######## | |
85 | ||
86 | # standard behaviour, with 1 extra reference via tied which is destroyed | |
87 | use Tie::Hash ; | |
88 | tie %h, Tie::StdHash; | |
89 | $a = tied %h; | |
90 | $a = 0 ; | |
91 | untie %h; | |
92 | EXPECT | |
93 | ######## | |
94 | ||
95 | # strict behaviour, without any extra references | |
4438c4b7 | 96 | use warnings 'untie'; |
49d42823 | 97 | use Tie::Hash ; |
98 | tie %h, Tie::StdHash; | |
99 | untie %h; | |
100 | EXPECT | |
101 | ######## | |
102 | ||
103 | # strict behaviour, with 1 extra references generating an error | |
4438c4b7 | 104 | use warnings 'untie'; |
49d42823 | 105 | use Tie::Hash ; |
106 | $a = tie %h, Tie::StdHash; | |
107 | untie %h; | |
108 | EXPECT | |
498ea806 | 109 | untie attempted while 1 inner references still exist |
49d42823 | 110 | ######## |
111 | ||
112 | # strict behaviour, with 1 extra references via tied generating an error | |
4438c4b7 | 113 | use warnings 'untie'; |
49d42823 | 114 | use Tie::Hash ; |
115 | tie %h, Tie::StdHash; | |
116 | $a = tied %h; | |
117 | untie %h; | |
118 | EXPECT | |
498ea806 | 119 | untie attempted while 1 inner references still exist |
49d42823 | 120 | ######## |
121 | ||
122 | # strict behaviour, with 1 extra references which are destroyed | |
4438c4b7 | 123 | use warnings 'untie'; |
49d42823 | 124 | use Tie::Hash ; |
125 | $a = tie %h, Tie::StdHash; | |
126 | $a = 0 ; | |
127 | untie %h; | |
128 | EXPECT | |
129 | ######## | |
130 | ||
131 | # strict behaviour, with extra 1 references via tied which are destroyed | |
4438c4b7 | 132 | use warnings 'untie'; |
49d42823 | 133 | use Tie::Hash ; |
134 | tie %h, Tie::StdHash; | |
135 | $a = tied %h; | |
136 | $a = 0 ; | |
137 | untie %h; | |
138 | EXPECT | |
139 | ######## | |
140 | ||
498ea806 | 141 | # strict error behaviour, with 2 extra references |
4438c4b7 | 142 | use warnings 'untie'; |
49d42823 | 143 | use Tie::Hash ; |
144 | $a = tie %h, Tie::StdHash; | |
145 | $b = tied %h ; | |
146 | untie %h; | |
147 | EXPECT | |
498ea806 | 148 | untie attempted while 2 inner references still exist |
49d42823 | 149 | ######## |
150 | ||
151 | # strict behaviour, check scope of strictness. | |
4438c4b7 | 152 | no warnings 'untie'; |
49d42823 | 153 | use Tie::Hash ; |
154 | $A = tie %H, Tie::StdHash; | |
155 | $C = $B = tied %H ; | |
156 | { | |
4438c4b7 | 157 | use warnings 'untie'; |
49d42823 | 158 | use Tie::Hash ; |
159 | tie %h, Tie::StdHash; | |
160 | untie %h; | |
161 | } | |
162 | untie %H; | |
163 | EXPECT | |
33c27489 | 164 | ######## |
ae21d580 | 165 | # Forbidden aggregate self-ties |
498ea806 | 166 | my ($a, $b) = (0, 0); |
33c27489 | 167 | sub Self::TIEHASH { bless $_[1], $_[0] } |
498ea806 | 168 | sub Self::DESTROY { $b = $_[0] + 1; } |
ae21d580 | 169 | { |
498ea806 | 170 | my %c = 42; |
ae21d580 JH |
171 | tie %c, 'Self', \%c; |
172 | } | |
173 | EXPECT | |
498ea806 | 174 | Self-ties of arrays and hashes are not supported |
ae21d580 JH |
175 | ######## |
176 | # Allowed scalar self-ties | |
498ea806 | 177 | my ($a, $b) = (0, 0); |
ae21d580 | 178 | sub Self::TIESCALAR { bless $_[1], $_[0] } |
498ea806 | 179 | sub Self::DESTROY { $b = $_[0] + 1; } |
33c27489 | 180 | { |
ae21d580 | 181 | my $c = 42; |
498ea806 | 182 | $a = $c + 0; |
ae21d580 | 183 | tie $c, 'Self', \$c; |
33c27489 | 184 | } |
498ea806 CN |
185 | die unless $a == 0 && $b == 43; |
186 | EXPECT | |
187 | ######## | |
188 | # Interaction of tie and vec | |
189 | ||
190 | my ($a, $b); | |
191 | use Tie::Scalar; | |
192 | tie $a,Tie::StdScalar or die; | |
193 | vec($b,1,1)=1; | |
194 | $a = $b; | |
195 | vec($a,1,1)=0; | |
196 | vec($b,1,1)=0; | |
197 | die unless $a eq $b; | |
bee2c548 CN |
198 | EXPECT |
199 | ######## | |
498ea806 CN |
200 | # An attempt at lvalueable barewords broke this |
201 | ||
202 | tie FH, 'main'; | |
203 | EXPECT | |
0358cc2c | 204 | |
498ea806 CN |
205 | ######## |
206 | # correct unlocalisation of tied hashes (patch #16431) | |
207 | use Tie::Hash ; | |
208 | tie %tied, Tie::StdHash; | |
209 | { local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'}; | |
210 | { local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'}; | |
211 | { local $ENV{'foo'} } warn "%ENV bad unlocalize" if exists $ENV{'foo'}; | |
212 | EXPECT | |
213 | ######## | |
bee2c548 CN |
214 | # Allowed glob self-ties |
215 | my $destroyed = 0; | |
498ea806 | 216 | my $printed = 0; |
bee2c548 CN |
217 | sub Self2::TIEHANDLE { bless $_[1], $_[0] } |
218 | sub Self2::DESTROY { $destroyed = 1; } | |
498ea806 | 219 | sub Self2::PRINT { $printed = 1; } |
bee2c548 CN |
220 | { |
221 | use Symbol; | |
222 | my $c = gensym; | |
223 | tie *$c, 'Self2', $c; | |
498ea806 | 224 | print $c 'Hello'; |
bee2c548 | 225 | } |
498ea806 CN |
226 | die "self-tied glob not PRINTed" unless $printed == 1; |
227 | die "self-tied glob not DESTROYed" unless $destroyed == 1; | |
bee2c548 CN |
228 | EXPECT |
229 | ######## | |
0358cc2c | 230 | |
bee2c548 CN |
231 | # Allowed IO self-ties |
232 | my $destroyed = 0; | |
233 | sub Self3::TIEHANDLE { bless $_[1], $_[0] } | |
234 | sub Self3::DESTROY { $destroyed = 1; } | |
235 | { | |
236 | use Symbol 'geniosym'; | |
237 | my $c = geniosym; | |
238 | tie *$c, 'Self3', $c; | |
239 | } | |
498ea806 | 240 | die "self-tied IO not DESTROYed" unless $destroyed == 1; |
62822184 | 241 | EXPECT |
0358cc2c | 242 | ######## |
62822184 | 243 |