tests for handles => CODE block
[gitmo/Moose.git] / t / 020_attributes / 011_more_attr_delegation.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 43;
7 use Test::Exception;
8
9 =pod
10
11 This tests the more complex
12 delegation cases and that they
13 do not fail at compile time.
14
15 =cut
16
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
74     package ChildG;
75     use Moose;
76
77     sub child_g_method_1 { "g1" }
78
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
91     package Parent;
92     use Moose;
93
94     sub parent_method_1 { "parrent_1" }
95
96     ::dies_ok {
97         has child_a => (
98             is      => "ro",
99             default => sub { ChildA->new },
100             handles => qr/.*/,
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 },
109             handles => qr/.*/,
110         );
111     } "allow all_methods with explicit isa";
112
113     ::lives_ok {
114         has child_b => (
115             is      => 'ro',
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 },
157             handles => ["child_e_method_2"],
158         );
159     } "can delegate to non moose class using explicit method list";
160
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 {
168                 $delegate_class = $_[1]->name;
169                 return;
170             },
171         );
172     } "subrefs on non moose class give no meta";
173
174     ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );
175
176     ::lives_ok {
177         has child_g => (
178             isa     => "ChildG",
179             default => sub { ChildG->new },
180             handles => ["child_g_method_1"],
181         );
182     } "can delegate to object even without explicit reader";
183
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 },
198             handles => sub { map { $_, $_ } grep { !/^parent_method_1$/ }$_[1]->get_all_method_names },
199         );
200     } "Test handles code ref for skipping predefined methods";
201
202
203     sub parent_method { "p" }
204 }
205
206 # sanity
207
208 isa_ok( my $p = Parent->new, "Parent" );
209 isa_ok( $p->child_a, "ChildA" );
210 isa_ok( $p->child_b, "ChildB" );
211 isa_ok( $p->child_c, "ChildC" );
212 isa_ok( $p->child_d, "ChildD" );
213 isa_ok( $p->child_e, "ChildE" );
214 isa_ok( $p->child_f, "ChildF" );
215 isa_ok( $p->child_i, "ChildI" );
216
217 ok(!$p->can('child_g'), '... no child_g accessor defined');
218 ok(!$p->can('child_h'), '... no child_h accessor defined');
219
220
221 is( $p->parent_method, "p", "parent method" );
222 is( $p->child_a->child_a_super_method, "as", "child supermethod" );
223 is( $p->child_a->child_a_method_1, "a1", "child method" );
224
225 can_ok( $p, "child_a_super_method" );
226 can_ok( $p, "child_a_method_1" );
227 can_ok( $p, "child_a_method_2" );
228 ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );
229
230 is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
231 is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );
232
233
234 can_ok( $p, "child_b_method_1" );
235 ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );
236
237
238 ok( !$p->can($_), "none of ChildD's methods ($_)" )
239     for grep { /^child/ } map { $_->name } ChildD->meta->get_all_methods();
240
241 can_ok( $p, "child_c_method_3_la" );
242 can_ok( $p, "child_c_method_4_la" );
243
244 is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );
245
246 can_ok( $p, "child_e_method_2" );
247 ok( !$p->can("child_e_method_1"), "but not child_e_method_1");
248
249 is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );
250
251 can_ok( $p, "child_g_method_1" );
252 is( $p->child_g_method_1, "g1", "delegate to moose class without reader (child_g_method_1)" );