9 if ($_[0] =~ /^Hides field 'b1' in base class/) {
25 use fields qw(b1 b2 b3);
29 use fields qw(b1 _b2 b2);
31 sub new { fields::new(shift); }
35 use fields qw(d1 d2 d3);
39 use fields qw(_d1 _d2);
44 use fields qw(b1 d1 _b1 _d1); # hide b1
48 use fields qw(_d3 d3);
59 package Foo::Bar::Baz;
61 use fields qw(foo bar baz);
63 # Test repeatability for when modules get reloaded.
65 use fields qw(b1 b2 b3);
69 use fields qw(b1 d1 _b1 _d1); # hide b1
76 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
80 my $str = join(",", @tmp);
81 print "$h => $str\n" if $DEBUG;
86 B1 => "b1:1,b2:2,b3:3",
87 B2 => "_b1:1,b1:2,_b2:3,b2:4",
88 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
89 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
90 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
91 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
93 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
96 plan tests => keys(%expect) + 21;
100 while (my($class, $exp) = each %expect) {
102 my $fstr = fstr(\%{$class."::FIELDS"});
103 is( $fstr, $exp, "\%FIELDS check for $class" );
106 # Did we get the appropriate amount of warnings?
109 # A simple object creation and AVHV attribute access test
110 my B2 $obj1 = D3->new;
115 # We should get compile time failures field name typos
116 eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
117 like $@, qr/^Attempt to access disallowed key 'notthere' in a restricted hash/;
120 @$obj1{"_b1", "b1"} = (17, 29);
121 is_deeply($obj1, { b1 => 29, _b1 => 17 });
123 @$obj1{'_b1', 'b1'} = (44,28);
124 is_deeply($obj1, { b1 => 28, _b1 => 44 });
126 eval { fields::phash };
127 like $@, qr/^Pseudo-hashes have been removed from Perl/;
131 # check if fields autovivify
134 use fields qw(foo bar);
135 sub new { fields::new($_[0]) }
138 my Foo $a = Foo->new();
139 $a->{foo} = ['a', 'ok', 'c'];
140 $a->{bar} = { A => 'ok' };
141 is( $a->{foo}[1], 'ok' );
142 is( $a->{bar}->{A},, 'ok' );
145 # check if fields autovivify
148 use fields qw(foo bar);
149 sub new { return fields::new($_[0]) }
152 my Bar $a = Bar::->new();
153 $a->{foo} = ['a', 'ok', 'c'];
154 $a->{bar} = { A => 'ok' };
155 is( $a->{foo}[1], 'ok' );
156 is( $a->{bar}->{A}, 'ok' );
166 package Test::Version;
168 use base qw(No::Version);
169 ::like( $No::Version::VERSION, qr/set by base.pm/ );
171 # Test Inverse of $VERSION bug base.pm should not clobber existing $VERSION
172 package Has::Version;
174 BEGIN { $Has::Version::VERSION = '42' };
176 package Test::Version2;
178 use base qw(Has::Version);
179 ::is( $Has::Version::VERSION, 42 );
189 $Eval2::VERSION = "1.02";
191 $Eval1::VERSION = "1.01";
198 is( $Eval1::VERSION, 1.01 );
200 is( $Eval2::VERSION, 1.02 );
203 eval q{use base 'reallyReAlLyNotexists'};
204 like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
205 'base with empty package');
207 eval q{use base 'reallyReAlLyNotexists'};
208 like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
209 ' still empty on 2nd load');
211 BEGIN { $Has::Version_0::VERSION = 0 }
213 package Test::Version3;
215 use base qw(Has::Version_0);
216 ::is( $Has::Version_0::VERSION, 0, '$VERSION==0 preserved' );
218 package Test::FooBar;
220 use fields qw(a b c);
223 my $self = fields::new(shift);
231 my $x = Test::FooBar->new( a => 1, b => 2);
233 is(ref $x, 'Test::FooBar', 'x is a Test::FooBar');
234 ok(exists $x->{a}, 'x has a');
235 ok(exists $x->{b}, 'x has b');
236 is(scalar keys %$x, 2, 'x has two fields');