Commit | Line | Data |
f10f6217 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | |
7 | use Stash::Manip; |
8 | |
9 | dies_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 | |
20 | my $foo_stash = Stash::Manip->new('Foo'); |
21 | ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet'); |
22 | ok(!$foo_stash->has_package_symbol('%foo'), '... the object agrees'); |
23 | ok(!defined($Foo::{foo}), '... checking doesn\' vivify'); |
24 | |
25 | lives_ok { |
26 | $foo_stash->add_package_symbol('%foo' => { one => 1 }); |
27 | } '... created %Foo::foo successfully'; |
28 | |
29 | # ... scalar should NOT be created here |
30 | |
31 | ok(!$foo_stash->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too'); |
32 | ok(!$foo_stash->has_package_symbol('@foo'), '... ARRAY shouldnt have been created too'); |
33 | ok(!$foo_stash->has_package_symbol('&foo'), '... CODE shouldnt have been created too'); |
34 | |
35 | ok(defined($Foo::{foo}), '... the %foo slot was created successfully'); |
36 | ok($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 | |
46 | my $foo = $foo_stash->get_package_symbol('%foo'); |
47 | is_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 | |
64 | ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet'); |
65 | |
66 | lives_ok { |
67 | $foo_stash->add_package_symbol('@bar' => [ 1, 2, 3 ]); |
68 | } '... created @Foo::bar successfully'; |
69 | |
70 | ok(defined($Foo::{bar}), '... the @bar slot was created successfully'); |
71 | ok($foo_stash->has_package_symbol('@bar'), '... the meta agrees'); |
72 | |
73 | # ... why does this not work ... |
74 | |
75 | ok(!$foo_stash->has_package_symbol('$bar'), '... SCALAR shouldnt have been created too'); |
76 | ok(!$foo_stash->has_package_symbol('%bar'), '... HASH shouldnt have been created too'); |
77 | ok(!$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 | |
90 | ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet'); |
91 | |
92 | lives_ok { |
93 | $foo_stash->add_package_symbol('$baz' => 10); |
94 | } '... created $Foo::baz successfully'; |
95 | |
96 | ok(defined($Foo::{baz}), '... the $baz slot was created successfully'); |
97 | ok($foo_stash->has_package_symbol('$baz'), '... the meta agrees'); |
98 | |
99 | ok(!$foo_stash->has_package_symbol('@baz'), '... ARRAY shouldnt have been created too'); |
100 | ok(!$foo_stash->has_package_symbol('%baz'), '... HASH shouldnt have been created too'); |
101 | ok(!$foo_stash->has_package_symbol('&baz'), '... CODE shouldnt have been created too'); |
102 | |
103 | is(${$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 | |
116 | ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet'); |
117 | |
118 | lives_ok { |
119 | $foo_stash->add_package_symbol('&funk' => sub { "Foo::funk" }); |
120 | } '... created &Foo::funk successfully'; |
121 | |
122 | ok(defined($Foo::{funk}), '... the &funk slot was created successfully'); |
123 | ok($foo_stash->has_package_symbol('&funk'), '... the meta agrees'); |
124 | |
125 | ok(!$foo_stash->has_package_symbol('$funk'), '... SCALAR shouldnt have been created too'); |
126 | ok(!$foo_stash->has_package_symbol('@funk'), '... ARRAY shouldnt have been created too'); |
127 | ok(!$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 | |
134 | is(Foo->funk(), 'Foo::funk', '... got the right value from the function'); |
135 | |
136 | # ---------------------------------------------------------------------- |
137 | ## test multiple slots in the glob |
138 | |
139 | my $ARRAY = [ 1, 2, 3 ]; |
140 | my $CODE = sub { "Foo::foo" }; |
141 | |
142 | lives_ok { |
143 | $foo_stash->add_package_symbol('@foo' => $ARRAY); |
144 | } '... created @Foo::foo successfully'; |
145 | |
146 | ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot was added successfully'); |
147 | is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
148 | |
149 | lives_ok { |
150 | $foo_stash->add_package_symbol('&foo' => $CODE); |
151 | } '... created &Foo::foo successfully'; |
152 | |
153 | ok($foo_stash->has_package_symbol('&foo'), '... the meta agrees'); |
154 | is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
155 | |
156 | lives_ok { |
157 | $foo_stash->add_package_symbol('$foo' => 'Foo::foo'); |
158 | } '... created $Foo::foo successfully'; |
159 | |
160 | ok($foo_stash->has_package_symbol('$foo'), '... the meta agrees'); |
161 | my $SCALAR = $foo_stash->get_package_symbol('$foo'); |
162 | is($$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 | |
169 | lives_ok { |
170 | $foo_stash->remove_package_symbol('%foo'); |
171 | } '... removed %Foo::foo successfully'; |
172 | |
173 | ok(!$foo_stash->has_package_symbol('%foo'), '... the %foo slot was removed successfully'); |
174 | ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists'); |
175 | ok($foo_stash->has_package_symbol('&foo'), '... the &foo slot still exists'); |
176 | ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists'); |
177 | |
178 | is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
179 | is($foo_stash->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
180 | is($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 | |
190 | lives_ok { |
191 | $foo_stash->remove_package_symbol('&foo'); |
192 | } '... removed &Foo::foo successfully'; |
193 | |
194 | ok(!$foo_stash->has_package_symbol('&foo'), '... the &foo slot no longer exists'); |
195 | |
196 | ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists'); |
197 | ok($foo_stash->has_package_symbol('$foo'), '... the $foo slot still exists'); |
198 | |
199 | is($foo_stash->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
200 | is($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 | |
210 | lives_ok { |
211 | $foo_stash->remove_package_symbol('$foo'); |
212 | } '... removed $Foo::foo successfully'; |
213 | |
214 | ok(!$foo_stash->has_package_symbol('$foo'), '... the $foo slot no longer exists'); |
215 | |
216 | ok($foo_stash->has_package_symbol('@foo'), '... the @foo slot still exists'); |
217 | |
218 | is($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 | |
230 | dies_ok { |
231 | $foo_stash->add_package_symbol('@bar', {}) |
232 | } "can't initialize a slot with the wrong type of value"; |
233 | |
234 | dies_ok { |
235 | $foo_stash->add_package_symbol('bar', []) |
236 | } "can't initialize a slot with the wrong type of value"; |
237 | |
238 | dies_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 | |
247 | dies_ok { |
248 | $foo_stash->add_package_symbol('$bar', *Bar::foo{IO}) |
249 | } "can't initialize a slot with the wrong type of value"; |
250 | |
f10f6217 |
251 | done_testing; |