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