Changelogging
[gitmo/Mouse.git] / t-failing / 030_roles / 013_method_aliasing_in_composition.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 $TODO = q{Mouse is not yet completed};
11 use Test::Exception;
12
13
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' }
21
22     requires 'role_bar';
23
24     package My::Class;
25     use Mouse;
26
27     ::lives_ok {
28         with 'My::Role' => { -alias => { bar => 'role_bar' } };
29     } '... this succeeds';
30
31     package My::Class::Failure;
32     use Mouse;
33
34     ::throws_ok {
35         with 'My::Role' => { -alias => { bar => 'role_bar' } };
36     } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
37
38     sub role_bar { 'FAIL' }
39 }
40
41 ok(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 {
48         with 'My::Role' => { -alias => { bar => 'role_bar' } };
49     } '... this succeeds';
50
51     sub bar { 'My::OtherRole::bar' }
52
53     package My::OtherRole::Failure;
54     use Mouse::Role;
55
56     ::throws_ok {
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';
59
60     sub role_bar { 'FAIL' }
61 }
62
63 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
64 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
65 ok(!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 {
72         with 'My::Role' => { -alias => { bar => 'role_bar' } };
73     } '... this succeeds';
74 }
75
76 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
77 ok(!My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is not required');
78
79 {
80     package Foo::Role;
81     use Mouse::Role;
82
83     sub foo { 'Foo::Role::foo' }
84
85     package Bar::Role;
86     use Mouse::Role;
87
88     sub foo { 'Bar::Role::foo' }
89
90     package Baz::Role;
91     use Mouse::Role;
92
93     sub foo { 'Baz::Role::foo' }
94
95     package My::Foo::Class;
96     use Mouse;
97
98     ::lives_ok {
99         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
100              'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
101              'Baz::Role';
102     } '... composed our roles correctly';
103
104     package My::Foo::Class::Broken;
105     use Mouse;
106
107     ::throws_ok {
108         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
109              'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
110              'Baz::Role';
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';
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');
120     is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
121     is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
122 }
123
124 {
125     package My::Foo::Role;
126     use Mouse::Role;
127
128     ::lives_ok {
129         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
130              'Bar::Role' => { -alias => { 'foo' => 'bar_foo' }, -excludes => 'foo' },
131              'Baz::Role';
132     } '... composed our roles correctly';
133 }
134
135 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
136 ok(!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 {
144         with 'Foo::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
145              'Bar::Role' => { -alias => { 'foo' => 'foo_foo' }, -excludes => 'foo' },
146              'Baz::Role';
147     } '... composed our roles correctly';
148 }
149
150 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
151 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
152
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
162 ok(My::Foo::AliasOnly->meta->has_method('foo'), 'we have a foo method');
163 ok(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 }
218
219 done_testing;