8 sub check_meta_sanity {
9 my ($meta, $class) = @_;
10 local $Test::Builder::Level = $Test::Builder::Level + 1;
11 isa_ok($meta, 'Moose::Meta::Class');
12 is($meta->name, $class);
13 ok($meta->has_method('foo'));
14 isa_ok($meta->get_method('foo'), 'Moose::Meta::Method');
15 ok($meta->has_attribute('bar'));
16 isa_ok($meta->get_attribute('bar'), 'Moose::Meta::Attribute');
18 if ( $meta->name eq 'Foo' ) {
19 ok($meta->does_role('Role1'), 'does Role1');
20 ok($meta->does_role('Role2'), 'does Role2');
24 map { [ $_->role->name, $_->class->name ] }
25 sort { $a->role->name cmp $b->role->name }
26 $meta->role_applications
29 [ 'Role1|Role2', 'Foo' ],
31 'role applications for Role1 and Role2'
50 with 'Role1', 'Role2';
51 has bar => (is => 'ro');
54 check_meta_sanity(Foo->meta, 'Foo');
56 Moose::Meta::Class->reinitialize('Foo');
57 check_meta_sanity(Foo->meta, 'Foo');
60 package Foo::Role::Method;
63 has foo => (is => 'rw');
67 package Foo::Role::Attribute;
69 has oof => (is => 'rw');
72 Moose::Util::MetaRole::apply_metaroles(
75 method => ['Foo::Role::Method'],
76 attribute => ['Foo::Role::Attribute'],
79 check_meta_sanity(Foo->meta, 'Foo');
80 does_ok(Foo->meta->get_method('foo'), 'Foo::Role::Method');
81 does_ok(Foo->meta->get_attribute('bar'), 'Foo::Role::Attribute');
83 Moose::Meta::Class->reinitialize('Foo');
84 check_meta_sanity(Foo->meta, 'Foo');
85 does_ok(Foo->meta->get_method('foo'), 'Foo::Role::Method');
86 does_ok(Foo->meta->get_attribute('bar'), 'Foo::Role::Attribute');
88 Foo->meta->get_method('foo')->foo('TEST');
89 Foo->meta->get_attribute('bar')->oof('TSET');
90 is(Foo->meta->get_method('foo')->foo, 'TEST');
91 is(Foo->meta->get_attribute('bar')->oof, 'TSET');
92 Moose::Meta::Class->reinitialize('Foo');
93 check_meta_sanity(Foo->meta, 'Foo');
94 is(Foo->meta->get_method('foo')->foo, 'TEST');
95 is(Foo->meta->get_attribute('bar')->oof, 'TSET');
98 package Bar::Role::Method;
103 package Bar::Role::Attribute;
110 Moose::Util::MetaRole::apply_metaroles(
113 method => ['Bar::Role::Method'],
114 attribute => ['Bar::Role::Attribute'],
118 has bar => (is => 'ro');
121 check_meta_sanity(Bar->meta, 'Bar');
122 does_ok(Bar->meta->get_method('foo'), 'Bar::Role::Method');
123 does_ok(Bar->meta->get_attribute('bar'), 'Bar::Role::Attribute');
125 Moose::Meta::Class->reinitialize('Bar');
126 check_meta_sanity(Bar->meta, 'Bar');
127 does_ok(Bar->meta->get_method('foo'), 'Bar::Role::Method');
128 does_ok(Bar->meta->get_attribute('bar'), 'Bar::Role::Attribute');
129 ok(!Moose::Util::does_role(Bar->meta->get_method('foo'), 'Foo::Role::Method'));
130 ok(!Moose::Util::does_role(Bar->meta->get_attribute('bar'), 'Foo::Role::Attribute'));
132 Moose::Util::MetaRole::apply_metaroles(
135 method => ['Foo::Role::Method'],
136 attribute => ['Foo::Role::Attribute'],
139 check_meta_sanity(Bar->meta, 'Bar');
140 does_ok(Bar->meta->get_method('foo'), 'Bar::Role::Method');
141 does_ok(Bar->meta->get_attribute('bar'), 'Bar::Role::Attribute');
142 does_ok(Bar->meta->get_method('foo'), 'Foo::Role::Method');
143 does_ok(Bar->meta->get_attribute('bar'), 'Foo::Role::Attribute');
146 package Bar::Meta::Method;
148 BEGIN { extends 'Moose::Meta::Method' };
152 package Bar::Meta::Attribute;
154 BEGIN { extends 'Moose::Meta::Attribute' };
158 Moose::Meta::Class->reinitialize(
160 method_metaclass => 'Bar::Meta::Method',
161 attribute_metaclass => 'Bar::Meta::Attribute',
166 package Baz::Meta::Class;
168 BEGIN { extends 'Moose::Meta::Class' };
172 return $self->SUPER::initialize(
174 method_metaclass => 'Bar::Meta::Method',
175 attribute_metaclass => 'Bar::Meta::Attribute'
182 use Moose -metaclass => 'Baz::Meta::Class';
184 has bar => (is => 'ro');
187 check_meta_sanity(Baz->meta, 'Baz');
188 isa_ok(Baz->meta->get_method('foo'), 'Bar::Meta::Method');
189 isa_ok(Baz->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
190 Moose::Meta::Class->reinitialize('Baz');
191 check_meta_sanity(Baz->meta, 'Baz');
192 isa_ok(Baz->meta->get_method('foo'), 'Bar::Meta::Method');
193 isa_ok(Baz->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
195 Moose::Util::MetaRole::apply_metaroles(
198 method => ['Foo::Role::Method'],
199 attribute => ['Foo::Role::Attribute'],
202 check_meta_sanity(Baz->meta, 'Baz');
203 isa_ok(Baz->meta->get_method('foo'), 'Bar::Meta::Method');
204 isa_ok(Baz->meta->get_attribute('bar'), 'Bar::Meta::Attribute');
205 does_ok(Baz->meta->get_method('foo'), 'Foo::Role::Method');
206 does_ok(Baz->meta->get_attribute('bar'), 'Foo::Role::Attribute');
209 package Baz::Meta::Method;
211 extends 'Moose::Meta::Method';
215 package Baz::Meta::Attribute;
217 extends 'Moose::Meta::Attribute';
221 Moose::Meta::Class->reinitialize(
223 method_metaclass => 'Baz::Meta::Method',
224 attribute_metaclass => 'Baz::Meta::Attribute',
232 before foo => sub { };
233 has bar => (is => 'ro');
235 __PACKAGE__->meta->make_immutable;
238 ok(Quux->meta->has_method('new'));
239 isa_ok(Quux->meta->get_method('new'), 'Moose::Meta::Method::Constructor');
240 ok(Quux->meta->has_method('meta'));
241 isa_ok(Quux->meta->get_method('meta'), 'Moose::Meta::Method::Meta');
242 ok(Quux->meta->has_method('foo'));
243 isa_ok(Quux->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
244 ok(Quux->meta->has_method('bar'));
245 isa_ok(Quux->meta->get_method('bar'), 'Moose::Meta::Method::Accessor');
246 ok(Quux->meta->has_method('DESTROY'));
247 isa_ok(Quux->meta->get_method('DESTROY'), 'Moose::Meta::Method::Destructor');
248 ok(Quux->meta->has_method('DEMOLISH'));
249 isa_ok(Quux->meta->get_method('DEMOLISH'), 'Moose::Meta::Method');
251 Quux->meta->make_mutable;
252 Moose::Meta::Class->reinitialize('Quux');
253 Quux->meta->make_immutable;
255 ok(Quux->meta->has_method('new'));
256 isa_ok(Quux->meta->get_method('new'), 'Moose::Meta::Method::Constructor');
257 ok(Quux->meta->has_method('meta'));
258 isa_ok(Quux->meta->get_method('meta'), 'Moose::Meta::Method::Meta');
259 ok(Quux->meta->has_method('foo'));
260 isa_ok(Quux->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
261 ok(Quux->meta->has_method('bar'));
262 isa_ok(Quux->meta->get_method('bar'), 'Moose::Meta::Method::Accessor');
263 ok(Quux->meta->has_method('DESTROY'));
264 isa_ok(Quux->meta->get_method('DESTROY'), 'Moose::Meta::Method::Destructor');
265 ok(Quux->meta->has_method('DEMOLISH'));
266 isa_ok(Quux->meta->get_method('DEMOLISH'), 'Moose::Meta::Method');
268 Quux->meta->make_mutable;
269 Moose::Util::MetaRole::apply_metaroles(
272 method => ['Foo::Role::Method'],
273 attribute => ['Foo::Role::Attribute'],
276 Quux->meta->make_immutable;
278 ok(Quux->meta->has_method('new'));
279 isa_ok(Quux->meta->get_method('new'), 'Moose::Meta::Method::Constructor');
280 { local $TODO = "constructor methods don't get metaroles yet";
281 does_ok(Quux->meta->get_method('new'), 'Foo::Role::Method');
283 ok(Quux->meta->has_method('meta'));
284 isa_ok(Quux->meta->get_method('meta'), 'Moose::Meta::Method::Meta');
285 { local $TODO = "meta methods don't get metaroles yet";
286 does_ok(Quux->meta->get_method('meta'), 'Foo::Role::Method');
288 ok(Quux->meta->has_method('foo'));
289 isa_ok(Quux->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
290 { local $TODO = "modified methods don't get metaroles yet";
291 does_ok(Quux->meta->get_method('foo'), 'Foo::Role::Method');
293 ok(Quux->meta->has_method('bar'));
294 isa_ok(Quux->meta->get_method('bar'), 'Moose::Meta::Method::Accessor');
295 { local $TODO = "accessor methods don't get metaroles yet";
296 does_ok(Quux->meta->get_method('bar'), 'Foo::Role::Method');
298 ok(Quux->meta->has_method('DESTROY'));
299 isa_ok(Quux->meta->get_method('DESTROY'), 'Moose::Meta::Method::Destructor');
300 { local $TODO = "destructor methods don't get metaroles yet";
301 does_ok(Quux->meta->get_method('DESTROY'), 'Foo::Role::Method');
303 ok(Quux->meta->has_method('DEMOLISH'));
304 isa_ok(Quux->meta->get_method('DEMOLISH'), 'Moose::Meta::Method');
305 does_ok(Quux->meta->get_method('DEMOLISH'), 'Foo::Role::Method');
310 with 'Role1', 'Role2';
313 ok( Role3->meta->does_role('Role1'), 'Role3 does Role1' );
314 ok( Role3->meta->does_role('Role2'), 'Role3 does Role2' );
316 Moose::Meta::Role->reinitialize('Role3');
318 ok( Role3->meta->does_role('Role1'), 'Role3 does Role1 after reinitialize' );
319 ok( Role3->meta->does_role('Role2'), 'Role3 does Role2 after reinitialize' );