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