Factor a c function doing all the hard work out of get_all_package_symbols.
[gitmo/Class-MOP.git] / t / 080_meta_package.t
CommitLineData
6d5355c3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
3609af79 6use Test::More tests => 97;
6d5355c3 7use Test::Exception;
8
988fb42e 9use Class::MOP;
10use Class::MOP::Package;
11
12
13dies_ok { Class::MOP::Package->get_all_package_symbols } q{... can't call get_all_package_symbols() as a class method};
14dies_ok { Class::MOP::Package->name } q{... can't call name() as a class method};
6d5355c3 15
16{
17 package Foo;
3609af79 18
19 use constant SOME_CONSTANT => 1;
6d5355c3 20
21 sub meta { Class::MOP::Package->initialize('Foo') }
22}
23
c46b802b 24# ----------------------------------------------------------------------
25## tests adding a HASH
26
6d5355c3 27ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
58d75218 28ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
3609af79 29ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
6d5355c3 30
31lives_ok {
58d75218 32 Foo->meta->add_package_symbol('%foo' => { one => 1 });
6d5355c3 33} '... created %Foo::foo successfully';
34
c46b802b 35# ... scalar should NOT be created here
36
c20522bd 37ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
c46b802b 38ok(!Foo->meta->has_package_symbol('@foo'), '... ARRAY shouldnt have been created too');
39ok(!Foo->meta->has_package_symbol('&foo'), '... CODE shouldnt have been created too');
c20522bd 40
6d5355c3 41ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
58d75218 42ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
6d5355c3 43
c46b802b 44# check the value ...
c20522bd 45
46{
47 no strict 'refs';
6d5355c3 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 52my $foo = Foo->meta->get_package_symbol('%foo');
6d5355c3 53is_deeply({ one => 1 }, $foo, '... got the right package variable back');
54
c46b802b 55# ... make sure changes propogate up
56
6d5355c3 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');
6d5355c3 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
6d5355c3 70ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
71
72lives_ok {
58d75218 73 Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
6d5355c3 74} '... created @Foo::bar successfully';
75
76ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
c46b802b 77ok(Foo->meta->has_package_symbol('@bar'), '... the meta agrees');
78
79# ... why does this not work ...
80
81ok(!Foo->meta->has_package_symbol('$bar'), '... SCALAR shouldnt have been created too');
82ok(!Foo->meta->has_package_symbol('%bar'), '... HASH shouldnt have been created too');
83ok(!Foo->meta->has_package_symbol('&bar'), '... CODE shouldnt have been created too');
84
85# check the value itself
6d5355c3 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
6d5355c3 95
c46b802b 96ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
6d5355c3 97
98lives_ok {
c46b802b 99 Foo->meta->add_package_symbol('$baz' => 10);
100} '... created $Foo::baz successfully';
101
102ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
103ok(Foo->meta->has_package_symbol('$baz'), '... the meta agrees');
6d5355c3 104
c46b802b 105ok(!Foo->meta->has_package_symbol('@baz'), '... ARRAY shouldnt have been created too');
106ok(!Foo->meta->has_package_symbol('%baz'), '... HASH shouldnt have been created too');
107ok(!Foo->meta->has_package_symbol('&baz'), '... CODE shouldnt have been created too');
108
109is(${Foo->meta->get_package_symbol('$baz')}, 10, '... got the right value back');
6d5355c3 110
111{
112 no strict 'refs';
c46b802b 113 ${'Foo::baz'} = 1;
114
115 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
116 is(${Foo->meta->get_package_symbol('$baz')}, 1, '... the meta agrees');
117}
118
119# ----------------------------------------------------------------------
120## test adding a CODE
121
122ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
123
124lives_ok {
125 Foo->meta->add_package_symbol('&funk' => sub { "Foo::funk" });
126} '... created &Foo::funk successfully';
127
128ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
129ok(Foo->meta->has_package_symbol('&funk'), '... the meta agrees');
6d5355c3 130
c46b802b 131ok(!Foo->meta->has_package_symbol('$funk'), '... SCALAR shouldnt have been created too');
132ok(!Foo->meta->has_package_symbol('@funk'), '... ARRAY shouldnt have been created too');
133ok(!Foo->meta->has_package_symbol('%funk'), '... HASH shouldnt have been created too');
134
135{
136 no strict 'refs';
137 ok(defined &{'Foo::funk'}, '... our &funk exists');
6d5355c3 138}
139
c46b802b 140is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
141
142# ----------------------------------------------------------------------
143## test multiple slots in the glob
144
145my $ARRAY = [ 1, 2, 3 ];
146my $CODE = sub { "Foo::foo" };
147
148lives_ok {
149 Foo->meta->add_package_symbol('@foo' => $ARRAY);
150} '... created @Foo::foo successfully';
151
152ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot was added successfully');
153is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
154
155lives_ok {
156 Foo->meta->add_package_symbol('&foo' => $CODE);
157} '... created &Foo::foo successfully';
158
159ok(Foo->meta->has_package_symbol('&foo'), '... the meta agrees');
160is(Foo->meta->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
6d5355c3 161
162lives_ok {
c46b802b 163 Foo->meta->add_package_symbol('$foo' => 'Foo::foo');
164} '... created $Foo::foo successfully';
6d5355c3 165
c46b802b 166ok(Foo->meta->has_package_symbol('$foo'), '... the meta agrees');
167my $SCALAR = Foo->meta->get_package_symbol('$foo');
168is($$SCALAR, 'Foo::foo', '... got the right scalar value back');
6d5355c3 169
170{
171 no strict 'refs';
c46b802b 172 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
6d5355c3 173}
174
175lives_ok {
58d75218 176 Foo->meta->remove_package_symbol('%foo');
6d5355c3 177} '... removed %Foo::foo successfully';
178
c46b802b 179ok(!Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
180ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists');
181ok(Foo->meta->has_package_symbol('&foo'), '... the &foo slot still exists');
182ok(Foo->meta->has_package_symbol('$foo'), '... the $foo slot still exists');
183
184is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
185is(Foo->meta->get_package_symbol('&foo'), $CODE, '... got the right value for &Foo::foo');
186is(Foo->meta->get_package_symbol('$foo'), $SCALAR, '... got the right value for $Foo::foo');
6d5355c3 187
c20522bd 188{
189 no strict 'refs';
190 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
c46b802b 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');
c20522bd 194}
195
c46b802b 196lives_ok {
197 Foo->meta->remove_package_symbol('&foo');
198} '... removed &Foo::foo successfully';
199
200ok(!Foo->meta->has_package_symbol('&foo'), '... the &foo slot no longer exists');
201
202ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists');
203ok(Foo->meta->has_package_symbol('$foo'), '... the $foo slot still exists');
204
205is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
206is(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
216lives_ok {
217 Foo->meta->remove_package_symbol('$foo');
218} '... removed $Foo::foo successfully';
219
220ok(!Foo->meta->has_package_symbol('$foo'), '... the $foo slot no longer exists');
221
222ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists');
223
224is(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
ae234dc6 234# get_all_package_symbols
235
236{
237 my %syms = Foo->meta->get_all_package_symbols;
ae234dc6 238 is_deeply(
239 [ sort keys %syms ],
240 [ sort Foo->meta->list_all_package_symbols ],
241 '... the fetched symbols are the same as the listed ones'
242 );
243}
244
245{
246 my %syms = Foo->meta->get_all_package_symbols('CODE');
247
248 is_deeply(
249 [ sort keys %syms ],
250 [ sort Foo->meta->list_all_package_symbols('CODE') ],
251 '... the fetched symbols are the same as the listed ones'
252 );
253
254 foreach my $symbol (keys %syms) {
255 is($syms{$symbol}, Foo->meta->get_package_symbol('&' . $symbol), '... got the right symbol');
256 }
257}
c46b802b 258
3609af79 259{
260 Foo->meta->add_package_symbol('%zork');
261
262 my %syms = Foo->meta->get_all_package_symbols('HASH');
263
264 is_deeply(
265 [ sort keys %syms ],
266 [ sort Foo->meta->list_all_package_symbols('HASH') ],
267 '... the fetched symbols are the same as the listed ones'
268 );
269
270 foreach my $symbol (keys %syms) {
271 is($syms{$symbol}, Foo->meta->get_package_symbol('%' . $symbol), '... got the right symbol');
272 }
273
274 no warnings 'once';
275 is_deeply(
276 \%syms,
277 { zork => \%Foo::zork },
278 "got the right ones",
279 );
280}
6d5355c3 281# check some errors
282
283dies_ok {
58d75218 284 Foo->meta->add_package_symbol('bar');
6d5355c3 285} '... no sigil for bar';
286
287dies_ok {
58d75218 288 Foo->meta->remove_package_symbol('bar');
6d5355c3 289} '... no sigil for bar';
290
291dies_ok {
58d75218 292 Foo->meta->get_package_symbol('bar');
6d5355c3 293} '... no sigil for bar';
294
295dies_ok {
58d75218 296 Foo->meta->has_package_symbol('bar');
6d5355c3 297} '... no sigil for bar';