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