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