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