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