7 unshift @INC, '../lib' if -d '../lib';
9 if ($_[0] =~ /^Hides field 'b1' in base class/) {
21 use fields qw(b1 b2 b3);
25 use fields qw(b1 _b2 b2);
27 sub new { bless [], shift }
31 use fields qw(d1 d2 d3);
35 use fields qw(_d1 _d2);
40 use fields qw(b1 d1 _b1 _d1); # hide b1
44 use fields qw(_d3 d3);
55 package Foo::Bar::Baz;
57 use fields qw(foo bar baz);
59 # Test repeatability for when modules get reloaded.
61 use fields qw(b1 b2 b3);
65 use fields qw(b1 d1 _b1 _d1); # hide b1
72 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
76 my $str = join(",", @tmp);
77 print "$h => $str\n" if $DEBUG;
82 B1 => "b1:1,b2:2,b3:3",
83 B2 => "_b1:1,b1:2,_b2:3,b2:4",
84 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
85 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
86 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
87 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
89 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
92 print "1..", int(keys %expect)+13, "\n";
94 while (my($class, $exp) = each %expect) {
96 my $fstr = fstr(\%{$class."::FIELDS"});
97 print "EXP: $exp\nGOT: $fstr\nnot " unless $fstr eq $exp;
98 print "ok ", ++$testno, "\n";
101 # Did we get the appropriate amount of warnings?
102 print "not " unless $w == 1;
103 print "ok ", ++$testno, "\n";
105 # A simple object creation and AVHV attribute access test
106 my B2 $obj1 = D3->new;
111 print "not " unless $obj1->[2] eq "B2" && $obj1->[5] eq "D3";
112 print "ok ", ++$testno, "\n";
114 # We should get compile time failures field name typos
115 eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
116 print "not " unless $@ && $@ =~ /^No such pseudo-hash field "notthere"/;
117 print "ok ", ++$testno, "\n";
120 @$obj1{"_b1", "b1"} = (17, 29);
121 print "not " unless "@$obj1[1,2]" eq "17 29";
122 print "ok ", ++$testno, "\n";
123 @$obj1[1,2] = (44,28);
124 print "not " unless "@$obj1{'b1','_b1','b1'}" eq "28 44 28";
125 print "ok ", ++$testno, "\n";
127 my $ph = fields::phash(a => 1, b => 2, c => 3);
128 print "not " unless fstr($ph) eq 'a:1,b:2,c:3';
129 print "ok ", ++$testno, "\n";
131 $ph = fields::phash([qw/a b c/], [1, 2, 3]);
132 print "not " unless fstr($ph) eq 'a:1,b:2,c:3';
133 print "ok ", ++$testno, "\n";
135 $ph = fields::phash([qw/a b c/], [1]);
136 print "not " if exists $ph->{b} or exists $ph->{c} or !exists $ph->{a};
137 print "ok ", ++$testno, "\n";
139 eval '$ph = fields::phash("odd")';
140 print "not " unless $@ && $@ =~ /^Odd number of/;
141 print "ok ", ++$testno, "\n";
145 # check if fields autovivify
148 use fields qw(foo bar);
149 sub new { bless [], $_[0]; }
152 my Foo $a = Foo->new();
153 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
154 $a->{bar} = { A => 'ok ' . ++$testno };
155 print $a->{foo}[1], "\n";
156 print $a->{bar}->{A}, "\n";
159 # check if fields autovivify
162 use fields qw(foo bar);
163 sub new { return fields::new($_[0]) }
166 my Bar $a = Bar::->new();
167 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
168 $a->{bar} = { A => 'ok ' . ++$testno };
169 print $a->{foo}[1], "\n";
170 print $a->{bar}->{A}, "\n";