File Coverage

blib/lib/Object/HashBase/Test.pm
Criterion Covered Total %
statement 101 106 95.2
branch 3 4 75.0
condition 1 2 50.0
subroutine 33 38 86.8
pod 0 5 0.0
total 138 155 89.0


line stmt bran cond sub pod time code
1             package Object::HashBase::Test;
2 2     2   450 use strict;
  2         4  
  2         58  
3 2     2   10 use warnings;
  2         4  
  2         63  
4              
5 2     2   10 use vars qw/$NO_RUN $TODO/;
  2         4  
  2         385  
6              
7             BEGIN {
8 2 100   2   10 if ($NO_RUN) {
9 1     0   5 *ok = sub { };
10 1     14   4 *is = sub { };
11 1     0   10 *is_deeply = sub { };
12 1     0   10 *like = sub { };
13 1     0   223 *done_testing = sub { };
14             }
15             else {
16 1         706 require Test::More;
17 1         66804 Test::More->import;
18             }
19             }
20              
21             return 1 if $NO_RUN;
22              
23             our $VERSION = '0.009';
24             # <-- START -->
25              
26             sub warnings(&) {
27 1     1 0 3 my $code = shift;
28 1         2 my @warnings;
29 1     1   5 local $SIG{__WARN__} = sub { push @warnings => @_ };
  1         43  
30 1         3 $code->();
31 1         321 return \@warnings;
32             }
33              
34             sub exception(&) {
35 2     2 0 5 my $code = shift;
36 2         10 local ($@, $!, $SIG{__DIE__});
37 2         4 my $ok = eval { $code->(); 1 };
  2         5  
  0         0  
38 2   50     92 my $error = $@ || 'SQUASHED ERROR';
39 2 50       19 return $ok ? undef : $error;
40             }
41              
42             BEGIN {
43 2     2   9 $INC{'Object/HashBase/Test/HBase.pm'} = __FILE__;
44              
45             package
46             Object::HashBase::Test::HBase;
47 2     2   947 use Object::HashBase qw/foo bar baz/;
  2         4  
  2         11  
48              
49 2         8 Object::HashBase::Test::is(FOO, 'foo', "FOO CONSTANT");
50 2         421 Object::HashBase::Test::is(BAR, 'bar', "BAR CONSTANT");
51 2         296 Object::HashBase::Test::is(BAZ, 'baz', "BAZ CONSTANT");
52             }
53              
54             BEGIN {
55             package
56             Object::HashBase::Test::HBaseSub;
57 2     2   343 use base 'Object::HashBase::Test::HBase';
  2         10  
  2         289  
58 2     2   13 use Object::HashBase qw/apple pear/;
  2         3  
  2         8  
59              
60 2     2   11 Object::HashBase::Test::is(FOO, 'foo', "FOO CONSTANT");
61 2         340 Object::HashBase::Test::is(BAR, 'bar', "BAR CONSTANT");
62 2         318 Object::HashBase::Test::is(BAZ, 'baz', "BAZ CONSTANT");
63 2         369 Object::HashBase::Test::is(APPLE, 'apple', "APPLE CONSTANT");
64 2         329 Object::HashBase::Test::is(PEAR, 'pear', "PEAR CONSTANT");
65             }
66              
67             my $one = Object::HashBase::Test::HBase->new(foo => 'a', bar => 'b', baz => 'c');
68             is($one->foo, 'a', "Accessor");
69             is($one->bar, 'b', "Accessor");
70             is($one->baz, 'c', "Accessor");
71             $one->set_foo('x');
72             is($one->foo, 'x', "Accessor set");
73             $one->set_foo(undef);
74              
75             is_deeply(
76             $one,
77             {
78             foo => undef,
79             bar => 'b',
80             baz => 'c',
81             },
82             'hash'
83             );
84              
85 0         0 BEGIN {
86             package
87             Object::HashBase::Test::Const::Test;
88 2     2   435 use Object::HashBase qw/foo/;
  2     0   4  
  2         7  
89              
90             sub do_it {
91 2     2   2 if (FOO()) {
92 2         8 return 'const';
93             }
94 0         0 return 'not const'
95             }
96             }
97              
98             my $pkg = 'Object::HashBase::Test::Const::Test';
99             is($pkg->do_it, 'const', "worked as expected");
100             {
101             local $SIG{__WARN__} = sub { };
102 1     1   4 *Object::HashBase::Test::Const::Test::FOO = sub { 0 };
103             }
104             ok(!$pkg->FOO, "overrode const sub");
105             {
106             local $TODO = "known to fail on $]" if $] le "5.006002";
107             is($pkg->do_it, 'const', "worked as expected, const was constant");
108             }
109              
110             BEGIN {
111 2     2   8 $INC{'Object/HashBase/Test/HBase/Wrapped.pm'} = __FILE__;
112              
113             package
114             Object::HashBase::Test::HBase::Wrapped;
115 2     2   14 use Object::HashBase qw/foo bar dup/;
  2         4  
  2         8  
116              
117 2         10 my $foo = __PACKAGE__->can('foo');
118 2     2   14 no warnings 'redefine';
  2         4  
  2         185  
119             *foo = sub {
120 4     4   7 my $self = shift;
121 4         15 $self->set_bar(1);
122 4         12 $self->$foo(@_);
123 2         113 };
124             }
125              
126 0         0 BEGIN {
127 2     2   169 $INC{'Object/HashBase/Test/HBase/Wrapped/Inherit.pm'} = __FILE__;
128              
129             package
130             Object::HashBase::Test::HBase::Wrapped::Inherit;
131 2     2   13 use base 'Object::HashBase::Test::HBase::Wrapped';
  2         5  
  2         209  
132 2     2   14 use Object::HashBase qw/baz dup/;
  2         4  
  2         9  
133             }
134              
135             my $o = Object::HashBase::Test::HBase::Wrapped::Inherit->new(foo => 1);
136             my $foo = $o->foo;
137             is($o->bar, 1, 'parent attribute sub not overridden');
138              
139             {
140             package
141             Foo;
142              
143             sub new;
144              
145 2     2   14 use Object::HashBase qw/foo bar baz/;
  2         11  
  2         9  
146              
147 1     1   4 sub new { 'foo' };
148             }
149              
150             is(Foo->new, 'foo', "Did not override existing 'new' method");
151              
152             BEGIN {
153 2     2   8 $INC{'Object/HashBase/Test/HBase2.pm'} = __FILE__;
154              
155             package
156             Object::HashBase::Test::HBase2;
157 2     2   14 use Object::HashBase qw/foo -bar ^baz ban +boo/;
  2         4  
  2         8  
158              
159 2         9 Object::HashBase::Test::is(FOO, 'foo', "FOO CONSTANT");
160 2         332 Object::HashBase::Test::is(BAR, 'bar', "BAR CONSTANT");
161 2         323 Object::HashBase::Test::is(BAZ, 'baz', "BAZ CONSTANT");
162 2         311 Object::HashBase::Test::is(BAT, 'bat', "BAT CONSTANT");
163 2         314 Object::HashBase::Test::is(BAN, 'ban', "BAN CONSTANT");
164 2         311 Object::HashBase::Test::is(BOO, 'boo', "BOO CONSTANT");
165             }
166              
167             my $ro = Object::HashBase::Test::HBase2->new(foo => 'foo', bar => 'bar', baz => 'baz', bat => 'bat', ban => 'ban');
168             is($ro->foo, 'foo', "got foo");
169             is($ro->bar, 'bar', "got bar");
170             is($ro->baz, 'baz', "got baz");
171             is($ro->bat, 'bat', "got bat");
172             ok(!$ro->can('set_bat'), "No setter for bat");
173             ok(!$ro->can('ban'), "No reader for ban");
174             ok(!$ro->can('boo'), "No reader for boo");
175             ok(!$ro->can('set_boo'), "No setter for boo");
176             is($ro->{ban}, 'ban', "ban attribute is set");
177             $ro->set_ban('xxx');
178             is($ro->{ban}, 'xxx', "ban attribute can be set");
179              
180             is($ro->set_foo('xxx'), 'xxx', "Can set foo");
181             is($ro->foo, 'xxx', "got foo");
182              
183             like(exception { $ro->set_bar('xxx') }, qr/'bar' is read-only/, "Cannot set bar");
184              
185             my $warnings = warnings { is($ro->set_baz('xxx'), 'xxx', 'set baz') };
186             like($warnings->[0], qr/set_baz\(\) is deprecated/, "Deprecation warning");
187              
188              
189              
190             is_deeply(
191             [Object::HashBase::attr_list('Object::HashBase::Test::HBase::Wrapped::Inherit')],
192             [qw/foo bar dup baz/],
193             "Got a list of attributes in order starting from base class, duplicates removed",
194             );
195              
196             my $x = Object::HashBase::Test::HBase::Wrapped::Inherit->new(foo => 1, baz => 2);
197             is($x->foo, 1, "set foo via pairs");
198             is($x->baz, 2, "set baz via pairs");
199              
200             # Now with hashref
201             my $y = Object::HashBase::Test::HBase::Wrapped::Inherit->new({foo => 1, baz => 2});
202             is($y->foo, 1, "set foo via hashref");
203             is($y->baz, 2, "set baz via hashref");
204              
205             # Now with hashref
206             my $z = Object::HashBase::Test::HBase::Wrapped::Inherit->new([
207             1, # foo
208             2, # bar
209             3, # dup
210             4, # baz
211             ]);
212             is($z->foo, 1, "set foo via arrayref");
213             is($z->baz, 4, "set baz via arrayref");
214              
215             like(
216             exception { Object::HashBase::Test::HBase::Wrapped::Inherit->new([1 .. 10]) },
217             qr/Too many arguments for Object::HashBase::Test::HBase::Wrapped::Inherit constructor/,
218             "Too many args in array form"
219             );
220              
221              
222             my $CAN_COUNT = 0;
223             my $CAN_COUNT2 = 0;
224             my $INIT_COUNT = 0;
225 0         0 BEGIN {
226 2     2   8 $INC{'Object/HashBase/Test/HBase3.pm'} = __FILE__;
227             package
228             Object::HashBase::Test::HBase3;
229 2     2   827 use Object::HashBase qw/foo/;
  2         4  
  2         8  
230              
231             sub can {
232 1     1 0 2 my $self = shift;
233 1         2 $CAN_COUNT++;
234 1         8 $self->SUPER::can(@_);
235             }
236              
237 2         378 $INC{'Object/HashBase/Test/HBase4.pm'} = __FILE__;
238             package
239             Object::HashBase::Test::HBase4;
240 2     2   14 use Object::HashBase qw/foo/;
  2         4  
  2         7  
241              
242             sub can {
243 1     1 0 2 my $self = shift;
244 1         2 $CAN_COUNT2++;
245 1         7 $self->SUPER::can(@_);
246             }
247              
248 2     2 0 4 sub init { $INIT_COUNT++ }
249             }
250              
251             is($CAN_COUNT, 0, "->can has not been called yet");
252             my $it = Object::HashBase::Test::HBase3->new;
253             is($CAN_COUNT, 1, "->can has been called once to check for init");
254             $it = Object::HashBase::Test::HBase3->new;
255             is($CAN_COUNT, 1, "->can was not called again, we cached it");
256              
257             is($CAN_COUNT2, 0, "->can has not been called yet");
258             is($INIT_COUNT, 0, "->init has not been called yet");
259             $it = Object::HashBase::Test::HBase4->new;
260             is($CAN_COUNT2, 1, "->can has been called once to check for init");
261             is($INIT_COUNT, 1, "->init has been called once");
262             $it = Object::HashBase::Test::HBase4->new;
263             is($CAN_COUNT2, 1, "->can was not called again, we cached it");
264             is($INIT_COUNT, 2, "->init has been called again");
265              
266             done_testing;
267              
268             1;