11 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
13 && $Package::Stash::IMPLEMENTATION eq 'PP';
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 # can't bless things into hashrefs yet
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 local $TODO = "can't inflate weird stash entries";
217 my $syms = $foo_stash->get_all_symbols('CODE');
220 [ sort keys %{ $syms } ],
221 [ sort $foo_stash->list_all_symbols('CODE') ],
222 '... the fetched symbols are the same as the listed ones'
225 foreach my $symbol (keys %{ $syms }) {
226 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
234 $foo_stash->add_symbol('%bare');
235 ok(!$foo_stash->has_symbol('$bare'),
236 "add_symbol with single argument doesn't vivify scalar slot");
240 $foo_stash->add_symbol('%zork', {});
242 my $syms = $foo_stash->get_all_symbols('HASH');
245 [ sort keys %{ $syms } ],
246 [ sort $foo_stash->list_all_symbols('HASH') ],
247 '... the fetched symbols are the same as the listed ones'
250 foreach my $symbol (keys %{ $syms }) {
251 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
256 { zork => *{ $Foo->{zork} }{HASH} },
257 "got the right ones",
264 $foo_stash->add_symbol('@bar', {})
265 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
268 $foo_stash->add_symbol('bar', [])
269 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
272 $foo_stash->add_symbol('$bar', sub { })
273 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
276 $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO})
277 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
279 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
280 "Foo:: isn't touched");
283 $Quux->{foo} = *{ Symbol::gensym() };
284 *{ $Quux->{foo} } = \23;
285 *{ $Quux->{foo} } = ["bar"];
286 *{ $Quux->{foo} } = { baz => 1 };
287 *{ $Quux->{foo} } = sub { };
288 *{ $Quux->{foo} } = *{ Symbol::geniosym() }{IO};
291 my $stash = Package::Stash->new($Quux);
296 '%foo' => { baz => 1 },
297 '&foo' => \&{ $Quux->{foo} },
298 'foo' => *{ $Quux->{foo} }{IO},
301 for my $sym ( sort keys %expect ) {
303 $stash->get_symbol($sym),
305 "got expected value for $sym"
309 $stash->add_symbol('%bar' => {x => 42});
311 $expect{'%bar'} = {x => 42};
313 for my $sym ( sort keys %expect ) {
315 $stash->get_symbol($sym),
317 "got expected value for $sym"
321 $stash->add_symbol('%bar' => {x => 43});
323 $expect{'%bar'} = {x => 43};
325 for my $sym ( sort keys %expect ) {
327 $stash->get_symbol($sym),
329 "got expected value for $sym"
334 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
335 "Quux:: isn't touched");
339 $Quuux->{foo} = *{ Symbol::gensym() };
340 *{ $Quuux->{foo} } = \(my $scalar);
341 *{ $Quuux->{foo} } = [];
343 $Quuux->{bar} = *{ Symbol::gensym() };
344 *{ $Quuux->{bar} } = [];
346 $Quuux->{baz} = *{ Symbol::gensym() };
347 *{ $Quuux->{baz} } = {};
348 *{ $Quuux->{baz} } = sub { };
352 $Quuux->{quuux} = \[];
354 $Quuux->{quuuux} = -1;
357 my $quuux = Package::Stash->new($Quuux);
359 [sort $quuux->list_all_symbols],
360 [qw(bar baz foo quuuux quuux quux)],
363 { local $TODO = $] < 5.010
364 ? "undef scalars aren't visible on 5.8"
367 [sort $quuux->list_all_symbols('SCALAR')],
369 "list_all_symbols SCALAR",
373 [sort $quuux->list_all_symbols('ARRAY')],
375 "list_all_symbols ARRAY",
378 [sort $quuux->list_all_symbols('HASH')],
380 "list_all_symbols HASH",
383 [sort $quuux->list_all_symbols('CODE')],
384 [qw(baz quuuux quuux quux)],
385 "list_all_symbols CODE",
389 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
390 "Quuux:: isn't touched");