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