new failing test case for method conflict detection in roles
[gitmo/Moose.git] / t / roles / role_conflict_edge_cases.t
CommitLineData
d30bc041 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
d30bc041 8
d30bc041 9=pod
10
d03bd989 11Check for repeated inheritance causing
12a method conflict (which is not really
d30bc041 13a conflict)
14
15=cut
16
17{
18 package Role::Base;
d30bc041 19 use Moose::Role;
d03bd989 20
d30bc041 21 sub foo { 'Role::Base::foo' }
d03bd989 22
d30bc041 23 package Role::Derived1;
d03bd989 24 use Moose::Role;
25
d30bc041 26 with 'Role::Base';
d03bd989 27
d30bc041 28 package Role::Derived2;
d03bd989 29 use Moose::Role;
d30bc041 30
31 with 'Role::Base';
d03bd989 32
d30bc041 33 package My::Test::Class1;
d03bd989 34 use Moose;
35
b10dde3a 36 ::is( ::exception {
d03bd989 37 with 'Role::Derived1', 'Role::Derived2';
b10dde3a 38 }, undef, '... roles composed okay (no conflicts)' );
d30bc041 39}
40
41ok(Role::Base->meta->has_method('foo'), '... have the method foo as expected');
42ok(Role::Derived1->meta->has_method('foo'), '... have the method foo as expected');
43ok(Role::Derived2->meta->has_method('foo'), '... have the method foo as expected');
44ok(My::Test::Class1->meta->has_method('foo'), '... have the method foo as expected');
45
46is(My::Test::Class1->foo, 'Role::Base::foo', '... got the right value from method');
0558683c 47
48=pod
49
d03bd989 50Check for repeated inheritance causing
51a method conflict with method modifiers
0558683c 52(which is not really a conflict)
53
54=cut
55
56{
57 package Role::Base2;
58 use Moose::Role;
d03bd989 59
0558683c 60 override 'foo' => sub { super() . ' -> Role::Base::foo' };
d03bd989 61
0558683c 62 package Role::Derived3;
d03bd989 63 use Moose::Role;
64
0558683c 65 with 'Role::Base2';
d03bd989 66
0558683c 67 package Role::Derived4;
d03bd989 68 use Moose::Role;
0558683c 69
70 with 'Role::Base2';
71
72 package My::Test::Class2::Base;
73 use Moose;
d03bd989 74
0558683c 75 sub foo { 'My::Test::Class2::Base' }
d03bd989 76
0558683c 77 package My::Test::Class2;
d03bd989 78 use Moose;
79
80 extends 'My::Test::Class2::Base';
81
b10dde3a 82 ::is( ::exception {
d03bd989 83 with 'Role::Derived3', 'Role::Derived4';
b10dde3a 84 }, undef, '... roles composed okay (no conflicts)' );
0558683c 85}
86
87ok(Role::Base2->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
88ok(Role::Derived3->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
89ok(Role::Derived4->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
90ok(My::Test::Class2->meta->has_method('foo'), '... have the method foo as expected');
74862722 91isa_ok(My::Test::Class2->meta->get_method('foo'), 'Moose::Meta::Method::Overridden');
0558683c 92ok(My::Test::Class2::Base->meta->has_method('foo'), '... have the method foo as expected');
93isa_ok(My::Test::Class2::Base->meta->get_method('foo'), 'Class::MOP::Method');
94
95is(My::Test::Class2::Base->foo, 'My::Test::Class2::Base', '... got the right value from method');
96is(My::Test::Class2->foo, 'My::Test::Class2::Base -> Role::Base::foo', '... got the right value from method');
97
98=pod
99
d03bd989 100Check for repeated inheritance of the
101same code. There are no conflicts with
0558683c 102before/around/after method modifiers.
103
d03bd989 104This tests around, but should work the
0558683c 105same for before/afters as well
106
107=cut
108
109{
110 package Role::Base3;
111 use Moose::Role;
d03bd989 112
0558683c 113 around 'foo' => sub { 'Role::Base::foo(' . (shift)->() . ')' };
d03bd989 114
0558683c 115 package Role::Derived5;
d03bd989 116 use Moose::Role;
117
0558683c 118 with 'Role::Base3';
d03bd989 119
0558683c 120 package Role::Derived6;
d03bd989 121 use Moose::Role;
0558683c 122
123 with 'Role::Base3';
124
125 package My::Test::Class3::Base;
126 use Moose;
d03bd989 127
0558683c 128 sub foo { 'My::Test::Class3::Base' }
d03bd989 129
0558683c 130 package My::Test::Class3;
d03bd989 131 use Moose;
132
133 extends 'My::Test::Class3::Base';
134
b10dde3a 135 ::is( ::exception {
d03bd989 136 with 'Role::Derived5', 'Role::Derived6';
b10dde3a 137 }, undef, '... roles composed okay (no conflicts)' );
0558683c 138}
139
140ok(Role::Base3->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
141ok(Role::Derived5->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
142ok(Role::Derived6->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
143ok(My::Test::Class3->meta->has_method('foo'), '... have the method foo as expected');
144isa_ok(My::Test::Class3->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
145ok(My::Test::Class3::Base->meta->has_method('foo'), '... have the method foo as expected');
146isa_ok(My::Test::Class3::Base->meta->get_method('foo'), 'Class::MOP::Method');
147
148is(My::Test::Class3::Base->foo, 'My::Test::Class3::Base', '... got the right value from method');
149is(My::Test::Class3->foo, 'Role::Base::foo(My::Test::Class3::Base)', '... got the right value from method');
d30bc041 150
151=pod
152
d03bd989 153Check for repeated inheritance causing
154a attr conflict (which is not really
a2eec5e7 155a conflict)
156
157=cut
158
159{
160 package Role::Base4;
a2eec5e7 161 use Moose::Role;
d03bd989 162
a2eec5e7 163 has 'foo' => (is => 'ro', default => 'Role::Base::foo');
d03bd989 164
a2eec5e7 165 package Role::Derived7;
d03bd989 166 use Moose::Role;
167
a2eec5e7 168 with 'Role::Base4';
d03bd989 169
a2eec5e7 170 package Role::Derived8;
d03bd989 171 use Moose::Role;
a2eec5e7 172
173 with 'Role::Base4';
d03bd989 174
a2eec5e7 175 package My::Test::Class4;
d03bd989 176 use Moose;
177
b10dde3a 178 ::is( ::exception {
d03bd989 179 with 'Role::Derived7', 'Role::Derived8';
b10dde3a 180 }, undef, '... roles composed okay (no conflicts)' );
a2eec5e7 181}
182
183ok(Role::Base4->meta->has_attribute('foo'), '... have the attribute foo as expected');
184ok(Role::Derived7->meta->has_attribute('foo'), '... have the attribute foo as expected');
185ok(Role::Derived8->meta->has_attribute('foo'), '... have the attribute foo as expected');
186ok(My::Test::Class4->meta->has_attribute('foo'), '... have the attribute foo as expected');
187
188is(My::Test::Class4->new->foo, 'Role::Base::foo', '... got the right value from method');
a28e50e4 189
bfb7eea9 190
191TODO:
192{
193 {
194 package Failing::Z;
195 use Moose::Role;
196 }
197 {
198 package Failing::A;
199 use Moose::Role;
200 sub foo { 'A' }
201 }
202 {
203 package Failing::B;
204 use Moose::Role;
205 with 'Failing::A';
206 sub foo { 'B' }
207 }
208 {
209 package Failing::C;
210 use Moose;
211
212 # this reports a conflict, as it should
213 #with 'B';
214
215 our $TODO;
216 local $TODO = 'the existence of an unrelated role is interfering somehow -- need to investigate';
217 ::like(
218 ::exception { with 'Failing::Z', 'Failing::B' },
219 qr/method name conflict/,
220 'these roles should not compose without conflicts',
221 );
222 }
223}
224
a28e50e4 225done_testing;