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