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