Commit | Line | Data |
38bf2a25 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use File::Spec; |
6 | use Scalar::Util 'reftype'; |
7 | use Class::MOP; |
8 | |
9 | BEGIN { |
10 | require_ok(File::Spec->catfile('examples', 'ArrayBasedStorage.pod')); |
11 | } |
12 | |
13 | { |
14 | package Foo; |
15 | |
16 | use strict; |
17 | use warnings; |
18 | use metaclass ( |
19 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
20 | ); |
21 | |
22 | Foo->meta->add_attribute('foo' => ( |
23 | accessor => 'foo', |
24 | clearer => 'clear_foo', |
25 | predicate => 'has_foo', |
26 | )); |
27 | |
28 | Foo->meta->add_attribute('bar' => ( |
29 | reader => 'get_bar', |
30 | writer => 'set_bar', |
31 | default => 'FOO is BAR' |
32 | )); |
33 | |
34 | sub new { |
35 | my $class = shift; |
36 | $class->meta->new_object(@_); |
37 | } |
38 | |
39 | package Bar; |
40 | use metaclass ( |
41 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
42 | ); |
43 | |
44 | use strict; |
45 | use warnings; |
46 | |
47 | use base 'Foo'; |
48 | |
49 | Bar->meta->add_attribute('baz' => ( |
50 | accessor => 'baz', |
51 | predicate => 'has_baz', |
52 | )); |
53 | |
54 | package Baz; |
55 | use metaclass ( |
56 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
57 | ); |
58 | |
59 | use strict; |
60 | use warnings; |
61 | use metaclass ( |
62 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
63 | ); |
64 | |
65 | Baz->meta->add_attribute('bling' => ( |
66 | accessor => 'bling', |
67 | default => 'Baz::bling' |
68 | )); |
69 | |
70 | package Bar::Baz; |
71 | use metaclass ( |
72 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
73 | ); |
74 | |
75 | use strict; |
76 | use warnings; |
77 | |
78 | use base 'Bar', 'Baz'; |
79 | } |
80 | |
81 | my $foo = Foo->new(); |
82 | isa_ok($foo, 'Foo'); |
83 | |
84 | is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY'); |
85 | |
86 | can_ok($foo, 'foo'); |
87 | can_ok($foo, 'has_foo'); |
88 | can_ok($foo, 'get_bar'); |
89 | can_ok($foo, 'set_bar'); |
90 | can_ok($foo, 'clear_foo'); |
91 | |
92 | ok(!$foo->has_foo, '... Foo::foo is not defined yet'); |
93 | is($foo->foo(), undef, '... Foo::foo is not defined yet'); |
94 | is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized'); |
95 | |
96 | $foo->foo('This is Foo'); |
97 | |
98 | ok($foo->has_foo, '... Foo::foo is defined now'); |
99 | is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"'); |
100 | |
101 | $foo->clear_foo; |
102 | |
103 | ok(!$foo->has_foo, '... Foo::foo is not defined anymore'); |
104 | is($foo->foo(), undef, '... Foo::foo is not defined anymore'); |
105 | |
106 | $foo->set_bar(42); |
107 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
108 | |
109 | my $foo2 = Foo->new(); |
110 | isa_ok($foo2, 'Foo'); |
111 | |
112 | is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY'); |
113 | |
114 | ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); |
115 | is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); |
116 | is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); |
117 | |
118 | $foo2->set_bar('DONT PANIC'); |
119 | is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC'); |
120 | |
121 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
122 | |
123 | # now Bar ... |
124 | |
125 | my $bar = Bar->new(); |
126 | isa_ok($bar, 'Bar'); |
127 | isa_ok($bar, 'Foo'); |
128 | |
129 | is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY'); |
130 | |
131 | can_ok($bar, 'foo'); |
132 | can_ok($bar, 'has_foo'); |
133 | can_ok($bar, 'get_bar'); |
134 | can_ok($bar, 'set_bar'); |
135 | can_ok($bar, 'baz'); |
136 | can_ok($bar, 'has_baz'); |
137 | |
138 | ok(!$bar->has_foo, '... Bar::foo is not defined yet'); |
139 | is($bar->foo(), undef, '... Bar::foo is not defined yet'); |
140 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
141 | ok(!$bar->has_baz, '... Bar::baz is not defined yet'); |
142 | is($bar->baz(), undef, '... Bar::baz is not defined yet'); |
143 | |
144 | $bar->foo('This is Bar::foo'); |
145 | |
146 | ok($bar->has_foo, '... Bar::foo is defined now'); |
147 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
148 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
149 | |
150 | $bar->baz('This is Bar::baz'); |
151 | |
152 | ok($bar->has_baz, '... Bar::baz is defined now'); |
153 | is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"'); |
154 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
155 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
156 | |
157 | # now Baz ... |
158 | |
159 | my $baz = Bar::Baz->new(); |
160 | isa_ok($baz, 'Bar::Baz'); |
161 | isa_ok($baz, 'Bar'); |
162 | isa_ok($baz, 'Foo'); |
163 | isa_ok($baz, 'Baz'); |
164 | |
165 | is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY'); |
166 | |
167 | can_ok($baz, 'foo'); |
168 | can_ok($baz, 'has_foo'); |
169 | can_ok($baz, 'get_bar'); |
170 | can_ok($baz, 'set_bar'); |
171 | can_ok($baz, 'baz'); |
172 | can_ok($baz, 'has_baz'); |
173 | can_ok($baz, 'bling'); |
174 | |
175 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
176 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
177 | |
178 | ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet'); |
179 | is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet'); |
180 | ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet'); |
181 | is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet'); |
182 | |
183 | $baz->foo('This is Bar::Baz::foo'); |
184 | |
185 | ok($baz->has_foo, '... Bar::Baz::foo is defined now'); |
186 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
187 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
188 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
189 | |
190 | $baz->baz('This is Bar::Baz::baz'); |
191 | |
192 | ok($baz->has_baz, '... Bar::Baz::baz is defined now'); |
193 | is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"'); |
194 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
195 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
196 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
197 | |
198 | Foo->meta->add_attribute( forgotten => is => "rw" ); |
199 | |
200 | my $new_baz = Bar::Baz->new; |
201 | |
202 | cmp_ok( scalar(@$new_baz), ">", scalar(@$baz), "additional slot due to refreshed meta instance" ); |
203 | |
204 | done_testing; |