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'); |
f892c0f0 |
12 | require_ok(File::Spec->catdir('examples', 'ArrayBasedStorage.pod')); |
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; |
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; |
8d2d4c67 |
54 | |
f892c0f0 |
55 | use strict; |
56 | use warnings; |
8d2d4c67 |
57 | use metaclass ( |
c23184fc |
58 | 'instance_metaclass' => 'ArrayBasedStorage::Instance', |
f892c0f0 |
59 | ); |
8d2d4c67 |
60 | |
f892c0f0 |
61 | Baz->meta->add_attribute('bling' => ( |
62 | accessor => 'bling', |
63 | default => 'Baz::bling' |
8d2d4c67 |
64 | )); |
65 | |
f892c0f0 |
66 | package Bar::Baz; |
8d2d4c67 |
67 | |
f892c0f0 |
68 | use strict; |
69 | use warnings; |
8d2d4c67 |
70 | |
71 | use base 'Bar', 'Baz'; |
f892c0f0 |
72 | } |
73 | |
74 | my $foo = Foo->new(); |
75 | isa_ok($foo, 'Foo'); |
76 | |
88dd563c |
77 | is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY'); |
78 | |
f892c0f0 |
79 | can_ok($foo, 'foo'); |
80 | can_ok($foo, 'has_foo'); |
81 | can_ok($foo, 'get_bar'); |
82 | can_ok($foo, 'set_bar'); |
8d2d4c67 |
83 | can_ok($foo, 'clear_foo'); |
f892c0f0 |
84 | |
85 | ok(!$foo->has_foo, '... Foo::foo is not defined yet'); |
86 | is($foo->foo(), undef, '... Foo::foo is not defined yet'); |
87 | is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized'); |
88 | |
89 | $foo->foo('This is Foo'); |
90 | |
91 | ok($foo->has_foo, '... Foo::foo is defined now'); |
92 | is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"'); |
93 | |
8d2d4c67 |
94 | $foo->clear_foo; |
95 | |
96 | ok(!$foo->has_foo, '... Foo::foo is not defined anymore'); |
97 | is($foo->foo(), undef, '... Foo::foo is not defined anymore'); |
98 | |
f892c0f0 |
99 | $foo->set_bar(42); |
100 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
101 | |
102 | my $foo2 = Foo->new(); |
103 | isa_ok($foo2, 'Foo'); |
104 | |
88dd563c |
105 | is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY'); |
106 | |
f892c0f0 |
107 | ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); |
108 | is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); |
109 | is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); |
110 | |
111 | $foo2->set_bar('DONT PANIC'); |
112 | is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC'); |
113 | |
114 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
115 | |
116 | # now Bar ... |
117 | |
118 | my $bar = Bar->new(); |
119 | isa_ok($bar, 'Bar'); |
120 | isa_ok($bar, 'Foo'); |
121 | |
88dd563c |
122 | is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY'); |
123 | |
f892c0f0 |
124 | can_ok($bar, 'foo'); |
125 | can_ok($bar, 'has_foo'); |
126 | can_ok($bar, 'get_bar'); |
127 | can_ok($bar, 'set_bar'); |
128 | can_ok($bar, 'baz'); |
129 | can_ok($bar, 'has_baz'); |
130 | |
131 | ok(!$bar->has_foo, '... Bar::foo is not defined yet'); |
132 | is($bar->foo(), undef, '... Bar::foo is not defined yet'); |
133 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
134 | ok(!$bar->has_baz, '... Bar::baz is not defined yet'); |
135 | is($bar->baz(), undef, '... Bar::baz is not defined yet'); |
136 | |
137 | $bar->foo('This is Bar::foo'); |
138 | |
139 | ok($bar->has_foo, '... Bar::foo is defined now'); |
140 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
141 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
142 | |
143 | $bar->baz('This is Bar::baz'); |
144 | |
145 | ok($bar->has_baz, '... Bar::baz is defined now'); |
146 | is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"'); |
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 | # now Baz ... |
151 | |
152 | my $baz = Bar::Baz->new(); |
153 | isa_ok($baz, 'Bar::Baz'); |
154 | isa_ok($baz, 'Bar'); |
155 | isa_ok($baz, 'Foo'); |
156 | isa_ok($baz, 'Baz'); |
157 | |
88dd563c |
158 | is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY'); |
159 | |
f892c0f0 |
160 | can_ok($baz, 'foo'); |
161 | can_ok($baz, 'has_foo'); |
162 | can_ok($baz, 'get_bar'); |
163 | can_ok($baz, 'set_bar'); |
164 | can_ok($baz, 'baz'); |
165 | can_ok($baz, 'has_baz'); |
166 | can_ok($baz, 'bling'); |
167 | |
168 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
169 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
170 | |
171 | ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet'); |
172 | is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet'); |
173 | ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet'); |
174 | is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet'); |
175 | |
176 | $baz->foo('This is Bar::Baz::foo'); |
177 | |
178 | ok($baz->has_foo, '... Bar::Baz::foo is defined now'); |
179 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
180 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
181 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
182 | |
183 | $baz->baz('This is Bar::Baz::baz'); |
184 | |
185 | ok($baz->has_baz, '... Bar::Baz::baz is defined now'); |
186 | is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"'); |
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 | |