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