Commit | Line | Data |
c6ddb1d1 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use lib 't/lib'; |
5 | use Test::More; |
6 | use Test::Fatal; |
7 | |
8 | use Package::Stash; |
9 | use Symbol; |
10 | |
11 | plan skip_all => "Anonymous stashes in PP need at least perl 5.14" |
12 | if Package::Stash::BROKEN_GLOB_ASSIGNMENT; |
13 | |
14 | my $Foo = {}; |
15 | $Foo->{SOME_CONSTANT} = \1; |
16 | |
17 | # ---------------------------------------------------------------------- |
18 | ## tests adding a HASH |
19 | |
20 | my $foo_stash = Package::Stash->new($Foo); |
21 | ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet'); |
22 | ok(!$foo_stash->has_symbol('%foo'), '... the object agrees'); |
23 | ok(!defined($Foo->{foo}), '... checking doesn\'t vivify'); |
24 | |
25 | is(exception { |
26 | $foo_stash->add_symbol('%foo' => { one => 1 }); |
27 | }, undef, '... created %Foo::foo successfully'); |
28 | |
29 | # ... scalar should NOT be created here |
30 | |
31 | ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too'); |
32 | ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too'); |
33 | ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too'); |
34 | |
35 | ok(defined($Foo->{foo}), '... the %foo slot was created successfully'); |
36 | ok($foo_stash->has_symbol('%foo'), '... the meta agrees'); |
37 | |
38 | # check the value ... |
39 | |
40 | ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly'); |
41 | is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly'); |
42 | |
43 | my $foo = $foo_stash->get_symbol('%foo'); |
44 | is_deeply({ one => 1 }, $foo, '... got the right package variable back'); |
45 | |
46 | # ... make sure changes propogate up |
47 | |
48 | $foo->{two} = 2; |
49 | |
50 | is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas'); |
51 | |
52 | ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly'); |
53 | is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly'); |
54 | |
55 | # ---------------------------------------------------------------------- |
56 | ## test adding an ARRAY |
57 | |
58 | ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet'); |
59 | |
60 | is(exception { |
61 | $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]); |
62 | }, undef, '... created @Foo::bar successfully'); |
63 | |
64 | ok(defined($Foo->{bar}), '... the @bar slot was created successfully'); |
65 | ok($foo_stash->has_symbol('@bar'), '... the meta agrees'); |
66 | |
67 | # ... why does this not work ... |
68 | |
69 | ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too'); |
70 | ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too'); |
71 | ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too'); |
72 | |
73 | # check the value itself |
74 | |
75 | is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly'); |
76 | is($Foo->{bar}[1], 2, '... our @bar was initialized correctly'); |
77 | |
78 | # ---------------------------------------------------------------------- |
79 | ## test adding a SCALAR |
80 | |
81 | ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet'); |
82 | |
83 | is(exception { |
84 | $foo_stash->add_symbol('$baz' => 10); |
85 | }, undef, '... created $Foo::baz successfully'); |
86 | |
87 | ok(defined($Foo->{baz}), '... the $baz slot was created successfully'); |
88 | ok($foo_stash->has_symbol('$baz'), '... the meta agrees'); |
89 | |
90 | ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too'); |
91 | ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too'); |
92 | ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too'); |
93 | |
94 | is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back'); |
95 | |
96 | ${ $Foo->{baz} } = 1; |
97 | |
98 | is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly'); |
99 | is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees'); |
100 | |
101 | # ---------------------------------------------------------------------- |
102 | ## test adding a CODE |
103 | |
104 | ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet'); |
105 | |
106 | is(exception { |
107 | $foo_stash->add_symbol('&funk' => sub { "Foo::funk" }); |
108 | }, undef, '... created &Foo::funk successfully'); |
109 | |
110 | ok(defined($Foo->{funk}), '... the &funk slot was created successfully'); |
111 | ok($foo_stash->has_symbol('&funk'), '... the meta agrees'); |
112 | |
113 | ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too'); |
114 | ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too'); |
115 | ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too'); |
116 | |
117 | ok(defined &{ $Foo->{funk} }, '... our &funk exists'); |
118 | |
119 | # can't bless things into hashrefs yet |
120 | # is($Foo->bless({})->funk(), 'Foo::funk', '... got the right value from the function'); |
121 | |
122 | # ---------------------------------------------------------------------- |
123 | ## test multiple slots in the glob |
124 | |
125 | my $ARRAY = [ 1, 2, 3 ]; |
126 | my $CODE = sub { "Foo::foo" }; |
127 | |
128 | is(exception { |
129 | $foo_stash->add_symbol('@foo' => $ARRAY); |
130 | }, undef, '... created @Foo::foo successfully'); |
131 | |
132 | ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully'); |
133 | is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
134 | |
135 | is(exception { |
136 | $foo_stash->add_symbol('&foo' => $CODE); |
137 | }, undef, '... created &Foo::foo successfully'); |
138 | |
139 | ok($foo_stash->has_symbol('&foo'), '... the meta agrees'); |
140 | is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
141 | |
142 | is(exception { |
143 | $foo_stash->add_symbol('$foo' => 'Foo::foo'); |
144 | }, undef, '... created $Foo::foo successfully'); |
145 | |
146 | ok($foo_stash->has_symbol('$foo'), '... the meta agrees'); |
147 | my $SCALAR = $foo_stash->get_symbol('$foo'); |
148 | is($$SCALAR, 'Foo::foo', '... got the right scalar value back'); |
149 | |
150 | is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar'); |
151 | |
152 | is(exception { |
153 | $foo_stash->remove_symbol('%foo'); |
154 | }, undef, '... removed %Foo::foo successfully'); |
155 | |
156 | ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully'); |
157 | ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists'); |
158 | ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists'); |
159 | ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists'); |
160 | |
161 | is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
162 | is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
163 | is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo'); |
164 | |
165 | ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully'); |
166 | ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed'); |
167 | ok(defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has NOT been removed'); |
168 | ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed'); |
169 | |
170 | is(exception { |
171 | $foo_stash->remove_symbol('&foo'); |
172 | }, undef, '... removed &Foo::foo successfully'); |
173 | |
174 | ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists'); |
175 | |
176 | ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists'); |
177 | ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists'); |
178 | |
179 | is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
180 | is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo'); |
181 | |
182 | ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully'); |
183 | ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed'); |
184 | ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed'); |
185 | ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed'); |
186 | |
187 | is(exception { |
188 | $foo_stash->remove_symbol('$foo'); |
189 | }, undef, '... removed $Foo::foo successfully'); |
190 | |
191 | ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists'); |
192 | |
193 | ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists'); |
194 | |
195 | is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
196 | |
197 | ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully'); |
198 | ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed'); |
199 | ok(!defined(${ $Foo->{foo} }), '... the $foo slot has now been removed'); |
200 | ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed'); |
201 | |
202 | { |
203 | my $syms = $foo_stash->get_all_symbols; |
204 | is_deeply( |
205 | [ sort keys %{ $syms } ], |
206 | [ sort $foo_stash->list_all_symbols ], |
207 | '... the fetched symbols are the same as the listed ones' |
208 | ); |
209 | } |
210 | |
211 | { |
212 | local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP') |
213 | ? "can't inflate weird stash entries" |
214 | : undef; |
215 | |
216 | is( |
217 | exception { |
218 | my $syms = $foo_stash->get_all_symbols('CODE'); |
219 | |
220 | is_deeply( |
221 | [ sort keys %{ $syms } ], |
222 | [ sort $foo_stash->list_all_symbols('CODE') ], |
223 | '... the fetched symbols are the same as the listed ones' |
224 | ); |
225 | |
226 | foreach my $symbol (keys %{ $syms }) { |
227 | is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol'); |
228 | } |
229 | }, |
230 | undef |
231 | ); |
232 | } |
233 | |
234 | { |
235 | $foo_stash->add_symbol('%bare'); |
236 | ok(!$foo_stash->has_symbol('$bare'), |
237 | "add_symbol with single argument doesn't vivify scalar slot"); |
238 | } |
239 | |
240 | { |
241 | $foo_stash->add_symbol('%zork', {}); |
242 | |
243 | my $syms = $foo_stash->get_all_symbols('HASH'); |
244 | |
245 | is_deeply( |
246 | [ sort keys %{ $syms } ], |
247 | [ sort $foo_stash->list_all_symbols('HASH') ], |
248 | '... the fetched symbols are the same as the listed ones' |
249 | ); |
250 | |
251 | foreach my $symbol (keys %{ $syms }) { |
252 | is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol'); |
253 | } |
254 | |
255 | is_deeply( |
256 | $syms, |
257 | { zork => *{ $Foo->{zork} }{HASH} }, |
258 | "got the right ones", |
259 | ); |
260 | } |
261 | |
262 | # check some errors |
263 | |
264 | like(exception { |
265 | $foo_stash->add_symbol('@bar', {}) |
266 | }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value"); |
267 | |
268 | like(exception { |
269 | $foo_stash->add_symbol('bar', []) |
270 | }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value"); |
271 | |
272 | like(exception { |
273 | $foo_stash->add_symbol('$bar', sub { }) |
274 | }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value"); |
275 | |
276 | like(exception { |
277 | $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO}) |
278 | }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value"); |
279 | |
280 | is_deeply([Package::Stash->new('Foo')->list_all_symbols], [], |
281 | "Foo:: isn't touched"); |
282 | |
283 | my $Quux = {}; |
284 | $Quux->{foo} = *{ Symbol::gensym() }; |
285 | *{ $Quux->{foo} } = \23; |
286 | *{ $Quux->{foo} } = ["bar"]; |
287 | *{ $Quux->{foo} } = { baz => 1 }; |
288 | *{ $Quux->{foo} } = sub { }; |
289 | *{ $Quux->{foo} } = *{ Symbol::geniosym() }{IO}; |
290 | |
291 | { |
292 | my $stash = Package::Stash->new($Quux); |
293 | |
294 | my %expect = ( |
295 | '$foo' => \23, |
296 | '@foo' => ["bar"], |
297 | '%foo' => { baz => 1 }, |
298 | '&foo' => \&{ $Quux->{foo} }, |
299 | 'foo' => *{ $Quux->{foo} }{IO}, |
300 | ); |
301 | |
302 | for my $sym ( sort keys %expect ) { |
303 | is_deeply( |
304 | $stash->get_symbol($sym), |
305 | $expect{$sym}, |
306 | "got expected value for $sym" |
307 | ); |
308 | } |
309 | |
310 | $stash->add_symbol('%bar' => {x => 42}); |
311 | |
312 | $expect{'%bar'} = {x => 42}; |
313 | |
314 | for my $sym ( sort keys %expect ) { |
315 | is_deeply( |
316 | $stash->get_symbol($sym), |
317 | $expect{$sym}, |
318 | "got expected value for $sym" |
319 | ); |
320 | } |
321 | |
322 | $stash->add_symbol('%bar' => {x => 43}); |
323 | |
324 | $expect{'%bar'} = {x => 43}; |
325 | |
326 | for my $sym ( sort keys %expect ) { |
327 | is_deeply( |
328 | $stash->get_symbol($sym), |
329 | $expect{$sym}, |
330 | "got expected value for $sym" |
331 | ); |
332 | } |
333 | } |
334 | |
335 | is_deeply([Package::Stash->new('Quux')->list_all_symbols], [], |
336 | "Quux:: isn't touched"); |
337 | |
338 | my $Quuux = {}; |
339 | |
340 | $Quuux->{foo} = *{ Symbol::gensym() }; |
341 | *{ $Quuux->{foo} } = \(my $scalar); |
342 | *{ $Quuux->{foo} } = []; |
343 | |
344 | $Quuux->{bar} = *{ Symbol::gensym() }; |
345 | *{ $Quuux->{bar} } = []; |
346 | |
347 | $Quuux->{baz} = *{ Symbol::gensym() }; |
348 | *{ $Quuux->{baz} } = {}; |
349 | *{ $Quuux->{baz} } = sub { }; |
350 | |
351 | $Quuux->{quux} = \1; |
352 | |
353 | $Quuux->{quuux} = \[]; |
354 | |
355 | $Quuux->{quuuux} = -1; |
356 | |
357 | { |
358 | my $quuux = Package::Stash->new($Quuux); |
359 | is_deeply( |
360 | [sort $quuux->list_all_symbols], |
361 | [qw(bar baz foo quuuux quuux quux)], |
362 | "list_all_symbols", |
363 | ); |
364 | { local $TODO = $] < 5.010 |
365 | ? "undef scalars aren't visible on 5.8" |
366 | : undef; |
367 | is_deeply( |
368 | [sort $quuux->list_all_symbols('SCALAR')], |
369 | [qw(foo)], |
370 | "list_all_symbols SCALAR", |
371 | ); |
372 | } |
373 | is_deeply( |
374 | [sort $quuux->list_all_symbols('ARRAY')], |
375 | [qw(bar foo)], |
376 | "list_all_symbols ARRAY", |
377 | ); |
378 | is_deeply( |
379 | [sort $quuux->list_all_symbols('HASH')], |
380 | [qw(baz)], |
381 | "list_all_symbols HASH", |
382 | ); |
383 | is_deeply( |
384 | [sort $quuux->list_all_symbols('CODE')], |
385 | [qw(baz quuuux quuux quux)], |
386 | "list_all_symbols CODE", |
387 | ); |
388 | } |
389 | |
390 | is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [], |
391 | "Quuux:: isn't touched"); |
392 | |
393 | done_testing; |