9 ok(exception { Package::Stash->name }, q{... can't call name() as a class method});
14 use constant 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_package_symbol('%foo'), '... the object agrees');
23 ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
26 $foo_stash->add_package_symbol('%foo' => { one => 1 });
27 }, '... created %Foo::foo successfully');
29 # ... scalar should NOT be created here
31 ok(!$foo_stash->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
32 ok(!$foo_stash->has_package_symbol('@foo'), '... ARRAY shouldnt have been created too');
33 ok(!$foo_stash->has_package_symbol('&foo'), '... CODE shouldnt have been created too');
35 ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
36 ok($foo_stash->has_package_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');
46 my $foo = $foo_stash->get_package_symbol('%foo');
47 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
49 # ... make sure changes propogate up
55 is(\%{'Foo::foo'}, $foo_stash->get_package_symbol('%foo'), '... our %foo is the same as the metas');
57 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
58 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
61 # ----------------------------------------------------------------------
62 ## test adding an ARRAY
64 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
67 $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]);
68 }, '... created @Foo::bar successfully');
70 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
71 ok($foo_stash->has_package_symbol('@bar'), '... the meta agrees');
73 # ... why does this not work ...
75 ok(!$foo_stash->has_package_symbol('$bar'), '... SCALAR shouldnt have been created too');
76 ok(!$foo_stash->has_package_symbol('%bar'), '... HASH shouldnt have been created too');
77 ok(!$foo_stash->has_package_symbol('&bar'), '... CODE shouldnt have been created too');
79 # check the value itself
83 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
84 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
87 # ----------------------------------------------------------------------
88 ## test adding a SCALAR
90 ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
93 $foo_stash->add_package_symbol('$baz' => 10);
94 }, '... created $Foo::baz successfully');
96 ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
97 ok($foo_stash->has_package_symbol('$baz'), '... the meta agrees');
99 ok(!$foo_stash->has_package_symbol('@baz'), '... ARRAY shouldnt have been created too');
100 ok(!$foo_stash->has_package_symbol('%baz'), '... HASH shouldnt have been created too');
101 ok(!$foo_stash->has_package_symbol('&baz'), '... CODE shouldnt have been created too');
103 is(${$foo_stash->get_package_symbol('$baz')}, 10, '... got the right value back');
109 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
110 is(${$foo_stash->get_package_symbol('$baz')}, 1, '... the meta agrees');
113 # ----------------------------------------------------------------------
114 ## test adding a CODE
116 ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
119 $foo_stash->add_package_symbol('&funk' => sub { "Foo::funk" });
120 }, '... created &Foo::funk successfully');
122 ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
123 ok($foo_stash->has_package_symbol('&funk'), '... the meta agrees');
125 ok(!$foo_stash->has_package_symbol('$funk'), '... SCALAR shouldnt have been created too');
126 ok(!$foo_stash->has_package_symbol('@funk'), '... ARRAY shouldnt have been created too');
127 ok(!$foo_stash->has_package_symbol('%funk'), '... HASH shouldnt have been created too');
131 ok(defined &{'Foo::funk'}, '... our &funk exists');
134 is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
136 # ----------------------------------------------------------------------
137 ## test multiple slots in the glob
139 my $ARRAY = [ 1, 2, 3 ];
140 my $CODE = sub { "Foo::foo" };
143 $foo_stash->add_package_symbol('@foo' => $ARRAY);
144 }, '... created @Foo::foo successfully');
146 ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot was added successfully');
147 is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
150 $foo_stash->add_package_symbol('&foo' => $CODE);
151 }, '... created &Foo::foo successfully');
153 ok($foo_stash->has_package_symbol('&foo'), '... the meta agrees');
154 is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
157 $foo_stash->add_package_symbol('$foo' => 'Foo::foo');
158 }, '... created $Foo::foo successfully');
160 ok($foo_stash->has_package_symbol('$foo'), '... the meta agrees');
161 my $SCALAR = $foo_stash->get_package_symbol('$foo');
162 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
166 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
170 $foo_stash->remove_package_symbol('%foo');
171 }, '... removed %Foo::foo successfully');
173 ok(!$foo_stash->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
174 ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
175 ok($foo_stash->has_package_symbol('&foo'), '... the &foo slot still exists');
176 ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists');
178 is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
179 is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
180 is($foo_stash->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
184 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
185 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
186 ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed');
187 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
191 $foo_stash->remove_package_symbol('&foo');
192 }, '... removed &Foo::foo successfully');
194 ok(!$foo_stash->has_package_symbol('&foo'), '... the &foo slot no longer exists');
196 ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
197 ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists');
199 is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
200 is($foo_stash->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
204 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
205 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
206 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
207 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
211 $foo_stash->remove_package_symbol('$foo');
212 }, '... removed $Foo::foo successfully');
214 ok(!$foo_stash->has_package_symbol('$foo'), '... the $foo slot no longer exists');
216 ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
218 is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
222 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
223 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
224 ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed');
225 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
231 $foo_stash->add_package_symbol('@bar', {})
232 }, "can't initialize a slot with the wrong type of value");
235 $foo_stash->add_package_symbol('bar', [])
236 }, "can't initialize a slot with the wrong type of value");
239 $foo_stash->add_package_symbol('$bar', sub { })
240 }, "can't initialize a slot with the wrong type of value");
248 $foo_stash->add_package_symbol('$bar', *Bar::foo{IO})
249 }, "can't initialize a slot with the wrong type of value");
251 # check compile time manipulation
258 our %foo = (baz => 1);
261 BEGIN { Package::Stash->new(__PACKAGE__)->remove_package_symbol('&foo') }
265 my $stash = Package::Stash->new('Baz');
266 is(${ $stash->get_package_symbol('$foo') }, 23, "got \$foo");
267 is_deeply($stash->get_package_symbol('@foo'), ['bar'], "got \@foo");
268 is_deeply($stash->get_package_symbol('%foo'), {baz => 1}, "got \%foo");
269 ok(!$stash->has_package_symbol('&foo'), "got \&foo");
270 is($stash->get_package_symbol('foo'), *Baz::foo{IO}, "got foo");
278 our %foo = (baz => 1);
284 my $stash = Package::Stash->new('Quux');
289 '%foo' => { baz => 1 },
290 '&foo' => \&Quux::foo,
291 'foo' => *Quux::foo{IO},
294 for my $sym ( sort keys %expect ) {
296 $stash->get_package_symbol($sym),
298 "got expected value for $sym"
302 $stash->add_package_symbol('%bar' => {x => 42});
304 $expect{'%bar'} = {x => 42};
306 for my $sym ( sort keys %expect ) {
308 $stash->get_package_symbol($sym),
310 "got expected value for $sym"
314 $stash->add_package_symbol('%bar' => {x => 43});
316 $expect{'%bar'} = {x => 43};
318 for my $sym ( sort keys %expect ) {
320 $stash->get_package_symbol($sym),
322 "got expected value for $sym"