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);
62 local $TODO = 'auto requires resolution is not supported';
63 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
64 ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
68 package My::AliasingRole;
72 with 'My::Role' => { -alias => { bar => 'role_bar' } };
73 } '... this succeeds';
76 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
77 ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
83 sub foo { 'Foo::Role::foo' }
88 sub foo { 'Bar::Role::foo' }
93 sub foo { 'Baz::Role::foo' }
95 package My::Foo::Class;
99 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
100 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
102 } '... composed our roles correctly';
104 package My::Foo::Class::Broken;
108 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
109 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
111 } '... composed our roles correctly';
115 my $foo = My::Foo::Class->new;
116 isa_ok($foo, 'My::Foo::Class');
117 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
118 is($foo->foo, 'Baz::Role::foo', '... got the right method');
119 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
120 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
124 package My::Foo::Role;
128 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
129 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
131 } '... composed our roles correctly';
134 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
135 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
139 package My::Foo::Role::Other;
140 use Test::More; # for $TODO
143 local $TODO = 'not supported';
146 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
147 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
149 } '... composed our roles correctly';
152 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
154 local $TODO = 'auto requires resolution is not supported';
155 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
158 package My::Foo::AliasOnly;
162 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
163 } '... composed our roles correctly';
166 ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
167 ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
184 with 'Role::Foo' => {
185 -alias => { x1 => 'foo_x1' },
189 'Compose Role::Foo into Role::Bar with alias and exclude';
196 my $bar = Role::Bar->meta;
197 ok( $bar->has_method($_), "has $_ method" )
198 for qw( x1 y1 foo_x1 );
208 with 'Role::Foo' => {
209 -alias => { x1 => 'foo_x1' },
213 'Compose Role::Foo into Role::Baz with alias and exclude';
217 my $baz = Role::Baz->meta;
218 ok( $baz->has_method($_), "has $_ method" )
220 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );