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