cleanup and more tests;
[gitmo/Class-MOP.git] / t / 012_package_variables.t
CommitLineData
52e8a34c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c46b802b 6use Test::More tests => 80;
52e8a34c 7use Test::Exception;
8
9BEGIN {
aa448b16 10 use_ok('Class::MOP');
52e8a34c 11}
12
13{
14 package Foo;
aa448b16 15 use metaclass;
52e8a34c 16}
17
c46b802b 18=pod
19
20This is the same test as 080_meta_package.t just here
21we call all the methods through Class::MOP::Class.
22
23=cut
24
25# ----------------------------------------------------------------------
26## tests adding a HASH
27
52e8a34c 28ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
58d75218 29ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
52e8a34c 30
31lives_ok {
58d75218 32 Foo->meta->add_package_symbol('%foo' => { one => 1 });
52e8a34c 33} '... created %Foo::foo successfully';
34
c46b802b 35# ... scalar should NOT be created here
36
37ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
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');
40
52e8a34c 41ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
58d75218 42ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
52e8a34c 43
c46b802b 44# check the value ...
45
52e8a34c 46{
47 no strict 'refs';
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');
52e8a34c 53is_deeply({ one => 1 }, $foo, '... got the right package variable back');
54
c46b802b 55# ... make sure changes propogate up
56
52e8a34c 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');
52e8a34c 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
52e8a34c 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 ]);
52e8a34c 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
52e8a34c 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
52e8a34c 95
c46b802b 96ok(!defined($Foo::{baz}), '... the $baz slot has not been created yet');
52e8a34c 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');
52e8a34c 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');
52e8a34c 110
111{
112 no strict 'refs';
c46b802b 113 ${'Foo::baz'} = 1;
52e8a34c 114
c46b802b 115 is(${'Foo::baz'}, 1, '... our $baz was assigned to correctly');
116 is(${Foo->meta->get_package_symbol('$baz')}, 1, '... the meta agrees');
52e8a34c 117}
118
c46b802b 119# ----------------------------------------------------------------------
120## test adding a CODE
121
122ok(!defined($Foo::{funk}), '... the &funk slot has not been created yet');
52e8a34c 123
124lives_ok {
c46b802b 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');
52e8a34c 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');
52e8a34c 134
135{
136 no strict 'refs';
c46b802b 137 ok(defined &{'Foo::funk'}, '... our &funk exists');
138}
139
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');
161
162lives_ok {
163 Foo->meta->add_package_symbol('$foo' => 'Foo::foo');
164} '... created $Foo::foo successfully';
165
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');
169
170{
171 no strict 'refs';
172 is(${'Foo::foo'}, 'Foo::foo', '... got the right value from the scalar');
52e8a34c 173}
174
175lives_ok {
58d75218 176 Foo->meta->remove_package_symbol('%foo');
52e8a34c 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');
187
188{
189 no strict 'refs';
190 ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
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');
193 ok(defined(*{"Foo::foo"}{SCALAR}), '... the $foo slot has NOT been removed');
194}
195
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');
213 ok(defined(*{"Foo::foo"}{SCALAR}), '... the $foo slot has NOT been removed');
214}
215
52e8a34c 216
217# check some errors
218
219dies_ok {
58d75218 220 Foo->meta->add_package_symbol('bar');
52e8a34c 221} '... no sigil for bar';
222
223dies_ok {
58d75218 224 Foo->meta->remove_package_symbol('bar');
52e8a34c 225} '... no sigil for bar';
226
227dies_ok {
58d75218 228 Foo->meta->get_package_symbol('bar');
52e8a34c 229} '... no sigil for bar';
230
231dies_ok {
58d75218 232 Foo->meta->has_package_symbol('bar');
52e8a34c 233} '... no sigil for bar';