6 use Test::More tests => 39;
14 sub child_a_super_method { "as" }
19 extends "ChildASuper";
21 sub child_a_method_1 { "a1" }
22 sub child_a_method_2 { Scalar::Util::blessed($_[0]) . " a2" }
29 sub child_a_method_3 { "a3" }
34 sub child_b_method_1 { "b1" }
35 sub child_b_method_2 { "b2" }
36 sub child_b_method_3 { "b3" }
41 sub child_c_method_1 { "c1" }
42 sub child_c_method_2 { "c2" }
43 sub child_c_method_3_la { "c3" }
44 sub child_c_method_4_la { "c4" }
49 sub child_d_method_1 { "d1" }
50 sub child_d_method_2 { "d2" }
55 sub new { bless {}, shift }
56 sub child_e_method_1 { "e1" }
57 sub child_e_method_2 { "e2" }
62 sub new { bless {}, shift }
63 sub child_f_method_1 { "f1" }
64 sub child_f_method_2 { "f2" }
69 sub child_g_method_1 { "g1" }
77 default => sub { ChildA->new },
80 } "all_methods requires explicit isa";
86 default => sub { ChildA->new },
89 } "allow all_methods with explicit isa";
94 default => sub { ChildB->new },
95 handles => [qw/child_b_method_1/],
97 } "don't need to declare isa if method list is predefined";
103 default => sub { ChildC->new },
106 } "can declare regex collector";
111 default => sub { ChildD->new },
113 my ( $class, $delegate_class ) = @_;
116 } "can't create attr with generative handles parameter and no isa";
122 default => sub { ChildD->new },
124 my ( $class, $delegate_class ) = @_;
128 } "can't create attr with generative handles parameter and no isa";
134 default => sub { ChildE->new },
135 handles => ["child_e_method_2"],
137 } "can delegate to non moose class using explicit method list";
144 default => sub { ChildF->new },
146 $delegate_class = $_[1]->name;
150 } "subrefs on non moose class give no meta";
152 ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );
157 default => sub { ChildG->new },
158 handles => ["child_g_method_1"],
160 } "can delegate to object even without explicit reader";
162 sub parent_method { "p" }
167 isa_ok( my $p = Parent->new, "Parent" );
168 isa_ok( $p->child_a, "ChildA" );
169 isa_ok( $p->child_b, "ChildB" );
170 isa_ok( $p->child_c, "ChildC" );
171 isa_ok( $p->child_d, "ChildD" );
172 isa_ok( $p->child_e, "ChildE" );
173 isa_ok( $p->child_f, "ChildF" );
175 ok(!$p->can('child_g'), '... no child_g accessor defined');
178 is( $p->parent_method, "p", "parent method" );
179 is( $p->child_a->child_a_super_method, "as", "child supermethod" );
180 is( $p->child_a->child_a_method_1, "a1", "child method" );
182 can_ok( $p, "child_a_super_method" );
183 can_ok( $p, "child_a_method_1" );
184 can_ok( $p, "child_a_method_2" );
185 ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );
187 is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
188 is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );
191 can_ok( $p, "child_b_method_1" );
192 ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );
195 ok( !$p->can($_), "none of ChildD's methods ($_)" )
196 for grep { /^child/ } map { $_->{name} } ChildD->meta->compute_all_applicable_methods();
198 can_ok( $p, "child_c_method_3_la" );
199 can_ok( $p, "child_c_method_4_la" );
201 is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );
203 can_ok( $p, "child_e_method_2" );
204 ok( !$p->can("child_e_method_1"), "but not child_e_method_1");
206 is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );
208 can_ok( $p, "child_g_method_1" );
209 is( $p->child_g_method_1, "g1", "delegate to moose class without reader (child_g_method_1)" );