Commit | Line | Data |
2eb717d5 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8768ecf3 |
6 | use Test::More tests => 71; |
2eb717d5 |
7 | use Test::Exception; |
8 | |
1d68af04 |
9 | BEGIN { |
10 | use_ok('Class::MOP'); |
2eb717d5 |
11 | } |
12 | |
13 | my $FOO_ATTR = Class::MOP::Attribute->new('$foo'); |
14 | my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => ( |
15 | accessor => 'bar' |
16 | )); |
17 | my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => ( |
18 | reader => 'get_baz', |
1d68af04 |
19 | writer => 'set_baz', |
2eb717d5 |
20 | )); |
21 | |
22286063 |
22 | my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); |
23 | |
1d68af04 |
24 | my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => ( |
25 | accessor => 'foo', |
26 | builder => 'build_foo' |
27 | )); |
28 | |
aef6c672 |
29 | is($FOO_ATTR->name, '$foo', '... got the attributes name correctly'); |
30 | is($BAR_ATTR->name, '$bar', '... got the attributes name correctly'); |
31 | is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly'); |
32 | |
2eb717d5 |
33 | { |
34 | package Foo; |
aa448b16 |
35 | use metaclass; |
2eb717d5 |
36 | |
727919c5 |
37 | my $meta = Foo->meta; |
2eb717d5 |
38 | ::lives_ok { |
39 | $meta->add_attribute($FOO_ATTR); |
40 | } '... we added an attribute to Foo successfully'; |
41 | ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute'); |
42 | ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo'); |
1d68af04 |
43 | |
2eb717d5 |
44 | ::ok(!$meta->has_method('foo'), '... no accessor created'); |
1d68af04 |
45 | |
22286063 |
46 | ::lives_ok { |
47 | $meta->add_attribute($BAR_ATTR_2); |
48 | } '... we added an attribute to Foo successfully'; |
49 | ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute'); |
1d68af04 |
50 | ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo'); |
22286063 |
51 | |
52 | ::ok(!$meta->has_method('bar'), '... no accessor created'); |
2eb717d5 |
53 | } |
54 | { |
55 | package Bar; |
56 | our @ISA = ('Foo'); |
1d68af04 |
57 | |
727919c5 |
58 | my $meta = Bar->meta; |
2eb717d5 |
59 | ::lives_ok { |
60 | $meta->add_attribute($BAR_ATTR); |
61 | } '... we added an attribute to Bar successfully'; |
62 | ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute'); |
63 | ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar'); |
64 | |
b25109b1 |
65 | my $attr = $meta->get_attribute('$bar'); |
66 | ::is($attr->get_read_method, 'bar', '... got the right read method for Bar'); |
67 | ::is($attr->get_write_method, 'bar', '... got the right write method for Bar'); |
68 | |
2eb717d5 |
69 | ::ok($meta->has_method('bar'), '... an accessor has been created'); |
1d68af04 |
70 | ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor'); |
2eb717d5 |
71 | } |
72 | { |
73 | package Baz; |
74 | our @ISA = ('Bar'); |
1d68af04 |
75 | |
727919c5 |
76 | my $meta = Baz->meta; |
2eb717d5 |
77 | ::lives_ok { |
78 | $meta->add_attribute($BAZ_ATTR); |
79 | } '... we added an attribute to Baz successfully'; |
1d68af04 |
80 | ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute'); |
2eb717d5 |
81 | ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz'); |
82 | |
b25109b1 |
83 | my $attr = $meta->get_attribute('$baz'); |
84 | ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz'); |
85 | ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz'); |
86 | |
2eb717d5 |
87 | ::ok($meta->has_method('get_baz'), '... a reader has been created'); |
88 | ::ok($meta->has_method('set_baz'), '... a writer has been created'); |
89 | |
ba38bf08 |
90 | ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor'); |
91 | ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor'); |
2eb717d5 |
92 | } |
93 | |
94 | { |
95 | my $meta = Baz->meta; |
96 | isa_ok($meta, 'Class::MOP::Class'); |
1d68af04 |
97 | |
058c1cf5 |
98 | is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"'); |
1d68af04 |
99 | is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"'); |
100 | is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"'); |
101 | |
2eb717d5 |
102 | is_deeply( |
c9e77dbb |
103 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
1d68af04 |
104 | [ |
c9e77dbb |
105 | $BAR_ATTR, |
106 | $BAZ_ATTR, |
1d68af04 |
107 | $FOO_ATTR, |
2eb717d5 |
108 | ], |
109 | '... got the right list of applicable attributes for Baz'); |
1d68af04 |
110 | |
c9e77dbb |
111 | is_deeply( |
112 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
113 | [ Bar->meta, Baz->meta, Foo->meta ], |
1d68af04 |
114 | '... got the right list of associated classes from the applicable attributes for Baz'); |
115 | |
cbd9f942 |
116 | my $attr; |
117 | lives_ok { |
118 | $attr = $meta->remove_attribute('$baz'); |
119 | } '... removed the $baz attribute successfully'; |
1d68af04 |
120 | is($attr, $BAZ_ATTR, '... got the right attribute back for Baz'); |
121 | |
122 | ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); |
123 | is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute'); |
cbd9f942 |
124 | |
125 | ok(!$meta->has_method('get_baz'), '... a reader has been removed'); |
126 | ok(!$meta->has_method('set_baz'), '... a writer has been removed'); |
127 | |
128 | is_deeply( |
c9e77dbb |
129 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
1d68af04 |
130 | [ |
c9e77dbb |
131 | $BAR_ATTR, |
1d68af04 |
132 | $FOO_ATTR, |
cbd9f942 |
133 | ], |
134 | '... got the right list of applicable attributes for Baz'); |
135 | |
c9e77dbb |
136 | is_deeply( |
137 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
138 | [ Bar->meta, Foo->meta ], |
139 | '... got the right list of associated classes from the applicable attributes for Baz'); |
140 | |
cbd9f942 |
141 | { |
142 | my $attr; |
143 | lives_ok { |
144 | $attr = Bar->meta->remove_attribute('$bar'); |
145 | } '... removed the $bar attribute successfully'; |
1d68af04 |
146 | is($attr, $BAR_ATTR, '... got the right attribute back for Bar'); |
2eb717d5 |
147 | |
1d68af04 |
148 | ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); |
2eb717d5 |
149 | |
cbd9f942 |
150 | ok(!Bar->meta->has_method('bar'), '... a accessor has been removed'); |
151 | } |
152 | |
153 | is_deeply( |
c9e77dbb |
154 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
1d68af04 |
155 | [ |
22286063 |
156 | $BAR_ATTR_2, |
1d68af04 |
157 | $FOO_ATTR, |
cbd9f942 |
158 | ], |
159 | '... got the right list of applicable attributes for Baz'); |
160 | |
c9e77dbb |
161 | is_deeply( |
162 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
22286063 |
163 | [ Foo->meta, Foo->meta ], |
c9e77dbb |
164 | '... got the right list of associated classes from the applicable attributes for Baz'); |
165 | |
22286063 |
166 | # remove attribute which is not there |
167 | my $val; |
168 | lives_ok { |
169 | $val = $meta->remove_attribute('$blammo'); |
170 | } '... attempted to remove the non-existent $blammo attribute'; |
171 | is($val, undef, '... got the right value back (undef)'); |
172 | |
cbd9f942 |
173 | } |
1d68af04 |
174 | |
175 | { |
176 | package Buzz; |
177 | use metaclass; |
178 | use Scalar::Util qw/blessed/; |
179 | |
180 | my $meta = Buzz->meta; |
181 | ::lives_ok { |
182 | $meta->add_attribute($FOO_ATTR_2); |
183 | } '... we added an attribute to Buzz successfully'; |
184 | |
185 | ::lives_ok { |
8768ecf3 |
186 | $meta->add_attribute( |
187 | Class::MOP::Attribute->new( |
188 | '$bar' => ( |
189 | accessor => 'bar', |
190 | predicate => 'has_bar', |
191 | clearer => 'clear_bar', |
192 | ) |
193 | ) |
194 | ); |
195 | } '... we added an attribute to Buzz successfully'; |
196 | |
197 | ::lives_ok { |
198 | $meta->add_attribute( |
199 | Class::MOP::Attribute->new( |
200 | '$bah' => ( |
201 | accessor => 'bah', |
202 | predicate => 'has_bah', |
203 | clearer => 'clear_bah', |
204 | default => 'BAH', |
205 | ) |
206 | ) |
207 | ); |
208 | } '... we added an attribute to Buzz successfully'; |
209 | |
210 | ::lives_ok { |
1d68af04 |
211 | $meta->add_method(build_foo => sub{ blessed shift; }); |
212 | } '... we added a method to Buzz successfully'; |
213 | } |
214 | |
215 | { |
216 | my $buzz; |
217 | ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully'; |
8768ecf3 |
218 | ::is($buzz->foo, 'Buzz', '...foo builder works as expected'); |
219 | ::ok(!$buzz->has_bar, '...bar is not set'); |
220 | ::is($buzz->bar, undef, '...bar returns undef'); |
221 | ::ok(!$buzz->has_bar, '...bar was not autovivified'); |
222 | |
223 | $buzz->bar(undef); |
224 | ::ok($buzz->has_bar, '...bar is set'); |
225 | ::is($buzz->bar, undef, '...bar is undef'); |
226 | $buzz->clear_bar; |
227 | ::ok(!$buzz->has_bar, '...bar is no longerset'); |
228 | |
229 | my $buzz2; |
230 | ::lives_ok { $buzz2 = Buzz->meta->new_object('$bar' => undef) } '...Buzz instantiated successfully'; |
231 | ::ok($buzz2->has_bar, '...bar is set'); |
232 | ::is($buzz2->bar, undef, '...bar is undef'); |
233 | |
234 | } |
235 | |
236 | { |
237 | my $buzz; |
238 | ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully'; |
239 | ::ok($buzz->has_bah, '...bah is set'); |
240 | ::is($buzz->bah, 'BAH', '...bah returns "BAH" '); |
241 | |
242 | my $buzz2; |
243 | ::lives_ok { $buzz2 = Buzz->meta->new_object('$bah' => undef) } '...Buzz instantiated successfully'; |
244 | ::ok($buzz2->has_bah, '...bah is set'); |
245 | ::is($buzz2->bah, undef, '...bah is undef'); |
246 | |
1d68af04 |
247 | } |