9 like(exception { Package::Stash->name }, qr/Can't call name as a class method/,
10 q{... can't call name() as a class method});
15 use constant 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\' 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');
43 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
44 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
47 my $foo = $foo_stash->get_symbol('%foo');
48 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
50 # ... make sure changes propogate up
56 is(\%{'Foo::foo'}, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
58 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
59 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
62 # ----------------------------------------------------------------------
63 ## test adding an ARRAY
65 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
68 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
69 }, undef, '... created @Foo::bar successfully');
71 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
72 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
74 # ... why does this not work ...
76 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
77 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
78 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
80 # check the value itself
84 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
85 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
88 # ----------------------------------------------------------------------
89 ## test adding a SCALAR
91 ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
94 $foo_stash->add_symbol('$baz' => 10);
95 }, undef, '... created $Foo::baz successfully');
97 ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
98 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
100 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
101 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
102 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
104 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
110 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
111 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
114 # ----------------------------------------------------------------------
115 ## test adding a CODE
117 ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
120 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
121 }, undef, '... created &Foo::funk successfully');
123 ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
124 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
126 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
127 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
128 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
132 ok(defined &{'Foo::funk'}, '... our &funk exists');
135 is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
137 # ----------------------------------------------------------------------
138 ## test multiple slots in the glob
140 my $ARRAY = [ 1, 2, 3 ];
141 my $CODE = sub { "Foo::foo" };
144 $foo_stash->add_symbol('@foo' => $ARRAY);
145 }, undef, '... created @Foo::foo successfully');
147 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
148 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
151 $foo_stash->add_symbol('&foo' => $CODE);
152 }, undef, '... created &Foo::foo successfully');
154 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
155 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
158 $foo_stash->add_symbol('$foo' => 'Foo::foo');
159 }, undef, '... created $Foo::foo successfully');
161 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
162 my $SCALAR = $foo_stash->get_symbol('$foo');
163 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
167 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
171 $foo_stash->remove_symbol('%foo');
172 }, undef, '... removed %Foo::foo successfully');
174 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
175 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still 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'), $CODE, '... got the right value for &Foo::foo');
181 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"}{ARRAY}), '... the @foo slot has NOT been removed');
187 ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed');
188 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
192 $foo_stash->remove_symbol('&foo');
193 }, undef, '... removed &Foo::foo successfully');
195 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
197 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
198 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
200 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
201 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
205 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
206 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
207 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
208 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
212 $foo_stash->remove_symbol('$foo');
213 }, undef, '... removed $Foo::foo successfully');
215 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
217 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
219 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
223 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
224 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
225 ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed');
226 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
232 $foo_stash->add_symbol('@bar', {})
233 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
236 $foo_stash->add_symbol('bar', [])
237 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
240 $foo_stash->add_symbol('$bar', sub { })
241 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
249 $foo_stash->add_symbol('$bar', *Bar::foo{IO})
250 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
252 # check compile time manipulation
259 our %foo = (baz => 1);
262 BEGIN { Package::Stash->new(__PACKAGE__)->remove_symbol('&foo') }
266 my $stash = Package::Stash->new('Baz');
267 is(${ $stash->get_symbol('$foo') }, 23, "got \$foo");
268 is_deeply($stash->get_symbol('@foo'), ['bar'], "got \@foo");
269 is_deeply($stash->get_symbol('%foo'), {baz => 1}, "got \%foo");
270 ok(!$stash->has_symbol('&foo'), "got \&foo");
271 is($stash->get_symbol('foo'), *Baz::foo{IO}, "got foo");
279 our %foo = (baz => 1);
285 my $stash = Package::Stash->new('Quux');
290 '%foo' => { baz => 1 },
291 '&foo' => \&Quux::foo,
292 'foo' => *Quux::foo{IO},
295 for my $sym ( sort keys %expect ) {
297 $stash->get_symbol($sym),
299 "got expected value for $sym"
303 $stash->add_symbol('%bar' => {x => 42});
305 $expect{'%bar'} = {x => 42};
307 for my $sym ( sort keys %expect ) {
309 $stash->get_symbol($sym),
311 "got expected value for $sym"
315 $stash->add_symbol('%bar' => {x => 43});
317 $expect{'%bar'} = {x => 43};
319 for my $sym ( sort keys %expect ) {
321 $stash->get_symbol($sym),
323 "got expected value for $sym"
335 use constant quux => 1;
336 use constant quuux => [];
341 my $quuux = Package::Stash->new('Quuux');
343 [sort $quuux->list_all_symbols],
344 [qw(BEGIN bar baz foo quuuux quuux quux)],
347 { local $TODO = $] < 5.010 ? "undef scalars aren't visible on 5.8" : undef;
349 [sort $quuux->list_all_symbols('SCALAR')],
351 "list_all_symbols SCALAR",
355 [sort $quuux->list_all_symbols('ARRAY')],
357 "list_all_symbols ARRAY",
360 [sort $quuux->list_all_symbols('HASH')],
362 "list_all_symbols HASH",
365 [sort $quuux->list_all_symbols('CODE')],
366 [qw(baz quuuux quuux quux)],
367 "list_all_symbols CODE",