putting the method modifiers in roles back in, we have to maintain backwards compat...
[gitmo/Moose.git] / t / 047_role_conflict_edge_cases.t
CommitLineData
d30bc041 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
0558683c 6use Test::More tests => 34;
d30bc041 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11 use_ok('Moose::Role');
12}
13
14=pod
15
16Check for repeated inheritence causing
17a method conflict (which is not really
18a conflict)
19
20=cut
21
22{
23 package Role::Base;
d30bc041 24 use Moose::Role;
25
26 sub foo { 'Role::Base::foo' }
27
28 package Role::Derived1;
d30bc041 29 use Moose::Role;
30
31 with 'Role::Base';
32
33 package Role::Derived2;
d30bc041 34 use Moose::Role;
35
36 with 'Role::Base';
37
38 package My::Test::Class1;
d30bc041 39 use Moose;
40
41 ::lives_ok {
42 with 'Role::Derived1', 'Role::Derived2';
43 } '... roles composed okay (no conflicts)';
44}
45
46ok(Role::Base->meta->has_method('foo'), '... have the method foo as expected');
47ok(Role::Derived1->meta->has_method('foo'), '... have the method foo as expected');
48ok(Role::Derived2->meta->has_method('foo'), '... have the method foo as expected');
49ok(My::Test::Class1->meta->has_method('foo'), '... have the method foo as expected');
50
51is(My::Test::Class1->foo, 'Role::Base::foo', '... got the right value from method');
0558683c 52
53=pod
54
55Check for repeated inheritence causing
56a 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
92ok(Role::Base2->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
93ok(Role::Derived3->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
94ok(Role::Derived4->meta->has_override_method_modifier('foo'), '... have the method foo as expected');
95ok(My::Test::Class2->meta->has_method('foo'), '... have the method foo as expected');
96isa_ok(My::Test::Class2->meta->get_method('foo'), 'Moose::Meta::Method::Overriden');
97ok(My::Test::Class2::Base->meta->has_method('foo'), '... have the method foo as expected');
98isa_ok(My::Test::Class2::Base->meta->get_method('foo'), 'Class::MOP::Method');
99
100is(My::Test::Class2::Base->foo, 'My::Test::Class2::Base', '... got the right value from method');
101is(My::Test::Class2->foo, 'My::Test::Class2::Base -> Role::Base::foo', '... got the right value from method');
102
103=pod
104
105Check for repeated inheritence of the
106same code. There are no conflicts with
107before/around/after method modifiers.
108
109This tests around, but should work the
110same 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
145ok(Role::Base3->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
146ok(Role::Derived5->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
147ok(Role::Derived6->meta->has_around_method_modifiers('foo'), '... have the method foo as expected');
148ok(My::Test::Class3->meta->has_method('foo'), '... have the method foo as expected');
149isa_ok(My::Test::Class3->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
150ok(My::Test::Class3::Base->meta->has_method('foo'), '... have the method foo as expected');
151isa_ok(My::Test::Class3::Base->meta->get_method('foo'), 'Class::MOP::Method');
152
153is(My::Test::Class3::Base->foo, 'My::Test::Class3::Base', '... got the right value from method');
154is(My::Test::Class3->foo, 'Role::Base::foo(My::Test::Class3::Base)', '... got the right value from method');
d30bc041 155
156=pod
157
158Check for repeated inheritence causing
a2eec5e7 159a attr conflict (which is not really
160a conflict)
161
162=cut
163
164{
165 package Role::Base4;
a2eec5e7 166 use Moose::Role;
167
168 has 'foo' => (is => 'ro', default => 'Role::Base::foo');
169
170 package Role::Derived7;
a2eec5e7 171 use Moose::Role;
172
173 with 'Role::Base4';
174
175 package Role::Derived8;
a2eec5e7 176 use Moose::Role;
177
178 with 'Role::Base4';
179
180 package My::Test::Class4;
a2eec5e7 181 use Moose;
182
183 ::lives_ok {
184 with 'Role::Derived7', 'Role::Derived8';
185 } '... roles composed okay (no conflicts)';
186}
187
188ok(Role::Base4->meta->has_attribute('foo'), '... have the attribute foo as expected');
189ok(Role::Derived7->meta->has_attribute('foo'), '... have the attribute foo as expected');
190ok(Role::Derived8->meta->has_attribute('foo'), '... have the attribute foo as expected');
191ok(My::Test::Class4->meta->has_attribute('foo'), '... have the attribute foo as expected');
192
193is(My::Test::Class4->new->foo, 'Role::Base::foo', '... got the right value from method');