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