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