6 use Test::More tests => 50;
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 is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
25 is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
26 is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
34 $meta->add_attribute($FOO_ATTR);
35 } '... we added an attribute to Foo successfully';
36 ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
37 ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
39 ::ok(!$meta->has_method('foo'), '... no accessor created');
42 $meta->add_attribute($BAR_ATTR_2);
43 } '... we added an attribute to Foo successfully';
44 ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
45 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
47 ::ok(!$meta->has_method('bar'), '... no accessor created');
55 $meta->add_attribute($BAR_ATTR);
56 } '... we added an attribute to Bar successfully';
57 ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
58 ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
60 my $attr = $meta->get_attribute('$bar');
61 ::is($attr->get_read_method, 'bar', '... got the right read method for Bar');
62 ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
64 ::ok($meta->has_method('bar'), '... an accessor has been created');
65 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
73 $meta->add_attribute($BAZ_ATTR);
74 } '... we added an attribute to Baz successfully';
75 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
76 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
78 my $attr = $meta->get_attribute('$baz');
79 ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz');
80 ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
82 ::ok($meta->has_method('get_baz'), '... a reader has been created');
83 ::ok($meta->has_method('set_baz'), '... a writer has been created');
85 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
86 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
91 isa_ok($meta, 'Class::MOP::Class');
93 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
94 is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');
95 is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');
98 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
104 '... got the right list of applicable attributes for Baz');
107 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
108 [ Bar->meta, Baz->meta, Foo->meta ],
109 '... got the right list of associated classes from the applicable attributes for Baz');
113 $attr = $meta->remove_attribute('$baz');
114 } '... removed the $baz attribute successfully';
115 is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
117 ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute');
118 is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');
120 ok(!$meta->has_method('get_baz'), '... a reader has been removed');
121 ok(!$meta->has_method('set_baz'), '... a writer has been removed');
124 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
129 '... got the right list of applicable attributes for Baz');
132 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
133 [ Bar->meta, Foo->meta ],
134 '... got the right list of associated classes from the applicable attributes for Baz');
139 $attr = Bar->meta->remove_attribute('$bar');
140 } '... removed the $bar attribute successfully';
141 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
143 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
145 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
149 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
154 '... got the right list of applicable attributes for Baz');
157 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
158 [ Foo->meta, Foo->meta ],
159 '... got the right list of associated classes from the applicable attributes for Baz');
161 # remove attribute which is not there
164 $val = $meta->remove_attribute('$blammo');
165 } '... attempted to remove the non-existent $blammo attribute';
166 is($val, undef, '... got the right value back (undef)');