8 use Test::Requires 'Package::Anon';
12 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
14 && $Package::Stash::IMPLEMENTATION eq 'PP';
16 my $Foo = Package::Anon->new('Foo');
17 $Foo->{SOME_CONSTANT} = \1;
19 # ----------------------------------------------------------------------
20 ## tests adding a HASH
22 my $foo_stash = Package::Stash->new($Foo);
23 ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet');
24 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
25 ok(!defined($Foo->{foo}), '... checking doesn\'t vivify');
28 $foo_stash->add_symbol('%foo' => { one => 1 });
29 }, undef, '... created %Foo::foo successfully');
31 # ... scalar should NOT be created here
33 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
34 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
35 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
37 ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
38 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
42 ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
43 is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
45 my $foo = $foo_stash->get_symbol('%foo');
46 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
48 # ... make sure changes propogate up
52 is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
54 ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
55 is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
57 # ----------------------------------------------------------------------
58 ## test adding an ARRAY
60 ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
63 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
64 }, undef, '... created @Foo::bar successfully');
66 ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
67 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
69 # ... why does this not work ...
71 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
72 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
73 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
75 # check the value itself
77 is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
78 is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
80 # ----------------------------------------------------------------------
81 ## test adding a SCALAR
83 ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
86 $foo_stash->add_symbol('$baz' => 10);
87 }, undef, '... created $Foo::baz successfully');
89 ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
90 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
92 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
93 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
94 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
96 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
100 is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
101 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
103 # ----------------------------------------------------------------------
104 ## test adding a CODE
106 ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
109 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
110 }, undef, '... created &Foo::funk successfully');
112 ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
113 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
115 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
116 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
117 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
119 ok(defined &{ $Foo->{funk} }, '... our &funk exists');
121 is($Foo->bless({})->funk(), 'Foo::funk', '... got the right value from the function');
123 # ----------------------------------------------------------------------
124 ## test multiple slots in the glob
126 my $ARRAY = [ 1, 2, 3 ];
127 my $CODE = sub { "Foo::foo" };
130 $foo_stash->add_symbol('@foo' => $ARRAY);
131 }, undef, '... created @Foo::foo successfully');
133 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
134 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
137 $foo_stash->add_symbol('&foo' => $CODE);
138 }, undef, '... created &Foo::foo successfully');
140 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
141 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
144 $foo_stash->add_symbol('$foo' => 'Foo::foo');
145 }, undef, '... created $Foo::foo successfully');
147 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
148 my $SCALAR = $foo_stash->get_symbol('$foo');
149 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
151 is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar');
154 $foo_stash->remove_symbol('%foo');
155 }, undef, '... removed %Foo::foo successfully');
157 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
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 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
162 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
163 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
164 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
166 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
167 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
168 ok(defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has NOT been removed');
169 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
172 $foo_stash->remove_symbol('&foo');
173 }, undef, '... removed &Foo::foo successfully');
175 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
177 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
178 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
180 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
181 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
183 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
184 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
185 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
186 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
189 $foo_stash->remove_symbol('$foo');
190 }, undef, '... removed $Foo::foo successfully');
192 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
194 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
196 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
198 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
199 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
200 ok(!defined(${ $Foo->{foo} }), '... the $foo slot has now been removed');
201 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
204 my $syms = $foo_stash->get_all_symbols;
206 [ sort keys %{ $syms } ],
207 [ sort $foo_stash->list_all_symbols ],
208 '... the fetched symbols are the same as the listed ones'
213 my $syms = $foo_stash->get_all_symbols('CODE');
216 [ sort keys %{ $syms } ],
217 [ sort $foo_stash->list_all_symbols('CODE') ],
218 '... the fetched symbols are the same as the listed ones'
221 foreach my $symbol (keys %{ $syms }) {
222 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
227 $foo_stash->add_symbol('%bare');
228 ok(!$foo_stash->has_symbol('$bare'),
229 "add_symbol with single argument doesn't vivify scalar slot");
233 $foo_stash->add_symbol('%zork', {});
235 my $syms = $foo_stash->get_all_symbols('HASH');
238 [ sort keys %{ $syms } ],
239 [ sort $foo_stash->list_all_symbols('HASH') ],
240 '... the fetched symbols are the same as the listed ones'
243 foreach my $symbol (keys %{ $syms }) {
244 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
249 { zork => *{ $Foo->{zork} }{HASH} },
250 "got the right ones",
257 $foo_stash->add_symbol('@bar', {})
258 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
261 $foo_stash->add_symbol('bar', [])
262 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
265 $foo_stash->add_symbol('$bar', sub { })
266 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
269 $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO})
270 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
272 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
273 "Foo:: isn't touched");
275 # *{ $Quux->{foo} } = \23 doesn't work on 5.12 and lower, apparently
276 my $Quux = Package::Anon->new('Quux');
278 my $gv = Symbol::gensym;
283 *$gv = *{ Symbol::geniosym() }{IO};
288 my $stash = Package::Stash->new($Quux);
293 '%foo' => { baz => 1 },
294 '&foo' => \&{ $Quux->{foo} },
295 'foo' => *{ $Quux->{foo} }{IO},
298 for my $sym ( sort keys %expect ) {
300 $stash->get_symbol($sym),
302 "got expected value for $sym"
306 $stash->add_symbol('%bar' => {x => 42});
308 $expect{'%bar'} = {x => 42};
310 for my $sym ( sort keys %expect ) {
312 $stash->get_symbol($sym),
314 "got expected value for $sym"
318 $stash->add_symbol('%bar' => {x => 43});
320 $expect{'%bar'} = {x => 43};
322 for my $sym ( sort keys %expect ) {
324 $stash->get_symbol($sym),
326 "got expected value for $sym"
331 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
332 "Quux:: isn't touched");
334 my $Quuux = Package::Anon->new('Quuux');
337 my $gv = Symbol::gensym;
338 *$gv = \(my $scalar);
340 $Quuux->{foo} = *$gv;
344 my $gv = Symbol::gensym;
346 $Quuux->{bar} = *$gv;
350 my $gv = Symbol::gensym;
353 $Quuux->{baz} = *$gv;
358 $Quuux->{quuux} = \[];
360 $Quuux->{quuuux} = -1;
363 my $quuux = Package::Stash->new($Quuux);
365 # Package::Anon adds a couple methods
366 [grep { $_ ne 'isa' && $_ ne 'can' } sort $quuux->list_all_symbols],
367 [qw(bar baz foo quuuux quuux quux)],
370 { local $TODO = $] < 5.010
371 ? "undef scalars aren't visible on 5.8"
374 [sort $quuux->list_all_symbols('SCALAR')],
376 "list_all_symbols SCALAR",
380 [sort $quuux->list_all_symbols('ARRAY')],
382 "list_all_symbols ARRAY",
385 [sort $quuux->list_all_symbols('HASH')],
387 "list_all_symbols HASH",
390 # Package::Anon adds a couple methods
391 [grep { $_ ne 'isa' && $_ ne 'can' } sort $quuux->list_all_symbols('CODE')],
392 [qw(baz quuuux quuux quux)],
393 "list_all_symbols CODE",
397 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
398 "Quuux:: isn't touched");