14 sub foo { 'Foo::foo' }
15 sub bar { 'Foo::bar' }
16 sub baz { 'Foo::baz' }
24 with 'My::Role' => { -alias => { bar => 'role_bar' } };
25 }, undef, '... 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 }, undef, '... 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 not 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 }, undef, '... 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 }, undef, '... composed our roles correctly' );
100 package My::Foo::Class::Broken;
103 ::like( ::exception {
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'/, '... composed our roles correctly' );
111 my $foo = My::Foo::Class->new;
112 isa_ok($foo, 'My::Foo::Class');
113 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
114 is($foo->foo, 'Baz::Role::foo', '... got the right method');
115 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
116 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
120 package My::Foo::Role;
124 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
125 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
127 }, undef, '... composed our roles correctly' );
130 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
131 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
135 package My::Foo::Role::Other;
139 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
140 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
142 }, undef, '... composed our roles correctly' );
145 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
146 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
149 package My::Foo::AliasOnly;
153 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
154 }, undef, '... composed our roles correctly' );
157 ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
158 ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
173 with 'Role::Foo' => {
174 -alias => { x1 => 'foo_x1' },
177 }, undef, 'Compose Role::Foo into Role::Bar with alias and exclude' );
184 my $bar = Role::Bar->meta;
185 ok( $bar->has_method($_), "has $_ method" )
186 for qw( x1 y1 foo_x1 );
194 with 'Role::Foo' => {
195 -alias => { x1 => 'foo_x1' },
198 }, undef, 'Compose Role::Foo into Role::Baz with alias and exclude' );
202 my $baz = Role::Baz->meta;
203 ok( $baz->has_method($_), "has $_ method" )
205 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );