un-TODO
[gitmo/Package-Stash-PP.git] / t / 001-basic.t
CommitLineData
f10f6217 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use Stash::Manip;
8
9dies_ok { Stash::Manip->name } q{... can't call name() as a class method};
10
11{
12 package Foo;
13
14 use constant SOME_CONSTANT => 1;
15}
16
17# ----------------------------------------------------------------------
18## tests adding a HASH
19
20my $foo_stash = Stash::Manip->new('Foo');
21ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
22ok(!$foo_stash->has_package_symbol('%foo'), '... the object agrees');
23ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
24
25lives_ok {
26 $foo_stash->add_package_symbol('%foo' => { one => 1 });
27} '... created %Foo::foo successfully';
28
29# ... scalar should NOT be created here
30
31ok(!$foo_stash->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
32ok(!$foo_stash->has_package_symbol('@foo'), '... ARRAY shouldnt have been created too');
33ok(!$foo_stash->has_package_symbol('&foo'), '... CODE shouldnt have been created too');
34
35ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
36ok($foo_stash->has_package_symbol('%foo'), '... the meta agrees');
37
38# check the value ...
39
40{
41 no strict 'refs';
42 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
43 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
44}
45
46my $foo = $foo_stash->get_package_symbol('%foo');
47is_deeply({ one => 1 }, $foo, '... got the right package variable back');
48
49# ... make sure changes propogate up
50
51$foo->{two} = 2;
52
53{
54 no strict 'refs';
55 is(\%{'Foo::foo'}, $foo_stash->get_package_symbol('%foo'), '... our %foo is the same as the metas');
56
57 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
58 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
59}
60
61# ----------------------------------------------------------------------
62## test adding an ARRAY
63
64ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
65
66lives_ok {
67 $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]);
68} '... created @Foo::bar successfully';
69
70ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
71ok($foo_stash->has_package_symbol('@bar'), '... the meta agrees');
72
73# ... why does this not work ...
74
75ok(!$foo_stash->has_package_symbol('$bar'), '... SCALAR shouldnt have been created too');
76ok(!$foo_stash->has_package_symbol('%bar'), '... HASH shouldnt have been created too');
77ok(!$foo_stash->has_package_symbol('&bar'), '... CODE shouldnt have been created too');
78
79# check the value itself
80
81{
82 no strict 'refs';
83 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
84 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
85}
86
87# ----------------------------------------------------------------------
88## test adding a SCALAR
89
90ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
91
92lives_ok {
93 $foo_stash->add_package_symbol('$baz' => 10);
94} '... created $Foo::baz successfully';
95
96ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
97ok($foo_stash->has_package_symbol('$baz'), '... the meta agrees');
98
99ok(!$foo_stash->has_package_symbol('@baz'), '... ARRAY shouldnt have been created too');
100ok(!$foo_stash->has_package_symbol('%baz'), '... HASH shouldnt have been created too');
101ok(!$foo_stash->has_package_symbol('&baz'), '... CODE shouldnt have been created too');
102
103is(${$foo_stash->get_package_symbol('$baz')}, 10, '... got the right value back');
104
105{
106 no strict 'refs';
107 ${'Foo::baz'} = 1;
108
109 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
110 is(${$foo_stash->get_package_symbol('$baz')}, 1, '... the meta agrees');
111}
112
113# ----------------------------------------------------------------------
114## test adding a CODE
115
116ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
117
118lives_ok {
119 $foo_stash->add_package_symbol('&funk' => sub { "Foo::funk" });
120} '... created &Foo::funk successfully';
121
122ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
123ok($foo_stash->has_package_symbol('&funk'), '... the meta agrees');
124
125ok(!$foo_stash->has_package_symbol('$funk'), '... SCALAR shouldnt have been created too');
126ok(!$foo_stash->has_package_symbol('@funk'), '... ARRAY shouldnt have been created too');
127ok(!$foo_stash->has_package_symbol('%funk'), '... HASH shouldnt have been created too');
128
129{
130 no strict 'refs';
131 ok(defined &{'Foo::funk'}, '... our &funk exists');
132}
133
134is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
135
136# ----------------------------------------------------------------------
137## test multiple slots in the glob
138
139my $ARRAY = [ 1, 2, 3 ];
140my $CODE = sub { "Foo::foo" };
141
142lives_ok {
143 $foo_stash->add_package_symbol('@foo' => $ARRAY);
144} '... created @Foo::foo successfully';
145
146ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot was added successfully');
147is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
148
149lives_ok {
150 $foo_stash->add_package_symbol('&foo' => $CODE);
151} '... created &Foo::foo successfully';
152
153ok($foo_stash->has_package_symbol('&foo'), '... the meta agrees');
154is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
155
156lives_ok {
157 $foo_stash->add_package_symbol('$foo' => 'Foo::foo');
158} '... created $Foo::foo successfully';
159
160ok($foo_stash->has_package_symbol('$foo'), '... the meta agrees');
161my $SCALAR = $foo_stash->get_package_symbol('$foo');
162is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
163
164{
165 no strict 'refs';
166 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
167}
168
169lives_ok {
170 $foo_stash->remove_package_symbol('%foo');
171} '... removed %Foo::foo successfully';
172
173ok(!$foo_stash->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
174ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
175ok($foo_stash->has_package_symbol('&foo'), '... the &foo slot still exists');
176ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists');
177
178is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
179is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
180is($foo_stash->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
181
182{
183 no strict 'refs';
184 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
185 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
186 ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed');
187 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
188}
189
190lives_ok {
191 $foo_stash->remove_package_symbol('&foo');
192} '... removed &Foo::foo successfully';
193
194ok(!$foo_stash->has_package_symbol('&foo'), '... the &foo slot no longer exists');
195
196ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
197ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists');
198
199is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
200is($foo_stash->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
201
202{
203 no strict 'refs';
204 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
205 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
206 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
207 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
208}
209
210lives_ok {
211 $foo_stash->remove_package_symbol('$foo');
212} '... removed $Foo::foo successfully';
213
214ok(!$foo_stash->has_package_symbol('$foo'), '... the $foo slot no longer exists');
215
216ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists');
217
218is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
219
220{
221 no strict 'refs';
222 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
223 ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed');
224 ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed');
225 ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed');
226}
227
3634ce60 228# check some errors
229
230dies_ok {
231 $foo_stash->add_package_symbol('@bar', {})
232} "can't initialize a slot with the wrong type of value";
233
234dies_ok {
235 $foo_stash->add_package_symbol('bar', [])
236} "can't initialize a slot with the wrong type of value";
237
238dies_ok {
239 $foo_stash->add_package_symbol('$bar', sub { })
240} "can't initialize a slot with the wrong type of value";
241
242{
243 package Bar;
244 open *foo, '<', $0;
245}
246
247dies_ok {
248 $foo_stash->add_package_symbol('$bar', *Bar::foo{IO})
249} "can't initialize a slot with the wrong type of value";
250
6ee333b8 251# check compile time manipulation
252
253{
254 package Baz;
255
256 our $foo = 23;
257 our @foo = "bar";
258 our %foo = (baz => 1);
259 sub foo { }
260 open *foo, '<', $0;
261 BEGIN { Stash::Manip->new(__PACKAGE__)->remove_package_symbol('&foo') }
262}
263
264{
265 my $stash = Stash::Manip->new('Baz');
6ee333b8 266 is(${ $stash->get_package_symbol('$foo') }, 23, "got \$foo");
6ee333b8 267 is_deeply($stash->get_package_symbol('@foo'), ['bar'], "got \@foo");
268 is_deeply($stash->get_package_symbol('%foo'), {baz => 1}, "got \%foo");
269 ok(!$stash->has_package_symbol('&foo'), "got \&foo");
270 is($stash->get_package_symbol('foo'), *Baz::foo{IO}, "got foo");
271}
272
f10f6217 273done_testing;