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