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