6 use Test::More tests => 70;
11 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
12 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
15 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
20 my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
22 my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => (
24 builder => 'build_foo'
27 is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
28 is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
29 is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
37 $meta->add_attribute($FOO_ATTR);
38 } '... we added an attribute to Foo successfully';
39 ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
40 ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
42 ::ok(!$meta->has_method('foo'), '... no accessor created');
45 $meta->add_attribute($BAR_ATTR_2);
46 } '... we added an attribute to Foo successfully';
47 ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
48 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
50 ::ok(!$meta->has_method('bar'), '... no accessor created');
58 $meta->add_attribute($BAR_ATTR);
59 } '... we added an attribute to Bar successfully';
60 ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
61 ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
63 my $attr = $meta->get_attribute('$bar');
64 ::is($attr->get_read_method, 'bar', '... got the right read method for Bar');
65 ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
67 ::ok($meta->has_method('bar'), '... an accessor has been created');
68 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
76 $meta->add_attribute($BAZ_ATTR);
77 } '... we added an attribute to Baz successfully';
78 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
79 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
81 my $attr = $meta->get_attribute('$baz');
82 ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz');
83 ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
85 ::ok($meta->has_method('get_baz'), '... a reader has been created');
86 ::ok($meta->has_method('set_baz'), '... a writer has been created');
88 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
89 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
94 isa_ok($meta, 'Class::MOP::Class');
96 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
97 is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');
98 is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');
101 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
107 '... got the right list of applicable attributes for Baz');
110 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
111 [ Bar->meta, Baz->meta, Foo->meta ],
112 '... got the right list of associated classes from the applicable attributes for Baz');
116 $attr = $meta->remove_attribute('$baz');
117 } '... removed the $baz attribute successfully';
118 is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
120 ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute');
121 is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');
123 ok(!$meta->has_method('get_baz'), '... a reader has been removed');
124 ok(!$meta->has_method('set_baz'), '... a writer has been removed');
127 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
132 '... got the right list of applicable attributes for Baz');
135 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
136 [ Bar->meta, Foo->meta ],
137 '... got the right list of associated classes from the applicable attributes for Baz');
142 $attr = Bar->meta->remove_attribute('$bar');
143 } '... removed the $bar attribute successfully';
144 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
146 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
148 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
152 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
157 '... got the right list of applicable attributes for Baz');
160 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
161 [ Foo->meta, Foo->meta ],
162 '... got the right list of associated classes from the applicable attributes for Baz');
164 # remove attribute which is not there
167 $val = $meta->remove_attribute('$blammo');
168 } '... attempted to remove the non-existent $blammo attribute';
169 is($val, undef, '... got the right value back (undef)');
176 use Scalar::Util qw/blessed/;
178 my $meta = Buzz->meta;
180 $meta->add_attribute($FOO_ATTR_2);
181 } '... we added an attribute to Buzz successfully';
184 $meta->add_attribute(
185 Class::MOP::Attribute->new(
188 predicate => 'has_bar',
189 clearer => 'clear_bar',
193 } '... we added an attribute to Buzz successfully';
196 $meta->add_attribute(
197 Class::MOP::Attribute->new(
200 predicate => 'has_bah',
201 clearer => 'clear_bah',
206 } '... we added an attribute to Buzz successfully';
209 $meta->add_method(build_foo => sub{ blessed shift; });
210 } '... we added a method to Buzz successfully';
215 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
216 ::is($buzz->foo, 'Buzz', '...foo builder works as expected');
217 ::ok(!$buzz->has_bar, '...bar is not set');
218 ::is($buzz->bar, undef, '...bar returns undef');
219 ::ok(!$buzz->has_bar, '...bar was not autovivified');
222 ::ok($buzz->has_bar, '...bar is set');
223 ::is($buzz->bar, undef, '...bar is undef');
225 ::ok(!$buzz->has_bar, '...bar is no longerset');
228 ::lives_ok { $buzz2 = Buzz->meta->new_object('$bar' => undef) } '...Buzz instantiated successfully';
229 ::ok($buzz2->has_bar, '...bar is set');
230 ::is($buzz2->bar, undef, '...bar is undef');
236 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
237 ::ok($buzz->has_bah, '...bah is set');
238 ::is($buzz->bah, 'BAH', '...bah returns "BAH" ');
241 ::lives_ok { $buzz2 = Buzz->meta->new_object('$bah' => undef) } '...Buzz instantiated successfully';
242 ::ok($buzz2->has_bah, '...bah is set');
243 ::is($buzz2->bah, undef, '...bah is undef');