Mouse::Role improved
[gitmo/Mouse.git] / t / 030_roles / failing / 013_method_aliasing_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 46;
7 use 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' }
18
19     requires 'role_bar';
20
21     package My::Class;
22     use Mouse;
23
24     ::lives_ok {
25         with 'My::Role' => { -alias => { bar => 'role_bar' } };
26     } '... this succeeds';
27
28     package My::Class::Failure;
29     use Mouse;
30
31     ::throws_ok {
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';
34
35     sub role_bar { 'FAIL' }
36 }
37
38 ok(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 {
45         with 'My::Role' => { -alias => { bar => 'role_bar' } };
46     } '... this succeeds';
47
48     sub bar { 'My::OtherRole::bar' }
49
50     package My::OtherRole::Failure;
51     use Mouse::Role;
52
53     ::throws_ok {
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';
56
57     sub role_bar { 'FAIL' }
58 }
59
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');
63
64 {
65     package My::AliasingRole;
66     use Mouse::Role;
67
68     ::lives_ok {
69         with 'My::Role' => { -alias => { bar => 'role_bar' } };
70     } '... this succeeds';
71 }
72
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');
75
76 {
77     package Foo::Role;
78     use Mouse::Role;
79
80     sub foo { 'Foo::Role::foo' }
81
82     package Bar::Role;
83     use Mouse::Role;
84
85     sub foo { 'Bar::Role::foo' }
86
87     package Baz::Role;
88     use Mouse::Role;
89
90     sub foo { 'Baz::Role::foo' }
91
92     package My::Foo::Class;
93     use Mouse;
94
95     ::lives_ok {
96         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
97              'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
98              'Baz::Role';
99     } '... composed our roles correctly';
100
101     package My::Foo::Class::Broken;
102     use Mouse;
103
104     ::throws_ok {
105         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
106              'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
107              'Baz::Role';
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';
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');
117     is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
118     is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
119 }
120
121 {
122     package My::Foo::Role;
123     use Mouse::Role;
124
125     ::lives_ok {
126         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
127              'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
128              'Baz::Role';
129     } '... composed our roles correctly';
130 }
131
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');
134
135
136 {
137     package My::Foo::Role::Other;
138     use Mouse::Role;
139
140     ::lives_ok {
141         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
142              'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
143              'Baz::Role';
144     } '... composed our roles correctly';
145 }
146
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');
149
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
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');
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 }