11 This tests the more complex
12 delegation cases and that they
13 do not fail at compile time.
22 sub child_a_super_method { "as" }
27 extends "ChildASuper";
29 sub child_a_method_1 { "a1" }
30 sub child_a_method_2 { Scalar::Util::blessed($_[0]) . " a2" }
37 sub child_a_method_3 { "a3" }
42 sub child_b_method_1 { "b1" }
43 sub child_b_method_2 { "b2" }
44 sub child_b_method_3 { "b3" }
49 sub child_c_method_1 { "c1" }
50 sub child_c_method_2 { "c2" }
51 sub child_c_method_3_la { "c3" }
52 sub child_c_method_4_la { "c4" }
57 sub child_d_method_1 { "d1" }
58 sub child_d_method_2 { "d2" }
63 sub new { bless {}, shift }
64 sub child_e_method_1 { "e1" }
65 sub child_e_method_2 { "e2" }
70 sub new { bless {}, shift }
71 sub child_f_method_1 { "f1" }
72 sub child_f_method_2 { "f2" }
77 sub child_g_method_1 { "g1" }
82 sub child_h_method_1 { "h1" }
83 sub parent_method_1 { "child_parent_1" }
88 sub child_i_method_1 { "i1" }
89 sub parent_method_1 { "child_parent_1" }
94 sub parent_method_1 { "parent_1" }
95 ::can_ok('Parent', 'parent_method_1');
100 default => sub { ChildA->new },
103 }, undef, "all_methods requires explicit isa" );
109 default => sub { ChildA->new },
112 }, undef, "allow all_methods with explicit isa" );
117 default => sub { ChildB->new },
118 handles => [qw/child_b_method_1/],
120 }, undef, "don't need to declare isa if method list is predefined" );
126 default => sub { ChildC->new },
129 }, undef, "can declare regex collector" );
131 ::isnt( ::exception {
134 default => sub { ChildD->new },
136 my ( $class, $delegate_class ) = @_;
139 }, undef, "can't create attr with generative handles parameter and no isa" );
145 default => sub { ChildD->new },
147 my ( $class, $delegate_class ) = @_;
151 }, undef, "can't create attr with generative handles parameter and no isa" );
157 default => sub { ChildE->new },
158 handles => ["child_e_method_2"],
160 }, undef, "can delegate to non moose class using explicit method list" );
167 default => sub { ChildF->new },
169 $delegate_class = $_[1]->name;
173 }, undef, "subrefs on non moose class give no meta" );
175 ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );
180 default => sub { ChildG->new },
181 handles => ["child_g_method_1"],
183 }, undef, "can delegate to object even without explicit reader" );
185 ::can_ok('Parent', 'parent_method_1');
186 ::isnt( ::exception {
190 default => sub { ChildH->new },
191 handles => sub { map { $_, $_ } $_[1]->get_all_method_names },
193 }, undef, "Can't override exisiting class method in delegate" );
194 ::can_ok('Parent', 'parent_method_1');
200 default => sub { ChildI->new },
202 map { $_, $_ } grep { !/^parent_method_1|meta$/ }
203 $_[1]->get_all_method_names;
206 }, undef, "Test handles code ref for skipping predefined methods" );
209 sub parent_method { "p" }
214 isa_ok( my $p = Parent->new, "Parent" );
215 isa_ok( $p->child_a, "ChildA" );
216 isa_ok( $p->child_b, "ChildB" );
217 isa_ok( $p->child_c, "ChildC" );
218 isa_ok( $p->child_d, "ChildD" );
219 isa_ok( $p->child_e, "ChildE" );
220 isa_ok( $p->child_f, "ChildF" );
221 isa_ok( $p->child_i, "ChildI" );
223 ok(!$p->can('child_g'), '... no child_g accessor defined');
224 ok(!$p->can('child_h'), '... no child_h accessor defined');
227 is( $p->parent_method, "p", "parent method" );
228 is( $p->child_a->child_a_super_method, "as", "child supermethod" );
229 is( $p->child_a->child_a_method_1, "a1", "child method" );
231 can_ok( $p, "child_a_super_method" );
232 can_ok( $p, "child_a_method_1" );
233 can_ok( $p, "child_a_method_2" );
234 ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );
236 is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
237 is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );
240 can_ok( $p, "child_b_method_1" );
241 ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );
244 ok( !$p->can($_), "none of ChildD's methods ($_)" )
245 for grep { /^child/ } map { $_->name } ChildD->meta->get_all_methods();
247 can_ok( $p, "child_c_method_3_la" );
248 can_ok( $p, "child_c_method_4_la" );
250 is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );
252 can_ok( $p, "child_e_method_2" );
253 ok( !$p->can("child_e_method_1"), "but not child_e_method_1");
255 is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );
257 can_ok( $p, "child_g_method_1" );
258 is( $p->child_g_method_1, "g1", "delegate to moose class without reader (child_g_method_1)" );
260 can_ok( $p, "child_i_method_1" );
261 is( $p->parent_method_1, "parent_1", "delegate doesn't override existing method" );