4 use Test::More tests => 73;
9 my $FOO_ATTR = Class::MOP::Attribute->new('$foo');
10 my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => (
13 my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => (
18 my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar');
20 my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => (
22 builder => 'build_foo'
25 is($FOO_ATTR->name, '$foo', '... got the attributes name correctly');
26 is($BAR_ATTR->name, '$bar', '... got the attributes name correctly');
27 is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly');
35 $meta->add_attribute($FOO_ATTR);
36 } '... we added an attribute to Foo successfully';
37 ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute');
38 ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo');
40 ::ok(!$meta->has_method('foo'), '... no accessor created');
43 $meta->add_attribute($BAR_ATTR_2);
44 } '... we added an attribute to Foo successfully';
45 ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute');
46 ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo');
48 ::ok(!$meta->has_method('bar'), '... no accessor created');
56 $meta->add_attribute($BAR_ATTR);
57 } '... we added an attribute to Bar successfully';
58 ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute');
59 ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar');
61 my $attr = $meta->get_attribute('$bar');
62 ::is($attr->get_read_method, 'bar', '... got the right read method for Bar');
63 ::is($attr->get_write_method, 'bar', '... got the right write method for Bar');
65 ::ok($meta->has_method('bar'), '... an accessor has been created');
66 ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor');
74 $meta->add_attribute($BAZ_ATTR);
75 } '... we added an attribute to Baz successfully';
76 ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute');
77 ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz');
79 my $attr = $meta->get_attribute('$baz');
80 ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz');
81 ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz');
83 ::ok($meta->has_method('get_baz'), '... a reader has been created');
84 ::ok($meta->has_method('set_baz'), '... a writer has been created');
86 ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor');
87 ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor');
94 my $meta = Foo2->meta;
96 Class::MOP::Attribute->new( '$foo2' => ( reader => 'foo2' ) ) );
98 ::ok( $meta->has_method('foo2'), '... a reader has been created' );
100 my $attr = $meta->get_attribute('$foo2');
101 ::is( $attr->get_read_method, 'foo2',
102 '... got the right read method for Foo2' );
103 ::is( $attr->get_write_method, undef,
104 '... got undef for the writer with a read-only attribute in Foo2' );
108 my $meta = Baz->meta;
109 isa_ok($meta, 'Class::MOP::Class');
111 is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"');
112 is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"');
113 is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"');
116 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
122 '... got the right list of applicable attributes for Baz');
125 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
126 [ Bar->meta, Baz->meta, Foo->meta ],
127 '... got the right list of associated classes from the applicable attributes for Baz');
131 $attr = $meta->remove_attribute('$baz');
132 } '... removed the $baz attribute successfully';
133 is($attr, $BAZ_ATTR, '... got the right attribute back for Baz');
135 ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute');
136 is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute');
138 ok(!$meta->has_method('get_baz'), '... a reader has been removed');
139 ok(!$meta->has_method('set_baz'), '... a writer has been removed');
142 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
147 '... got the right list of applicable attributes for Baz');
150 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
151 [ Bar->meta, Foo->meta ],
152 '... got the right list of associated classes from the applicable attributes for Baz');
157 $attr = Bar->meta->remove_attribute('$bar');
158 } '... removed the $bar attribute successfully';
159 is($attr, $BAR_ATTR, '... got the right attribute back for Bar');
161 ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute');
163 ok(!Bar->meta->has_method('bar'), '... a accessor has been removed');
167 [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
172 '... got the right list of applicable attributes for Baz');
175 [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ],
176 [ Foo->meta, Foo->meta ],
177 '... got the right list of associated classes from the applicable attributes for Baz');
179 # remove attribute which is not there
182 $val = $meta->remove_attribute('$blammo');
183 } '... attempted to remove the non-existent $blammo attribute';
184 is($val, undef, '... got the right value back (undef)');
191 use Scalar::Util qw/blessed/;
193 my $meta = Buzz->meta;
195 $meta->add_attribute($FOO_ATTR_2);
196 } '... we added an attribute to Buzz successfully';
199 $meta->add_attribute(
200 Class::MOP::Attribute->new(
203 predicate => 'has_bar',
204 clearer => 'clear_bar',
208 } '... we added an attribute to Buzz successfully';
211 $meta->add_attribute(
212 Class::MOP::Attribute->new(
215 predicate => 'has_bah',
216 clearer => 'clear_bah',
221 } '... we added an attribute to Buzz successfully';
224 $meta->add_method(build_foo => sub{ blessed shift; });
225 } '... we added a method to Buzz successfully';
230 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
231 ::is($buzz->foo, 'Buzz', '...foo builder works as expected');
232 ::ok(!$buzz->has_bar, '...bar is not set');
233 ::is($buzz->bar, undef, '...bar returns undef');
234 ::ok(!$buzz->has_bar, '...bar was not autovivified');
237 ::ok($buzz->has_bar, '...bar is set');
238 ::is($buzz->bar, undef, '...bar is undef');
240 ::ok(!$buzz->has_bar, '...bar is no longerset');
243 ::lives_ok { $buzz2 = Buzz->meta->new_object('$bar' => undef) } '...Buzz instantiated successfully';
244 ::ok($buzz2->has_bar, '...bar is set');
245 ::is($buzz2->bar, undef, '...bar is undef');
251 ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully';
252 ::ok($buzz->has_bah, '...bah is set');
253 ::is($buzz->bah, 'BAH', '...bah returns "BAH" ');
256 ::lives_ok { $buzz2 = Buzz->meta->new_object('$bah' => undef) } '...Buzz instantiated successfully';
257 ::ok($buzz2->has_bah, '...bah is set');
258 ::is($buzz2->bah, undef, '...bah is undef');