4 if( $ENV{PERL_CORE} ) {
13 $Field = $Has_PH ? "pseudo-hash field" : "class field";
20 $SIG{__WARN__} = sub {
21 if ($_[0] =~ /^Hides field '.*?' in base class/) {
31 use Test::More tests => 29;
33 BEGIN { use_ok('base'); }
36 use fields qw(b1 b2 b3);
40 use fields qw(b1 _b2 b2);
42 sub new { fields::new(shift) }
45 use fields qw(b4 _b5 b6 _b7);
49 use fields qw(d1 d2 d3);
53 use fields qw(_d1 _d2);
59 use fields qw(b1 d1 _b1 _d1); # hide b1
63 use fields qw(_d3 d3);
71 # Test that multiple inheritance fails.
73 eval { 'base'->import(qw(B2 M B3)); };
74 ::like($@, qr/can't multiply inherit fields/i,
75 'No multiple field inheritance');
80 package Foo::Bar::Baz;
82 use fields qw(foo bar baz);
84 # Test repeatability for when modules get reloaded.
86 use fields qw(b1 b2 b3);
90 use fields qw(b1 d1 _b1 _d1); # hide b1
93 # Test that a package with only private fields gets inherited properly
102 # Test that an intermediate package with no fields doesn't cause a problem.
117 B1 => [qw(b1 b2 b3)],
118 D1 => [qw(b1 b2 b3 d1 d2 d3)],
119 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)],
122 B2 => [qw(_b1 b1 _b2 b2)],
123 D3 => [(undef,undef,undef,
124 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden
125 D4 => [(undef,undef,undef,
126 qw(b2 b1 d1),undef,undef,qw(_d3 d3))],
128 D5 => [undef, 'b1', undef, 'b2'],
130 B3 => [qw(b4 _b5 b6 _b7)],
137 D8A => [undef, 'b1'],
139 'Foo::Bar' => [qw(b1 b2 b3)],
140 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)],
143 while(my($class, $efields) = each %EXPECT) {
145 my %fields = %{$class.'::FIELDS'};
147 foreach my $idx (1..@$efields) {
148 my $key = $efields->[$idx-1];
150 $expected_fields{$key} = $idx;
153 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class");
156 # Did we get the appropriate amount of warnings?
157 is( $W, 1, 'right warnings' );
160 # A simple object creation and attribute access test
161 my B2 $obj1 = D3->new;
166 # We should get compile time failures field name typos
167 eval q(return; my D3 $obj3 = $obj2; $obj3->{notthere} = "");
169 qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
170 "Compile failure of undeclared fields (helem)";
174 # We should get compile time failures field name typos
175 skip "Doesn't work before 5.9", 2 if $] < 5.009;
176 eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ());
178 qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
179 "Compile failure of undeclared fields (hslice)";
180 eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ());
182 $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/,
183 "Compile failure of undeclared fields (hslice (block form))";
186 @$obj1{"_b1", "b1"} = (17, 29);
187 is( $obj1->{_b1}, 17 );
188 is( $obj1->{b1}, 29 );
190 @$obj1{'_b1', 'b1'} = (44,28);
191 is( $obj1->{_b1}, 44 );
192 is( $obj1->{b1}, 28 );
196 # Break multiple inheritance with a field name clash.
198 use fields qw(yo this _lah meep 42);
201 use fields qw(_yo ahhh this);
206 # The error must occur at run time for the eval to catch it.
208 'base'->import(qw(E1 E2));
210 ::like( $@, qr/Can't multiply inherit fields/i, 'Again, no multi inherit' );
213 # Test that a package with no fields can inherit from a package with
214 # fields, and that pseudohash messages don't show up
219 sub _mk_obj { fields::new($_[0])->{'b1'} };
228 local $SIG{__WARN__} = sub { $w++ };
231 # used tp emit a warning that pseudohashes are deprecated, because
232 # %FIELDS wasn't blessed.
235 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own");
238 # [perl #31078] an intermediate class with no additional fields caused
239 # hidden fields in base class to get stomped on
243 use fields qw(X1 _X2);
246 $self = fields::new($self) unless ref $self;
248 $self->{_X2} = "_x2";
251 sub get_X2 { my X $self = shift; $self->{_X2} }
258 $self = fields::new($self) unless ref $self;
270 $self = fields::new($self) unless ref $self;
279 is($c->get_X2, '_x2', "empty intermediate class");