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