14 sub foo { 'Foo::foo' }
15 sub bar { 'Foo::bar' }
16 sub baz { 'Foo::baz' }
24 with 'My::Role' => { -alias => { bar => 'role_bar' } };
25 } '... this succeeds';
27 package My::Class::Failure;
31 with 'My::Role' => { -alias => { bar => 'role_bar' } };
32 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
34 sub role_bar { 'FAIL' }
37 ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
40 package My::OtherRole;
44 with 'My::Role' => { -alias => { bar => 'role_bar' } };
45 } '... this succeeds';
47 sub bar { 'My::OtherRole::bar' }
49 package My::OtherRole::Failure;
53 with 'My::Role' => { -alias => { bar => 'role_bar' } };
54 } qr/Cannot create a method alias if a local method of the same name exists/, '... cannot alias to a name that exists';
56 sub role_bar { 'FAIL' }
59 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
60 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
61 ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
64 package My::AliasingRole;
68 with 'My::Role' => { -alias => { bar => 'role_bar' } };
69 } '... this succeeds';
72 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
73 ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
79 sub foo { 'Foo::Role::foo' }
84 sub foo { 'Bar::Role::foo' }
89 sub foo { 'Baz::Role::foo' }
91 package My::Foo::Class;
95 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
96 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
98 } '... composed our roles correctly';
100 package My::Foo::Class::Broken;
104 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
105 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
107 } qr/Due to a method name conflict in roles 'Bar::Role' and 'Foo::Role', the method 'foo_foo' must be implemented or excluded by 'My::Foo::Class::Broken'/,
108 '... composed our roles correctly';
112 my $foo = My::Foo::Class->new;
113 isa_ok($foo, 'My::Foo::Class');
114 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
115 is($foo->foo, 'Baz::Role::foo', '... got the right method');
116 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
117 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
121 package My::Foo::Role;
125 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
126 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
128 } '... composed our roles correctly';
131 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
132 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
136 package My::Foo::Role::Other;
140 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
141 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
143 } '... composed our roles correctly';
146 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
147 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
150 package My::Foo::AliasOnly;
154 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
155 } '... composed our roles correctly';
158 ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
159 ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
176 with 'Role::Foo' => {
177 -alias => { x1 => 'foo_x1' },
181 'Compose Role::Foo into Role::Bar with alias and exclude';
188 my $bar = Role::Bar->meta;
189 ok( $bar->has_method($_), "has $_ method" )
190 for qw( x1 y1 foo_x1 );
200 with 'Role::Foo' => {
201 -alias => { x1 => 'foo_x1' },
205 'Compose Role::Foo into Role::Baz with alias and exclude';
209 my $baz = Role::Baz->meta;
210 ok( $baz->has_method($_), "has $_ method" )
212 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );