6 use Moose::Util qw( does_role );
9 package Foo::Meta::Attribute;
14 package Foo::Meta::Attribute2;
22 has foo => (is => 'ro');
28 Moose::Util::MetaRole::apply_metaroles(
30 class_metaroles => { attribute => ['Foo::Meta::Attribute'] },
31 role_metaroles => { applied_attribute => ['Foo::Meta::Attribute2'] },
35 has bar => (is => 'ro');
38 ok(Moose::Util::does_role(Foo->meta->get_attribute('bar'), 'Foo::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
39 ok(!Moose::Util::does_role(Foo->meta->get_attribute('bar'), 'Foo::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
40 ok(!Moose::Util::does_role(Foo->meta->get_attribute('foo'), 'Foo::Meta::Attribute'), "attrs defined in the role don't get the metarole applied");
41 ok(!Moose::Util::does_role(Foo->meta->get_attribute('foo'), 'Foo::Meta::Attribute'), "attrs defined in the role don't get the role metarole defined in the class applied");
44 package Bar::Meta::Attribute;
49 package Bar::Meta::Attribute2;
56 Moose::Util::MetaRole::apply_metaroles(
58 class_metaroles => { attribute => ['Bar::Meta::Attribute'] },
59 role_metaroles => { applied_attribute => ['Bar::Meta::Attribute2'] },
62 has foo => (is => 'ro');
70 has bar => (is => 'ro');
73 ok(!Moose::Util::does_role(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute'), "attrs defined in the class don't get the class metarole from the role applied");
74 ok(!Moose::Util::does_role(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
75 ok(Moose::Util::does_role(Bar->meta->get_attribute('foo'), 'Bar::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
76 ok(!Moose::Util::does_role(Bar->meta->get_attribute('foo'), 'Bar::Meta::Attribute'), "attrs defined in the role don't get the class metarole applied");
79 package Baz::Meta::Attribute;
84 package Baz::Meta::Attribute2;
91 Moose::Util::MetaRole::apply_metaroles(
93 class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
94 role_metaroles => { applied_attribute => ['Baz::Meta::Attribute2'] },
97 has foo => (is => 'ro');
103 Moose::Util::MetaRole::apply_metaroles(
105 class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
106 role_metaroles => { applied_attribute => ['Baz::Meta::Attribute2'] },
110 has bar => (is => 'ro');
113 ok(Moose::Util::does_role(Baz->meta->get_attribute('bar'), 'Baz::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
114 ok(!Moose::Util::does_role(Baz->meta->get_attribute('bar'), 'Baz::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
115 ok(Moose::Util::does_role(Baz->meta->get_attribute('foo'), 'Baz::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
116 ok(!Moose::Util::does_role(Baz->meta->get_attribute('foo'), 'Baz::Meta::Attribute'), "attrs defined in the role don't get the class metarole applied");
119 package Accessor::Modifying::Role;
122 around _process_options => sub {
125 my ($name, $params) = @_;
127 $params->{reader} .= '_foo';
142 package Class::With::Trait;
144 Moose::Util::MetaRole::apply_metaroles(
147 attribute => ['Accessor::Modifying::Role'],
159 can_ok('Class::With::Trait', 'foo');
160 can_ok('Class::With::Trait', 'bar_foo');
164 package Role::With::Trait;
166 Moose::Util::MetaRole::apply_metaroles(
169 applied_attribute => ['Accessor::Modifying::Role'],
181 return $self->can('foo_foo');
186 package Class::With::Role::With::Trait;
188 with 'Role::With::Trait';
197 return $self->can('bar');
202 can_ok('Class::With::Role::With::Trait', 'foo_foo');
203 can_ok('Class::With::Role::With::Trait', 'bar');
207 package Quux::Meta::Role::Attribute;
215 has foo => (traits => ['Quux::Meta::Role::Attribute'], is => 'ro');
216 has baz => (is => 'ro');
222 Moose::Util::MetaRole::apply_metaroles(
225 applied_attribute => ['Quux::Meta::Role::Attribute']
229 has bar => (is => 'ro');
235 with 'Quux::Role1', 'Quux::Role2';
239 my $foo = Quux->meta->get_attribute('foo');
240 does_ok($foo, 'Quux::Meta::Role::Attribute',
241 "individual attribute trait applied correctly");
243 my $baz = Quux->meta->get_attribute('baz');
244 ok(! does_role($baz, 'Quux::Meta::Role::Attribute'),
245 "applied_attribute traits do not end up applying to attributes from other roles during composition");
247 my $bar = Quux->meta->get_attribute('bar');
248 does_ok($bar, 'Quux::Meta::Role::Attribute',
249 "attribute metarole applied correctly");
255 Moose::Util::MetaRole::apply_metaroles(
258 applied_attribute => ['Quux::Meta::Role::Attribute']
262 has foo => (is => 'ro');
271 has bar => (is => 'ro');
275 package ConsumesBoth;
277 with 'HasMeta', 'NoMeta';
281 my $foo = ConsumesBoth->meta->get_attribute('foo');
282 does_ok($foo, 'Quux::Meta::Role::Attribute',
283 'applied_attribute traits are preserved when one role consumes another');
285 my $bar = ConsumesBoth->meta->get_attribute('bar');
286 ok(! does_role($bar, 'Quux::Meta::Role::Attribute'),
287 "applied_attribute traits do not spill over from consumed role");