6 use Test::More tests => 15;
11 use_ok('Moose::Role');
16 Role which requires a method implemented
17 in another role as an override (it does
18 not remove the requirement)
23 package Role::RequireFoo;
33 package Role::ProvideFoo;
39 with 'Role::RequireFoo';
40 } '... the required "foo" method will not exist yet (but we will live)';
42 override 'foo' => sub { 'Role::ProvideFoo::foo' };
46 [ Role::ProvideFoo->meta->get_required_method_list ],
48 '... foo method is still required for Role::ProvideFoo');
54 Role which requires a method implemented
55 in the consuming class as an override.
56 It will fail since method modifiers are
57 second class citizens.
62 package Class::ProvideFoo::Base;
65 sub foo { 'Class::ProvideFoo::Base::foo' }
67 package Class::ProvideFoo::Override1;
70 extends 'Class::ProvideFoo::Base';
73 with 'Role::RequireFoo';
74 } '... the required "foo" method will not exist yet (and we will die)';
76 override 'foo' => sub { 'Class::ProvideFoo::foo' };
78 package Class::ProvideFoo::Override2;
81 extends 'Class::ProvideFoo::Base';
83 override 'foo' => sub { 'Class::ProvideFoo::foo' };
86 with 'Role::RequireFoo';
87 } '... the required "foo" method exists, but it is an override (and we will die)';
93 Now same thing, but with a before
99 package Class::ProvideFoo::Before1;
102 extends 'Class::ProvideFoo::Base';
105 with 'Role::RequireFoo';
106 } '... the required "foo" method will not exist yet (and we will die)';
108 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
110 package Class::ProvideFoo::Before2;
113 extends 'Class::ProvideFoo::Base';
115 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
118 with 'Role::RequireFoo';
119 } '... the required "foo" method exists, but it is a before (and we will die)';
121 package Class::ProvideFoo::Before3;
124 extends 'Class::ProvideFoo::Base';
127 with 'Role::RequireFoo';
128 } '... the required "foo" method will not exist yet (and we will die)';
130 sub foo { 'Class::ProvideFoo::foo' }
131 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
133 package Class::ProvideFoo::Before4;
136 extends 'Class::ProvideFoo::Base';
138 sub foo { 'Class::ProvideFoo::foo' }
139 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
141 ::isa_ok(__PACKAGE__->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
142 ::is(__PACKAGE__->meta->get_method('foo')->get_original_method->package_name, __PACKAGE__,
143 '... but the original method is from our package');
146 with 'Role::RequireFoo';
147 } '... the required "foo" method exists in the symbol table (and we will live)';
149 package Class::ProvideFoo::Before5;
152 extends 'Class::ProvideFoo::Base';
154 before 'foo' => sub { 'Class::ProvideFoo::foo:before' };
156 ::isa_ok(__PACKAGE__->meta->get_method('foo'), 'Class::MOP::Method::Wrapped');
157 ::isnt(__PACKAGE__->meta->get_method('foo')->get_original_method->package_name, __PACKAGE__,
158 '... but the original method is not from our package');
161 with 'Role::RequireFoo';
162 } '... the required "foo" method exists, but it is a before wrapping the super (and we will die)';
167 Now same thing, but with a method from an attribute
174 package Class::ProvideFoo::Attr1;
177 extends 'Class::ProvideFoo::Base';
180 with 'Role::RequireFoo';
181 } '... the required "foo" method will not exist yet (and we will die)';
183 has 'foo' => (is => 'ro');
185 package Class::ProvideFoo::Attr2;
188 extends 'Class::ProvideFoo::Base';
190 has 'foo' => (is => 'ro');
193 with 'Role::RequireFoo';
194 } '... the required "foo" method exists, but it is a before (and we will die)';