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