6 use Test::More tests => 18;
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, but it is an override (and we will die)';
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, but it is a before (and we will die)';
128 package Class::ProvideFoo::Before3;
131 extends 'Class::ProvideFoo::Base';
134 with 'Role::RequireFoo';
135 } '... the required "foo" method will not exist yet (and we will die)';
137 sub foo { 'Class::ProvideFoo::foo' }
138 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
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)';
156 package Class::ProvideFoo::Before5;
159 extends 'Class::ProvideFoo::Base';
161 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
163 ::isa_ok(__PACKAGE__->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
164 ::isnt(__PACKAGE__->meta->get_method('foo')->get_original_method->package_name, __PACKAGE__,
165 '... but the original method is not from our package');
168 with 'Role::RequireFoo';
169 } '... the required "foo" method exists, but it is a before wrapping the super (and we will die)';
174 Now same thing, but with a method from an attribute
181 package Class::ProvideFoo::Attr1;
184 extends 'Class::ProvideFoo::Base';
187 with 'Role::RequireFoo';
188 } '... the required "foo" method will be found in the superclass (but then overriden)';
190 has 'foo' => (is => 'ro');
192 package Class::ProvideFoo::Attr2;
195 extends 'Class::ProvideFoo::Base';
197 has 'foo' => (is => 'ro');
200 with 'Role::RequireFoo';
201 } '... the required "foo" method exists, but it is a before (and we will die)';
205 # a method required in a role, but then
206 # implemented in the superclass (as an
207 # attribute accessor too)
210 package Foo::Class::Base;
229 default => sub { (shift)->bar + 1 }
233 package Foo::Class::Child;
235 extends 'Foo::Class::Base';
239 } '... our role combined successfully';