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)";
167 # We should get compile time failures field name typos
168 skip "Doesn't work before 5.9", 2 if $] < 5.009;
169 eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ());
171 qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
172 "Compile failure of undeclared fields (hslice)";
173 eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ());
175 $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
176 "Compile failure of undeclared fields (hslice (block form))";
179 @$obj1{"_b1", "b1"} = (17, 29);
180 is( $obj1->{_b1}, 17 );
181 is( $obj1->{b1}, 29 );
183 @$obj1{'_b1', 'b1'} = (44,28);
184 is( $obj1->{_b1}, 44 );
185 is( $obj1->{b1}, 28 );
189 # Break multiple inheritance with a field name clash.
191 use fields qw(yo this _lah meep 42);
194 use fields qw(_yo ahhh this);
199 # The error must occur at run time for the eval to catch it.
201 'base'->import(qw(E1 E2));
203 ::like( $@, qr/Can't multiply inherit fields/i, 'Again, no multi inherit' );
206 # Test that a package with no fields can inherit from a package with
207 # fields, and that pseudohash messages don't show up
212 sub _mk_obj { fields::new($_[0])->{'b1'} };
221 local $SIG{__WARN__} = sub { $w++ };
224 # used tp emit a warning that pseudohashes are deprecated, because
225 # %FIELDS wasn't blessed.
228 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own");
231 # [perl #31078] an intermediate class with no additional fields caused
232 # hidden fields in base class to get stomped on
236 use fields qw(X1 _X2);
239 $self = fields::new($self) unless ref $self;
241 # FIXME. This code is dead on blead because the test is skipped.
242 # The test states that it's being skipped because restricted hashes
243 # don't support a feature. Presumably we need to make that feature
245 # use Devel::Peek; Dump($self);
246 $self->{_X2} = "_x2";
249 sub get_X2 { my X $self = shift; $self->{_X2} }
256 $self = fields::new($self) unless ref $self;
268 $self = fields::new($self) unless ref $self;
278 is($c->get_X2, '_x2', "empty intermediate class");
282 skip "restricted hashes don't support private fields properly", 1;