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/) {
60 if ($_[0] =~ /^Pseudo-hashes are deprecated/ &&
61 ($] >= 5.008 && $] < 5.009)) {
62 print "# $_[0]"; # Yes, we know they are deprecated.
73 use fields qw(b1 b2 b3);
77 use fields qw(b1 _b2 b2);
79 sub new { bless [], shift }
83 use fields qw(d1 d2 d3);
87 use fields qw(_d1 _d2);
92 use fields qw(b1 d1 _b1 _d1); # hide b1
96 use fields qw(_d3 d3);
107 package Foo::Bar::Baz;
109 use fields qw(foo bar baz);
111 # Test repeatability for when modules get reloaded.
113 use fields qw(b1 b2 b3);
117 use fields qw(b1 d1 _b1 _d1); # hide b1
124 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
128 my $str = join(",", @tmp);
129 print "$h => $str\n" if $DEBUG;
136 B1 => "b1:1,b2:2,b3:3",
137 B2 => "_b1:1,b1:2,_b2:3,b2:4",
138 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
139 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
140 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
141 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
143 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
145 $Total_tests += int(keys %expect);
148 while (my($class, $exp) = each %expect) {
150 my $fstr = fstr(\%{$class."::FIELDS"});
151 ok( $fstr eq $exp, "'$fstr' eq '$exp'" );
154 # Did we get the appropriate amount of warnings?
157 # A simple object creation and AVHV attribute access test
158 my B2 $obj1 = D3->new;
163 ok( $obj1->[2] eq "B2" && $obj1->[5] eq "D3" );
165 # We should get compile time failures field name typos
166 eval q{ my D3 $obj3 = $obj2; $obj3->{notthere} = "" };
167 ok( $@ && $@ =~ /^No such pseudo-hash field "notthere"/,
168 'compile error -- field name typos' );
173 @$obj1{"_b1", "b1"} = (17, 29);
174 ok( "@$obj1[1,2]" eq "17 29" );
176 @$obj1[1,2] = (44,28);
177 ok( "@$obj1{'b1','_b1','b1'}" eq "28 44 28" );
180 ok( 1, 'test skipped for perl < 5.6.0' );
181 ok( 1, 'test skipped for perl < 5.6.0' );
184 my $ph = fields::phash(a => 1, b => 2, c => 3);
185 ok( fstr($ph) eq 'a:1,b:2,c:3' );
187 $ph = fields::phash([qw/a b c/], [1, 2, 3]);
188 ok( fstr($ph) eq 'a:1,b:2,c:3' );
190 # The way exists() works with psuedohashes changed from 5.005 to 5.6
191 $ph = fields::phash([qw/a b c/], [1]);
193 ok( !( exists $ph->{b} or exists $ph->{c} or !exists $ph->{a} ) );
196 ok( !( defined $ph->{b} or defined $ph->{c} or !defined $ph->{a} ) );
199 eval { $ph = fields::phash("odd") };
200 ok( $@ && $@ =~ /^Odd number of/ );
203 # check if fields autovivify
206 use fields qw(foo bar);
207 sub new { bless [], $_[0]; }
210 my Foo $a = Foo->new();
211 $a->{foo} = ['a', 'ok', 'c'];
212 $a->{bar} = { A => 'ok' };
213 ok( $a->{foo}[1] eq 'ok' );
214 ok( $a->{bar}->{A} eq 'ok' );
217 ok( 1, 'test skipped for perl < 5.6.0' );
218 ok( 1, 'test skipped for perl < 5.6.0' );
221 # check if fields autovivify
224 use fields qw(foo bar);
225 sub new { return fields::new($_[0]) }
228 my Bar $a = Bar::->new();
229 $a->{foo} = ['a', 'ok', 'c'];
230 $a->{bar} = { A => 'ok' };
231 ok( $a->{foo}[1] eq 'ok' );
232 ok( $a->{bar}->{A} eq 'ok' );