don't rely on the ::PP BROKEN_ constants existing in tests
[gitmo/Package-Stash.git] / t / bare-anon-basic.t
CommitLineData
c6ddb1d1 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use lib 't/lib';
5use Test::More;
6use Test::Fatal;
7
8use Package::Stash;
9use Symbol;
10
11plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
acbc69c4 12 if $] < 5.014
ace6563d 13 && $Package::Stash::IMPLEMENTATION eq 'PP';
c6ddb1d1 14
15my $Foo = {};
16$Foo->{SOME_CONSTANT} = \1;
17
18# ----------------------------------------------------------------------
19## tests adding a HASH
20
21my $foo_stash = Package::Stash->new($Foo);
22ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet');
23ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
24ok(!defined($Foo->{foo}), '... checking doesn\'t vivify');
25
26is(exception {
27 $foo_stash->add_symbol('%foo' => { one => 1 });
28}, undef, '... created %Foo::foo successfully');
29
30# ... scalar should NOT be created here
31
32ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
33ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
34ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
35
36ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
37ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
38
39# check the value ...
40
41ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
42is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
43
44my $foo = $foo_stash->get_symbol('%foo');
45is_deeply({ one => 1 }, $foo, '... got the right package variable back');
46
47# ... make sure changes propogate up
48
49$foo->{two} = 2;
50
51is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
52
53ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
54is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
55
56# ----------------------------------------------------------------------
57## test adding an ARRAY
58
59ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
60
61is(exception {
62 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
63}, undef, '... created @Foo::bar successfully');
64
65ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
66ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
67
68# ... why does this not work ...
69
70ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
71ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
72ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
73
74# check the value itself
75
76is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
77is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
78
79# ----------------------------------------------------------------------
80## test adding a SCALAR
81
82ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
83
84is(exception {
85 $foo_stash->add_symbol('$baz' => 10);
86}, undef, '... created $Foo::baz successfully');
87
88ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
89ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
90
91ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
92ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
93ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
94
95is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
96
97${ $Foo->{baz} } = 1;
98
99is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
100is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
101
102# ----------------------------------------------------------------------
103## test adding a CODE
104
105ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
106
107is(exception {
108 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
109}, undef, '... created &Foo::funk successfully');
110
111ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
112ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
113
114ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
115ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
116ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
117
118ok(defined &{ $Foo->{funk} }, '... our &funk exists');
119
120# can't bless things into hashrefs yet
121# is($Foo->bless({})->funk(), 'Foo::funk', '... got the right value from the function');
122
123# ----------------------------------------------------------------------
124## test multiple slots in the glob
125
126my $ARRAY = [ 1, 2, 3 ];
127my $CODE = sub { "Foo::foo" };
128
129is(exception {
130 $foo_stash->add_symbol('@foo' => $ARRAY);
131}, undef, '... created @Foo::foo successfully');
132
133ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
134is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
135
136is(exception {
137 $foo_stash->add_symbol('&foo' => $CODE);
138}, undef, '... created &Foo::foo successfully');
139
140ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
141is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
142
143is(exception {
144 $foo_stash->add_symbol('$foo' => 'Foo::foo');
145}, undef, '... created $Foo::foo successfully');
146
147ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
148my $SCALAR = $foo_stash->get_symbol('$foo');
149is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
150
151is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar');
152
153is(exception {
154 $foo_stash->remove_symbol('%foo');
155}, undef, '... removed %Foo::foo successfully');
156
157ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
158ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
159ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
160ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
161
162is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
163is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
164is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
165
166ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
167ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
168ok(defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has NOT been removed');
169ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
170
171is(exception {
172 $foo_stash->remove_symbol('&foo');
173}, undef, '... removed &Foo::foo successfully');
174
175ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
176
177ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
178ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
179
180is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
181is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
182
183ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
184ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
185ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
186ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
187
188is(exception {
189 $foo_stash->remove_symbol('$foo');
190}, undef, '... removed $Foo::foo successfully');
191
192ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
193
194ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
195
196is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
197
198ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
199ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
200ok(!defined(${ $Foo->{foo} }), '... the $foo slot has now been removed');
201ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
202
203{
204 my $syms = $foo_stash->get_all_symbols;
205 is_deeply(
206 [ sort keys %{ $syms } ],
207 [ sort $foo_stash->list_all_symbols ],
208 '... the fetched symbols are the same as the listed ones'
209 );
210}
211
212{
213 local $TODO = ($Package::Stash::IMPLEMENTATION eq 'PP')
214 ? "can't inflate weird stash entries"
215 : undef;
216
217 is(
218 exception {
219 my $syms = $foo_stash->get_all_symbols('CODE');
220
221 is_deeply(
222 [ sort keys %{ $syms } ],
223 [ sort $foo_stash->list_all_symbols('CODE') ],
224 '... the fetched symbols are the same as the listed ones'
225 );
226
227 foreach my $symbol (keys %{ $syms }) {
228 is($syms->{$symbol}, $foo_stash->get_symbol('&' . $symbol), '... got the right symbol');
229 }
230 },
231 undef
232 );
233}
234
235{
236 $foo_stash->add_symbol('%bare');
237 ok(!$foo_stash->has_symbol('$bare'),
238 "add_symbol with single argument doesn't vivify scalar slot");
239}
240
241{
242 $foo_stash->add_symbol('%zork', {});
243
244 my $syms = $foo_stash->get_all_symbols('HASH');
245
246 is_deeply(
247 [ sort keys %{ $syms } ],
248 [ sort $foo_stash->list_all_symbols('HASH') ],
249 '... the fetched symbols are the same as the listed ones'
250 );
251
252 foreach my $symbol (keys %{ $syms }) {
253 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
254 }
255
256 is_deeply(
257 $syms,
258 { zork => *{ $Foo->{zork} }{HASH} },
259 "got the right ones",
260 );
261}
262
263# check some errors
264
265like(exception {
266 $foo_stash->add_symbol('@bar', {})
267}, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
268
269like(exception {
270 $foo_stash->add_symbol('bar', [])
271}, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
272
273like(exception {
274 $foo_stash->add_symbol('$bar', sub { })
275}, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
276
277like(exception {
278 $foo_stash->add_symbol('$bar', *{ Symbol::geniosym() }{IO})
279}, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
280
281is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
282 "Foo:: isn't touched");
283
284my $Quux = {};
285$Quux->{foo} = *{ Symbol::gensym() };
286*{ $Quux->{foo} } = \23;
287*{ $Quux->{foo} } = ["bar"];
288*{ $Quux->{foo} } = { baz => 1 };
289*{ $Quux->{foo} } = sub { };
290*{ $Quux->{foo} } = *{ Symbol::geniosym() }{IO};
291
292{
293 my $stash = Package::Stash->new($Quux);
294
295 my %expect = (
296 '$foo' => \23,
297 '@foo' => ["bar"],
298 '%foo' => { baz => 1 },
299 '&foo' => \&{ $Quux->{foo} },
300 'foo' => *{ $Quux->{foo} }{IO},
301 );
302
303 for my $sym ( sort keys %expect ) {
304 is_deeply(
305 $stash->get_symbol($sym),
306 $expect{$sym},
307 "got expected value for $sym"
308 );
309 }
310
311 $stash->add_symbol('%bar' => {x => 42});
312
313 $expect{'%bar'} = {x => 42};
314
315 for my $sym ( sort keys %expect ) {
316 is_deeply(
317 $stash->get_symbol($sym),
318 $expect{$sym},
319 "got expected value for $sym"
320 );
321 }
322
323 $stash->add_symbol('%bar' => {x => 43});
324
325 $expect{'%bar'} = {x => 43};
326
327 for my $sym ( sort keys %expect ) {
328 is_deeply(
329 $stash->get_symbol($sym),
330 $expect{$sym},
331 "got expected value for $sym"
332 );
333 }
334}
335
336is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
337 "Quux:: isn't touched");
338
339my $Quuux = {};
340
341$Quuux->{foo} = *{ Symbol::gensym() };
342*{ $Quuux->{foo} } = \(my $scalar);
343*{ $Quuux->{foo} } = [];
344
345$Quuux->{bar} = *{ Symbol::gensym() };
346*{ $Quuux->{bar} } = [];
347
348$Quuux->{baz} = *{ Symbol::gensym() };
349*{ $Quuux->{baz} } = {};
350*{ $Quuux->{baz} } = sub { };
351
352$Quuux->{quux} = \1;
353
354$Quuux->{quuux} = \[];
355
356$Quuux->{quuuux} = -1;
357
358{
359 my $quuux = Package::Stash->new($Quuux);
360 is_deeply(
361 [sort $quuux->list_all_symbols],
362 [qw(bar baz foo quuuux quuux quux)],
363 "list_all_symbols",
364 );
365 { local $TODO = $] < 5.010
366 ? "undef scalars aren't visible on 5.8"
367 : undef;
368 is_deeply(
369 [sort $quuux->list_all_symbols('SCALAR')],
370 [qw(foo)],
371 "list_all_symbols SCALAR",
372 );
373 }
374 is_deeply(
375 [sort $quuux->list_all_symbols('ARRAY')],
376 [qw(bar foo)],
377 "list_all_symbols ARRAY",
378 );
379 is_deeply(
380 [sort $quuux->list_all_symbols('HASH')],
381 [qw(baz)],
382 "list_all_symbols HASH",
383 );
384 is_deeply(
385 [sort $quuux->list_all_symbols('CODE')],
386 [qw(baz quuuux quuux quux)],
387 "list_all_symbols CODE",
388 );
389}
390
391is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
392 "Quuux:: isn't touched");
393
394done_testing;