Commit | Line | Data |
6d5355c3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
c20522bd |
6 | use Test::More tests => 43; |
6d5355c3 |
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'); |
58d75218 |
21 | ok(!Foo->meta->has_package_symbol('%foo'), '... the meta agrees'); |
6d5355c3 |
22 | |
23 | lives_ok { |
58d75218 |
24 | Foo->meta->add_package_symbol('%foo' => { one => 1 }); |
6d5355c3 |
25 | } '... created %Foo::foo successfully'; |
26 | |
c20522bd |
27 | ok(!Foo->meta->has_package_symbol('$foo'), '... SCALAR shouldnt have been created too'); |
28 | |
6d5355c3 |
29 | ok(defined($Foo::{foo}), '... the %foo slot was created successfully'); |
58d75218 |
30 | ok(Foo->meta->has_package_symbol('%foo'), '... the meta agrees'); |
6d5355c3 |
31 | |
32 | { |
33 | no strict 'refs'; |
c20522bd |
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'; |
6d5355c3 |
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 |
52 | my $foo = Foo->meta->get_package_symbol('%foo'); |
6d5355c3 |
53 | is_deeply({ one => 1 }, $foo, '... got the right package variable back'); |
54 | |
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'); |
6d5355c3 |
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 { |
58d75218 |
68 | Foo->meta->add_package_symbol('@bar' => [ 1, 2, 3 ]); |
6d5355c3 |
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 { |
58d75218 |
84 | Foo->meta->add_package_symbol('%baz'); |
6d5355c3 |
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 { |
58d75218 |
100 | Foo->meta->add_package_symbol('@bling'); |
6d5355c3 |
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 { |
58d75218 |
113 | Foo->meta->remove_package_symbol('%foo'); |
6d5355c3 |
114 | } '... removed %Foo::foo successfully'; |
115 | |
58d75218 |
116 | ok(Foo->meta->has_package_symbol('%foo'), '... the %foo slot was removed successfully'); |
6d5355c3 |
117 | |
c20522bd |
118 | { |
119 | no strict 'refs'; |
120 | ok(!defined(*{"Foo::foo"}{HASH}), '... the %foo slot has been removed successfully'); |
121 | } |
122 | |
6d5355c3 |
123 | # check some errors |
124 | |
125 | dies_ok { |
58d75218 |
126 | Foo->meta->add_package_symbol('bar'); |
6d5355c3 |
127 | } '... no sigil for bar'; |
128 | |
129 | dies_ok { |
58d75218 |
130 | Foo->meta->remove_package_symbol('bar'); |
6d5355c3 |
131 | } '... no sigil for bar'; |
132 | |
133 | dies_ok { |
58d75218 |
134 | Foo->meta->get_package_symbol('bar'); |
6d5355c3 |
135 | } '... no sigil for bar'; |
136 | |
137 | dies_ok { |
58d75218 |
138 | Foo->meta->has_package_symbol('bar'); |
6d5355c3 |
139 | } '... no sigil for bar'; |