6 use Test::More tests => 46;
15 sub foo { 'Foo::foo' }
16 sub bar { 'Foo::bar' }
17 sub baz { 'Foo::baz' }
25 with 'My::Role' => { -alias => { bar => 'role_bar' } };
26 } '... this succeeds';
28 package My::Class::Failure;
32 with 'My::Role' => { -alias => { bar => 'role_bar' } };
33 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
35 sub role_bar { 'FAIL' }
38 ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
41 package My::OtherRole;
45 with 'My::Role' => { -alias => { bar => 'role_bar' } };
46 } '... this succeeds';
48 sub bar { 'My::OtherRole::bar' }
50 package My::OtherRole::Failure;
54 with 'My::Role' => { -alias => { bar => 'role_bar' } };
55 } qr/Cannot create a method alias if a local method of the same name exists/, '... cannot alias to a name that exists';
57 sub role_bar { 'FAIL' }
60 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
61 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
62 ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
65 package My::AliasingRole;
69 with 'My::Role' => { -alias => { bar => 'role_bar' } };
70 } '... this succeeds';
73 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
74 ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
80 sub foo { 'Foo::Role::foo' }
85 sub foo { 'Bar::Role::foo' }
90 sub foo { 'Baz::Role::foo' }
92 package My::Foo::Class;
96 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
97 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
99 } '... composed our roles correctly';
101 package My::Foo::Class::Broken;
105 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
106 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
108 } 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'/,
109 '... composed our roles correctly';
113 my $foo = My::Foo::Class->new;
114 isa_ok($foo, 'My::Foo::Class');
115 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
116 is($foo->foo, 'Baz::Role::foo', '... got the right method');
117 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
118 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
122 package My::Foo::Role;
126 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
127 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
129 } '... composed our roles correctly';
132 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
133 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
137 package My::Foo::Role::Other;
141 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
142 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
144 } '... composed our roles correctly';
147 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
148 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
151 package My::Foo::AliasOnly;
155 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
156 } '... composed our roles correctly';
159 ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
160 ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
177 with 'Role::Foo' => {
178 -alias => { x1 => 'foo_x1' },
182 'Compose Role::Foo into Role::Bar with alias and exclude';
189 my $bar = Role::Bar->meta;
190 ok( $bar->has_method($_), "has $_ method" )
191 for qw( x1 y1 foo_x1 );
201 with 'Role::Foo' => {
202 -alias => { x1 => 'foo_x1' },
206 'Compose Role::Foo into Role::Baz with alias and exclude';
210 my $baz = Role::Baz->meta;
211 ok( $baz->has_method($_), "has $_ method" )
213 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );