Commit | Line | Data |
54b1cdf0 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
66559261 |
6 | use Test::More tests => 43; |
54b1cdf0 |
7 | use Test::Exception; |
8 | |
f4f3e701 |
9 | =pod |
10 | |
d03bd989 |
11 | This tests the more complex |
12 | delegation cases and that they |
f4f3e701 |
13 | do not fail at compile time. |
14 | |
15 | =cut |
16 | |
54b1cdf0 |
17 | { |
18 | |
19 | package ChildASuper; |
20 | use Moose; |
21 | |
22 | sub child_a_super_method { "as" } |
23 | |
24 | package ChildA; |
25 | use Moose; |
26 | |
27 | extends "ChildASuper"; |
28 | |
29 | sub child_a_method_1 { "a1" } |
30 | sub child_a_method_2 { Scalar::Util::blessed($_[0]) . " a2" } |
31 | |
32 | package ChildASub; |
33 | use Moose; |
34 | |
35 | extends "ChildA"; |
36 | |
37 | sub child_a_method_3 { "a3" } |
38 | |
39 | package ChildB; |
40 | use Moose; |
41 | |
42 | sub child_b_method_1 { "b1" } |
43 | sub child_b_method_2 { "b2" } |
44 | sub child_b_method_3 { "b3" } |
45 | |
46 | package ChildC; |
47 | use Moose; |
48 | |
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" } |
53 | |
54 | package ChildD; |
55 | use Moose; |
56 | |
57 | sub child_d_method_1 { "d1" } |
58 | sub child_d_method_2 { "d2" } |
59 | |
60 | package ChildE; |
61 | # no Moose |
62 | |
63 | sub new { bless {}, shift } |
64 | sub child_e_method_1 { "e1" } |
65 | sub child_e_method_2 { "e2" } |
66 | |
67 | package ChildF; |
68 | # no Moose |
69 | |
70 | sub new { bless {}, shift } |
71 | sub child_f_method_1 { "f1" } |
72 | sub child_f_method_2 { "f2" } |
73 | |
f3c4e20e |
74 | package ChildG; |
75 | use Moose; |
76 | |
77 | sub child_g_method_1 { "g1" } |
78 | |
66559261 |
79 | package ChildH; |
80 | use Moose; |
81 | |
82 | sub child_h_method_1 { "h1" } |
83 | sub parent_method_1 { "child_parent_1" } |
84 | |
85 | package ChildI; |
86 | use Moose; |
87 | |
88 | sub child_i_method_1 { "i1" } |
89 | sub parent_method_1 { "child_parent_1" } |
90 | |
54b1cdf0 |
91 | package Parent; |
92 | use Moose; |
93 | |
575ca974 |
94 | sub parent_method_1 { "parent_1" } |
66559261 |
95 | |
54b1cdf0 |
96 | ::dies_ok { |
97 | has child_a => ( |
98 | is => "ro", |
99 | default => sub { ChildA->new }, |
98aae381 |
100 | handles => qr/.*/, |
54b1cdf0 |
101 | ); |
102 | } "all_methods requires explicit isa"; |
103 | |
104 | ::lives_ok { |
105 | has child_a => ( |
106 | isa => "ChildA", |
107 | is => "ro", |
108 | default => sub { ChildA->new }, |
98aae381 |
109 | handles => qr/.*/, |
54b1cdf0 |
110 | ); |
111 | } "allow all_methods with explicit isa"; |
112 | |
113 | ::lives_ok { |
114 | has child_b => ( |
452bac1b |
115 | is => 'ro', |
54b1cdf0 |
116 | default => sub { ChildB->new }, |
117 | handles => [qw/child_b_method_1/], |
118 | ); |
119 | } "don't need to declare isa if method list is predefined"; |
120 | |
121 | ::lives_ok { |
122 | has child_c => ( |
123 | isa => "ChildC", |
124 | is => "ro", |
125 | default => sub { ChildC->new }, |
126 | handles => qr/_la$/, |
127 | ); |
128 | } "can declare regex collector"; |
129 | |
130 | ::dies_ok { |
131 | has child_d => ( |
132 | is => "ro", |
133 | default => sub { ChildD->new }, |
134 | handles => sub { |
135 | my ( $class, $delegate_class ) = @_; |
136 | } |
137 | ); |
138 | } "can't create attr with generative handles parameter and no isa"; |
139 | |
140 | ::lives_ok { |
141 | has child_d => ( |
142 | isa => "ChildD", |
143 | is => "ro", |
144 | default => sub { ChildD->new }, |
145 | handles => sub { |
146 | my ( $class, $delegate_class ) = @_; |
147 | return; |
148 | } |
149 | ); |
150 | } "can't create attr with generative handles parameter and no isa"; |
151 | |
152 | ::lives_ok { |
153 | has child_e => ( |
154 | isa => "ChildE", |
155 | is => "ro", |
156 | default => sub { ChildE->new }, |
452bac1b |
157 | handles => ["child_e_method_2"], |
54b1cdf0 |
158 | ); |
159 | } "can delegate to non moose class using explicit method list"; |
160 | |
aff2941e |
161 | my $delegate_class; |
162 | ::lives_ok { |
163 | has child_f => ( |
164 | isa => "ChildF", |
165 | is => "ro", |
166 | default => sub { ChildF->new }, |
167 | handles => sub { |
4e848edb |
168 | $delegate_class = $_[1]->name; |
452bac1b |
169 | return; |
aff2941e |
170 | }, |
171 | ); |
172 | } "subrefs on non moose class give no meta"; |
173 | |
174 | ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" ); |
d03bd989 |
175 | |
f3c4e20e |
176 | ::lives_ok { |
177 | has child_g => ( |
178 | isa => "ChildG", |
179 | default => sub { ChildG->new }, |
180 | handles => ["child_g_method_1"], |
181 | ); |
d03bd989 |
182 | } "can delegate to object even without explicit reader"; |
54b1cdf0 |
183 | |
66559261 |
184 | ::dies_ok { |
185 | has child_h => ( |
186 | isa => "ChildH", |
187 | is => "ro", |
188 | default => sub { ChildH->new }, |
189 | handles => sub { map { $_, $_ } $_[1]->get_all_method_names }, |
190 | ); |
191 | } "Can't override exisiting class method in delegate"; |
192 | |
193 | ::lives_ok { |
194 | has child_i => ( |
195 | isa => "ChildI", |
196 | is => "ro", |
197 | default => sub { ChildI->new }, |
575ca974 |
198 | handles => sub { |
199 | map { $_, $_ } grep { !/^parent_method_1$/ } |
200 | $_[1]->get_all_method_names; |
201 | }, |
66559261 |
202 | ); |
203 | } "Test handles code ref for skipping predefined methods"; |
204 | |
205 | |
54b1cdf0 |
206 | sub parent_method { "p" } |
207 | } |
208 | |
209 | # sanity |
210 | |
211 | isa_ok( my $p = Parent->new, "Parent" ); |
212 | isa_ok( $p->child_a, "ChildA" ); |
452bac1b |
213 | isa_ok( $p->child_b, "ChildB" ); |
54b1cdf0 |
214 | isa_ok( $p->child_c, "ChildC" ); |
215 | isa_ok( $p->child_d, "ChildD" ); |
216 | isa_ok( $p->child_e, "ChildE" ); |
aff2941e |
217 | isa_ok( $p->child_f, "ChildF" ); |
66559261 |
218 | isa_ok( $p->child_i, "ChildI" ); |
54b1cdf0 |
219 | |
f3c4e20e |
220 | ok(!$p->can('child_g'), '... no child_g accessor defined'); |
66559261 |
221 | ok(!$p->can('child_h'), '... no child_h accessor defined'); |
f3c4e20e |
222 | |
54b1cdf0 |
223 | |
224 | is( $p->parent_method, "p", "parent method" ); |
225 | is( $p->child_a->child_a_super_method, "as", "child supermethod" ); |
226 | is( $p->child_a->child_a_method_1, "a1", "child method" ); |
227 | |
228 | can_ok( $p, "child_a_super_method" ); |
229 | can_ok( $p, "child_a_method_1" ); |
230 | can_ok( $p, "child_a_method_2" ); |
231 | ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" ); |
232 | |
233 | is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" ); |
234 | is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" ); |
235 | |
236 | |
237 | can_ok( $p, "child_b_method_1" ); |
238 | ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" ); |
239 | |
240 | |
241 | ok( !$p->can($_), "none of ChildD's methods ($_)" ) |
3b82ee4f |
242 | for grep { /^child/ } map { $_->name } ChildD->meta->get_all_methods(); |
54b1cdf0 |
243 | |
244 | can_ok( $p, "child_c_method_3_la" ); |
245 | can_ok( $p, "child_c_method_4_la" ); |
246 | |
247 | is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" ); |
248 | |
249 | can_ok( $p, "child_e_method_2" ); |
250 | ok( !$p->can("child_e_method_1"), "but not child_e_method_1"); |
251 | |
252 | is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" ); |
f3c4e20e |
253 | |
254 | can_ok( $p, "child_g_method_1" ); |
255 | is( $p->child_g_method_1, "g1", "delegate to moose class without reader (child_g_method_1)" ); |