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 },
73 handles => all_methods,
75 } "all_methods requires explicit isa";
81 default => sub { ChildA->new },
82 handles => all_methods,
84 } "allow all_methods with explicit isa";
88 default => sub { ChildB->new },
89 handles => [qw/child_b_method_1/],
91 } "don't need to declare isa if method list is predefined";
97 default => sub { ChildC->new },
100 } "can declare regex collector";
105 default => sub { ChildD->new },
107 my ( $class, $delegate_class ) = @_;
110 } "can't create attr with generative handles parameter and no isa";
116 default => sub { ChildD->new },
118 my ( $class, $delegate_class ) = @_;
122 } "can't create attr with generative handles parameter and no isa";
128 default => sub { ChildE->new },
129 handles => "child_e_method_2",
131 } "can delegate to non moose class using explicit method list";
138 default => sub { ChildF->new },
140 $delegate_class = $_[1]->name;
143 } "subrefs on non moose class give no meta";
145 ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );
147 sub parent_method { "p" }
152 isa_ok( my $p = Parent->new, "Parent" );
153 isa_ok( $p->child_a, "ChildA" );
154 ok( !$p->can("child_b"), "no child b accessor" );
155 isa_ok( $p->child_c, "ChildC" );
156 isa_ok( $p->child_d, "ChildD" );
157 isa_ok( $p->child_e, "ChildE" );
158 isa_ok( $p->child_f, "ChildF" );
161 is( $p->parent_method, "p", "parent method" );
162 is( $p->child_a->child_a_super_method, "as", "child supermethod" );
163 is( $p->child_a->child_a_method_1, "a1", "child method" );
165 can_ok( $p, "child_a_super_method" );
166 can_ok( $p, "child_a_method_1" );
167 can_ok( $p, "child_a_method_2" );
168 ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );
170 is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
171 is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );
174 can_ok( $p, "child_b_method_1" );
175 ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );
178 ok( !$p->can($_), "none of ChildD's methods ($_)" )
179 for grep { /^child/ } map { $_->{name} } ChildD->meta->compute_all_applicable_methods();
181 can_ok( $p, "child_c_method_3_la" );
182 can_ok( $p, "child_c_method_4_la" );
184 is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );
186 can_ok( $p, "child_e_method_2" );
187 ok( !$p->can("child_e_method_1"), "but not child_e_method_1");
189 is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );