Mouse::Role improved
[gitmo/Mouse.git] / t / 030_roles / failing / 013_method_aliasing_in_composition.t
CommitLineData
67199842 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6cfa1e5e 6use Test::More tests => 46;
67199842 7use Test::Exception;
8
9
10
11{
12 package My::Role;
13 use Mouse::Role;
14
15 sub foo { 'Foo::foo' }
16 sub bar { 'Foo::bar' }
17 sub baz { 'Foo::baz' }
c2d7552a 18
67199842 19 requires 'role_bar';
20
21 package My::Class;
22 use Mouse;
23
24 ::lives_ok {
6cfa1e5e 25 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 26 } '... this succeeds';
c2d7552a 27
67199842 28 package My::Class::Failure;
29 use Mouse;
30
31 ::throws_ok {
6cfa1e5e 32 with 'My::Role' => { -alias => { bar => 'role_bar' } };
c2d7552a 33 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
34
67199842 35 sub role_bar { 'FAIL' }
36}
37
38ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
39
40{
41 package My::OtherRole;
42 use Mouse::Role;
43
44 ::lives_ok {
6cfa1e5e 45 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 46 } '... this succeeds';
47
48 sub bar { 'My::OtherRole::bar' }
c2d7552a 49
67199842 50 package My::OtherRole::Failure;
51 use Mouse::Role;
52
53 ::throws_ok {
6cfa1e5e 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';
c2d7552a 56
57 sub role_bar { 'FAIL' }
67199842 58}
59
60ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
6cfa1e5e 61ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
67199842 62ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
63
64{
65 package My::AliasingRole;
66 use Mouse::Role;
67
68 ::lives_ok {
6cfa1e5e 69 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 70 } '... this succeeds';
71}
72
73ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
6cfa1e5e 74ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
67199842 75
76{
77 package Foo::Role;
78 use Mouse::Role;
c2d7552a 79
67199842 80 sub foo { 'Foo::Role::foo' }
c2d7552a 81
67199842 82 package Bar::Role;
83 use Mouse::Role;
c2d7552a 84
85 sub foo { 'Bar::Role::foo' }
67199842 86
87 package Baz::Role;
88 use Mouse::Role;
c2d7552a 89
90 sub foo { 'Baz::Role::foo' }
91
67199842 92 package My::Foo::Class;
93 use Mouse;
c2d7552a 94
67199842 95 ::lives_ok {
6cfa1e5e 96 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
97 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
67199842 98 'Baz::Role';
c2d7552a 99 } '... composed our roles correctly';
100
67199842 101 package My::Foo::Class::Broken;
102 use Mouse;
c2d7552a 103
67199842 104 ::throws_ok {
6cfa1e5e 105 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
106 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
67199842 107 'Baz::Role';
c2d7552a 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';
67199842 110}
111
112{
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');
c2d7552a 117 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
118 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
67199842 119}
120
121{
122 package My::Foo::Role;
123 use Mouse::Role;
124
125 ::lives_ok {
6cfa1e5e 126 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
127 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
67199842 128 'Baz::Role';
129 } '... composed our roles correctly';
130}
131
132ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
133ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
134
135
136{
137 package My::Foo::Role::Other;
138 use Mouse::Role;
139
140 ::lives_ok {
6cfa1e5e 141 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
142 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
67199842 143 'Baz::Role';
144 } '... composed our roles correctly';
145}
146
147ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
148ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
149
6cfa1e5e 150{
151 package My::Foo::AliasOnly;
152 use Mouse;
153
154 ::lives_ok {
155 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
156 } '... composed our roles correctly';
157}
158
159ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
160ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
161
162{
163 package Role::Foo;
164 use Mouse::Role;
165
166 sub x1 {}
167 sub y1 {}
168}
169
170{
171 package Role::Bar;
172 use Mouse::Role;
173
174 use Test::Exception;
175
176 lives_ok {
177 with 'Role::Foo' => {
178 -alias => { x1 => 'foo_x1' },
179 -excludes => ['y1'],
180 };
181 }
182 'Compose Role::Foo into Role::Bar with alias and exclude';
183
184 sub x1 {}
185 sub y1 {}
186}
187
188{
189 my $bar = Role::Bar->meta;
190 ok( $bar->has_method($_), "has $_ method" )
191 for qw( x1 y1 foo_x1 );
192}
193
194{
195 package Role::Baz;
196 use Mouse::Role;
197
198 use Test::Exception;
199
200 lives_ok {
201 with 'Role::Foo' => {
202 -alias => { x1 => 'foo_x1' },
203 -excludes => ['y1'],
204 };
205 }
206 'Compose Role::Foo into Role::Baz with alias and exclude';
207}
208
209{
210 my $baz = Role::Baz->meta;
211 ok( $baz->has_method($_), "has $_ method" )
212 for qw( x1 foo_x1 );
213 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );
214}