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