Changelogging
[gitmo/Mouse.git] / t-failing / 030_roles / 013_method_aliasing_in_composition.t
CommitLineData
67199842 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
67199842 5
6use strict;
7use warnings;
8
fde8e43f 9use Test::More;
10$TODO = q{Mouse is not yet completed};
67199842 11use Test::Exception;
12
13
67199842 14{
15 package My::Role;
16 use Mouse::Role;
17
18 sub foo { 'Foo::foo' }
19 sub bar { 'Foo::bar' }
20 sub baz { 'Foo::baz' }
c2d7552a 21
67199842 22 requires 'role_bar';
23
24 package My::Class;
25 use Mouse;
26
27 ::lives_ok {
6cfa1e5e 28 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 29 } '... this succeeds';
c2d7552a 30
67199842 31 package My::Class::Failure;
32 use Mouse;
33
34 ::throws_ok {
6cfa1e5e 35 with 'My::Role' => { -alias => { bar => 'role_bar' } };
c2d7552a 36 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
37
67199842 38 sub role_bar { 'FAIL' }
39}
40
41ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
42
43{
44 package My::OtherRole;
45 use Mouse::Role;
46
47 ::lives_ok {
6cfa1e5e 48 with 'My::Role' => { -alias => { bar => 'role_bar' } };
67199842 49 } '... this succeeds';
50
51 sub bar { 'My::OtherRole::bar' }
c2d7552a 52
67199842 53 package My::OtherRole::Failure;
54 use Mouse::Role;
55
56 ::throws_ok {
6cfa1e5e 57 with 'My::Role' => { -alias => { bar => 'role_bar' } };
58 } qr/Cannot create a method alias if a local method of the same name exists/, '... cannot alias to a name that exists';
c2d7552a 59
60 sub role_bar { 'FAIL' }
67199842 61}
62
63ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
6cfa1e5e 64ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
67199842 65ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
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
fde8e43f 107 ::throws_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';
fde8e43f 111 } 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'/,
112 '... composed our roles correctly';
67199842 113}
114
115{
116 my $foo = My::Foo::Class->new;
117 isa_ok($foo, 'My::Foo::Class');
118 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
119 is($foo->foo, 'Baz::Role::foo', '... got the right method');
c2d7552a 120 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
121 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
67199842 122}
123
124{
125 package My::Foo::Role;
126 use Mouse::Role;
127
128 ::lives_ok {
6cfa1e5e 129 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
130 'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
67199842 131 'Baz::Role';
132 } '... composed our roles correctly';
133}
134
135ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
136ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
137
138
139{
140 package My::Foo::Role::Other;
141 use Mouse::Role;
142
143 ::lives_ok {
6cfa1e5e 144 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
145 'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
67199842 146 'Baz::Role';
147 } '... composed our roles correctly';
148}
149
150ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
151ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
fde8e43f 152
6cfa1e5e 153{
154 package My::Foo::AliasOnly;
155 use Mouse;
156
157 ::lives_ok {
158 with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' } },
159 } '... composed our roles correctly';
160}
161
162ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
163ok(My::Foo::AliasOnly->meta->has_method('foo_foo'), '.. and the aliased foo_foo method');
164
165{
166 package Role::Foo;
167 use Mouse::Role;
168
169 sub x1 {}
170 sub y1 {}
171}
172
173{
174 package Role::Bar;
175 use Mouse::Role;
176
177 use Test::Exception;
178
179 lives_ok {
180 with 'Role::Foo' => {
181 -alias => { x1 => 'foo_x1' },
182 -excludes => ['y1'],
183 };
184 }
185 'Compose Role::Foo into Role::Bar with alias and exclude';
186
187 sub x1 {}
188 sub y1 {}
189}
190
191{
192 my $bar = Role::Bar->meta;
193 ok( $bar->has_method($_), "has $_ method" )
194 for qw( x1 y1 foo_x1 );
195}
196
197{
198 package Role::Baz;
199 use Mouse::Role;
200
201 use Test::Exception;
202
203 lives_ok {
204 with 'Role::Foo' => {
205 -alias => { x1 => 'foo_x1' },
206 -excludes => ['y1'],
207 };
208 }
209 'Compose Role::Foo into Role::Baz with alias and exclude';
210}
211
212{
213 my $baz = Role::Baz->meta;
214 ok( $baz->has_method($_), "has $_ method" )
215 for qw( x1 foo_x1 );
216 ok( ! $baz->has_method('y1'), 'Role::Baz has no y1 method' );
217}
fde8e43f 218
219done_testing;