6 $Field = $Has_PH ? "pseudo-hash field" : "class field";
13 $SIG{__WARN__} = sub {
14 if ($_[0] =~ /^Hides field '.*?' in base class/) {
24 use Test::More tests => 29;
26 BEGIN { use_ok('base'); }
29 use fields qw(b1 b2 b3);
33 use fields qw(b1 _b2 b2);
35 sub new { fields::new(shift) }
38 use fields qw(b4 _b5 b6 _b7);
42 use fields qw(d1 d2 d3);
46 use fields qw(_d1 _d2);
52 use fields qw(b1 d1 _b1 _d1); # hide b1
56 use fields qw(_d3 d3);
64 # Test that multiple inheritance fails.
66 eval { 'base'->import(qw(B2 M B3)); };
67 ::like($@, qr/can't multiply inherit %FIELDS/i,
68 'No multiple field inheritance');
73 package Foo::Bar::Baz;
75 use fields qw(foo bar baz);
77 # Test repeatability for when modules get reloaded.
79 use fields qw(b1 b2 b3);
83 use fields qw(b1 d1 _b1 _d1); # hide b1
86 # Test that a package with only private fields gets inherited properly
95 # Test that an intermediate package with no fields doesn't cause a problem.
110 B1 => [qw(b1 b2 b3)],
111 D1 => [qw(b1 b2 b3 d1 d2 d3)],
112 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)],
115 B2 => [qw(_b1 b1 _b2 b2)],
116 D3 => [(undef,undef,undef,
117 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden
118 D4 => [(undef,undef,undef,
119 qw(b2 b1 d1),undef,undef,qw(_d3 d3))],
121 D5 => [undef, 'b1', undef, 'b2'],
123 B3 => [qw(b4 _b5 b6 _b7)],
130 D8A => [undef, 'b1'],
132 'Foo::Bar' => [qw(b1 b2 b3)],
133 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)],
136 while(my($class, $efields) = each %EXPECT) {
138 my %fields = %{$class.'::FIELDS'};
140 foreach my $idx (1..@$efields) {
141 my $key = $efields->[$idx-1];
143 $expected_fields{$key} = $idx;
146 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class");
149 # Did we get the appropriate amount of warnings?
150 is( $W, 1, 'right warnings' );
153 # A simple object creation and attribute access test
154 my B2 $obj1 = D3->new;
159 # We should get compile time failures field name typos
160 eval q(return; my D3 $obj3 = $obj2; $obj3->{notthere} = "");
162 qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
163 "Compile failure of undeclared fields (helem)";
166 # We should get compile time failures field name typos
167 eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ());
169 qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
170 "Compile failure of undeclared fields (hslice)";
171 eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ());
173 $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
174 "Compile failure of undeclared fields (hslice (block form))";
176 @$obj1{"_b1", "b1"} = (17, 29);
177 is( $obj1->{_b1}, 17 );
178 is( $obj1->{b1}, 29 );
180 @$obj1{'_b1', 'b1'} = (44,28);
181 is( $obj1->{_b1}, 44 );
182 is( $obj1->{b1}, 28 );
186 # Break multiple inheritance with a field name clash.
188 use fields qw(yo this _lah meep 42);
191 use fields qw(_yo ahhh this);
196 # The error must occur at run time for the eval to catch it.
198 'base'->import(qw(E1 E2));
200 ::like( $@, qr/Can't multiply inherit %FIELDS/i, 'Again, no multi inherit' );
203 # Test that a package with no fields can inherit from a package with
204 # fields, and that pseudohash messages don't show up
209 sub _mk_obj { fields::new($_[0])->{'b1'} };
218 local $SIG{__WARN__} = sub { $w++ };
221 # used tp emit a warning that pseudohashes are deprecated, because
222 # %FIELDS wasn't blessed.
225 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own");
228 # [perl #31078] an intermediate class with no additional fields caused
229 # hidden fields in base class to get stomped on
233 use fields qw(X1 _X2);
236 $self = fields::new($self) unless ref $self;
238 # FIXME. This code is dead on blead becase the test is skipped.
239 # The test states that it's being skipped becaues restricted hashes
240 # don't support a feature. Presumably we need to make that feature
242 # use Devel::Peek; Dump($self);
243 $self->{_X2} = "_x2";
246 sub get_X2 { my X $self = shift; $self->{_X2} }
253 $self = fields::new($self) unless ref $self;
265 $self = fields::new($self) unless ref $self;
275 is($c->get_X2, '_x2', "empty intermediate class");
279 skip "restricted hashes don't support private fields properly", 1;