11 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
12 if Package::Stash::BROKEN_GLOB_ASSIGNMENT;
15 $Foo->{SOME_CONSTANT} = \1;
17 # ----------------------------------------------------------------------
18 ## tests adding a HASH
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');
26 $foo_stash->add_symbol('%foo' => { one => 1 });
27 }, undef, '... created %Foo::foo successfully');
29 # ... scalar should NOT be created here
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');
35 ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
36 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
40 ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
41 is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
43 my $foo = $foo_stash->get_symbol('%foo');
44 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
46 # ... make sure changes propogate up
50 is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
52 ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
53 is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
55 # ----------------------------------------------------------------------
56 ## test adding an ARRAY
58 ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
61 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
62 }, undef, '... created @Foo::bar successfully');
64 ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
65 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
67 # ... why does this not work ...
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');
73 # check the value itself
75 is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
76 is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
78 # ----------------------------------------------------------------------
79 ## test adding a SCALAR
81 ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
84 $foo_stash->add_symbol('$baz' => 10);
85 }, undef, '... created $Foo::baz successfully');
87 ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
88 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
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');
94 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
98 is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
99 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
101 # ----------------------------------------------------------------------
102 ## test adding a CODE
104 ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
107 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
108 }, undef, '... created &Foo::funk successfully');
110 ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
111 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
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');
117 ok(defined &{ $Foo->{funk} }, '... our &funk exists');
119 # can't bless things into hashrefs yet
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 local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP')
213 ? "can't inflate weird stash entries"
218 my $syms = $foo_stash->get_all_symbols('CODE');
221 [ sort keys %{ $syms } ],
222 [ sort $foo_stash->list_all_symbols('CODE') ],
223 '... the fetched symbols are the same as the listed ones'
226 foreach my $symbol (keys %{ $syms }) {
227 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
235 $foo_stash->add_symbol('%bare');
236 ok(!$foo_stash->has_symbol('$bare'),
237 "add_symbol with single argument doesn't vivify scalar slot");
241 $foo_stash->add_symbol('%zork', {});
243 my $syms = $foo_stash->get_all_symbols('HASH');
246 [ sort keys %{ $syms } ],
247 [ sort $foo_stash->list_all_symbols('HASH') ],
248 '... the fetched symbols are the same as the listed ones'
251 foreach my $symbol (keys %{ $syms }) {
252 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
257 { zork => *{ $Foo->{zork} }{HASH} },
258 "got the right ones",
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");
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");
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");
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");
280 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
281 "Foo:: isn't touched");
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};
292 my $stash = Package::Stash->new($Quux);
297 '%foo' => { baz => 1 },
298 '&foo' => \&{ $Quux->{foo} },
299 'foo' => *{ $Quux->{foo} }{IO},
302 for my $sym ( sort keys %expect ) {
304 $stash->get_symbol($sym),
306 "got expected value for $sym"
310 $stash->add_symbol('%bar' => {x => 42});
312 $expect{'%bar'} = {x => 42};
314 for my $sym ( sort keys %expect ) {
316 $stash->get_symbol($sym),
318 "got expected value for $sym"
322 $stash->add_symbol('%bar' => {x => 43});
324 $expect{'%bar'} = {x => 43};
326 for my $sym ( sort keys %expect ) {
328 $stash->get_symbol($sym),
330 "got expected value for $sym"
335 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
336 "Quux:: isn't touched");
340 $Quuux->{foo} = *{ Symbol::gensym() };
341 *{ $Quuux->{foo} } = \(my $scalar);
342 *{ $Quuux->{foo} } = [];
344 $Quuux->{bar} = *{ Symbol::gensym() };
345 *{ $Quuux->{bar} } = [];
347 $Quuux->{baz} = *{ Symbol::gensym() };
348 *{ $Quuux->{baz} } = {};
349 *{ $Quuux->{baz} } = sub { };
353 $Quuux->{quuux} = \[];
355 $Quuux->{quuuux} = -1;
358 my $quuux = Package::Stash->new($Quuux);
360 [sort $quuux->list_all_symbols],
361 [qw(bar baz foo quuuux quuux quux)],
364 { local $TODO = $] < 5.010
365 ? "undef scalars aren't visible on 5.8"
368 [sort $quuux->list_all_symbols('SCALAR')],
370 "list_all_symbols SCALAR",
374 [sort $quuux->list_all_symbols('ARRAY')],
376 "list_all_symbols ARRAY",
379 [sort $quuux->list_all_symbols('HASH')],
381 "list_all_symbols HASH",
384 [sort $quuux->list_all_symbols('CODE')],
385 [qw(baz quuuux quuux quux)],
386 "list_all_symbols CODE",
390 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
391 "Quuux:: isn't touched");