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