8 use Class::MOP::Package;
11 isnt( exception { Class::MOP::Package->get_all_package_symbols }, undef, q{... can't call get_all_package_symbols() as a class method} );
12 isnt( exception { Class::MOP::Package->name }, undef, q{... can't call name() as a class method} );
17 use constant SOME_CONSTANT => 1;
19 sub meta { Class::MOP::Package->initialize('Foo') }
22 # ----------------------------------------------------------------------
23 ## tests adding a HASH
25 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
26 ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
27 ok(!defined($Foo::{foo}), '... checking doesn\' vivify');
30 Foo->meta->add_package_symbol('%foo' => { one => 1 });
31 }, undef, '... created %Foo::foo successfully' );
33 # ... scalar should NOT be created here
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');
39 ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
40 ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
46 ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly');
47 is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly');
50 my $foo = Foo->meta->get_package_symbol('%foo');
51 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
53 # ... make sure changes propogate up
59 is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
61 ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
62 is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');
65 # ----------------------------------------------------------------------
66 ## test adding an ARRAY
68 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
71 Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
72 }, undef, '... created @Foo::bar successfully' );
74 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
75 ok(Foo->meta->has_package_symbol('@bar'), '... the meta agrees');
77 # ... why does this not work ...
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');
83 # check the value itself
87 is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
88 is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
91 # ----------------------------------------------------------------------
92 ## test adding a SCALAR
94 ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
97 Foo->meta->add_package_symbol('$baz' => 10);
98 }, undef, '... created $Foo::baz successfully' );
100 ok(defined($Foo::{baz}), '... the $baz slot was created successfully');
101 ok(Foo->meta->has_package_symbol('$baz'), '... the meta agrees');
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');
107 is(${Foo->meta->get_package_symbol('$baz')}, 10, '... got the right value back');
113 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
114 is(${Foo->meta->get_package_symbol('$baz')}, 1, '... the meta agrees');
117 # ----------------------------------------------------------------------
118 ## test adding a CODE
120 ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
123 Foo->meta->add_package_symbol('&funk' => sub { "Foo::funk" });
124 }, undef, '... created &Foo::funk successfully' );
126 ok(defined($Foo::{funk}), '... the &funk slot was created successfully');
127 ok(Foo->meta->has_package_symbol('&funk'), '... the meta agrees');
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');
135 ok(defined &{'Foo::funk'}, '... our &funk exists');
138 is(Foo->funk(), 'Foo::funk', '... got the right value from the function');
140 # ----------------------------------------------------------------------
141 ## test multiple slots in the glob
143 my $ARRAY = [ 1, 2, 3 ];
144 my $CODE = sub { "Foo::foo" };
147 Foo->meta->add_package_symbol('@foo' => $ARRAY);
148 }, undef, '... created @Foo::foo successfully' );
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');
154 Foo->meta->add_package_symbol('&foo' => $CODE);
155 }, undef, '... created &Foo::foo successfully' );
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');
161 Foo->meta->add_package_symbol('$foo' => 'Foo::foo');
162 }, undef, '... created $Foo::foo successfully' );
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');
170 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
174 Foo->meta->remove_package_symbol('%foo');
175 }, undef, '... removed %Foo::foo successfully' );
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');
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');
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');
191 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
195 Foo->meta->remove_package_symbol('&foo');
196 }, undef, '... removed &Foo::foo successfully' );
198 ok(!Foo->meta->has_package_symbol('&foo'), '... the &foo slot no longer exists');
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');
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');
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');
211 ok(defined(${"Foo::foo"}), '... the $foo slot has NOT been removed');
215 Foo->meta->remove_package_symbol('$foo');
216 }, undef, '... removed $Foo::foo successfully' );
218 ok(!Foo->meta->has_package_symbol('$foo'), '... the $foo slot no longer exists');
220 ok(Foo->meta->has_package_symbol('@foo'), '... the @foo slot still exists');
222 is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for @Foo::foo');
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');
232 # get_all_package_symbols
235 my $syms = Foo->meta->get_all_package_symbols;
237 [ sort keys %{ $syms } ],
238 [ sort Foo->meta->list_all_package_symbols ],
239 '... the fetched symbols are the same as the listed ones'
244 my $syms = Foo->meta->get_all_package_symbols('CODE');
247 [ sort keys %{ $syms } ],
248 [ sort Foo->meta->list_all_package_symbols('CODE') ],
249 '... the fetched symbols are the same as the listed ones'
252 foreach my $symbol (keys %{ $syms }) {
253 is($syms->{$symbol}, Foo->meta->get_package_symbol('&' . $symbol), '... got the right symbol');
258 Foo->meta->add_package_symbol('%zork');
260 my $syms = Foo->meta->get_all_package_symbols('HASH');
263 [ sort keys %{ $syms } ],
264 [ sort Foo->meta->list_all_package_symbols('HASH') ],
265 '... the fetched symbols are the same as the listed ones'
268 foreach my $symbol (keys %{ $syms }) {
269 is($syms->{$symbol}, Foo->meta->get_package_symbol('%' . $symbol), '... got the right symbol');
275 { zork => \%Foo::zork },
276 "got the right ones",