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