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