Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / Moose-t-failing / 030_roles / 007_roles_and_req_method_edge_cases.t
CommitLineData
67199842 1#!/usr/bin/perl
c47cf415 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
67199842 5
6use strict;
7use warnings;
8
c47cf415 9use Test::More;
10$TODO = q{Mouse is not yet completed};
67199842 11use Test::Exception;
12
13=pod
14
15NOTE:
6cfa1e5e 16A fair amount of these tests will likely be irrelevant
67199842 17once we have more fine grained control over the class
18building process. A lot of the edge cases tested here
6cfa1e5e 19are actually related to class construction order and
67199842 20not any real functionality.
21- SL
22
6cfa1e5e 23Role which requires a method implemented
24in another role as an override (it does
67199842 25not remove the requirement)
26
27=cut
28
29{
30 package Role::RequireFoo;
31 use strict;
32 use warnings;
33 use Mouse::Role;
6cfa1e5e 34
67199842 35 requires 'foo';
6cfa1e5e 36
67199842 37 package Role::ProvideFoo;
38 use strict;
39 use warnings;
40 use Mouse::Role;
6cfa1e5e 41
67199842 42 ::lives_ok {
43 with 'Role::RequireFoo';
44 } '... the required "foo" method will not exist yet (but we will live)';
6cfa1e5e 45
46 override 'foo' => sub { 'Role::ProvideFoo::foo' };
67199842 47}
48
49is_deeply(
6cfa1e5e 50 [ Role::ProvideFoo->meta->get_required_method_list ],
51 [ 'foo' ],
67199842 52 '... foo method is still required for Role::ProvideFoo');
53
54=pod
55
6cfa1e5e 56Role which requires a method implemented
57in the consuming class as an override.
58It will fail since method modifiers are
67199842 59second class citizens.
60
61=cut
62
63{
64 package Class::ProvideFoo::Base;
65 use Mouse;
66
67 sub foo { 'Class::ProvideFoo::Base::foo' }
6cfa1e5e 68
67199842 69 package Class::ProvideFoo::Override1;
70 use Mouse;
6cfa1e5e 71
67199842 72 extends 'Class::ProvideFoo::Base';
6cfa1e5e 73
67199842 74 ::lives_ok {
75 with 'Role::RequireFoo';
76 } '... the required "foo" method will be found in the superclass';
6cfa1e5e 77
78 override 'foo' => sub { 'Class::ProvideFoo::foo' };
79
67199842 80 package Class::ProvideFoo::Override2;
81 use Mouse;
6cfa1e5e 82
67199842 83 extends 'Class::ProvideFoo::Base';
6cfa1e5e 84
85 override 'foo' => sub { 'Class::ProvideFoo::foo' };
86
67199842 87 ::lives_ok {
88 with 'Role::RequireFoo';
89 } '... the required "foo" method exists, although it is overriden locally';
90
91}
92
93=pod
94
6cfa1e5e 95Now same thing, but with a before
67199842 96method modifier.
97
98=cut
99
100{
101 package Class::ProvideFoo::Before1;
102 use Mouse;
6cfa1e5e 103
67199842 104 extends 'Class::ProvideFoo::Base';
6cfa1e5e 105
67199842 106 ::lives_ok {
107 with 'Role::RequireFoo';
108 } '... the required "foo" method will be found in the superclass';
6cfa1e5e 109
110 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
111
67199842 112 package Class::ProvideFoo::Before2;
113 use Mouse;
6cfa1e5e 114
67199842 115 extends 'Class::ProvideFoo::Base';
6cfa1e5e 116
117 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
118
67199842 119 ::lives_ok {
120 with 'Role::RequireFoo';
6cfa1e5e 121 } '... the required "foo" method exists, although it is a before modifier locally';
122
67199842 123 package Class::ProvideFoo::Before3;
124 use Mouse;
6cfa1e5e 125
67199842 126 extends 'Class::ProvideFoo::Base';
6cfa1e5e 127
67199842 128 sub foo { 'Class::ProvideFoo::foo' }
6cfa1e5e 129 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
130
67199842 131 ::lives_ok {
132 with 'Role::RequireFoo';
6cfa1e5e 133 } '... the required "foo" method exists locally, and it is modified locally';
134
67199842 135 package Class::ProvideFoo::Before4;
136 use Mouse;
6cfa1e5e 137
67199842 138 extends 'Class::ProvideFoo::Base';
6cfa1e5e 139
140 sub foo { 'Class::ProvideFoo::foo' }
141 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
67199842 142
c47cf415 143 ::isa_ok(__PACKAGE__->meta->get_method('foo'), 'Mouse::Meta::Method');
6cfa1e5e 144 ::is(__PACKAGE__->meta->get_method('foo')->get_original_method->package_name, __PACKAGE__,
67199842 145 '... but the original method is from our package');
6cfa1e5e 146
67199842 147 ::lives_ok {
148 with 'Role::RequireFoo';
6cfa1e5e 149 } '... the required "foo" method exists in the symbol table (and we will live)';
150
151}
67199842 152
153=pod
154
155Now same thing, but with a method from an attribute
156method modifier.
157
158=cut
159
160{
6cfa1e5e 161
67199842 162 package Class::ProvideFoo::Attr1;
163 use Mouse;
6cfa1e5e 164
67199842 165 extends 'Class::ProvideFoo::Base';
6cfa1e5e 166
67199842 167 ::lives_ok {
168 with 'Role::RequireFoo';
169 } '... the required "foo" method will be found in the superclass (but then overriden)';
6cfa1e5e 170
67199842 171 has 'foo' => (is => 'ro');
6cfa1e5e 172
67199842 173 package Class::ProvideFoo::Attr2;
174 use Mouse;
6cfa1e5e 175
67199842 176 extends 'Class::ProvideFoo::Base';
6cfa1e5e 177
178 has 'foo' => (is => 'ro');
179
67199842 180 ::lives_ok {
181 with 'Role::RequireFoo';
182 } '... the required "foo" method exists, and is an accessor';
6cfa1e5e 183}
67199842 184
185# ...
6cfa1e5e 186# a method required in a role, but then
187# implemented in the superclass (as an
67199842 188# attribute accessor too)
6cfa1e5e 189
67199842 190{
191 package Foo::Class::Base;
192 use Mouse;
6cfa1e5e 193
194 has 'bar' => (
195 isa => 'Int',
196 is => 'rw',
67199842 197 default => sub { 1 }
198 );
199}
200{
201 package Foo::Role;
202 use Mouse::Role;
6cfa1e5e 203
67199842 204 requires 'bar';
6cfa1e5e 205
206 has 'foo' => (
207 isa => 'Int',
208 is => 'rw',
209 lazy => 1,
210 default => sub { (shift)->bar + 1 }
67199842 211 );
212}
213{
214 package Foo::Class::Child;
215 use Mouse;
216 extends 'Foo::Class::Base';
6cfa1e5e 217
67199842 218 ::lives_ok {
219 with 'Foo::Role';
220 } '... our role combined successfully';
221}
222
223# a method required in a role and implemented in a superclass, with a method
224# modifier in the subclass. this should live, but dies in 0.26 -- hdp,
225# 2007-10-11
226
227{
228 package Bar::Class::Base;
229 use Mouse;
230
231 sub bar { "hello!" }
232}
233{
234 package Bar::Role;
235 use Mouse::Role;
236 requires 'bar';
237}
238{
239 package Bar::Class::Child;
240 use Mouse;
241 extends 'Bar::Class::Base';
242 after bar => sub { "o noes" };
243 # technically we could run lives_ok here, too, but putting it into a
244 # grandchild class makes it more obvious why this matters.
245}
246{
247 package Bar::Class::Grandchild;
248 use Mouse;
249 extends 'Bar::Class::Child';
250 ::lives_ok {
251 with 'Bar::Role';
252 } 'required method exists in superclass as non-modifier, so we live';
253}
254
255{
256 package Bar2::Class::Base;
257 use Mouse;
258
259 sub bar { "hello!" }
260}
261{
262 package Bar2::Role;
263 use Mouse::Role;
264 requires 'bar';
265}
266{
267 package Bar2::Class::Child;
268 use Mouse;
269 extends 'Bar2::Class::Base';
270 override bar => sub { "o noes" };
271 # technically we could run lives_ok here, too, but putting it into a
272 # grandchild class makes it more obvious why this matters.
273}
274{
275 package Bar2::Class::Grandchild;
276 use Mouse;
277 extends 'Bar2::Class::Child';
278 ::lives_ok {
279 with 'Bar2::Role';
280 } 'required method exists in superclass as non-modifier, so we live';
281}
c47cf415 282
283done_testing;