7 BEGIN { $Package::Stash::IMPLEMENTATION = 'PP' }
11 ok(exists $INC{'Package/Stash/PP.pm'}, "loaded PP");
12 ok(!exists $INC{'Package/Stash/XS.pm'}, "didn't load XS");
14 like(exception { Package::Stash->name }, qr/Can't call name as a class method/,
15 q{... can't call name() as a class method});
20 use constant SOME_CONSTANT => 1;
23 # ----------------------------------------------------------------------
24 ## tests adding a HASH
26 my $foo_stash = Package::Stash->new('Foo');
27 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
28 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
29 ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
32 $foo_stash->add_symbol('%foo' => { one => 1 });
33 }, undef, '... created %Foo::foo successfully');
35 # ... scalar should NOT be created here
37 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
38 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
39 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
41 ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
42 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
48 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
49 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
52 my $foo = $foo_stash->get_symbol('%foo');
53 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
55 # ... make sure changes propogate up
61 is(\%{'Foo::foo'}, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
63 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
64 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
67 # ----------------------------------------------------------------------
68 ## test adding an ARRAY
70 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
73 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
74 }, undef, '... created @Foo::bar successfully');
76 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
77 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
79 # ... why does this not work ...
81 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
82 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
83 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
85 # check the value itself
89 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
90 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
93 # ----------------------------------------------------------------------
94 ## test adding a SCALAR
96 ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
99 $foo_stash->add_symbol('$baz' => 10);
100 }, undef, '... created $Foo::baz successfully');
102 ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
103 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
105 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
106 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
107 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
109 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
115 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
116 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
119 # ----------------------------------------------------------------------
120 ## test adding a CODE
122 ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
125 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
126 }, undef, '... created &Foo::funk successfully');
128 ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
129 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
131 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
132 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
133 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
137 ok(defined &{'Foo::funk'}, '... our &funk exists');
140 is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
142 # ----------------------------------------------------------------------
143 ## test multiple slots in the glob
145 my $ARRAY = [ 1, 2, 3 ];
146 my $CODE = sub { "Foo::foo" };
149 $foo_stash->add_symbol('@foo' => $ARRAY);
150 }, undef, '... created @Foo::foo successfully');
152 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
153 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
156 $foo_stash->add_symbol('&foo' => $CODE);
157 }, undef, '... created &Foo::foo successfully');
159 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
160 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
163 $foo_stash->add_symbol('$foo' => 'Foo::foo');
164 }, undef, '... created $Foo::foo successfully');
166 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
167 my $SCALAR = $foo_stash->get_symbol('$foo');
168 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
172 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
176 $foo_stash->remove_symbol('%foo');
177 }, undef, '... removed %Foo::foo successfully');
179 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
180 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
181 ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
182 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
184 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
185 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
186 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
190 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
191 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
192 ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed');
193 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
197 $foo_stash->remove_symbol('&foo');
198 }, undef, '... removed &Foo::foo successfully');
200 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
202 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
203 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
205 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
206 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
210 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
211 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
212 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
213 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
217 $foo_stash->remove_symbol('$foo');
218 }, undef, '... removed $Foo::foo successfully');
220 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
222 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
224 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
228 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
229 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
230 ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed');
231 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
235 my $syms = $foo_stash->get_all_symbols;
237 [ sort keys %{ $syms } ],
238 [ sort $foo_stash->list_all_symbols ],
239 '... the fetched symbols are the same as the listed ones'
244 my $syms = $foo_stash->get_all_symbols('CODE');
247 [ sort keys %{ $syms } ],
248 [ sort $foo_stash->list_all_symbols('CODE') ],
249 '... the fetched symbols are the same as the listed ones'
252 foreach my $symbol (keys %{ $syms }) {
253 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
258 $foo_stash->add_symbol('%bare');
259 ok(!$foo_stash->has_symbol('$bare'),
260 "add_symbol with single argument doesn't vivify scalar slot");
264 $foo_stash->add_symbol('%zork', {});
266 my $syms = $foo_stash->get_all_symbols('HASH');
269 [ sort keys %{ $syms } ],
270 [ sort $foo_stash->list_all_symbols('HASH') ],
271 '... the fetched symbols are the same as the listed ones'
274 foreach my $symbol (keys %{ $syms }) {
275 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
280 { zork => *{ $Foo::{zork} }{HASH} },
281 "got the right ones",
288 $foo_stash->add_symbol('@bar', {})
289 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
292 $foo_stash->add_symbol('bar', [])
293 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
296 $foo_stash->add_symbol('$bar', sub { })
297 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
305 $foo_stash->add_symbol('$bar', *Bar::foo{IO})
306 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
308 # check compile time manipulation
315 our %foo = (baz => 1);
318 BEGIN { Package::Stash->new(__PACKAGE__)->remove_symbol('&foo') }
322 my $stash = Package::Stash->new('Baz');
323 is(${ $stash->get_symbol('$foo') }, 23, "got \$foo");
324 is_deeply($stash->get_symbol('@foo'), ['bar'], "got \@foo");
325 is_deeply($stash->get_symbol('%foo'), {baz => 1}, "got \%foo");
326 ok(!$stash->has_symbol('&foo'), "got \&foo");
327 is($stash->get_symbol('foo'), *Baz::foo{IO}, "got foo");
335 our %foo = (baz => 1);
341 my $stash = Package::Stash->new('Quux');
346 '%foo' => { baz => 1 },
347 '&foo' => \&Quux::foo,
348 'foo' => *Quux::foo{IO},
351 for my $sym ( sort keys %expect ) {
353 $stash->get_symbol($sym),
355 "got expected value for $sym"
359 $stash->add_symbol('%bar' => {x => 42});
361 $expect{'%bar'} = {x => 42};
363 for my $sym ( sort keys %expect ) {
365 $stash->get_symbol($sym),
367 "got expected value for $sym"
371 $stash->add_symbol('%bar' => {x => 43});
373 $expect{'%bar'} = {x => 43};
375 for my $sym ( sort keys %expect ) {
377 $stash->get_symbol($sym),
379 "got expected value for $sym"
391 use constant quux => 1;
392 use constant quuux => [];
397 my $quuux = Package::Stash->new('Quuux');
399 [sort $quuux->list_all_symbols],
400 [qw(BEGIN bar baz foo quuuux quuux quux)],
403 { local $TODO = $] < 5.010
404 ? "undef scalars aren't visible on 5.8"
407 [sort $quuux->list_all_symbols('SCALAR')],
409 "list_all_symbols SCALAR",
413 [sort $quuux->list_all_symbols('ARRAY')],
415 "list_all_symbols ARRAY",
418 [sort $quuux->list_all_symbols('HASH')],
420 "list_all_symbols HASH",
423 [sort $quuux->list_all_symbols('CODE')],
424 [qw(baz quuuux quuux quux)],
425 "list_all_symbols CODE",
429 for my $package ('Foo:Bar', 'Foo/Bar', 'Foo Bar', 'Foo:::Bar', '') {
431 exception { Package::Stash->new($package) },
432 qr/^$package is not a module name/,
433 "$package is not a module name"
438 exception { Package::Stash->new([]) },
439 qr/^Package::Stash->new must be passed the name of the package to access/,
440 "module name must be a string"
444 exception { Package::Stash->new(undef) },
445 qr/^Package::Stash->new must be passed the name of the package to access/,
446 "module name must be a string"