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