11 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
13 && $Package::Stash::IMPLEMENTATION eq 'PP';
15 plan skip_all => "This isn't really going to work yet, probably";
18 $Foo->{SOME_CONSTANT} = \1;
20 # ----------------------------------------------------------------------
21 ## tests adding a HASH
23 my $foo_stash = Package::Stash->new($Foo);
24 ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet');
25 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
26 ok(!defined($Foo->{foo}), '... checking doesn\'t vivify');
29 $foo_stash->add_symbol('%foo' => { one => 1 });
30 }, undef, '... created %Foo::foo successfully');
32 # ... scalar should NOT be created here
34 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
35 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
36 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
38 ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
39 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
43 ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
44 is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
46 my $foo = $foo_stash->get_symbol('%foo');
47 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
49 # ... make sure changes propogate up
53 is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
55 ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
56 is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
58 # ----------------------------------------------------------------------
59 ## test adding an ARRAY
61 ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
64 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
65 }, undef, '... created @Foo::bar successfully');
67 ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
68 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
70 # ... why does this not work ...
72 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
73 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
74 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
76 # check the value itself
78 is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
79 is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
81 # ----------------------------------------------------------------------
82 ## test adding a SCALAR
84 ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
87 $foo_stash->add_symbol('$baz' => 10);
88 }, undef, '... created $Foo::baz successfully');
90 ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
91 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
93 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
94 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
95 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
97 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
101 is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
102 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
104 # ----------------------------------------------------------------------
105 ## test adding a CODE
107 ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
110 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
111 }, undef, '... created &Foo::funk successfully');
113 ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
114 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
116 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
117 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
118 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
120 ok(defined &{ $Foo->{funk} }, '... our &funk exists');
122 # can't bless things into hashrefs yet
123 # is($Foo->bless({})->funk(), 'Foo::funk', '... got the right value from the function');
125 # ----------------------------------------------------------------------
126 ## test multiple slots in the glob
128 my $ARRAY = [ 1, 2, 3 ];
129 my $CODE = sub { "Foo::foo" };
132 $foo_stash->add_symbol('@foo' => $ARRAY);
133 }, undef, '... created @Foo::foo successfully');
135 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
136 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
139 $foo_stash->add_symbol('&foo' => $CODE);
140 }, undef, '... created &Foo::foo successfully');
142 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
143 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
146 $foo_stash->add_symbol('$foo' => 'Foo::foo');
147 }, undef, '... created $Foo::foo successfully');
149 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
150 my $SCALAR = $foo_stash->get_symbol('$foo');
151 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
153 is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar');
156 $foo_stash->remove_symbol('%foo');
157 }, undef, '... removed %Foo::foo successfully');
159 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
160 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
161 ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
162 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
164 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
165 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
166 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
168 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
169 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
170 ok(defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has NOT been removed');
171 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
174 $foo_stash->remove_symbol('&foo');
175 }, undef, '... removed &Foo::foo successfully');
177 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
179 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
180 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
182 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
183 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
185 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
186 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
187 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
188 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
191 $foo_stash->remove_symbol('$foo');
192 }, undef, '... removed $Foo::foo successfully');
194 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
196 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
198 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
200 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
201 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
202 ok(!defined(${ $Foo->{foo} }), '... the $foo slot has now been removed');
203 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
206 my $syms = $foo_stash->get_all_symbols;
208 [ sort keys %{ $syms } ],
209 [ sort $foo_stash->list_all_symbols ],
210 '... the fetched symbols are the same as the listed ones'
215 local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP')
216 ? "can't inflate weird stash entries"
221 my $syms = $foo_stash->get_all_symbols('CODE');
224 [ sort keys %{ $syms } ],
225 [ sort $foo_stash->list_all_symbols('CODE') ],
226 '... the fetched symbols are the same as the listed ones'
229 foreach my $symbol (keys %{ $syms }) {
230 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
238 $foo_stash->add_symbol('%bare');
239 ok(!$foo_stash->has_symbol('$bare'),
240 "add_symbol with single argument doesn't vivify scalar slot");
244 $foo_stash->add_symbol('%zork', {});
246 my $syms = $foo_stash->get_all_symbols('HASH');
249 [ sort keys %{ $syms } ],
250 [ sort $foo_stash->list_all_symbols('HASH') ],
251 '... the fetched symbols are the same as the listed ones'
254 foreach my $symbol (keys %{ $syms }) {
255 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
260 { zork => *{ $Foo->{zork} }{HASH} },
261 "got the right ones",
268 $foo_stash->add_symbol('@bar', {})
269 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
272 $foo_stash->add_symbol('bar', [])
273 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
276 $foo_stash->add_symbol('$bar', sub { })
277 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
280 $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO})
281 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
283 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
284 "Foo:: isn't touched");
287 $Quux->{foo} = *{ Symbol::gensym() };
288 *{ $Quux->{foo} } = \23;
289 *{ $Quux->{foo} } = ["bar"];
290 *{ $Quux->{foo} } = { baz => 1 };
291 *{ $Quux->{foo} } = sub { };
292 *{ $Quux->{foo} } = *{ Symbol::geniosym() }{IO};
295 my $stash = Package::Stash->new($Quux);
300 '%foo' => { baz => 1 },
301 '&foo' => \&{ $Quux->{foo} },
302 'foo' => *{ $Quux->{foo} }{IO},
305 for my $sym ( sort keys %expect ) {
307 $stash->get_symbol($sym),
309 "got expected value for $sym"
313 $stash->add_symbol('%bar' => {x => 42});
315 $expect{'%bar'} = {x => 42};
317 for my $sym ( sort keys %expect ) {
319 $stash->get_symbol($sym),
321 "got expected value for $sym"
325 $stash->add_symbol('%bar' => {x => 43});
327 $expect{'%bar'} = {x => 43};
329 for my $sym ( sort keys %expect ) {
331 $stash->get_symbol($sym),
333 "got expected value for $sym"
338 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
339 "Quux:: isn't touched");
343 $Quuux->{foo} = *{ Symbol::gensym() };
344 *{ $Quuux->{foo} } = \(my $scalar);
345 *{ $Quuux->{foo} } = [];
347 $Quuux->{bar} = *{ Symbol::gensym() };
348 *{ $Quuux->{bar} } = [];
350 $Quuux->{baz} = *{ Symbol::gensym() };
351 *{ $Quuux->{baz} } = {};
352 *{ $Quuux->{baz} } = sub { };
356 $Quuux->{quuux} = \[];
358 $Quuux->{quuuux} = -1;
361 my $quuux = Package::Stash->new($Quuux);
363 [sort $quuux->list_all_symbols],
364 [qw(bar baz foo quuuux quuux quux)],
367 { local $TODO = $] < 5.010
368 ? "undef scalars aren't visible on 5.8"
371 [sort $quuux->list_all_symbols('SCALAR')],
373 "list_all_symbols SCALAR",
377 [sort $quuux->list_all_symbols('ARRAY')],
379 "list_all_symbols ARRAY",
382 [sort $quuux->list_all_symbols('HASH')],
384 "list_all_symbols HASH",
387 [sort $quuux->list_all_symbols('CODE')],
388 [qw(baz quuuux quuux quux)],
389 "list_all_symbols CODE",
393 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
394 "Quuux:: isn't touched");