6 use Test::More tests => 35;
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" }
72 default => sub { ChildA->new },
75 } "all_methods requires explicit isa";
81 default => sub { ChildA->new },
84 } "allow all_methods with explicit isa";
89 default => sub { ChildB->new },
90 handles => [qw/child_b_method_1/],
92 } "don't need to declare isa if method list is predefined";
98 default => sub { ChildC->new },
101 } "can declare regex collector";
106 default => sub { ChildD->new },
108 my ( $class, $delegate_class ) = @_;
111 } "can't create attr with generative handles parameter and no isa";
117 default => sub { ChildD->new },
119 my ( $class, $delegate_class ) = @_;
123 } "can't create attr with generative handles parameter and no isa";
129 default => sub { ChildE->new },
130 handles => ["child_e_method_2"],
132 } "can delegate to non moose class using explicit method list";
139 default => sub { ChildF->new },
141 $delegate_class = $_[1]->name;
145 } "subrefs on non moose class give no meta";
147 ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );
149 sub parent_method { "p" }
154 isa_ok( my $p = Parent->new, "Parent" );
155 isa_ok( $p->child_a, "ChildA" );
156 isa_ok( $p->child_b, "ChildB" );
157 isa_ok( $p->child_c, "ChildC" );
158 isa_ok( $p->child_d, "ChildD" );
159 isa_ok( $p->child_e, "ChildE" );
160 isa_ok( $p->child_f, "ChildF" );
163 is( $p->parent_method, "p", "parent method" );
164 is( $p->child_a->child_a_super_method, "as", "child supermethod" );
165 is( $p->child_a->child_a_method_1, "a1", "child method" );
167 can_ok( $p, "child_a_super_method" );
168 can_ok( $p, "child_a_method_1" );
169 can_ok( $p, "child_a_method_2" );
170 ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );
172 is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
173 is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );
176 can_ok( $p, "child_b_method_1" );
177 ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );
180 ok( !$p->can($_), "none of ChildD's methods ($_)" )
181 for grep { /^child/ } map { $_->{name} } ChildD->meta->compute_all_applicable_methods();
183 can_ok( $p, "child_c_method_3_la" );
184 can_ok( $p, "child_c_method_4_la" );
186 is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );
188 can_ok( $p, "child_e_method_2" );
189 ok( !$p->can("child_e_method_1"), "but not child_e_method_1");
191 is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );