-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHashBase.t
317 lines (260 loc) · 7.81 KB
/
HashBase.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use strict;
use warnings;
use Test::More;
sub warnings(&) {
my $code = shift;
my @warnings;
local $SIG{__WARN__} = sub { push @warnings => @_ };
$code->();
return \@warnings;
}
sub exception(&) {
my $code = shift;
local ($@, $!, $SIG{__DIE__});
my $ok = eval { $code->(); 1 };
my $error = $@ || 'SQUASHED ERROR';
return $ok ? undef : $error;
}
BEGIN {
$INC{'Object/HashBase/Test/HBase.pm'} = __FILE__;
package
main::HBase;
use DBIx::QuickORM::Util::HashBase qw/foo bar baz/;
main::is(FOO, 'foo', "FOO CONSTANT");
main::is(BAR, 'bar', "BAR CONSTANT");
main::is(BAZ, 'baz', "BAZ CONSTANT");
}
{
BEGIN {
$INC{'Object/HashBase/Test/HBaseINIT.pm'} = __FILE__;
$INC{'Object/HashBase/Test/HBaseINIT2.pm'} = __FILE__;
$INC{'main/HBaseINIT.pm'} = __FILE__;
}
package
main::HBaseINIT;
use DBIx::QuickORM::Util::HashBase qw/foo bar baz/;
$main::counter = 0;
add_pre_init { shift->{pre_init_1} = $main::counter++ };
add_pre_init { shift->{pre_init_2} = $main::counter++ };
sub init { shift->{init} = $main::counter++ };
add_post_init { shift->{post_init_1} = $main::counter++ };
add_post_init { shift->{post_init_2} = $main::counter++ };
package
main::HBaseINIT2;
use parent 'main::HBaseINIT';
use DBIx::QuickORM::Util::HashBase qw/boop/;
add_pre_init { shift->{pre_init_3} = $main::counter++ };
sub init { $_[0]->SUPER::init(); $_[0]->{init2} = $main::counter++ };
add_post_init { shift->{post_init_3} = $main::counter++ };
}
BEGIN {
package
main::HBaseSub;
use base 'main::HBase';
use DBIx::QuickORM::Util::HashBase qw/apple pear/;
main::is(FOO, 'foo', "FOO CONSTANT");
main::is(BAR, 'bar', "BAR CONSTANT");
main::is(BAZ, 'baz', "BAZ CONSTANT");
main::is(APPLE, 'apple', "APPLE CONSTANT");
main::is(PEAR, 'pear', "PEAR CONSTANT");
}
my $one = main::HBase->new(foo => 'a', bar => 'b', baz => 'c');
is($one->foo, 'a', "Accessor");
is($one->bar, 'b', "Accessor");
is($one->baz, 'c', "Accessor");
$one->set_foo('x');
is($one->foo, 'x', "Accessor set");
$one->set_foo(undef);
is_deeply(
$one,
{
foo => undef,
bar => 'b',
baz => 'c',
},
'hash'
);
{
my @warns;
local $SIG{__WARN__} = sub { push @warns => @_ };
main::HBaseINIT->add_post_init(sub { shift->{late_post} = 'yes' });
ok(!@warns, "No Warnings adding a post-init") or diag(@_);
}
$main::counter = 0;
my $two = main::HBaseINIT->new();
is_deeply(
$two,
{
pre_init_1 => 0,
pre_init_2 => 1,
init => 2,
post_init_2 => 3,
post_init_1 => 4,
late_post => 'yes',
},
"inits ran in the correct order"
);
$main::counter = 0;
my $three = main::HBaseINIT2->new();
is_deeply(
$three,
{
pre_init_1 => 0,
pre_init_2 => 1,
pre_init_3 => 2,
init => 3,
init2 => 4,
post_init_3 => 5,
post_init_2 => 6,
post_init_1 => 7,
late_post => 'yes',
},
"inits ran in the correct order"
);
BEGIN {
package
main::Const::Test;
use DBIx::QuickORM::Util::HashBase qw/foo/;
sub do_it {
if (FOO()) {
return 'const';
}
return 'not const'
}
}
my $pkg = 'main::Const::Test';
is($pkg->do_it, 'const', "worked as expected");
{
local $SIG{__WARN__} = sub { };
*main::Const::Test::FOO = sub { 0 };
}
ok(!$pkg->FOO, "overrode const sub");
{
local $TODO = "known to fail on $]" if $] le "5.006002";
is($pkg->do_it, 'const', "worked as expected, const was constant");
}
BEGIN {
$INC{'Object/HashBase/Test/HBase/Wrapped.pm'} = __FILE__;
package
main::HBase::Wrapped;
use DBIx::QuickORM::Util::HashBase qw/foo bar dup/;
my $foo = __PACKAGE__->can('foo');
no warnings 'redefine';
*foo = sub {
my $self = shift;
$self->set_bar(1);
$self->$foo(@_);
};
}
BEGIN {
$INC{'Object/HashBase/Test/HBase/Wrapped/Inherit.pm'} = __FILE__;
package
main::HBase::Wrapped::Inherit;
use base 'main::HBase::Wrapped';
use DBIx::QuickORM::Util::HashBase qw/baz dup/;
}
my $o = main::HBase::Wrapped::Inherit->new(foo => 1);
my $foo = $o->foo;
is($o->bar, 1, 'parent attribute sub not overridden');
{
package
Foo;
sub new;
use DBIx::QuickORM::Util::HashBase qw/foo bar baz/;
sub new { 'foo' };
}
is(Foo->new, 'foo', "Did not override existing 'new' method");
BEGIN {
$INC{'Object/HashBase/Test/HBase2.pm'} = __FILE__;
package
main::HBase2;
use DBIx::QuickORM::Util::HashBase qw/foo -bar ^baz <bat >ban +boo/;
main::is(FOO, 'foo', "FOO CONSTANT");
main::is(BAR, 'bar', "BAR CONSTANT");
main::is(BAZ, 'baz', "BAZ CONSTANT");
main::is(BAT, 'bat', "BAT CONSTANT");
main::is(BAN, 'ban', "BAN CONSTANT");
main::is(BOO, 'boo', "BOO CONSTANT");
}
my $ro = main::HBase2->new(foo => 'foo', bar => 'bar', baz => 'baz', bat => 'bat', ban => 'ban');
is($ro->foo, 'foo', "got foo");
is($ro->bar, 'bar', "got bar");
is($ro->baz, 'baz', "got baz");
is($ro->bat, 'bat', "got bat");
ok(!$ro->can('set_bat'), "No setter for bat");
ok(!$ro->can('ban'), "No reader for ban");
ok(!$ro->can('boo'), "No reader for boo");
ok(!$ro->can('set_boo'), "No setter for boo");
is($ro->{ban}, 'ban', "ban attribute is set");
$ro->set_ban('xxx');
is($ro->{ban}, 'xxx', "ban attribute can be set");
is($ro->set_foo('xxx'), 'xxx', "Can set foo");
is($ro->foo, 'xxx', "got foo");
like(exception { $ro->set_bar('xxx') }, qr/'bar' is read-only/, "Cannot set bar");
my $warnings = warnings { is($ro->set_baz('xxx'), 'xxx', 'set baz') };
like($warnings->[0], qr/set_baz\(\) is deprecated/, "Deprecation warning");
is_deeply(
[DBIx::QuickORM::Util::HashBase::attr_list('main::HBase::Wrapped::Inherit')],
[qw/foo bar dup baz/],
"Got a list of attributes in order starting from base class, duplicates removed",
);
my $x = main::HBase::Wrapped::Inherit->new(foo => 1, baz => 2);
is($x->foo, 1, "set foo via pairs");
is($x->baz, 2, "set baz via pairs");
# Now with hashref
my $y = main::HBase::Wrapped::Inherit->new({foo => 1, baz => 2});
is($y->foo, 1, "set foo via hashref");
is($y->baz, 2, "set baz via hashref");
# Now with hashref
my $z = main::HBase::Wrapped::Inherit->new([
1, # foo
2, # bar
3, # dup
4, # baz
]);
is($z->foo, 1, "set foo via arrayref");
is($z->baz, 4, "set baz via arrayref");
like(
exception { main::HBase::Wrapped::Inherit->new([1 .. 10]) },
qr/Too many arguments for main::HBase::Wrapped::Inherit constructor/,
"Too many args in array form"
);
my $CAN_COUNT = 0;
my $CAN_COUNT2 = 0;
my $INIT_COUNT = 0;
BEGIN {
$INC{'Object/HashBase/Test/HBase3.pm'} = __FILE__;
package
main::HBase3;
use DBIx::QuickORM::Util::HashBase qw/foo/;
sub can {
my $self = shift;
$CAN_COUNT++;
$self->SUPER::can(@_);
}
$INC{'Object/HashBase/Test/HBase4.pm'} = __FILE__;
package
main::HBase4;
use DBIx::QuickORM::Util::HashBase qw/foo/;
sub can {
my $self = shift;
$CAN_COUNT2++;
$self->SUPER::can(@_);
}
sub init { $INIT_COUNT++ }
}
is($CAN_COUNT, 0, "->can has not been called yet");
my $it = main::HBase3->new;
is($CAN_COUNT, 1, "->can has been called once to check for init");
$it = main::HBase3->new;
is($CAN_COUNT, 1, "->can was not called again, we cached it");
is($CAN_COUNT2, 0, "->can has not been called yet");
is($INIT_COUNT, 0, "->init has not been called yet");
$it = main::HBase4->new;
is($CAN_COUNT2, 1, "->can has been called once to check for init");
is($INIT_COUNT, 1, "->init has been called once");
$it = main::HBase4->new;
is($CAN_COUNT2, 1, "->can was not called again, we cached it");
is($INIT_COUNT, 2, "->init has been called again");
done_testing;
1;