1 # The fields.pm and base.pm regression tests from 5.6.0
3 # We skip this on 5.9.0 and up since pseudohashes were removed and a lot
6 print "1..0 # skip pseudo-hashes removed in 5.9.0\n";
11 use vars qw($Total_tests);
14 BEGIN { $| = 1; $^W = 1; }
15 print "1..$Total_tests\n";
18 print "ok $test_num\n";
21 # Insert your test code below (better if it prints "ok 13"
22 # (correspondingly "not ok 13") depending on the success of chunk 13
25 my($test, $name) = @_;
26 print "not " unless $test;
28 print " - $name" if defined $name;
35 return 0 unless @$a1 == @$a2;
38 unless($a1->[$_] eq $a2->[$_]) {
46 # Change this to your # of ok() calls + 1
47 BEGIN { $Total_tests = 14 }
55 $SIG{__WARN__} = sub {
56 if ($_[0] =~ /^Hides field 'b1' in base class/) {
68 use fields qw(b1 b2 b3);
72 use fields qw(b1 _b2 b2);
74 sub new { bless [], shift }
78 use fields qw(d1 d2 d3);
82 use fields qw(_d1 _d2);
87 use fields qw(b1 d1 _b1 _d1); # hide b1
91 use fields qw(_d3 d3);
102 package Foo::Bar::Baz;
104 use fields qw(foo bar baz);
106 # Test repeatability for when modules get reloaded.
108 use fields qw(b1 b2 b3);
112 use fields qw(b1 d1 _b1 _d1); # hide b1
119 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
123 my $str = join(",", @tmp);
124 print "$h => $str\n" if $DEBUG;
131 B1 => "b1:1,b2:2,b3:3",
132 B2 => "_b1:1,b1:2,_b2:3,b2:4",
133 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
134 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
135 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
136 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
138 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
140 $Total_tests += int(keys %expect);
143 while (my($class, $exp) = each %expect) {
145 my $fstr = fstr(\%{$class."::FIELDS"});
146 ok( $fstr eq $exp, "'$fstr' eq '$exp'" );
149 # Did we get the appropriate amount of warnings?
152 # A simple object creation and AVHV attribute access test
153 my B2 $obj1 = D3->new;
158 ok( $obj1->[2] eq "B2" && $obj1->[5] eq "D3" );
160 # We should get compile time failures field name typos
161 eval q{ my D3 $obj3 = $obj2; $obj3->{notthere} = "" };
162 ok( $@ && $@ =~ /^No such pseudo-hash field "notthere"/,
163 'compile error -- field name typos' );
168 @$obj1{"_b1", "b1"} = (17, 29);
169 ok( "@$obj1[1,2]" eq "17 29" );
171 @$obj1[1,2] = (44,28);
172 ok( "@$obj1{'b1','_b1','b1'}" eq "28 44 28" );
175 ok( 1, 'test skipped for perl < 5.6.0' );
176 ok( 1, 'test skipped for perl < 5.6.0' );
179 my $ph = fields::phash(a => 1, b => 2, c => 3);
180 ok( fstr($ph) eq 'a:1,b:2,c:3' );
182 $ph = fields::phash([qw/a b c/], [1, 2, 3]);
183 ok( fstr($ph) eq 'a:1,b:2,c:3' );
185 # The way exists() works with psuedohashes changed from 5.005 to 5.6
186 $ph = fields::phash([qw/a b c/], [1]);
188 ok( !( exists $ph->{b} or exists $ph->{c} or !exists $ph->{a} ) );
191 ok( !( defined $ph->{b} or defined $ph->{c} or !defined $ph->{a} ) );
194 eval { $ph = fields::phash("odd") };
195 ok( $@ && $@ =~ /^Odd number of/ );
198 # check if fields autovivify
201 use fields qw(foo bar);
202 sub new { bless [], $_[0]; }
205 my Foo $a = Foo->new();
206 $a->{foo} = ['a', 'ok', 'c'];
207 $a->{bar} = { A => 'ok' };
208 ok( $a->{foo}[1] eq 'ok' );
209 ok( $a->{bar}->{A} eq 'ok' );
212 ok( 1, 'test skipped for perl < 5.6.0' );
213 ok( 1, 'test skipped for perl < 5.6.0' );
216 # check if fields autovivify
219 use fields qw(foo bar);
220 sub new { return fields::new($_[0]) }
223 my Bar $a = Bar::->new();
224 $a->{foo} = ['a', 'ok', 'c'];
225 $a->{bar} = { A => 'ok' };
226 ok( $a->{foo}[1] eq 'ok' );
227 ok( $a->{bar}->{A} eq 'ok' );