broken-tests
[gitmo/Class-MOP.git] / t / 080_meta_package.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 43;
7 use Test::Exception;
8
9 BEGIN {
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
20 ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet');
21 ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
22
23 lives_ok {
24     Foo->meta->add_package_symbol('%foo' => { one => 1 });
25 } '... created %Foo::foo successfully';
26
27 ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too');
28
29 ok(defined($Foo::{foo}), '... the %foo slot was created successfully');
30 ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees');
31
32 {
33     no strict 'refs';
34     ok(defined(*{"Foo::foo"}{HASH}), '... the %foo (HASH) slot was created successfully');
35
36     ok(!defined(*{"Foo::foo"}{SCALAR}), '... but the $foo slot was not created');
37     ok(!Foo->meta->has_package_symbol('$foo'), '... and the meta agrees');    
38
39     ok(!defined(*{"Foo::foo"}{ARRAY}),  '... but the @foo slot was not created');
40     ok(!Foo->meta->has_package_symbol('@foo'), '... and the meta agrees');    
41
42     ok(!defined(*{"Foo::foo"}{CODE}),   '... but the &foo slot was not created');
43     ok(!Foo->meta->has_package_symbol('&foo'), '... and the meta agrees');    
44 }
45
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
52 my $foo = Foo->meta->get_package_symbol('%foo');
53 is_deeply({ one => 1 }, $foo, '... got the right package variable back');
54
55 $foo->{two} = 2;
56
57 {
58     no strict 'refs';
59     is(\%{'Foo::foo'}, Foo->meta->get_package_symbol('%foo'), '... our %foo is the same as the metas');
60     
61     ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly');
62     is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly');    
63 }
64
65 ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet');
66
67 lives_ok {
68     Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]);
69 } '... created @Foo::bar successfully';
70
71 ok(defined($Foo::{bar}), '... the @bar slot was created successfully');
72
73 {
74     no strict 'refs';
75     is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly');
76     is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly');
77 }
78
79 # now without initial value
80
81 ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet');
82
83 lives_ok {
84     Foo->meta->add_package_symbol('%baz');
85 } '... created %Foo::baz successfully';
86
87 ok(defined($Foo::{baz}), '... the %baz slot was created successfully');
88
89 {
90     no strict 'refs';
91     ${'Foo::baz'}{one} = 1;
92
93     ok(exists ${'Foo::baz'}{one}, '... our %baz was initialized correctly');
94     is(${'Foo::baz'}{one}, 1, '... our %baz was initialized correctly');
95 }
96
97 ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet');
98
99 lives_ok {
100     Foo->meta->add_package_symbol('@bling');
101 } '... created @Foo::bling successfully';
102
103 ok(defined($Foo::{bling}), '... the @bling slot was created successfully');
104
105 {
106     no strict 'refs';
107     is(scalar @{'Foo::bling'}, 0, '... our @bling was initialized correctly');
108     ${'Foo::bling'}[1] = 2;
109     is(${'Foo::bling'}[1], 2, '... our @bling was assigned too correctly');
110 }
111
112 lives_ok {
113     Foo->meta->remove_package_symbol('%foo');
114 } '... removed %Foo::foo successfully';
115
116 ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully');
117
118 {
119     no strict 'refs';
120     ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully');
121 }
122
123 # check some errors
124
125 dies_ok {
126     Foo->meta->add_package_symbol('bar');
127 } '... no sigil for bar';
128
129 dies_ok {
130     Foo->meta->remove_package_symbol('bar');
131 } '... no sigil for bar';
132
133 dies_ok {
134     Foo->meta->get_package_symbol('bar');
135 } '... no sigil for bar';
136
137 dies_ok {
138     Foo->meta->has_package_symbol('bar');
139 } '... no sigil for bar';