-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial.t
executable file
·283 lines (231 loc) · 6.87 KB
/
partial.t
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env perl
# -*- mode: perl; coding: utf-8 -*-
#----------------------------------------
use strict;
use warnings qw(FATAL all NONFATAL misc);
use FindBin; BEGIN { do "$FindBin::Bin/t_lib.pl" }
#----------------------------------------
use Test::More;
sub eval_ok {
my ($text, $title) = @_;
local $@ = '';
local $SIG{__WARN__} = sub { die @_ };
eval $text;
is $@, '', $title;
}
sub error_like {
my ($text, $pattern, $title) = @_;
local $@ = '';
eval "use strict; $text";
like $@, $pattern, $title;
}
my $T;
{
$T = q|[MFields basic]|;
eval_ok(q{
package T1; use YATT::Lite::Inc;
use YATT::Lite::MFields
(qw/cf_foo1 cf_foo2 cf_foo3/);
}, "$T class T1");
my $dummy = %T1::FIELDS;
is_deeply [sort keys %T1::FIELDS]
, [qw/cf_foo1 cf_foo2 cf_foo3/]
, "$T t1 fields";
error_like q{my T1 $t1; defined $t1->{cf_foo}}
, qr/^No such class field "cf_foo" in variable \$t1 of type T1/
, "$T field name error T1->cf_foo is detected";
eval_ok q{my T1 $t1; defined $t1->{cf_foo1}}
, "$T correct field name should not raise error";
eval_ok(q{
package T2; use YATT::Lite::Inc;
use YATT::Lite::MFields (qw/cf_bar1 cf_bar2/);
}, "$T class T2");
eval_ok(q{
package T3; use YATT::Lite::Inc;
use parent qw/T1 T2/;
use YATT::Lite::MFields;
}, "$T class T3");
$dummy = %T3::FIELDS;
is_deeply [sort keys %T3::FIELDS]
, [qw/cf_bar1 cf_bar2 cf_foo1 cf_foo2 cf_foo3/]
, "$T t3 fields";
error_like q{my T3 $t; defined $t->{cf_foo}}
, qr/^No such class field "cf_foo" in variable \$t of type T3/
, "$T field name error T3->cf_foo is detected";
eval_ok q{my T3 $t; defined $t->{cf_foo1}}
, "$T correct field name should not raise error";
}
{
$T = "[\$meta->has]";
eval_ok(q{
package U1; use YATT::Lite::Inc;
use YATT::Lite::MFields sub {
my ($meta) = @_;
$meta->has(name => is => 'ro', doc => "Name of the user");
$meta->has(age => is => 'rw', doc => "Age of the user");
$meta->has($_) for qw/weight height/;
};
}, "$T class U1");
my $dummy = %U1::FIELDS;
is_deeply [sort keys %U1::FIELDS]
, [qw/age height name weight/]
, "$T U1 fields";
error_like q{my U1 $t; defined $t->{ageee}}
, qr/^No such class field "ageee" in variable \$t of type U1/
, "$T field name error U1->ageee is detected";
eval_ok q{my U1 $t; defined $t->{age}}
, "$T correct field name should not raise error";
}
{
$T = '[Partial]';
error_like(<<'END'
package t3_Err; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial unknown => 'opt';
END
, qr/^Unknown Partial opt: unknown/);
eval_ok(q{
package t3_Foo; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial
fields => [qw/foo1 foo2/];
}, "$T use .. fields");
my $dummy = %t3_Foo::FIELDS;
is_deeply [sort keys %t3_Foo::FIELDS]
, [qw/foo1 foo2/]
, "$T \%FIELDS";
eval_ok(q{
package t3_Bar; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial;
use YATT::Lite::MFields
qw/barx bary barz/;
}, "$T with MFields");
$dummy = %t3_Bar::FIELDS;
is_deeply [sort keys %t3_Bar::FIELDS]
, [qw/barx bary barz/]
, "$T \%FIELDS";
eval_ok(q{
package t3_App1; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Object;
use t3_Foo;
use t3_Bar;
sub m1 {
(my MY $x) = @_;
join "", $x->{foo1}, $x->{foo2}, $x->{barx}, $x->{bary}, $x->{barz};
}
1;
}, "$T use (partial) Foo and Bar");
$dummy = %t3_App1::FIELDS;
is_deeply [sort keys %t3_App1::FIELDS]
, [qw/barx bary barz foo1 foo2/]
, "$T \%FIELDS";
error_like(q{
package t3_App2; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Object;
use t3_Foo;
use t3_Bar;
sub m1 {
(my MY $self) = @_;
$self->{ng};
}
1;
}
, qr/^No such class field "ng" in variable \$self of type t3_App2/
, "$T field error is detected at compile time.");
}
{
$T = '[Partial inherits Partial]';
eval_ok(q{
package t4_Foo; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial
(fields => [qw/foo3 foo4/], parents => ['t3_Foo']);
}, "$T partial t4_Foo");
my $dummy = %t4_Foo::FIELDS;
is_deeply [sort keys %t4_Foo::FIELDS]
, [qw/foo1 foo2 foo3 foo4/]
, "$T \%FIELDS";
eval_ok(q{
package t4_Bar; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial
(parents => ['t3_Bar'], fields => [qw/bara barb/]);
}, "$T partial t4_Bar");
$dummy = %t4_Bar::FIELDS;
is_deeply [sort keys %t4_Bar::FIELDS]
, [qw/bara barb barx bary barz/]
, "$T \%FIELDS";
eval_ok(<<'END'
package t4_App1; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use base qw/YATT::Lite::Object/;
use t4_Foo;
use t4_Bar;
sub m1 {
(my MY $x) = @_;
join "", $x->{foo1}, $x->{foo2}, $x->{barx}, $x->{bary}, $x->{barz};
}
1;
END
, "$T partital t4_App1");
is_deeply \@t4_App1::ISA
, [qw/YATT::Lite::Object t4_Foo t4_Bar/]
, "$T 'use PartialMod' adds ISA";
$dummy = %t4_App1::FIELDS;
is_deeply [sort keys %t4_App1::FIELDS]
, [qw/bara barb barx bary barz foo1 foo2 foo3 foo4/]
, "$T partial t4_App1 fields";
error_like(<<'END'
package t4_App2; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use base qw/YATT::Lite::Object/;
use t4_Foo;
use t4_Bar;
sub m1 {
(my MY $self) = @_;
$self->{ng};
}
1;
END
, qr/^No such class field "ng" in variable \$self of type t4_App2/
, "$T partital t4_App2 field error is detected at compile time.");
}
{
$T = '[Parital Diamond inheritance]e';
# Diamond inheritance.
eval_ok(q{
package t5_C1; use YATT::Lite::Inc;
use YATT::Lite::MFields
(qw/foo1 foo2 foo3/);
}, "$T base class");
eval_ok(q{
package t5_S1; use YATT::Lite::Inc;
use YATT::Lite::Partial parent => 't5_C1', fields => [qw/bar1 bar2/];
}, "$T subclass 1");
eval_ok(q{
package t5_S2; use YATT::Lite::Inc;
use YATT::Lite::Partial parent => 't5_C1', fields => [qw/barx bary/];
}, "$T subclass 2");
eval_ok(q{
package t5_Diamond; use YATT::Lite::Inc;
use base qw/YATT::Lite::Object/;
use YATT::Lite::MFields
(qw/d1 d2/);
use t5_S1;
use t5_S2;
}, "$T Diamond inheritance");
my $dummy = %t5_Diamond::FIELDS;
is_deeply [sort keys %t5_Diamond::FIELDS]
, [qw/bar1 bar2 barx bary d1 d2 foo1 foo2 foo3/]
, "$T \%FIELDS";
}
{
$T = '[-Entity, -CON]';
eval_ok(q{
package t6_Foo; use YATT::Lite::Inc; sub MY () {__PACKAGE__}
use YATT::Lite::Partial
(fields => [qw/foo3 foo4/], -Entity, -CON);
Entity bar => sub {
my ($this) = shift;
defined $CON;
};
}, "$T defined.(without error for \$CON)");
no warnings 'once';
ok $t6_Foo::{'EntNS'}, "$T *EntNS";
ok t6_Foo::EntNS->can('entity_bar'), "$T EntNS->entity_bar()";
}
done_testing();