8 use Test::Requires 'Package::Anon';
12 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
13 if Package::Stash::BROKEN_GLOB_ASSIGNMENT;
15 my $Foo = Package::Anon->new('Foo');
16 $Foo->{SOME_CONSTANT} = \1;
18 # ----------------------------------------------------------------------
19 ## tests adding a HASH
21 my $foo_stash = Package::Stash->new($Foo);
22 ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet');
23 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
24 ok(!defined($Foo->{foo}), '... checking doesn\'t vivify');
27 $foo_stash->add_symbol('%foo' => { one => 1 });
28 }, undef, '... created %Foo::foo successfully');
30 # ... scalar should NOT be created here
32 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
33 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
34 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
36 ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
37 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
41 ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
42 is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
44 my $foo = $foo_stash->get_symbol('%foo');
45 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
47 # ... make sure changes propogate up
51 is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
53 ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
54 is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
56 # ----------------------------------------------------------------------
57 ## test adding an ARRAY
59 ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
62 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
63 }, undef, '... created @Foo::bar successfully');
65 ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
66 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
68 # ... why does this not work ...
70 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
71 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
72 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
74 # check the value itself
76 is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
77 is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
79 # ----------------------------------------------------------------------
80 ## test adding a SCALAR
82 ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
85 $foo_stash->add_symbol('$baz' => 10);
86 }, undef, '... created $Foo::baz successfully');
88 ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
89 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
91 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
92 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
93 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
95 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
99 is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
100 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
102 # ----------------------------------------------------------------------
103 ## test adding a CODE
105 ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
108 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
109 }, undef, '... created &Foo::funk successfully');
111 ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
112 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
114 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
115 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
116 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
118 ok(defined &{ $Foo->{funk} }, '... our &funk exists');
120 is($Foo->bless({})->funk(), 'Foo::funk', '... got the right value from the function');
122 # ----------------------------------------------------------------------
123 ## test multiple slots in the glob
125 my $ARRAY = [ 1, 2, 3 ];
126 my $CODE = sub { "Foo::foo" };
129 $foo_stash->add_symbol('@foo' => $ARRAY);
130 }, undef, '... created @Foo::foo successfully');
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');
136 $foo_stash->add_symbol('&foo' => $CODE);
137 }, undef, '... created &Foo::foo successfully');
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');
143 $foo_stash->add_symbol('$foo' => 'Foo::foo');
144 }, undef, '... created $Foo::foo successfully');
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');
150 is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar');
153 $foo_stash->remove_symbol('%foo');
154 }, undef, '... removed %Foo::foo successfully');
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');
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');
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');
171 $foo_stash->remove_symbol('&foo');
172 }, undef, '... removed &Foo::foo successfully');
174 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
176 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
177 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
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');
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');
188 $foo_stash->remove_symbol('$foo');
189 }, undef, '... removed $Foo::foo successfully');
191 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
193 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
195 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
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');
203 my $syms = $foo_stash->get_all_symbols;
205 [ sort keys %{ $syms } ],
206 [ sort $foo_stash->list_all_symbols ],
207 '... the fetched symbols are the same as the listed ones'
212 my $syms = $foo_stash->get_all_symbols('CODE');
215 [ sort keys %{ $syms } ],
216 [ sort $foo_stash->list_all_symbols('CODE') ],
217 '... the fetched symbols are the same as the listed ones'
220 foreach my $symbol (keys %{ $syms }) {
221 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
226 $foo_stash->add_symbol('%bare');
227 ok(!$foo_stash->has_symbol('$bare'),
228 "add_symbol with single argument doesn't vivify scalar slot");
232 $foo_stash->add_symbol('%zork', {});
234 my $syms = $foo_stash->get_all_symbols('HASH');
237 [ sort keys %{ $syms } ],
238 [ sort $foo_stash->list_all_symbols('HASH') ],
239 '... the fetched symbols are the same as the listed ones'
242 foreach my $symbol (keys %{ $syms }) {
243 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
248 { zork => *{ $Foo->{zork} }{HASH} },
249 "got the right ones",
256 $foo_stash->add_symbol('@bar', {})
257 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
260 $foo_stash->add_symbol('bar', [])
261 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
264 $foo_stash->add_symbol('$bar', sub { })
265 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
268 $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO})
269 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
271 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
272 "Foo:: isn't touched");
274 my $Quux = Package::Anon->new('Quux');
275 $Quux->{foo} = *{ Symbol::gensym() };
276 *{ $Quux->{foo} } = \23;
277 *{ $Quux->{foo} } = ["bar"];
278 *{ $Quux->{foo} } = { baz => 1 };
279 *{ $Quux->{foo} } = sub { };
280 *{ $Quux->{foo} } = *{ Symbol::geniosym() }{IO};
283 my $stash = Package::Stash->new($Quux);
288 '%foo' => { baz => 1 },
289 '&foo' => \&{ $Quux->{foo} },
290 'foo' => *{ $Quux->{foo} }{IO},
293 for my $sym ( sort keys %expect ) {
295 $stash->get_symbol($sym),
297 "got expected value for $sym"
301 $stash->add_symbol('%bar' => {x => 42});
303 $expect{'%bar'} = {x => 42};
305 for my $sym ( sort keys %expect ) {
307 $stash->get_symbol($sym),
309 "got expected value for $sym"
313 $stash->add_symbol('%bar' => {x => 43});
315 $expect{'%bar'} = {x => 43};
317 for my $sym ( sort keys %expect ) {
319 $stash->get_symbol($sym),
321 "got expected value for $sym"
326 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
327 "Quux:: isn't touched");
329 my $Quuux = Package::Anon->new('Quuux');
331 $Quuux->{foo} = *{ Symbol::gensym() };
332 *{ $Quuux->{foo} } = \(my $scalar);
333 *{ $Quuux->{foo} } = [];
335 $Quuux->{bar} = *{ Symbol::gensym() };
336 *{ $Quuux->{bar} } = [];
338 $Quuux->{baz} = *{ Symbol::gensym() };
339 *{ $Quuux->{baz} } = {};
340 *{ $Quuux->{baz} } = sub { };
344 $Quuux->{quuux} = \[];
346 $Quuux->{quuuux} = -1;
349 my $quuux = Package::Stash->new($Quuux);
351 # Package::Anon adds a couple methods
352 [grep { $_ ne 'isa' && $_ ne 'can' } sort $quuux->list_all_symbols],
353 [qw(bar baz foo quuuux quuux quux)],
356 { local $TODO = $] < 5.010
357 ? "undef scalars aren't visible on 5.8"
360 [sort $quuux->list_all_symbols('SCALAR')],
362 "list_all_symbols SCALAR",
366 [sort $quuux->list_all_symbols('ARRAY')],
368 "list_all_symbols ARRAY",
371 [sort $quuux->list_all_symbols('HASH')],
373 "list_all_symbols HASH",
376 # Package::Anon adds a couple methods
377 [grep { $_ ne 'isa' && $_ ne 'can' } sort $quuux->list_all_symbols('CODE')],
378 [qw(baz quuuux quuux quux)],
379 "list_all_symbols CODE",
383 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
384 "Quuux:: isn't touched");