6 use Test::More tests => 17;
12 A fair amount of these tests will likely be irrelevant
13 once we have more fine grained control over the class
14 building process. A lot of the edge cases tested here
15 are actually related to class construction order and
16 not any real functionality.
23 use_ok('Moose::Role');
28 Role which requires a method implemented
29 in another role as an override (it does
30 not remove the requirement)
35 package Role::RequireFoo;
42 package Role::ProvideFoo;
48 with 'Role::RequireFoo';
49 } '... the required "foo" method will not exist yet (but we will live)';
51 override 'foo' => sub { 'Role::ProvideFoo::foo' };
55 [ Role::ProvideFoo->meta->get_required_method_list ],
57 '... foo method is still required for Role::ProvideFoo');
61 Role which requires a method implemented
62 in the consuming class as an override.
63 It will fail since method modifiers are
64 second class citizens.
69 package Class::ProvideFoo::Base;
72 sub foo { 'Class::ProvideFoo::Base::foo' }
74 package Class::ProvideFoo::Override1;
77 extends 'Class::ProvideFoo::Base';
80 with 'Role::RequireFoo';
81 } '... the required "foo" method will be found in the superclass';
83 override 'foo' => sub { 'Class::ProvideFoo::foo' };
85 package Class::ProvideFoo::Override2;
88 extends 'Class::ProvideFoo::Base';
90 override 'foo' => sub { 'Class::ProvideFoo::foo' };
93 with 'Role::RequireFoo';
94 } '... the required "foo" method exists, although it is overriden locally';
100 Now same thing, but with a before
106 package Class::ProvideFoo::Before1;
109 extends 'Class::ProvideFoo::Base';
112 with 'Role::RequireFoo';
113 } '... the required "foo" method will be found in the superclass';
115 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
117 package Class::ProvideFoo::Before2;
120 extends 'Class::ProvideFoo::Base';
122 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
125 with 'Role::RequireFoo';
126 } '... the required "foo" method exists, although it is a before modifier locally';
128 package Class::ProvideFoo::Before3;
131 extends 'Class::ProvideFoo::Base';
133 sub foo { 'Class::ProvideFoo::foo' }
134 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
137 with 'Role::RequireFoo';
138 } '... the required "foo" method exists locally, and it is modified locally';
140 package Class::ProvideFoo::Before4;
143 extends 'Class::ProvideFoo::Base';
145 sub foo { 'Class::ProvideFoo::foo' }
146 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
148 ::isa_ok(__PACKAGE__->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
149 ::is(__PACKAGE__->meta->get_method('foo')->get_original_method->package_name, __PACKAGE__,
150 '... but the original method is from our package');
153 with 'Role::RequireFoo';
154 } '... the required "foo" method exists in the symbol table (and we will live)';
160 Now same thing, but with a method from an attribute
167 package Class::ProvideFoo::Attr1;
170 extends 'Class::ProvideFoo::Base';
173 with 'Role::RequireFoo';
174 } '... the required "foo" method will be found in the superclass (but then overriden)';
176 has 'foo' => (is => 'ro');
178 package Class::ProvideFoo::Attr2;
181 extends 'Class::ProvideFoo::Base';
183 has 'foo' => (is => 'ro');
186 with 'Role::RequireFoo';
187 } '... the required "foo" method exists, but it is a before (and we will die)';
191 # a method required in a role, but then
192 # implemented in the superclass (as an
193 # attribute accessor too)
196 package Foo::Class::Base;
215 default => sub { (shift)->bar + 1 }
219 package Foo::Class::Child;
221 extends 'Foo::Class::Base';
225 } '... our role combined successfully';
228 # a method required in a role and implemented in a superclass, with a method
229 # modifier in the subclass. this should live, but dies in 0.26 -- hdp,
233 package Bar::Class::Base;
244 package Bar::Class::Child;
246 extends 'Bar::Class::Base';
247 after bar => sub { "o noes" };
248 # technically we could run lives_ok here, too, but putting it into a
249 # grandchild class makes it more obvious why this matters.
252 package Bar::Class::Grandchild;
254 extends 'Bar::Class::Child';
257 } 'required method exists in superclass as non-modifier, so we live';
261 package Bar2::Class::Base;
272 package Bar2::Class::Child;
274 extends 'Bar2::Class::Base';
275 override bar => sub { "o noes" };
276 # technically we could run lives_ok here, too, but putting it into a
277 # grandchild class makes it more obvious why this matters.
280 package Bar2::Class::Grandchild;
282 extends 'Bar2::Class::Child';
285 } 'required method exists in superclass as non-modifier, so we live';