12 $SIG{__WARN__} = sub {
13 if ($_[0] =~ /^Hides field '.*?' in base class/) {
23 use Test::More tests => 26;
25 BEGIN { use_ok('base'); }
28 use fields qw(b1 b2 b3);
32 use fields qw(b1 _b2 b2);
34 sub new { fields::new(shift) }
37 use fields qw(b4 _b5 b6 _b7);
41 use fields qw(d1 d2 d3);
45 use fields qw(_d1 _d2);
51 use fields qw(b1 d1 _b1 _d1); # hide b1
55 use fields qw(_d3 d3);
63 # Test that multiple inheritance fails.
65 eval { 'base'->import(qw(B2 M B3)); };
66 ::like($@, qr/can't multiply inherit %FIELDS/i,
67 'No multiple field inheritance');
72 package Foo::Bar::Baz;
74 use fields qw(foo bar baz);
76 # Test repeatability for when modules get reloaded.
78 use fields qw(b1 b2 b3);
82 use fields qw(b1 d1 _b1 _d1); # hide b1
85 # Test that a package with only private fields gets inherited properly
94 # Test that an intermediate package with no fields doesn't cause a problem.
109 B1 => [qw(b1 b2 b3)],
110 D1 => [qw(b1 b2 b3 d1 d2 d3)],
111 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)],
114 B2 => [qw(_b1 b1 _b2 b2)],
115 D3 => [(undef,undef,undef,
116 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden
117 D4 => [(undef,undef,undef,
118 qw(b2 b1 d1),undef,undef,qw(_d3 d3))],
120 D5 => [undef, 'b1', undef, 'b2'],
122 B3 => [qw(b4 _b5 b6 _b7)],
129 D8A => [undef, 'b1'],
131 'Foo::Bar' => [qw(b1 b2 b3)],
132 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)],
135 while(my($class, $efields) = each %EXPECT) {
137 my %fields = %{$class.'::FIELDS'};
139 foreach my $idx (1..@$efields) {
140 my $key = $efields->[$idx-1];
142 $expected_fields{$key} = $idx;
145 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class");
148 # Did we get the appropriate amount of warnings?
149 is( $W, 1, 'right warnings' );
152 # A simple object creation and attribute access test
153 my B2 $obj1 = D3->new;
158 # We should get compile time failures field name typos
159 eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
162 qr/^No such pseudo-hash field "notthere" in variable \$obj3 of type D3/;
166 qr/^Attempt to access disallowed key 'notthere' in a restricted hash/;
170 @$obj1{"_b1", "b1"} = (17, 29);
171 is( $obj1->{_b1}, 17 );
172 is( $obj1->{b1}, 29 );
174 @$obj1{'_b1', 'b1'} = (44,28);
175 is( $obj1->{_b1}, 44 );
176 is( $obj1->{b1}, 28 );
180 # Break multiple inheritance with a field name clash.
182 use fields qw(yo this _lah meep 42);
185 use fields qw(_yo ahhh this);
190 # The error must occur at run time for the eval to catch it.
192 'base'->import(qw(E1 E2));
194 ::like( $@, qr/Can't multiply inherit %FIELDS/i, 'Again, no multi inherit' );
197 # Test that a package with no fields can inherit from a package with
198 # fields, and that pseudohash messages don't show up
203 sub _mk_obj { fields::new($_[0])->{'b1'} };
212 local $SIG{__WARN__} = sub { $w++ };
215 # used tp emit a warning that pseudohashes are deprecated, because
216 # %FIELDS wasn't blessed.
219 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own");