10 local $SIG{__WARN__} = sub { push @warnings => @_ };
17 local ($@, $!, $SIG{__DIE__});
18 my $ok = eval { $code->(); 1 };
19 my $error = $@ || 'SQUASHED ERROR';
20 return $ok ? undef : $error;
24 $INC{'Object/HashBase/Test/HBase.pm'} = __FILE__;
28 use Test2::Util::HashBase qw/foo bar baz/;
30 main::is(FOO, 'foo', "FOO CONSTANT");
31 main::is(BAR, 'bar', "BAR CONSTANT");
32 main::is(BAZ, 'baz', "BAZ CONSTANT");
38 use base 'main::HBase';
39 use Test2::Util::HashBase qw/apple pear/;
41 main::is(FOO, 'foo', "FOO CONSTANT");
42 main::is(BAR, 'bar', "BAR CONSTANT");
43 main::is(BAZ, 'baz', "BAZ CONSTANT");
44 main::is(APPLE, 'apple', "APPLE CONSTANT");
45 main::is(PEAR, 'pear', "PEAR CONSTANT");
48 my $one = main::HBase->new(foo => 'a', bar => 'b', baz => 'c');
49 is($one->foo, 'a', "Accessor");
50 is($one->bar, 'b', "Accessor");
51 is($one->baz, 'c', "Accessor");
53 is($one->foo, 'x', "Accessor set");
69 use Test2::Util::HashBase qw/foo/;
79 my $pkg = 'main::Const::Test';
80 is($pkg->do_it, 'const', "worked as expected");
82 local $SIG{__WARN__} = sub { };
83 *main::Const::Test::FOO = sub { 0 };
85 ok(!$pkg->FOO, "overrode const sub");
87 local $TODO = "known to fail on $]" if $] le "5.006002";
88 is($pkg->do_it, 'const', "worked as expected, const was constant");
92 $INC{'Object/HashBase/Test/HBase/Wrapped.pm'} = __FILE__;
96 use Test2::Util::HashBase qw/foo bar dup/;
98 my $foo = __PACKAGE__->can('foo');
99 no warnings 'redefine';
108 $INC{'Object/HashBase/Test/HBase/Wrapped/Inherit.pm'} = __FILE__;
111 main::HBase::Wrapped::Inherit;
112 use base 'main::HBase::Wrapped';
113 use Test2::Util::HashBase qw/baz dup/;
116 my $o = main::HBase::Wrapped::Inherit->new(foo => 1);
118 is($o->bar, 1, 'parent attribute sub not overridden');
126 use Test2::Util::HashBase qw/foo bar baz/;
131 is(Foo->new, 'foo', "Did not override existing 'new' method");
134 $INC{'Object/HashBase/Test/HBase2.pm'} = __FILE__;
138 use Test2::Util::HashBase qw/foo -bar ^baz <bat >ban +boo/;
140 main::is(FOO, 'foo', "FOO CONSTANT");
141 main::is(BAR, 'bar', "BAR CONSTANT");
142 main::is(BAZ, 'baz', "BAZ CONSTANT");
143 main::is(BAT, 'bat', "BAT CONSTANT");
144 main::is(BAN, 'ban', "BAN CONSTANT");
145 main::is(BOO, 'boo', "BOO CONSTANT");
148 my $ro = main::HBase2->new(foo => 'foo', bar => 'bar', baz => 'baz', bat => 'bat', ban => 'ban');
149 is($ro->foo, 'foo', "got foo");
150 is($ro->bar, 'bar', "got bar");
151 is($ro->baz, 'baz', "got baz");
152 is($ro->bat, 'bat', "got bat");
153 ok(!$ro->can('set_bat'), "No setter for bat");
154 ok(!$ro->can('ban'), "No reader for ban");
155 ok(!$ro->can('boo'), "No reader for boo");
156 ok(!$ro->can('set_boo'), "No setter for boo");
157 is($ro->{ban}, 'ban', "ban attribute is set");
159 is($ro->{ban}, 'xxx', "ban attribute can be set");
161 is($ro->set_foo('xxx'), 'xxx', "Can set foo");
162 is($ro->foo, 'xxx', "got foo");
164 like(exception { $ro->set_bar('xxx') }, qr/'bar' is read-only/, "Cannot set bar");
166 my $warnings = warnings { is($ro->set_baz('xxx'), 'xxx', 'set baz') };
167 like($warnings->[0], qr/set_baz\(\) is deprecated/, "Deprecation warning");
172 [Test2::Util::HashBase::attr_list('main::HBase::Wrapped::Inherit')],
173 [qw/foo bar dup baz/],
174 "Got a list of attributes in order starting from base class, duplicates removed",
177 my $x = main::HBase::Wrapped::Inherit->new(foo => 1, baz => 2);
178 is($x->foo, 1, "set foo via pairs");
179 is($x->baz, 2, "set baz via pairs");
182 my $y = main::HBase::Wrapped::Inherit->new({foo => 1, baz => 2});
183 is($y->foo, 1, "set foo via hashref");
184 is($y->baz, 2, "set baz via hashref");
187 my $z = main::HBase::Wrapped::Inherit->new([
193 is($z->foo, 1, "set foo via arrayref");
194 is($z->baz, 4, "set baz via arrayref");
197 exception { main::HBase::Wrapped::Inherit->new([1 .. 10]) },
198 qr/Too many arguments for main::HBase::Wrapped::Inherit constructor/,
199 "Too many args in array form"
207 $INC{'Object/HashBase/Test/HBase3.pm'} = __FILE__;
210 use Test2::Util::HashBase qw/foo/;
215 $self->SUPER::can(@_);
218 $INC{'Object/HashBase/Test/HBase4.pm'} = __FILE__;
221 use Test2::Util::HashBase qw/foo/;
226 $self->SUPER::can(@_);
229 sub init { $INIT_COUNT++ }
232 is($CAN_COUNT, 0, "->can has not been called yet");
233 my $it = main::HBase3->new;
234 is($CAN_COUNT, 1, "->can has been called once to check for init");
235 $it = main::HBase3->new;
236 is($CAN_COUNT, 1, "->can was not called again, we cached it");
238 is($CAN_COUNT2, 0, "->can has not been called yet");
239 is($INIT_COUNT, 0, "->init has not been called yet");
240 $it = main::HBase4->new;
241 is($CAN_COUNT2, 1, "->can has been called once to check for init");
242 is($INIT_COUNT, 1, "->init has been called once");
243 $it = main::HBase4->new;
244 is($CAN_COUNT2, 1, "->can was not called again, we cached it");
245 is($INIT_COUNT, 2, "->init has been called again");