1use strict; 2use warnings; 3 4use Test::More; 5 6use Moose (); 7use Moose::Meta::Class; 8use Moose::Meta::Role; 9use Moose::Util; 10 11my $role1 = Moose::Meta::Role->initialize('Foo'); 12$role1->add_attribute( foo => ( is => 'ro' ) ); 13 14ok( $role1->has_attribute('foo'), 'Foo role has a foo attribute' ); 15 16my $foo_attr = $role1->get_attribute('foo'); 17is( 18 $foo_attr->associated_role->name, 'Foo', 19 'associated_role for foo attr is Foo role' 20); 21 22isa_ok( 23 $foo_attr->attribute_for_class('Moose::Meta::Attribute'), 24 'Moose::Meta::Attribute', 25 'attribute returned by ->attribute_for_class' 26); 27 28my $role2 = Moose::Meta::Role->initialize('Bar'); 29$role1->apply($role2); 30 31ok( $role2->has_attribute('foo'), 'Bar role has a foo attribute' ); 32 33is( 34 $foo_attr->associated_role->name, 'Foo', 35 'associated_role for foo attr is still Foo role' 36); 37 38isa_ok( 39 $foo_attr->attribute_for_class('Moose::Meta::Attribute'), 40 'Moose::Meta::Attribute', 41 'attribute returned by ->attribute_for_class' 42); 43 44my $role3 = Moose::Meta::Role->initialize('Baz'); 45my $combined = Moose::Meta::Role->combine( [ $role1->name ], [ $role3->name ] ); 46 47ok( $combined->has_attribute('foo'), 'combined role has a foo attribute' ); 48 49is( 50 $foo_attr->associated_role->name, 'Foo', 51 'associated_role for foo attr is still Foo role' 52); 53 54my $class = Moose::Meta::Class->create( 55 package => 'WithRoles', 56 roles => [$role1], 57); 58 59ok( $class->has_attribute('foo'), 'class has a foo attribute' ); 60ok( 61 $class->get_attribute('foo')->has_role_attribute, 62 'foo attribute in class has an associated role_attribute' 63); 64is( 65 $class->get_attribute('foo')->role_attribute, 66 $foo_attr, 67 'foo attribute in class links to attribute object from role' 68); 69 70done_testing; 71