Commit | Line | Data |
52e8a34c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
efd3d14c |
6 | use Test::More tests => 86; |
52e8a34c |
7 | use Test::Exception; |
8 | |
efd3d14c |
9 | use Class::MOP; |
52e8a34c |
10 | |
11 | { |
12 | package Foo; |
aa448b16 |
13 | use metaclass; |
52e8a34c |
14 | } |
15 | |
c46b802b |
16 | =pod |
17 | |
18 | This is the same test as 080_meta_package.t just here |
19 | we call all the methods through Class::MOP::Class. |
20 | |
21 | =cut |
22 | |
23 | # ---------------------------------------------------------------------- |
24 | ## tests adding a HASH |
25 | |
52e8a34c |
26 | ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet'); |
58d75218 |
27 | ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees'); |
52e8a34c |
28 | |
29 | lives_ok { |
58d75218 |
30 | Foo->meta->add_package_symbol('%foo' => { one => 1 }); |
52e8a34c |
31 | } '... created %Foo::foo successfully'; |
32 | |
c46b802b |
33 | # ... scalar should NOT be created here |
34 | |
35 | ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too'); |
36 | ok(!Foo->meta->has_package_symbol('@foo'), '... ARRAY shouldnt have been created too'); |
37 | ok(!Foo->meta->has_package_symbol('&foo'), '... CODE shouldnt have been created too'); |
38 | |
52e8a34c |
39 | ok(defined($Foo::{foo}), '... the %foo slot was created successfully'); |
58d75218 |
40 | ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees'); |
52e8a34c |
41 | |
c46b802b |
42 | # check the value ... |
43 | |
52e8a34c |
44 | { |
45 | no strict 'refs'; |
46 | ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly'); |
47 | is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly'); |
48 | } |
49 | |
58d75218 |
50 | my $foo = Foo->meta->get_package_symbol('%foo'); |
52e8a34c |
51 | is_deeply({ one => 1 }, $foo, '... got the right package variable back'); |
52 | |
c46b802b |
53 | # ... make sure changes propogate up |
54 | |
52e8a34c |
55 | $foo->{two} = 2; |
56 | |
57 | { |
58 | no strict 'refs'; |
58d75218 |
59 | is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas'); |
52e8a34c |
60 | |
61 | ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly'); |
62 | is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly'); |
63 | } |
64 | |
c46b802b |
65 | # ---------------------------------------------------------------------- |
66 | ## test adding an ARRAY |
67 | |
52e8a34c |
68 | ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet'); |
69 | |
70 | lives_ok { |
58d75218 |
71 | Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]); |
52e8a34c |
72 | } '... created @Foo::bar successfully'; |
73 | |
74 | ok(defined($Foo::{bar}), '... the @bar slot was created successfully'); |
c46b802b |
75 | ok(Foo->meta->has_package_symbol('@bar'), '... the meta agrees'); |
76 | |
77 | # ... why does this not work ... |
78 | |
79 | ok(!Foo->meta->has_package_symbol('$bar'), '... SCALAR shouldnt have been created too'); |
80 | ok(!Foo->meta->has_package_symbol('%bar'), '... HASH shouldnt have been created too'); |
81 | ok(!Foo->meta->has_package_symbol('&bar'), '... CODE shouldnt have been created too'); |
82 | |
83 | # check the value itself |
52e8a34c |
84 | |
85 | { |
86 | no strict 'refs'; |
87 | is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly'); |
88 | is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly'); |
89 | } |
90 | |
c46b802b |
91 | # ---------------------------------------------------------------------- |
92 | ## test adding a SCALAR |
52e8a34c |
93 | |
c46b802b |
94 | ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet'); |
52e8a34c |
95 | |
96 | lives_ok { |
c46b802b |
97 | Foo->meta->add_package_symbol('$baz' => 10); |
98 | } '... created $Foo::baz successfully'; |
99 | |
100 | ok(defined($Foo::{baz}), '... the $baz slot was created successfully'); |
101 | ok(Foo->meta->has_package_symbol('$baz'), '... the meta agrees'); |
52e8a34c |
102 | |
c46b802b |
103 | ok(!Foo->meta->has_package_symbol('@baz'), '... ARRAY shouldnt have been created too'); |
104 | ok(!Foo->meta->has_package_symbol('%baz'), '... HASH shouldnt have been created too'); |
105 | ok(!Foo->meta->has_package_symbol('&baz'), '... CODE shouldnt have been created too'); |
106 | |
107 | is(${Foo->meta->get_package_symbol('$baz')}, 10, '... got the right value back'); |
52e8a34c |
108 | |
109 | { |
110 | no strict 'refs'; |
c46b802b |
111 | ${'Foo::baz'} = 1; |
52e8a34c |
112 | |
c46b802b |
113 | is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly'); |
114 | is(${Foo->meta->get_package_symbol('$baz')}, 1, '... the meta agrees'); |
52e8a34c |
115 | } |
116 | |
c46b802b |
117 | # ---------------------------------------------------------------------- |
118 | ## test adding a CODE |
119 | |
120 | ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet'); |
52e8a34c |
121 | |
122 | lives_ok { |
c46b802b |
123 | Foo->meta->add_package_symbol('&funk' => sub { "Foo::funk" }); |
124 | } '... created &Foo::funk successfully'; |
125 | |
126 | ok(defined($Foo::{funk}), '... the &funk slot was created successfully'); |
127 | ok(Foo->meta->has_package_symbol('&funk'), '... the meta agrees'); |
52e8a34c |
128 | |
c46b802b |
129 | ok(!Foo->meta->has_package_symbol('$funk'), '... SCALAR shouldnt have been created too'); |
130 | ok(!Foo->meta->has_package_symbol('@funk'), '... ARRAY shouldnt have been created too'); |
131 | ok(!Foo->meta->has_package_symbol('%funk'), '... HASH shouldnt have been created too'); |
52e8a34c |
132 | |
133 | { |
134 | no strict 'refs'; |
c46b802b |
135 | ok(defined &{'Foo::funk'}, '... our &funk exists'); |
136 | } |
137 | |
138 | is(Foo->funk(), 'Foo::funk', '... got the right value from the function'); |
139 | |
140 | # ---------------------------------------------------------------------- |
141 | ## test multiple slots in the glob |
142 | |
143 | my $ARRAY = [ 1, 2, 3 ]; |
144 | my $CODE = sub { "Foo::foo" }; |
145 | |
146 | lives_ok { |
147 | Foo->meta->add_package_symbol('@foo' => $ARRAY); |
148 | } '... created @Foo::foo successfully'; |
149 | |
150 | ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot was added successfully'); |
151 | is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
152 | |
153 | lives_ok { |
154 | Foo->meta->add_package_symbol('&foo' => $CODE); |
155 | } '... created &Foo::foo successfully'; |
156 | |
157 | ok(Foo->meta->has_package_symbol('&foo'), '... the meta agrees'); |
158 | is(Foo->meta->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
159 | |
160 | lives_ok { |
161 | Foo->meta->add_package_symbol('$foo' => 'Foo::foo'); |
162 | } '... created $Foo::foo successfully'; |
163 | |
164 | ok(Foo->meta->has_package_symbol('$foo'), '... the meta agrees'); |
165 | my $SCALAR = Foo->meta->get_package_symbol('$foo'); |
166 | is($$SCALAR, 'Foo::foo', '... got the right scalar value back'); |
167 | |
168 | { |
169 | no strict 'refs'; |
170 | is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar'); |
52e8a34c |
171 | } |
172 | |
173 | lives_ok { |
58d75218 |
174 | Foo->meta->remove_package_symbol('%foo'); |
52e8a34c |
175 | } '... removed %Foo::foo successfully'; |
176 | |
c46b802b |
177 | ok(!Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully'); |
178 | ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists'); |
179 | ok(Foo->meta->has_package_symbol('&foo'), '... the &foo slot still exists'); |
180 | ok(Foo->meta->has_package_symbol('$foo'), '... the $foo slot still exists'); |
181 | |
182 | is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
183 | is(Foo->meta->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo'); |
184 | is(Foo->meta->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo'); |
185 | |
186 | { |
187 | no strict 'refs'; |
188 | ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully'); |
189 | ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed'); |
190 | ok(defined(*{"Foo::foo"}{CODE}), '... the &foo slot has NOT been removed'); |
d852f4d2 |
191 | ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed'); |
c46b802b |
192 | } |
193 | |
194 | lives_ok { |
195 | Foo->meta->remove_package_symbol('&foo'); |
196 | } '... removed &Foo::foo successfully'; |
197 | |
198 | ok(!Foo->meta->has_package_symbol('&foo'), '... the &foo slot no longer exists'); |
199 | |
200 | ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists'); |
201 | ok(Foo->meta->has_package_symbol('$foo'), '... the $foo slot still exists'); |
202 | |
203 | is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
204 | is(Foo->meta->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo'); |
205 | |
206 | { |
207 | no strict 'refs'; |
208 | ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully'); |
209 | ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed'); |
210 | ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed'); |
d852f4d2 |
211 | ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed'); |
212 | } |
213 | |
214 | lives_ok { |
215 | Foo->meta->remove_package_symbol('$foo'); |
216 | } '... removed $Foo::foo successfully'; |
217 | |
218 | ok(!Foo->meta->has_package_symbol('$foo'), '... the $foo slot no longer exists'); |
219 | |
220 | ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists'); |
221 | |
222 | is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo'); |
223 | |
224 | { |
225 | no strict 'refs'; |
226 | ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully'); |
227 | ok(!defined(*{"Foo::foo"}{CODE}), '... the &foo slot has now been removed'); |
228 | ok(!defined(${"Foo::foo"}), '... the $foo slot has now been removed'); |
229 | ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed'); |
c46b802b |
230 | } |
231 | |
52e8a34c |
232 | |
233 | # check some errors |
234 | |
235 | dies_ok { |
58d75218 |
236 | Foo->meta->add_package_symbol('bar'); |
52e8a34c |
237 | } '... no sigil for bar'; |
238 | |
239 | dies_ok { |
58d75218 |
240 | Foo->meta->remove_package_symbol('bar'); |
52e8a34c |
241 | } '... no sigil for bar'; |
242 | |
243 | dies_ok { |
58d75218 |
244 | Foo->meta->get_package_symbol('bar'); |
52e8a34c |
245 | } '... no sigil for bar'; |
246 | |
247 | dies_ok { |
58d75218 |
248 | Foo->meta->has_package_symbol('bar'); |
52e8a34c |
249 | } '... no sigil for bar'; |