Commit | Line | Data |
52e8a34c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 34; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Class::MOP', ':universal'); |
11 | } |
12 | |
13 | { |
14 | package Foo; |
15 | } |
16 | |
17 | ok(!defined($Foo::{foo}), '... the %foo slot has not been created yet'); |
18 | ok(!Foo->meta->has_package_variable('%foo'), '... the meta agrees'); |
19 | |
20 | lives_ok { |
21 | Foo->meta->add_package_variable('%foo' => { one => 1 }); |
22 | } '... created %Foo::foo successfully'; |
23 | |
24 | ok(defined($Foo::{foo}), '... the %foo slot was created successfully'); |
25 | ok(Foo->meta->has_package_variable('%foo'), '... the meta agrees'); |
26 | |
27 | { |
28 | no strict 'refs'; |
29 | ok(exists ${'Foo::foo'}{one}, '... our %foo was initialized correctly'); |
30 | is(${'Foo::foo'}{one}, 1, '... our %foo was initialized correctly'); |
31 | } |
32 | |
33 | my $foo = Foo->meta->get_package_variable('%foo'); |
34 | is_deeply({ one => 1 }, $foo, '... got the right package variable back'); |
35 | |
36 | $foo->{two} = 2; |
37 | |
38 | { |
39 | no strict 'refs'; |
40 | is(\%{'Foo::foo'}, Foo->meta->get_package_variable('%foo'), '... our %foo is the same as the metas'); |
41 | |
42 | ok(exists ${'Foo::foo'}{two}, '... our %foo was updated correctly'); |
43 | is(${'Foo::foo'}{two}, 2, '... our %foo was updated correctly'); |
44 | } |
45 | |
46 | ok(!defined($Foo::{bar}), '... the @bar slot has not been created yet'); |
47 | |
48 | lives_ok { |
49 | Foo->meta->add_package_variable('@bar' => [ 1, 2, 3 ]); |
50 | } '... created @Foo::bar successfully'; |
51 | |
52 | ok(defined($Foo::{bar}), '... the @bar slot was created successfully'); |
53 | |
54 | { |
55 | no strict 'refs'; |
56 | is(scalar @{'Foo::bar'}, 3, '... our @bar was initialized correctly'); |
57 | is(${'Foo::bar'}[1], 2, '... our @bar was initialized correctly'); |
58 | } |
59 | |
60 | # now without initial value |
61 | |
62 | ok(!defined($Foo::{baz}), '... the %baz slot has not been created yet'); |
63 | |
64 | lives_ok { |
65 | Foo->meta->add_package_variable('%baz'); |
66 | } '... created %Foo::baz successfully'; |
67 | |
68 | ok(defined($Foo::{baz}), '... the %baz slot was created successfully'); |
69 | |
70 | { |
71 | no strict 'refs'; |
72 | ${'Foo::baz'}{one} = 1; |
73 | |
74 | ok(exists ${'Foo::baz'}{one}, '... our %baz was initialized correctly'); |
75 | is(${'Foo::baz'}{one}, 1, '... our %baz was initialized correctly'); |
76 | } |
77 | |
78 | ok(!defined($Foo::{bling}), '... the @bling slot has not been created yet'); |
79 | |
80 | lives_ok { |
81 | Foo->meta->add_package_variable('@bling'); |
82 | } '... created @Foo::bling successfully'; |
83 | |
84 | ok(defined($Foo::{bling}), '... the @bling slot was created successfully'); |
85 | |
86 | { |
87 | no strict 'refs'; |
88 | is(scalar @{'Foo::bling'}, 0, '... our @bling was initialized correctly'); |
89 | ${'Foo::bling'}[1] = 2; |
90 | is(${'Foo::bling'}[1], 2, '... our @bling was assigned too correctly'); |
91 | } |
92 | |
93 | lives_ok { |
94 | Foo->meta->remove_package_variable('%foo'); |
95 | } '... removed %Foo::foo successfully'; |
96 | |
97 | ok(!defined($Foo::{foo}), '... the %foo slot was removed successfully'); |
98 | |
99 | # check some errors |
100 | |
101 | dies_ok { |
102 | Foo->meta->add_package_variable('bar'); |
103 | } '... no sigil for bar'; |
104 | |
105 | dies_ok { |
106 | Foo->meta->remove_package_variable('bar'); |
107 | } '... no sigil for bar'; |
108 | |
109 | dies_ok { |
110 | Foo->meta->get_package_variable('bar'); |
111 | } '... no sigil for bar'; |
112 | |
113 | dies_ok { |
114 | Foo->meta->has_package_variable('bar'); |
115 | } '... no sigil for bar'; |
116 | |
117 | |
118 | dies_ok { |
119 | Foo->meta->get_package_variable('@.....bar'); |
120 | } '... could not fetch variable'; |