#!/usr/bin/perl -w
my
(
$Has_PH
,
$Field
);
BEGIN {
$Has_PH
= $] < 5.009;
$Field
=
$Has_PH
?
"pseudo-hash field"
:
"class field"
;
}
my
$W
;
BEGIN {
$W
= 0;
$SIG
{__WARN__} =
sub
{
if
(
$_
[0] =~ /^Hides field
'.*?'
in base class/) {
$W
++;
}
else
{
warn
$_
[0];
}
};
}
BEGIN { use_ok(
'base'
); }
sub
new { fields::new(
shift
) }
sub
m {}
eval
{
'base'
->
import
(
qw(B2 M B3)
); };
::like($@,
qr/can't multiply inherit fields/
i,
'No multiple field inheritance'
);
my
%EXPECT
= (
B1
=> [
qw(b1 b2 b3)
],
D1
=> [
qw(b1 b2 b3 d1 d2 d3)
],
D2
=> [
qw(b1 b2 b3 _d1 _d2 d1 d2)
],
M
=> [
qw()
],
B2
=> [
qw(_b1 b1 _b2 b2)
],
D3
=> [(
undef
,
undef
,
undef
,
qw(b2 b1 d1 _b1 _d1)
)],
D4
=> [(
undef
,
undef
,
undef
,
qw(b2 b1 d1)
,
undef
,
undef
,
qw(_d3 d3)
)],
D5
=> [
undef
,
'b1'
,
undef
,
'b2'
],
B3
=> [
qw(b4 _b5 b6 _b7)
],
B7
=> [
qw(_b1)
],
D7
=> [
undef
,
'b1'
],
B8
=> [
qw(_b1)
],
D8
=> [
undef
],
D8A
=> [
undef
,
'b1'
],
'Foo::Bar'
=> [
qw(b1 b2 b3)
],
'Foo::Bar::Baz'
=> [
qw(b1 b2 b3 foo bar baz)
],
);
while
(
my
(
$class
,
$efields
) =
each
%EXPECT
) {
no
strict
'refs'
;
my
%fields
= %{
$class
.
'::FIELDS'
};
my
%expected_fields
;
foreach
my
$idx
(1..
@$efields
) {
my
$key
=
$efields
->[
$idx
-1];
next
unless
$key
;
$expected_fields
{
$key
} =
$idx
;
}
::is_deeply(\
%fields
, \
%expected_fields
,
"%FIELDS check: $class"
);
}
is(
$W
, 1,
'right warnings'
);
my
B2
$obj1
= D3->new;
$obj1
->{b1} =
"B2"
;
my
D3
$obj2
=
$obj1
;
$obj2
->{b1} =
"D3"
;
eval
q(return; my D3 $obj3 = $obj2; $obj3->{notthere} = "")
;
like $@,
qr/^No such $Field "notthere" in variable \$obj3 of type D3/
,
"Compile failure of undeclared fields (helem)"
;
SKIP: {
skip
"Doesn't work before 5.9"
, 2
if
$] < 5.009;
eval
q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ()
);
like $@,
qr/^No such $Field "notthere" in variable \$obj3 of type D3/
,
"Compile failure of undeclared fields (hslice)"
;
eval
q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ()
);
like
$@,
qr/^No such $Field "notthere" in variable \$obj3 of type D3/
,
"Compile failure of undeclared fields (hslice (block form))"
;
}
@$obj1
{
"_b1"
,
"b1"
} = (17, 29);
is(
$obj1
->{_b1}, 17 );
is(
$obj1
->{b1}, 29 );
@$obj1
{
'_b1'
,
'b1'
} = (44,28);
is(
$obj1
->{_b1}, 44 );
is(
$obj1
->{b1}, 28 );
use
fields
qw(yo this _lah meep 42)
;
eval
{
'base'
->
import
(
qw(E1 E2)
);
};
::like( $@,
qr/Can't multiply inherit fields/
i, 'Again,
no
multi inherit' );
sub
_mk_obj { fields::new(
$_
[0])->{
'b1'
} };
{
my
$w
= 0;
local
$SIG
{__WARN__} =
sub
{
$w
++ };
B9->_mk_obj();
D9->_mk_obj();
is (
$w
, 0,
"pseudohash warnings in derived class with no fields of it's own"
);
}
{
sub
new {
my
X
$self
=
shift
;
$self
= fields::new(
$self
)
unless
ref
$self
;
$self
->{X1} =
"x1"
;
$self
->{_X2} =
"_x2"
;
return
$self
;
}
sub
get_X2 {
my
X
$self
=
shift
;
$self
->{_X2} }
sub
new {
my
Y
$self
=
shift
;
$self
= fields::new(
$self
)
unless
ref
$self
;
$self
->SUPER::new();
return
$self
;
}
sub
new {
my
Z
$self
=
shift
;
$self
= fields::new(
$self
)
unless
ref
$self
;
$self
->SUPER::new();
$self
->{Z1} =
'z1'
;
return
$self
;
}
if
(
$Has_PH
) {
my
Z
$c
= Z->new();
is(
$c
->get_X2,
'_x2'
,
"empty intermediate class"
);
}
else
{
SKIP: {
skip
"restricted hashes don't support private fields properly"
, 1;
}
}
}