Update tests
[gitmo/Mouse.git] / t / 030_roles / 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);
6fea087b 61{
62local $TODO = 'auto requires resolution is not supported';
6cfa1e5e 63ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
67199842 64ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
6fea087b 65}
67199842 66
67{
68 package My::AliasingRole;
69 use Mouse::Role;
70
71 ::lives_ok {
6cfa1e5e 72 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 73 } '... this succeeds';
74}
75
76ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
6cfa1e5e 77ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
67199842 78
79{
80 package Foo::Role;
81 use Mouse::Role;
c2d7552a 82
67199842 83 sub foo { 'Foo::Role::foo' }
c2d7552a 84
67199842 85 package Bar::Role;
86 use Mouse::Role;
c2d7552a 87
88 sub foo { 'Bar::Role::foo' }
67199842 89
90 package Baz::Role;
91 use Mouse::Role;
c2d7552a 92
93 sub foo { 'Baz::Role::foo' }
94
67199842 95 package My::Foo::Class;
96 use Mouse;
c2d7552a 97
67199842 98 ::lives_ok {
6cfa1e5e 99 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
100 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
67199842 101 'Baz::Role';
c2d7552a 102 } '... composed our roles correctly';
103
67199842 104 package My::Foo::Class::Broken;
105 use Mouse;
c2d7552a 106
6fea087b 107 ::dies_ok {
6cfa1e5e 108 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
109 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
67199842 110 'Baz::Role';
6fea087b 111 } '... composed our roles correctly';
67199842 112}
113
114{
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');
c2d7552a 119 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
120 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
67199842 121}
122
123{
124 package My::Foo::Role;
125 use Mouse::Role;
126
127 ::lives_ok {
6cfa1e5e 128 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
129 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
67199842 130 'Baz::Role';
131 } '... composed our roles correctly';
132}
133
134ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
135ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
136
137
138{
139 package My::Foo::Role::Other;
6fea087b 140 use Test::More; # for $TODO
67199842 141 use Mouse::Role;
142
6fea087b 143 local $TODO = 'not supported';
144
67199842 145 ::lives_ok {
6cfa1e5e 146 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
147 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
67199842 148 'Baz::Role';
149 } '... composed our roles correctly';
150}
151
152ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
6fea087b 153{
154local $TODO = 'auto requires resolution is not supported';
67199842 155ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
6fea087b 156}
6cfa1e5e 157{
158 package My::Foo::AliasOnly;
159 use Mouse;
160
161 ::lives_ok {
162 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
163 } '... composed our roles correctly';
164}
165
166ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
167ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
168
169{
170 package Role::Foo;
171 use Mouse::Role;
172
173 sub x1 {}
174 sub y1 {}
175}
176
177{
178 package Role::Bar;
179 use Mouse::Role;
180
181 use Test::Exception;
182
183 lives_ok {
184 with 'Role::Foo' => {
185 -alias => { x1 => 'foo_x1' },
186 -excludes => ['y1'],
187 };
188 }
189 'Compose Role::Foo into Role::Bar with alias and exclude';
190
191 sub x1 {}
192 sub y1 {}
193}
194
195{
196 my $bar = Role::Bar->meta;
197 ok( $bar->has_method($_), "has $_ method" )
198 for qw( x1 y1 foo_x1 );
199}
200
201{
202 package Role::Baz;
203 use Mouse::Role;
204
205 use Test::Exception;
206
207 lives_ok {
208 with 'Role::Foo' => {
209 -alias => { x1 => 'foo_x1' },
210 -excludes => ['y1'],
211 };
212 }
213 'Compose Role::Foo into Role::Baz with alias and exclude';
214}
215
216{
217 my $baz = Role::Baz->meta;
218 ok( $baz->has_method($_), "has $_ method" )
219 for qw( x1 foo_x1 );
220 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );
221}