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