better test for scalar existence
[gitmo/Package-Stash.git] / t / impl-selection / basic-xs.t
CommitLineData
a5129e53 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More;
5use Test::Fatal;
6use Test::Requires 'Package::Stash::XS';
7
8BEGIN { $Package::Stash::IMPLEMENTATION = 'XS' }
9
10use Package::Stash;
11
12ok(exists $INC{'Package/Stash/XS.pm'}, "loaded XS");
13ok(!exists $INC{'Package/Stash/PP.pm'}, "didn't load PP");
14
15like(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
27my $foo_stash = Package::Stash->new('Foo');
28ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
29ok(!$foo_stash->has_symbol('%foo'), '... the object agrees');
30ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
31
32is(exception {
33 $foo_stash->add_symbol('%foo' => { one => 1 });
34}, undef, '... created %Foo::foo successfully');
35
36# ... scalar should NOT be created here
37
38ok(!$foo_stash->has_symbol('$foo'), '... SCALAR shouldnt have been created too');
39ok(!$foo_stash->has_symbol('@foo'), '... ARRAY shouldnt have been created too');
40ok(!$foo_stash->has_symbol('&foo'), '... CODE shouldnt have been created too');
41
42ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
43ok($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
53my $foo = $foo_stash->get_symbol('%foo');
54is_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
71ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
72
73is(exception {
74 $foo_stash->add_symbol('@bar' => [ 1, 2, 3 ]);
75}, undef, '... created @Foo::bar successfully');
76
77ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
78ok($foo_stash->has_symbol('@bar'), '... the meta agrees');
79
80# ... why does this not work ...
81
82ok(!$foo_stash->has_symbol('$bar'), '... SCALAR shouldnt have been created too');
83ok(!$foo_stash->has_symbol('%bar'), '... HASH shouldnt have been created too');
84ok(!$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
97ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
98
99is(exception {
100 $foo_stash->add_symbol('$baz' => 10);
101}, undef, '... created $Foo::baz successfully');
102
103ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
104ok($foo_stash->has_symbol('$baz'), '... the meta agrees');
105
106ok(!$foo_stash->has_symbol('@baz'), '... ARRAY shouldnt have been created too');
107ok(!$foo_stash->has_symbol('%baz'), '... HASH shouldnt have been created too');
108ok(!$foo_stash->has_symbol('&baz'), '... CODE shouldnt have been created too');
109
110is(${$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
123ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
124
125is(exception {
126 $foo_stash->add_symbol('&funk' => sub { "Foo::funk" });
127}, undef, '... created &Foo::funk successfully');
128
129ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
130ok($foo_stash->has_symbol('&funk'), '... the meta agrees');
131
132ok(!$foo_stash->has_symbol('$funk'), '... SCALAR shouldnt have been created too');
133ok(!$foo_stash->has_symbol('@funk'), '... ARRAY shouldnt have been created too');
134ok(!$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
141is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
142
143# ----------------------------------------------------------------------
144## test multiple slots in the glob
145
146my $ARRAY = [ 1, 2, 3 ];
147my $CODE = sub { "Foo::foo" };
148
149is(exception {
150 $foo_stash->add_symbol('@foo' => $ARRAY);
151}, undef, '... created @Foo::foo successfully');
152
153ok($foo_stash->has_symbol('@foo'), '... the @foo slot was added successfully');
154is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
155
156is(exception {
157 $foo_stash->add_symbol('&foo' => $CODE);
158}, undef, '... created &Foo::foo successfully');
159
160ok($foo_stash->has_symbol('&foo'), '... the meta agrees');
161is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
162
163is(exception {
164 $foo_stash->add_symbol('$foo' => 'Foo::foo');
165}, undef, '... created $Foo::foo successfully');
166
167ok($foo_stash->has_symbol('$foo'), '... the meta agrees');
168my $SCALAR = $foo_stash->get_symbol('$foo');
169is($$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
176is(exception {
177 $foo_stash->remove_symbol('%foo');
178}, undef, '... removed %Foo::foo successfully');
179
180ok(!$foo_stash->has_symbol('%foo'), '... the %foo slot was removed successfully');
181ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
182ok($foo_stash->has_symbol('&foo'), '... the &foo slot still exists');
183ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
184
185is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
186is($foo_stash->get_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
187is($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
197is(exception {
198 $foo_stash->remove_symbol('&foo');
199}, undef, '... removed &Foo::foo successfully');
200
201ok(!$foo_stash->has_symbol('&foo'), '... the &foo slot no longer exists');
202
203ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
204ok($foo_stash->has_symbol('$foo'), '... the $foo slot still exists');
205
206is($foo_stash->get_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
207is($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
217is(exception {
218 $foo_stash->remove_symbol('$foo');
219}, undef, '... removed $Foo::foo successfully');
220
221ok(!$foo_stash->has_symbol('$foo'), '... the $foo slot no longer exists');
222
223ok($foo_stash->has_symbol('@foo'), '... the @foo slot still exists');
224
225is($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
261 my $syms = $foo_stash->get_all_symbols('HASH');
262
263 is_deeply(
264 [ sort keys %{ $syms } ],
265 [ sort $foo_stash->list_all_symbols('HASH') ],
266 '... the fetched symbols are the same as the listed ones'
267 );
268
269 foreach my $symbol (keys %{ $syms }) {
270 is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol');
271 }
272
273 no warnings 'once';
274 is_deeply(
275 $syms,
276 { zork => \%Foo::zork },
277 "got the right ones",
278 );
279}
280
281# check some errors
282
283like(exception {
284 $foo_stash->add_symbol('@bar', {})
285}, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value");
286
287like(exception {
288 $foo_stash->add_symbol('bar', [])
289}, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value");
290
291like(exception {
292 $foo_stash->add_symbol('$bar', sub { })
293}, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
294
295{
296 package Bar;
297 open *foo, '<', $0;
298}
299
300like(exception {
301 $foo_stash->add_symbol('$bar', *Bar::foo{IO})
302}, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value");
303
304# check compile time manipulation
305
306{
307 package Baz;
308
309 our $foo = 23;
310 our @foo = "bar";
311 our %foo = (baz => 1);
312 sub foo { }
313 open *foo, '<', $0;
314 BEGIN { Package::Stash->new(__PACKAGE__)->remove_symbol('&foo') }
315}
316
317{
318 my $stash = Package::Stash->new('Baz');
319 is(${ $stash->get_symbol('$foo') }, 23, "got \$foo");
320 is_deeply($stash->get_symbol('@foo'), ['bar'], "got \@foo");
321 is_deeply($stash->get_symbol('%foo'), {baz => 1}, "got \%foo");
322 ok(!$stash->has_symbol('&foo'), "got \&foo");
323 is($stash->get_symbol('foo'), *Baz::foo{IO}, "got foo");
324}
325
326{
327 package Quux;
328
329 our $foo = 23;
330 our @foo = "bar";
331 our %foo = (baz => 1);
332 sub foo { }
333 open *foo, '<', $0;
334}
335
336{
337 my $stash = Package::Stash->new('Quux');
338
339 my %expect = (
340 '$foo' => \23,
341 '@foo' => ["bar"],
342 '%foo' => { baz => 1 },
343 '&foo' => \&Quux::foo,
344 'foo' => *Quux::foo{IO},
345 );
346
347 for my $sym ( sort keys %expect ) {
348 is_deeply(
349 $stash->get_symbol($sym),
350 $expect{$sym},
351 "got expected value for $sym"
352 );
353 }
354
355 $stash->add_symbol('%bar' => {x => 42});
356
357 $expect{'%bar'} = {x => 42};
358
359 for my $sym ( sort keys %expect ) {
360 is_deeply(
361 $stash->get_symbol($sym),
362 $expect{$sym},
363 "got expected value for $sym"
364 );
365 }
366
367 $stash->add_symbol('%bar' => {x => 43});
368
369 $expect{'%bar'} = {x => 43};
370
371 for my $sym ( sort keys %expect ) {
372 is_deeply(
373 $stash->get_symbol($sym),
374 $expect{$sym},
375 "got expected value for $sym"
376 );
377 }
378}
379
380{
381 package Quuux;
382 our $foo;
383 our @foo;
384 our @bar;
385 our %baz;
386 sub baz { }
387 use constant quux => 1;
388 use constant quuux => [];
389 sub quuuux;
390}
391
392{
393 my $quuux = Package::Stash->new('Quuux');
394 is_deeply(
395 [sort $quuux->list_all_symbols],
396 [qw(BEGIN bar baz foo quuuux quuux quux)],
397 "list_all_symbols",
398 );
7ef54f40 399 { local $TODO = $] < 5.010
400 ? "undef scalars aren't visible on 5.8"
a5129e53 401 : undef;
402 is_deeply(
403 [sort $quuux->list_all_symbols('SCALAR')],
404 [qw(foo)],
405 "list_all_symbols SCALAR",
406 );
407 }
408 is_deeply(
409 [sort $quuux->list_all_symbols('ARRAY')],
410 [qw(bar foo)],
411 "list_all_symbols ARRAY",
412 );
413 is_deeply(
414 [sort $quuux->list_all_symbols('HASH')],
415 [qw(baz)],
416 "list_all_symbols HASH",
417 );
418 is_deeply(
419 [sort $quuux->list_all_symbols('CODE')],
420 [qw(baz quuuux quuux quux)],
421 "list_all_symbols CODE",
422 );
423}
424
425done_testing;