6 use Test::Requires 'Package::Stash::XS';
8 BEGIN { $Package::Stash::IMPLEMENTATION = 'XS' }
12 ok(exists $INC{'Package/Stash/XS.pm'}, "loaded XS");
13 ok(!exists $INC{'Package/Stash/PP.pm'}, "didn't load PP");
15 like(exception { Package::Stash->name }, qr/Can't call name as a class method/,
16 q{... can't call name() as a class method});
21 use constant SOME_CONSTANT => 1;
24 # ----------------------------------------------------------------------
25 ## tests adding a HASH
27 my $foo_stash = Package::Stash->new('Foo');
28 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
29 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
30 ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
33 $foo_stash->add_symbol('%foo' => { one => 1 });
34 }, undef, '... created %Foo::foo successfully');
36 # ... scalar should NOT be created here
38 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
39 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
40 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
42 ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
43 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
49 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
50 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
53 my $foo = $foo_stash->get_symbol('%foo');
54 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
56 # ... make sure changes propogate up
62 is(\%{'Foo::foo'}, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
64 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
65 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
68 # ----------------------------------------------------------------------
69 ## test adding an ARRAY
71 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
74 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
75 }, undef, '... created @Foo::bar successfully');
77 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
78 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
80 # ... why does this not work ...
82 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
83 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
84 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
86 # check the value itself
90 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
91 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
94 # ----------------------------------------------------------------------
95 ## test adding a SCALAR
97 ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
100 $foo_stash->add_symbol('$baz' => 10);
101 }, undef, '... created $Foo::baz successfully');
103 ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
104 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
106 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
107 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
108 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
110 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
116 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
117 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
120 # ----------------------------------------------------------------------
121 ## test adding a CODE
123 ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
126 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
127 }, undef, '... created &Foo::funk successfully');
129 ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
130 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
132 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
133 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
134 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
138 ok(defined &{'Foo::funk'}, '... our &funk exists');
141 is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
143 # ----------------------------------------------------------------------
144 ## test multiple slots in the glob
146 my $ARRAY = [ 1, 2, 3 ];
147 my $CODE = sub { "Foo::foo" };
150 $foo_stash->add_symbol('@foo' => $ARRAY);
151 }, undef, '... created @Foo::foo successfully');
153 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
154 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
157 $foo_stash->add_symbol('&foo' => $CODE);
158 }, undef, '... created &Foo::foo successfully');
160 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
161 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
164 $foo_stash->add_symbol('$foo' => 'Foo::foo');
165 }, undef, '... created $Foo::foo successfully');
167 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
168 my $SCALAR = $foo_stash->get_symbol('$foo');
169 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
173 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
177 $foo_stash->remove_symbol('%foo');
178 }, undef, '... removed %Foo::foo successfully');
180 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
181 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
182 ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
183 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
185 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
186 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
187 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
191 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
192 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
193 ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed');
194 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
198 $foo_stash->remove_symbol('&foo');
199 }, undef, '... removed &Foo::foo successfully');
201 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
203 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
204 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
206 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
207 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
211 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
212 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
213 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
214 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
218 $foo_stash->remove_symbol('$foo');
219 }, undef, '... removed $Foo::foo successfully');
221 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
223 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
225 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
229 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
230 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
231 ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed');
232 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
236 my $syms = $foo_stash->get_all_symbols;
238 [ sort keys %{ $syms } ],
239 [ sort $foo_stash->list_all_symbols ],
240 '... the fetched symbols are the same as the listed ones'
245 my $syms = $foo_stash->get_all_symbols('CODE');
248 [ sort keys %{ $syms } ],
249 [ sort $foo_stash->list_all_symbols('CODE') ],
250 '... the fetched symbols are the same as the listed ones'
253 foreach my $symbol (keys %{ $syms }) {
254 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
259 $foo_stash->add_symbol('%zork');
260 ok(!$foo_stash->has_symbol('$zork'),
261 "add_symbol with single argument doesn't vivify scalar slot");
263 my $syms = $foo_stash->get_all_symbols('HASH');
266 [ sort keys %{ $syms } ],
267 [ sort $foo_stash->list_all_symbols('HASH') ],
268 '... the fetched symbols are the same as the listed ones'
271 foreach my $symbol (keys %{ $syms }) {
272 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
278 { zork => \%Foo::zork },
279 "got the right ones",
286 $foo_stash->add_symbol('@bar', {})
287 }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
290 $foo_stash->add_symbol('bar', [])
291 }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
294 $foo_stash->add_symbol('$bar', sub { })
295 }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
303 $foo_stash->add_symbol('$bar', *Bar::foo{IO})
304 }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
306 # check compile time manipulation
313 our %foo = (baz => 1);
316 BEGIN { Package::Stash->new(__PACKAGE__)->remove_symbol('&foo') }
320 my $stash = Package::Stash->new('Baz');
321 is(${ $stash->get_symbol('$foo') }, 23, "got \$foo");
322 is_deeply($stash->get_symbol('@foo'), ['bar'], "got \@foo");
323 is_deeply($stash->get_symbol('%foo'), {baz => 1}, "got \%foo");
324 ok(!$stash->has_symbol('&foo'), "got \&foo");
325 is($stash->get_symbol('foo'), *Baz::foo{IO}, "got foo");
333 our %foo = (baz => 1);
339 my $stash = Package::Stash->new('Quux');
344 '%foo' => { baz => 1 },
345 '&foo' => \&Quux::foo,
346 'foo' => *Quux::foo{IO},
349 for my $sym ( sort keys %expect ) {
351 $stash->get_symbol($sym),
353 "got expected value for $sym"
357 $stash->add_symbol('%bar' => {x => 42});
359 $expect{'%bar'} = {x => 42};
361 for my $sym ( sort keys %expect ) {
363 $stash->get_symbol($sym),
365 "got expected value for $sym"
369 $stash->add_symbol('%bar' => {x => 43});
371 $expect{'%bar'} = {x => 43};
373 for my $sym ( sort keys %expect ) {
375 $stash->get_symbol($sym),
377 "got expected value for $sym"
389 use constant quux => 1;
390 use constant quuux => [];
395 my $quuux = Package::Stash->new('Quuux');
397 [sort $quuux->list_all_symbols],
398 [qw(BEGIN bar baz foo quuuux quuux quux)],
401 { local $TODO = $] < 5.010
402 ? "undef scalars aren't visible on 5.8"
405 [sort $quuux->list_all_symbols('SCALAR')],
407 "list_all_symbols SCALAR",
411 [sort $quuux->list_all_symbols('ARRAY')],
413 "list_all_symbols ARRAY",
416 [sort $quuux->list_all_symbols('HASH')],
418 "list_all_symbols HASH",
421 [sort $quuux->list_all_symbols('CODE')],
422 [qw(baz quuuux quuux quux)],
423 "list_all_symbols CODE",
427 for my $package ('Foo:Bar', 'Foo/Bar', 'Foo Bar', 'Foo:::Bar', '') {
429 exception { Package::Stash->new($package) },
430 qr/^$package is not a module name/,
431 "$package is not a module name"
436 exception { Package::Stash->new([]) },
437 qr/^Package::Stash->new must be passed the name of the package to access/,
438 "module name must be a string"
442 exception { Package::Stash->new(undef) },
443 qr/^Package::Stash->new must be passed the name of the package to access/,
444 "module name must be a string"