b681caf5e38949fb1a3e4b1e7a3e40f6a576cd07
[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 => 14;
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 Moose::Role;
25     
26     sub foo { 'Role::Base::foo' }
27     
28     package Role::Derived1;
29     use Moose::Role;  
30     
31     with 'Role::Base';
32     
33     package Role::Derived2;
34     use Moose::Role; 
35
36     with 'Role::Base';
37     
38     package My::Test::Class1;
39     use Moose;      
40     
41     ::lives_ok {
42         with 'Role::Derived1', 'Role::Derived2';   
43     } '... roles composed okay (no conflicts)';
44 }
45
46 ok(Role::Base->meta->has_method('foo'), '... have the method foo as expected');
47 ok(Role::Derived1->meta->has_method('foo'), '... have the method foo as expected');
48 ok(Role::Derived2->meta->has_method('foo'), '... have the method foo as expected');
49 ok(My::Test::Class1->meta->has_method('foo'), '... have the method foo as expected');
50
51 is(My::Test::Class1->foo, 'Role::Base::foo', '... got the right value from method');
52
53 =pod
54
55 Check for repeated inheritence causing 
56 a method conflict with method modifiers 
57 (which is not really a conflict)
58
59 =cut
60
61 =begin nonesense
62
63 {
64     package Role::Base2;
65     use Moose::Role;
66     
67     override 'foo' => sub { super() . ' -> Role::Base::foo' };
68     
69     package Role::Derived3;
70     use Moose::Role;  
71     
72     with 'Role::Base2';
73     
74     package Role::Derived4;
75     use Moose::Role; 
76
77     with 'Role::Base2';
78
79     package My::Test::Class2::Base;
80     use Moose;
81     
82     sub foo { 'My::Test::Class2::Base' }
83     
84     package My::Test::Class2;
85     use Moose;  
86     
87     extends 'My::Test::Class2::Base';    
88     
89     ::lives_ok {
90         with 'Role::Derived3', 'Role::Derived4';   
91     } '... roles composed okay (no conflicts)';
92 }
93
94 ok(Role::Base2->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
95 ok(Role::Derived3->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
96 ok(Role::Derived4->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
97 ok(My::Test::Class2->meta->has_method('foo'), '... have the method foo as expected');
98 isa_ok(My::Test::Class2->meta->get_method('foo'), 'Moose::Meta::Method::Overriden');
99 ok(My::Test::Class2::Base->meta->has_method('foo'), '... have the method foo as expected');
100 isa_ok(My::Test::Class2::Base->meta->get_method('foo'), 'Class::MOP::Method');
101
102 is(My::Test::Class2::Base->foo, 'My::Test::Class2::Base', '... got the right value from method');
103 is(My::Test::Class2->foo, 'My::Test::Class2::Base -> Role::Base::foo', '... got the right value from method');
104
105 =cut
106
107 =pod
108
109 Check for repeated inheritence of the 
110 same code. There are no conflicts with 
111 before/around/after method modifiers.
112
113 This tests around, but should work the 
114 same for before/afters as well
115
116 =cut
117
118 =begin nonesense
119
120 {
121     package Role::Base3;
122     use Moose::Role;
123     
124     around 'foo' => sub { 'Role::Base::foo(' . (shift)->() . ')' };
125     
126     package Role::Derived5;
127     use Moose::Role;  
128     
129     with 'Role::Base3';
130     
131     package Role::Derived6;
132     use Moose::Role; 
133
134     with 'Role::Base3';
135
136     package My::Test::Class3::Base;
137     use Moose;
138     
139     sub foo { 'My::Test::Class3::Base' }
140     
141     package My::Test::Class3;
142     use Moose;  
143     
144     extends 'My::Test::Class3::Base';    
145     
146     ::lives_ok {
147         with 'Role::Derived5', 'Role::Derived6';   
148     } '... roles composed okay (no conflicts)';
149 }
150
151 ok(Role::Base3->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
152 ok(Role::Derived5->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
153 ok(Role::Derived6->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
154 ok(My::Test::Class3->meta->has_method('foo'), '... have the method foo as expected');
155 isa_ok(My::Test::Class3->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
156 ok(My::Test::Class3::Base->meta->has_method('foo'), '... have the method foo as expected');
157 isa_ok(My::Test::Class3::Base->meta->get_method('foo'), 'Class::MOP::Method');
158
159 is(My::Test::Class3::Base->foo, 'My::Test::Class3::Base', '... got the right value from method');
160 is(My::Test::Class3->foo, 'Role::Base::foo(My::Test::Class3::Base)', '... got the right value from method');
161
162 =cut
163
164 =pod
165
166 Check for repeated inheritence causing 
167 a attr conflict (which is not really 
168 a conflict)
169
170 =cut
171
172 {
173     package Role::Base4;
174     use Moose::Role;
175     
176     has 'foo' => (is => 'ro', default => 'Role::Base::foo');
177     
178     package Role::Derived7;
179     use Moose::Role;  
180     
181     with 'Role::Base4';
182     
183     package Role::Derived8;
184     use Moose::Role; 
185
186     with 'Role::Base4';
187     
188     package My::Test::Class4;
189     use Moose;      
190     
191     ::lives_ok {
192         with 'Role::Derived7', 'Role::Derived8';   
193     } '... roles composed okay (no conflicts)';
194 }
195
196 ok(Role::Base4->meta->has_attribute('foo'), '... have the attribute foo as expected');
197 ok(Role::Derived7->meta->has_attribute('foo'), '... have the attribute foo as expected');
198 ok(Role::Derived8->meta->has_attribute('foo'), '... have the attribute foo as expected');
199 ok(My::Test::Class4->meta->has_attribute('foo'), '... have the attribute foo as expected');
200
201 is(My::Test::Class4->new->foo, 'Role::Base::foo', '... got the right value from method');