actually, this isn't our fault, this is just generic 5.8 brokenness
[gitmo/Package-Stash.git] / t / bare-anon-basic.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use lib 't/lib';
5 use Test::More;
6 use Test::Fatal;
7
8 use Package::Stash;
9 use Symbol;
10
11 plan skip_all => "Anonymous stashes in PP need at least perl 5.14"
12     if $] < 5.014
13     && $Package::Stash::IMPLEMENTATION eq 'PP';
14
15 plan skip_all => "This isn't really going to work yet, probably";
16
17 my $Foo = {};
18 $Foo->{SOME_CONSTANT} = \1;
19
20 # ----------------------------------------------------------------------
21 ## tests adding a HASH
22
23 my $foo_stash = Package::Stash->new($Foo);
24 ok(!defined($Foo->{foo}), '... the %foo slot has not been created yet');
25 ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
26 ok(!defined($Foo->{foo}), '... checking doesn\'t vivify');
27
28 is(exception {
29     $foo_stash->add_symbol('%foo' => { one => 1 });
30 }, undef, '... created %Foo::foo successfully');
31
32 # ... scalar should NOT be created here
33
34 ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
35 ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
36 ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
37
38 ok(defined($Foo->{foo}), '... the %foo slot was created successfully');
39 ok($foo_stash->has_symbol('%foo'), '... the meta agrees');
40
41 # check the value ...
42
43 ok(exists $Foo->{foo}{one}, '... our %foo was initialized correctly');
44 is($Foo->{foo}{one}, 1, '... our %foo was initialized correctly');
45
46 my $foo = $foo_stash->get_symbol('%foo');
47 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
48
49 # ... make sure changes propogate up
50
51 $foo->{two} = 2;
52
53 is(\%{ $Foo->{foo} }, $foo_stash->get_symbol('%foo'), '... our %foo is the same as the metas');
54
55 ok(exists ${ $Foo->{foo} }{two}, '... our %foo was updated correctly');
56 is(${ $Foo->{foo} }{two}, 2, '... our %foo was updated correctly');
57
58 # ----------------------------------------------------------------------
59 ## test adding an ARRAY
60
61 ok(!defined($Foo->{bar}), '... the @bar slot has not been created yet');
62
63 is(exception {
64     $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
65 }, undef, '... created @Foo::bar successfully');
66
67 ok(defined($Foo->{bar}), '... the @bar slot was created successfully');
68 ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
69
70 # ... why does this not work ...
71
72 ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
73 ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
74 ok(!$foo_stash->has_symbol('&bar'), '... CODE shouldnt have been created too');
75
76 # check the value itself
77
78 is(scalar @{ $Foo->{bar} }, 3, '... our @bar was initialized correctly');
79 is($Foo->{bar}[1], 2, '... our @bar was initialized correctly');
80
81 # ----------------------------------------------------------------------
82 ## test adding a SCALAR
83
84 ok(!defined($Foo->{baz}), '... the $baz slot has not been created yet');
85
86 is(exception {
87     $foo_stash->add_symbol('$baz' => 10);
88 }, undef, '... created $Foo::baz successfully');
89
90 ok(defined($Foo->{baz}), '... the $baz slot was created successfully');
91 ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
92
93 ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
94 ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
95 ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
96
97 is(${$foo_stash->get_symbol('$baz')}, 10, '... got the right value back');
98
99 ${ $Foo->{baz} } = 1;
100
101 is(${ $Foo->{baz} }, 1, '... our $baz was assigned to correctly');
102 is(${$foo_stash->get_symbol('$baz')}, 1, '... the meta agrees');
103
104 # ----------------------------------------------------------------------
105 ## test adding a CODE
106
107 ok(!defined($Foo->{funk}), '... the &funk slot has not been created yet');
108
109 is(exception {
110     $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
111 }, undef, '... created &Foo::funk successfully');
112
113 ok(defined($Foo->{funk}), '... the &funk slot was created successfully');
114 ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
115
116 ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
117 ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
118 ok(!$foo_stash->has_symbol('%funk'), '... HASH shouldnt have been created too');
119
120 ok(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
128 my $ARRAY = [ 1, 2, 3 ];
129 my $CODE = sub { "Foo::foo" };
130
131 is(exception {
132     $foo_stash->add_symbol('@foo' => $ARRAY);
133 }, undef, '... created @Foo::foo successfully');
134
135 ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
136 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
137
138 is(exception {
139     $foo_stash->add_symbol('&foo' => $CODE);
140 }, undef, '... created &Foo::foo successfully');
141
142 ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
143 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
144
145 is(exception {
146     $foo_stash->add_symbol('$foo' => 'Foo::foo');
147 }, undef, '... created $Foo::foo successfully');
148
149 ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
150 my $SCALAR = $foo_stash->get_symbol('$foo');
151 is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
152
153 is(${ $Foo->{foo} }, 'Foo::foo', '... got the right value from the scalar');
154
155 is(exception {
156     $foo_stash->remove_symbol('%foo');
157 }, undef, '... removed %Foo::foo successfully');
158
159 ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
160 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
161 ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
162 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
163
164 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
165 is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
166 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
167
168 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
169 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
170 ok(defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has NOT been removed');
171 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
172
173 is(exception {
174     $foo_stash->remove_symbol('&foo');
175 }, undef, '... removed &Foo::foo successfully');
176
177 ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
178
179 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
180 ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
181
182 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
183 is($foo_stash->get_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
184
185 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
186 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
187 ok(defined(*{ $Foo->{foo} }{ARRAY}), '... the @foo slot has NOT been removed');
188 ok(defined(${ $Foo->{foo} }), '... the $foo slot has NOT been removed');
189
190 is(exception {
191     $foo_stash->remove_symbol('$foo');
192 }, undef, '... removed $Foo::foo successfully');
193
194 ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
195
196 ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
197
198 is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
199
200 ok(!defined(*{ $Foo->{foo} }{HASH}), '... the %foo slot has been removed successfully');
201 ok(!defined(*{ $Foo->{foo} }{CODE}), '... the &foo slot has now been removed');
202 ok(!defined(${ $Foo->{foo} }), '... the $foo slot has now been removed');
203 ok(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 = "can't inflate weird stash entries";
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
265 like(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
269 like(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
273 like(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
277 like(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
281 is_deeply([Package::Stash->new('Foo')->list_all_symbols], [],
282           "Foo:: isn't touched");
283
284 my $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
336 is_deeply([Package::Stash->new('Quux')->list_all_symbols], [],
337           "Quux:: isn't touched");
338
339 my $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
391 is_deeply([Package::Stash->new('Quuux')->list_all_symbols], [],
392           "Quuux:: isn't touched");
393
394 done_testing;