Debian Codegraph @

t/35-stash.t

raw
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Variable::Magic qw<
9 wizard cast dispell
10 VMG_UVAR VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
11 >;
12
13 my $run;
14 if (VMG_UVAR) {
15 plan tests => 43;
16 $run = 1;
17 } else {
18 plan skip_all => 'uvar magic is required to test symbol table hooks';
19 }
20
21 our %mg;
22
23 my $code = 'wizard '
24 . join (', ', map { <<CB;
25 $_ => sub {
26 my \$d = \$_[1];
27 return 0 if \$d->{guard};
28 local \$d->{guard} = 1;
29 push \@{\$mg{$_}}, \$_[2];
30 ()
31 }
32 CB
33 } qw<fetch store exists delete>);
34
35 $code .= ', data => sub { +{ guard => 0 } }';
36
37 my $wiz = eval $code;
38 diag $@ if $@;
39
40 cast %Hlagh::, $wiz;
41
42 {
43 local %mg;
44
45 eval q{
46 die "ok\n";
47 package Hlagh;
48 our $thing;
49 {
50 package NotHlagh;
51 our $what = @Hlagh::stuff;
52 }
53 };
54
55 is $@, "ok\n", 'stash: variables compiled fine';
56 is_deeply \%mg, {
57 fetch => [ qw<thing stuff> ],
58 store => [ qw<thing stuff> ],
59 }, 'stash: variables';
60 }
61
62 {
63 local %mg;
64
65 eval q[
66 die "ok\n";
67 package Hlagh;
68 sub eat;
69 sub shoot;
70 sub leave { "bye" };
71 sub shoot { "bang" };
72 ];
73
74 is $@, "ok\n", 'stash: function definitions compiled fine';
75 is_deeply \%mg, {
76 store => [ qw<eat shoot leave shoot> ],
77 }, 'stash: function definitions';
78 }
79
80 {
81 local %mg;
82
83 eval q{
84 die "ok\n";
85 package Hlagh;
86 eat();
87 shoot();
88 leave();
89 roam();
90 yawn();
91 roam();
92 };
93
94 my @calls = qw<eat shoot leave roam yawn roam>;
95 my (@fetch, @store, @delete);
96 if ("$]" >= 5.011_002 && "$]" < 5.021_004) {
97 @fetch = @calls;
98 @store = map { ($_) x 2 } @calls;
99 } else {
100 @fetch = @calls;
101 @store = @calls;
102 }
103 my %expected = (
104 fetch => \@fetch,
105 store => \@store,
106 );
107 if ("$]" >= 5.012 && "$]" < 5.021_004) {
108 $expected{delete} = [qw<roam yawn yawn roam>];
109 } elsif ("$]" >= 5.021_004) {
110 $expected{delete} = [qw<yawn roam>];
111 }
112
113 is $@, "ok\n", 'stash: function calls compiled fine';
114 is_deeply \%mg, \%expected, 'stash: function calls';
115 }
116
117 {
118 local %mg;
119
120 eval q{ Hlagh->shoot() };
121
122 is $@, '', 'stash: valid method call ran fine';
123 my %expected = ( fetch => [ qw<shoot> ] );
124 # Typeglob reification may cause a store in 5.28+
125 if ("$]" >= 5.027 && %mg == 2) {
126 $expected{store} = $expected{fetch};
127 }
128 is_deeply \%mg, \%expected, 'stash: valid method call';
129 }
130
131 {
132 local %mg;
133
134 eval q{ Hlagh->shoot() };
135
136 is $@, '', 'stash: second valid method call ran fine';
137 is_deeply \%mg, {
138 fetch => [ qw<shoot> ],
139 }, 'stash: second valid method call';
140 }
141
142 {
143 local %mg;
144
145 eval q{ my $meth = 'shoot'; Hlagh->$meth() };
146
147 is $@, '', 'stash: valid dynamic method call ran fine';
148 is_deeply \%mg, {
149 store => [ qw<shoot> ],
150 }, 'stash: valid dynamic method call';
151 }
152
153 {
154 local %mg;
155
156 eval q[
157 package Hlagher;
158 our @ISA;
159 BEGIN { @ISA = 'Hlagh' }
160 Hlagher->leave()
161 ];
162
163 is $@, '', 'inherited valid method call ran fine';
164 is_deeply \%mg, {
165 fetch => [ qw<ISA leave> ],
166 }, 'stash: inherited valid method call';
167 }
168
169 {
170 local %mg;
171
172 eval q{ Hlagher->leave() };
173
174 is $@, '', 'second inherited valid method call ran fine';
175 is_deeply \%mg, { }, 'stash: second inherited valid method call doesn\'t call magic';
176 }
177
178 {
179 local %mg;
180
181 eval q{ Hlagher->shoot() };
182
183 is $@, '', 'inherited previously called valid method call ran fine';
184 is_deeply \%mg, {
185 fetch => [ qw<shoot> ],
186 }, 'stash: inherited previously called valid method call';
187 }
188
189 {
190 local %mg;
191
192 eval q{ Hlagher->shoot() };
193
194 is $@, '', 'second inherited previously called valid method call ran fine';
195 is_deeply \%mg, { }, 'stash: second inherited previously called valid method call doesn\'t call magic';
196 }
197
198 {
199 local %mg;
200
201 eval q{ Hlagh->unknown() };
202
203 like $@, qr/^Can't locate object method "unknown" via package "Hlagh"/, 'stash: invalid method call croaked';
204 is_deeply \%mg, {
205 fetch => [ qw<unknown> ],
206 store => [ qw<unknown AUTOLOAD> ],
207 }, 'stash: invalid method call';
208 }
209
210 {
211 local %mg;
212
213 eval q{ my $meth = 'unknown_too'; Hlagh->$meth() };
214
215 like $@, qr/^Can't locate object method "unknown_too" via package "Hlagh"/, 'stash: invalid dynamic method call croaked';
216 is_deeply \%mg, {
217 store => [ qw<unknown_too AUTOLOAD> ],
218 }, 'stash: invalid dynamic method call';
219 }
220
221 {
222 local %mg;
223
224 eval q{ Hlagher->also_unknown() };
225
226 like $@, qr/^Can't locate object method "also_unknown" via package "Hlagher"/, 'stash: invalid inherited method call croaked';
227 is_deeply \%mg, {
228 fetch => [ qw<also_unknown AUTOLOAD> ],
229 }, 'stash: invalid method call';
230 }
231
232 {
233 local %mg;
234
235 my @expected_stores = qw<nevermentioned eat shoot>;
236 @expected_stores = map { ($_) x 2 } @expected_stores if "$]" < 5.017_004;
237 push @expected_stores, 'nevermentioned' if "$]" < 5.017_001;
238
239 eval q{
240 package Hlagh;
241 undef &nevermentioned;
242 undef &eat;
243 undef &shoot;
244 };
245
246 is $@, '', 'stash: delete executed fine';
247 is_deeply \%mg, { store => \@expected_stores }, 'stash: delete';
248 }
249
250 END {
251 is_deeply \%mg, { }, 'stash: magic that remains at END time' if $run;
252 }
253
254 dispell %Hlagh::, $wiz;
255
256 {
257 package AutoHlagh;
258
259 use vars qw<$AUTOLOAD>;
260
261 sub AUTOLOAD { return $AUTOLOAD }
262 }
263
264 cast %AutoHlagh::, $wiz;
265
266 {
267 local %mg;
268
269 my $res = eval q{ AutoHlagh->autoloaded() };
270
271 is $@, '', 'stash: autoloaded method call ran fine';
272 is $res, 'AutoHlagh::autoloaded',
273 'stash: autoloaded method call returned the right thing';
274 is_deeply \%mg, {
275 fetch => [ qw<autoloaded> ],
276 store => [ qw<autoloaded AUTOLOAD AUTOLOAD> ],
277 }, 'stash: autoloaded method call';
278 }
279
280 {
281 package AutoHlagher;
282
283 our @ISA;
284 BEGIN { @ISA = ('AutoHlagh') }
285 }
286
287 {
288 local %mg;
289
290 my $res = eval q{ AutoHlagher->also_autoloaded() };
291
292 is $@, '', 'stash: inherited autoloaded method call ran fine';
293 is $res, 'AutoHlagher::also_autoloaded',
294 'stash: inherited autoloaded method returned the right thing';
295 is_deeply \%mg, {
296 fetch => [ qw<also_autoloaded AUTOLOAD> ],
297 store => [ qw<AUTOLOAD> ],
298 }, 'stash: inherited autoloaded method call';
299 }
300
301 dispell %AutoHlagh::, $wiz;
302
303 my $uo = 0;
304 $code = 'wizard '
305 . join (', ', map { <<CB;
306 $_ => sub {
307 my \$d = \$_[1];
308 return 0 if \$d->{guard};
309 local \$d->{guard} = 1;
310 ++\$uo;
311 ()
312 }
313 CB
314 } qw<fetch store exists delete>);
315
316 my $uo_exp =
317 "$]" < 5.011_002 ? 2 :
318 "$]" >= 5.012 && "$]" < 5.021_004 ? 5 :
319 3;
320
321 $code .= ', data => sub { +{ guard => 0 } }';
322
323 $wiz = eval $code . ', op_info => ' . VMG_OP_INFO_NAME;
324 diag $@ if $@;
325
326 cast %Hlagh::, $wiz;
327
328 is $uo, 0, 'stash: no undef op before function call with op name';
329 eval q{
330 die "ok\n";
331 package Hlagh;
332 meh();
333 };
334 is $@, "ok\n", 'stash: function call with op name compiled fine';
335 is $uo, $uo_exp, 'stash: undef op after function call with op name';
336
337 dispell %Hlagh::, $wiz;
338 is $uo, $uo_exp, 'stash: undef op after dispell for function call with op name';
339
340 $uo = 0;
341
342 $wiz = eval $code . ', op_info => ' . VMG_OP_INFO_OBJECT;
343 diag $@ if $@;
344
345 cast %Hlagh::, $wiz;
346
347 is $uo, 0, 'stash: no undef op before function call with op object';
348 eval q{
349 die "ok\n";
350 package Hlagh;
351 wat();
352 };
353 is $@, "ok\n", 'stash: function call with op object compiled fine';
354 is $uo, $uo_exp,
355 'stash: undef op after dispell for function call with op object';
356
357 dispell %Hlagh::, $wiz;
358 is $uo, $uo_exp,
359 'stash: undef op after dispell for function call with op object';