6 use Test::More tests => 71;
13 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
14 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
17 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
22 my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
24 my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => (
26 builder => 'build_foo'
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');
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');
44 ::ok(!$meta->has_method('foo'), '... no accessor created');
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');
50 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
52 ::ok(!$meta->has_method('bar'), '... no accessor created');
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');
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');
69 ::ok($meta->has_method('bar'), '... an accessor has been created');
70 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
78 $meta->add_attribute($BAZ_ATTR);
79 } '... we added an attribute to Baz successfully';
80 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
81 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
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');
87 ::ok($meta->has_method('get_baz'), '... a reader has been created');
88 ::ok($meta->has_method('set_baz'), '... a writer has been created');
90 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
91 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
96 isa_ok($meta, 'Class::MOP::Class');
98 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
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"');
103 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
109 '... got the right list of applicable attributes for Baz');
112 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
113 [ Bar->meta, Baz->meta, Foo->meta ],
114 '... got the right list of associated classes from the applicable attributes for Baz');
118 $attr = $meta->remove_attribute('$baz');
119 } '... removed the $baz attribute successfully';
120 is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
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');
125 ok(!$meta->has_method('get_baz'), '... a reader has been removed');
126 ok(!$meta->has_method('set_baz'), '... a writer has been removed');
129 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
134 '... got the right list of applicable attributes for Baz');
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');
144 $attr = Bar->meta->remove_attribute('$bar');
145 } '... removed the $bar attribute successfully';
146 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
148 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
150 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
154 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
159 '... got the right list of applicable attributes for Baz');
162 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
163 [ Foo->meta, Foo->meta ],
164 '... got the right list of associated classes from the applicable attributes for Baz');
166 # remove attribute which is not there
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)');
178 use Scalar::Util qw/blessed/;
180 my $meta = Buzz->meta;
182 $meta->add_attribute($FOO_ATTR_2);
183 } '... we added an attribute to Buzz successfully';
186 $meta->add_attribute(
187 Class::MOP::Attribute->new(
190 predicate => 'has_bar',
191 clearer => 'clear_bar',
195 } '... we added an attribute to Buzz successfully';
198 $meta->add_attribute(
199 Class::MOP::Attribute->new(
202 predicate => 'has_bah',
203 clearer => 'clear_bah',
208 } '... we added an attribute to Buzz successfully';
211 $meta->add_method(build_foo => sub{ blessed shift; });
212 } '... we added a method to Buzz successfully';
217 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
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');
224 ::ok($buzz->has_bar, '...bar is set');
225 ::is($buzz->bar, undef, '...bar is undef');
227 ::ok(!$buzz->has_bar, '...bar is no longerset');
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');
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" ');
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');