11 Foo->meta->add_attribute('bar');
14 sub check_meta_sanity {
15 my ($meta, $class) = @_;
16 isa_ok($meta, 'Class::MOP::Class');
17 is($meta->name, $class);
18 ok($meta->has_method('foo'));
19 isa_ok($meta->get_method('foo'), 'Class::MOP::Method');
20 ok($meta->has_attribute('bar'));
21 isa_ok($meta->get_attribute('bar'), 'Class::MOP::Attribute');
24 can_ok('Foo', 'meta');
27 check_meta_sanity($meta, 'Foo');
30 $meta = $meta->reinitialize($meta->name);
32 check_meta_sanity($meta, 'Foo');
35 $meta = $meta->reinitialize($meta);
37 check_meta_sanity($meta, 'Foo');
40 $meta->reinitialize('');
41 }, qr/You must pass a package name or an existing Class::MOP::Package instance/ );
44 $meta->reinitialize($meta->new_object);
45 }, qr/You must pass a package name or an existing Class::MOP::Package instance/ );
48 package Bar::Meta::Method;
49 use base 'Class::MOP::Method';
50 __PACKAGE__->meta->add_attribute('test', accessor => 'test');
54 package Bar::Meta::Attribute;
55 use base 'Class::MOP::Attribute';
56 __PACKAGE__->meta->add_attribute('tset', accessor => 'tset');
62 Bar->meta->add_method('foo' => Bar::Meta::Method->wrap(sub {}, name => 'foo', package_name => 'Bar'));
63 Bar->meta->add_attribute(Bar::Meta::Attribute->new('bar'));
67 check_meta_sanity($meta, 'Bar');
68 isa_ok(Bar->meta->get_method('foo'), 'Bar::Meta::Method');
69 isa_ok(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
71 $meta = $meta->reinitialize('Bar');
73 check_meta_sanity($meta, 'Bar');
74 isa_ok(Bar->meta->get_method('foo'), 'Bar::Meta::Method');
75 isa_ok(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
77 Bar->meta->get_method('foo')->test('FOO');
78 Bar->meta->get_attribute('bar')->tset('OOF');
80 is(Bar->meta->get_method('foo')->test, 'FOO');
81 is(Bar->meta->get_attribute('bar')->tset, 'OOF');
83 $meta = $meta->reinitialize('Bar');
85 is(Bar->meta->get_method('foo')->test, 'FOO');
86 is(Bar->meta->get_attribute('bar')->tset, 'OOF');
89 package Baz::Meta::Attribute;
90 use base 'Class::MOP::Attribute';
94 package Baz::Meta::Method;
95 use base 'Class::MOP::Method';
100 use metaclass meta_name => undef;
103 Class::MOP::class_of('Baz')->add_attribute('bar');
106 $meta = Class::MOP::class_of('Baz');
107 check_meta_sanity($meta, 'Baz');
108 ok(!$meta->get_method('foo')->isa('Baz::Meta::Method'));
109 ok(!$meta->get_attribute('bar')->isa('Baz::Meta::Attribute'));
111 $meta = $meta->reinitialize(
113 attribute_metaclass => 'Baz::Meta::Attribute',
114 method_metaclass => 'Baz::Meta::Method'
117 check_meta_sanity($meta, 'Baz');
118 isa_ok($meta->get_method('foo'), 'Baz::Meta::Method');
119 isa_ok($meta->get_attribute('bar'), 'Baz::Meta::Attribute');
124 attribute_metaclass => 'Bar::Meta::Attribute',
125 method_metaclass => 'Bar::Meta::Method';
128 Quux->meta->add_attribute('bar');
132 check_meta_sanity($meta, 'Quux');
133 isa_ok(Quux->meta->get_method('foo'), 'Bar::Meta::Method');
134 isa_ok(Quux->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
136 $meta = $meta->reinitialize(
138 attribute_metaclass => 'Baz::Meta::Attribute',
139 method_metaclass => 'Baz::Meta::Method',
144 package Quuux::Meta::Attribute;
145 use base 'Class::MOP::Attribute';
147 sub install_accessors {}
154 Quuux->meta->add_attribute('bar', reader => 'bar');
158 check_meta_sanity($meta, 'Quuux');
159 ok($meta->has_method('bar'));
161 $meta = $meta->reinitialize(
163 attribute_metaclass => 'Quuux::Meta::Attribute',
166 check_meta_sanity($meta, 'Quuux');
167 ok(!$meta->has_method('bar'));
170 package Blah::Meta::Method;
171 use base 'Class::MOP::Method';
173 __PACKAGE__->meta->add_attribute('foo', reader => 'foo', default => 'TEST');
177 package Blah::Meta::Attribute;
178 use base 'Class::MOP::Attribute';
180 __PACKAGE__->meta->add_attribute('oof', reader => 'oof', default => 'TSET');
185 use metaclass no_meta => 1;
187 Class::MOP::class_of('Blah')->add_attribute('bar');
190 $meta = Class::MOP::class_of('Blah');
191 check_meta_sanity($meta, 'Blah');
193 $meta = Class::MOP::Class->reinitialize(
195 attribute_metaclass => 'Blah::Meta::Attribute',
196 method_metaclass => 'Blah::Meta::Method',
199 check_meta_sanity($meta, 'Blah');
200 can_ok($meta->get_method('foo'), 'foo');
201 is($meta->get_method('foo')->foo, 'TEST');
202 can_ok($meta->get_attribute('bar'), 'oof');
203 is($meta->get_attribute('bar')->oof, 'TSET');