Commit | Line | Data |
a5129e53 |
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 | { |
1715486b |
259 | $foo_stash->add_symbol('%bare'); |
260 | ok(!$foo_stash->has_symbol('$bare'), |
abe3605b |
261 | "add_symbol with single argument doesn't vivify scalar slot"); |
1715486b |
262 | } |
263 | |
264 | { |
265 | $foo_stash->add_symbol('%zork', {}); |
a5129e53 |
266 | |
267 | my $syms = $foo_stash->get_all_symbols('HASH'); |
268 | |
269 | is_deeply( |
270 | [ sort keys %{ $syms } ], |
271 | [ sort $foo_stash->list_all_symbols('HASH') ], |
272 | '... the fetched symbols are the same as the listed ones' |
273 | ); |
274 | |
275 | foreach my $symbol (keys %{ $syms }) { |
276 | is($syms->{$symbol}, $foo_stash->get_symbol('%' . $symbol), '... got the right symbol'); |
277 | } |
278 | |
a5129e53 |
279 | is_deeply( |
280 | $syms, |
1715486b |
281 | { zork => *{ $Foo::{zork} }{HASH} }, |
a5129e53 |
282 | "got the right ones", |
283 | ); |
284 | } |
285 | |
286 | # check some errors |
287 | |
288 | like(exception { |
289 | $foo_stash->add_symbol('@bar', {}) |
290 | }, qr/HASH.*is not of type ARRAY/, "can't initialize a slot with the wrong type of value"); |
291 | |
292 | like(exception { |
293 | $foo_stash->add_symbol('bar', []) |
294 | }, qr/ARRAY.*is not of type IO/, "can't initialize a slot with the wrong type of value"); |
295 | |
296 | like(exception { |
297 | $foo_stash->add_symbol('$bar', sub { }) |
298 | }, qr/CODE.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value"); |
299 | |
300 | { |
301 | package Bar; |
302 | open *foo, '<', $0; |
303 | } |
304 | |
305 | like(exception { |
306 | $foo_stash->add_symbol('$bar', *Bar::foo{IO}) |
307 | }, qr/IO.*is not of type SCALAR/, "can't initialize a slot with the wrong type of value"); |
308 | |
309 | # check compile time manipulation |
310 | |
311 | { |
312 | package Baz; |
313 | |
314 | our $foo = 23; |
315 | our @foo = "bar"; |
316 | our %foo = (baz => 1); |
317 | sub foo { } |
318 | open *foo, '<', $0; |
319 | BEGIN { Package::Stash->new(__PACKAGE__)->remove_symbol('&foo') } |
320 | } |
321 | |
322 | { |
323 | my $stash = Package::Stash->new('Baz'); |
324 | is(${ $stash->get_symbol('$foo') }, 23, "got \$foo"); |
325 | is_deeply($stash->get_symbol('@foo'), ['bar'], "got \@foo"); |
326 | is_deeply($stash->get_symbol('%foo'), {baz => 1}, "got \%foo"); |
327 | ok(!$stash->has_symbol('&foo'), "got \&foo"); |
328 | is($stash->get_symbol('foo'), *Baz::foo{IO}, "got foo"); |
329 | } |
330 | |
331 | { |
332 | package Quux; |
333 | |
334 | our $foo = 23; |
335 | our @foo = "bar"; |
336 | our %foo = (baz => 1); |
337 | sub foo { } |
338 | open *foo, '<', $0; |
339 | } |
340 | |
341 | { |
342 | my $stash = Package::Stash->new('Quux'); |
343 | |
344 | my %expect = ( |
345 | '$foo' => \23, |
346 | '@foo' => ["bar"], |
347 | '%foo' => { baz => 1 }, |
348 | '&foo' => \&Quux::foo, |
349 | 'foo' => *Quux::foo{IO}, |
350 | ); |
351 | |
352 | for my $sym ( sort keys %expect ) { |
353 | is_deeply( |
354 | $stash->get_symbol($sym), |
355 | $expect{$sym}, |
356 | "got expected value for $sym" |
357 | ); |
358 | } |
359 | |
360 | $stash->add_symbol('%bar' => {x => 42}); |
361 | |
362 | $expect{'%bar'} = {x => 42}; |
363 | |
364 | for my $sym ( sort keys %expect ) { |
365 | is_deeply( |
366 | $stash->get_symbol($sym), |
367 | $expect{$sym}, |
368 | "got expected value for $sym" |
369 | ); |
370 | } |
371 | |
372 | $stash->add_symbol('%bar' => {x => 43}); |
373 | |
374 | $expect{'%bar'} = {x => 43}; |
375 | |
376 | for my $sym ( sort keys %expect ) { |
377 | is_deeply( |
378 | $stash->get_symbol($sym), |
379 | $expect{$sym}, |
380 | "got expected value for $sym" |
381 | ); |
382 | } |
383 | } |
384 | |
385 | { |
386 | package Quuux; |
387 | our $foo; |
388 | our @foo; |
389 | our @bar; |
390 | our %baz; |
391 | sub baz { } |
392 | use constant quux => 1; |
393 | use constant quuux => []; |
394 | sub quuuux; |
395 | } |
396 | |
397 | { |
398 | my $quuux = Package::Stash->new('Quuux'); |
399 | is_deeply( |
400 | [sort $quuux->list_all_symbols], |
401 | [qw(BEGIN bar baz foo quuuux quuux quux)], |
402 | "list_all_symbols", |
403 | ); |
7ef54f40 |
404 | { local $TODO = $] < 5.010 |
405 | ? "undef scalars aren't visible on 5.8" |
a5129e53 |
406 | : undef; |
407 | is_deeply( |
408 | [sort $quuux->list_all_symbols('SCALAR')], |
409 | [qw(foo)], |
410 | "list_all_symbols SCALAR", |
411 | ); |
412 | } |
413 | is_deeply( |
414 | [sort $quuux->list_all_symbols('ARRAY')], |
415 | [qw(bar foo)], |
416 | "list_all_symbols ARRAY", |
417 | ); |
418 | is_deeply( |
419 | [sort $quuux->list_all_symbols('HASH')], |
420 | [qw(baz)], |
421 | "list_all_symbols HASH", |
422 | ); |
423 | is_deeply( |
424 | [sort $quuux->list_all_symbols('CODE')], |
425 | [qw(baz quuuux quuux quux)], |
426 | "list_all_symbols CODE", |
427 | ); |
428 | } |
429 | |
abe3605b |
430 | for my $package ('Foo:Bar', 'Foo/Bar', 'Foo Bar', 'Foo:::Bar', '') { |
431 | like( |
432 | exception { Package::Stash->new($package) }, |
433 | qr/^$package is not a module name/, |
434 | "$package is not a module name" |
435 | ); |
436 | } |
437 | |
438 | like( |
439 | exception { Package::Stash->new([]) }, |
440 | qr/^Package::Stash->new must be passed the name of the package to access/, |
441 | "module name must be a string" |
442 | ); |
443 | |
444 | like( |
445 | exception { Package::Stash->new(undef) }, |
446 | qr/^Package::Stash->new must be passed the name of the package to access/, |
447 | "module name must be a string" |
448 | ); |
449 | |
a5129e53 |
450 | done_testing; |