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