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