File: 011_more_attr_delegation.t

package info (click to toggle)
libmoose-perl 1.09-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,004 kB
  • ctags: 1,472
  • sloc: perl: 25,387; makefile: 2
file content (263 lines) | stat: -rw-r--r-- 6,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;

=pod

This tests the more complex
delegation cases and that they
do not fail at compile time.

=cut

{

    package ChildASuper;
    use Moose;

    sub child_a_super_method { "as" }

    package ChildA;
    use Moose;

    extends "ChildASuper";

    sub child_a_method_1 { "a1" }
    sub child_a_method_2 { Scalar::Util::blessed($_[0]) . " a2" }

    package ChildASub;
    use Moose;

    extends "ChildA";

    sub child_a_method_3 { "a3" }

    package ChildB;
    use Moose;

    sub child_b_method_1 { "b1" }
    sub child_b_method_2 { "b2" }
    sub child_b_method_3 { "b3" }

    package ChildC;
    use Moose;

    sub child_c_method_1 { "c1" }
    sub child_c_method_2 { "c2" }
    sub child_c_method_3_la { "c3" }
    sub child_c_method_4_la { "c4" }

    package ChildD;
    use Moose;

    sub child_d_method_1 { "d1" }
    sub child_d_method_2 { "d2" }

    package ChildE;
    # no Moose

    sub new { bless {}, shift }
    sub child_e_method_1 { "e1" }
    sub child_e_method_2 { "e2" }

    package ChildF;
    # no Moose

    sub new { bless {}, shift }
    sub child_f_method_1 { "f1" }
    sub child_f_method_2 { "f2" }

    package ChildG;
    use Moose;

    sub child_g_method_1 { "g1" }

    package ChildH;
    use Moose;

    sub child_h_method_1 { "h1" }
    sub parent_method_1 { "child_parent_1" }

    package ChildI;
    use Moose;

    sub child_i_method_1 { "i1" }
    sub parent_method_1 { "child_parent_1" }

    package Parent;
    use Moose;

    sub parent_method_1 { "parent_1" }
    ::can_ok('Parent', 'parent_method_1');

    ::dies_ok {
        has child_a => (
            is      => "ro",
            default => sub { ChildA->new },
            handles => qr/.*/,
        );
    } "all_methods requires explicit isa";

    ::lives_ok {
        has child_a => (
            isa     => "ChildA",
            is      => "ro",
            default => sub { ChildA->new },
            handles => qr/.*/,
        );
    } "allow all_methods with explicit isa";

    ::lives_ok {
        has child_b => (
            is      => 'ro',
            default => sub { ChildB->new },
            handles => [qw/child_b_method_1/],
        );
    } "don't need to declare isa if method list is predefined";

    ::lives_ok {
        has child_c => (
            isa     => "ChildC",
            is      => "ro",
            default => sub { ChildC->new },
            handles => qr/_la$/,
        );
    } "can declare regex collector";

    ::dies_ok {
        has child_d => (
            is      => "ro",
            default => sub { ChildD->new },
            handles => sub {
                my ( $class, $delegate_class ) = @_;
            }
        );
    } "can't create attr with generative handles parameter and no isa";

    ::lives_ok {
        has child_d => (
            isa     => "ChildD",
            is      => "ro",
            default => sub { ChildD->new },
            handles => sub {
                my ( $class, $delegate_class ) = @_;
                return;
            }
        );
    } "can't create attr with generative handles parameter and no isa";

    ::lives_ok {
        has child_e => (
            isa     => "ChildE",
            is      => "ro",
            default => sub { ChildE->new },
            handles => ["child_e_method_2"],
        );
    } "can delegate to non moose class using explicit method list";

    my $delegate_class;
    ::lives_ok {
        has child_f => (
            isa     => "ChildF",
            is      => "ro",
            default => sub { ChildF->new },
            handles => sub {
                $delegate_class = $_[1]->name;
                return;
            },
        );
    } "subrefs on non moose class give no meta";

    ::is( $delegate_class, "ChildF", "plain classes are handed down to subs" );

    ::lives_ok {
        has child_g => (
            isa     => "ChildG",
            default => sub { ChildG->new },
            handles => ["child_g_method_1"],
        );
    } "can delegate to object even without explicit reader";

    ::can_ok('Parent', 'parent_method_1');
    ::dies_ok {
        has child_h => (
            isa     => "ChildH",
            is      => "ro",
            default => sub { ChildH->new },
            handles => sub { map { $_, $_ } $_[1]->get_all_method_names },
        );
    } "Can't override exisiting class method in delegate";
    ::can_ok('Parent', 'parent_method_1');

    ::lives_ok {
        has child_i => (
            isa     => "ChildI",
            is      => "ro",
            default => sub { ChildI->new },
            handles => sub {
                map { $_, $_ } grep { !/^parent_method_1|meta$/ }
                    $_[1]->get_all_method_names;
            },
        );
    } "Test handles code ref for skipping predefined methods";


    sub parent_method { "p" }
}

# sanity

isa_ok( my $p = Parent->new, "Parent" );
isa_ok( $p->child_a, "ChildA" );
isa_ok( $p->child_b, "ChildB" );
isa_ok( $p->child_c, "ChildC" );
isa_ok( $p->child_d, "ChildD" );
isa_ok( $p->child_e, "ChildE" );
isa_ok( $p->child_f, "ChildF" );
isa_ok( $p->child_i, "ChildI" );

ok(!$p->can('child_g'), '... no child_g accessor defined');
ok(!$p->can('child_h'), '... no child_h accessor defined');


is( $p->parent_method, "p", "parent method" );
is( $p->child_a->child_a_super_method, "as", "child supermethod" );
is( $p->child_a->child_a_method_1, "a1", "child method" );

can_ok( $p, "child_a_super_method" );
can_ok( $p, "child_a_method_1" );
can_ok( $p, "child_a_method_2" );
ok( !$p->can( "child_a_method_3" ), "but not subclass of delegate class" );

is( $p->child_a_method_1, $p->child_a->child_a_method_1, "delegate behaves the same" );
is( $p->child_a_method_2, "ChildA a2", "delegates are their own invocants" );


can_ok( $p, "child_b_method_1" );
ok( !$p->can("child_b_method_2"), "but not ChildB's unspecified siblings" );


ok( !$p->can($_), "none of ChildD's methods ($_)" )
    for grep { /^child/ } map { $_->name } ChildD->meta->get_all_methods();

can_ok( $p, "child_c_method_3_la" );
can_ok( $p, "child_c_method_4_la" );

is( $p->child_c_method_3_la, "c3", "ChildC method delegated OK" );

can_ok( $p, "child_e_method_2" );
ok( !$p->can("child_e_method_1"), "but not child_e_method_1");

is( $p->child_e_method_2, "e2", "delegate to non moose class (child_e_method_2)" );

can_ok( $p, "child_g_method_1" );
is( $p->child_g_method_1, "g1", "delegate to moose class without reader (child_g_method_1)" );

can_ok( $p, "child_i_method_1" );
is( $p->parent_method_1, "parent_1", "delegate doesn't override existing method" );

done_testing;