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