Message-ID: <f1gj4usu5m76bv88a3ldptnmo6ld7d44ri@4ax.com>
[p5sagit/p5-mst-13.2.git] / lib / fields.t
CommitLineData
f1192cee 1#!./perl -w
2
f1192cee 3my $w;
4
5BEGIN {
11162842 6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
f1192cee 8 $SIG{__WARN__} = sub {
9 if ($_[0] =~ /^Hides field 'b1' in base class/) {
10 $w++;
11 return;
12 }
13 print $_[0];
14 };
15}
16
b47ba5cf 17use strict;
9f1b1f2d 18use warnings;
b47ba5cf 19use vars qw($DEBUG);
20
f1192cee 21package B1;
22use fields qw(b1 b2 b3);
23
24package B2;
25use fields '_b1';
26use fields qw(b1 _b2 b2);
27
28sub new { bless [], shift }
29
30package D1;
31use base 'B1';
32use fields qw(d1 d2 d3);
33
34package D2;
35use base 'B1';
36use fields qw(_d1 _d2);
37use fields qw(d1 d2);
38
39package D3;
40use base 'B2';
41use fields qw(b1 d1 _b1 _d1); # hide b1
42
43package D4;
44use base 'D3';
45use fields qw(_d3 d3);
46
47package M;
48sub m {}
49
50package D5;
51use base qw(M B2);
52
53package Foo::Bar;
54use base 'B1';
55
56package Foo::Bar::Baz;
57use base 'Foo::Bar';
58use fields qw(foo bar baz);
59
f30a1143 60# Test repeatability for when modules get reloaded.
61package B1;
62use fields qw(b1 b2 b3);
63
64package D3;
65use base 'B2';
66use fields qw(b1 d1 _b1 _d1); # hide b1
67
f1192cee 68package main;
69
479ba383 70sub fstr {
f1192cee 71 my $h = shift;
72 my @tmp;
73 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
74 my $v = $h->{$k};
75 push(@tmp, "$k:$v");
76 }
77 my $str = join(",", @tmp);
78 print "$h => $str\n" if $DEBUG;
79 $str;
80}
81
82my %expect = (
83 B1 => "b1:1,b2:2,b3:3",
84 B2 => "_b1:1,b1:2,_b2:3,b2:4",
85 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
86 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
87 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
88 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
89 D5 => "b1:2,b2:4",
90 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
91);
92
e8f84f55 93print "1..", int(keys %expect)+15, "\n";
f1192cee 94my $testno = 0;
95while (my($class, $exp) = each %expect) {
96 no strict 'refs';
97 my $fstr = fstr(\%{$class."::FIELDS"});
98 print "EXP: $exp\nGOT: $fstr\nnot " unless $fstr eq $exp;
99 print "ok ", ++$testno, "\n";
100}
101
102# Did we get the appropriate amount of warnings?
103print "not " unless $w == 1;
104print "ok ", ++$testno, "\n";
105
106# A simple object creation and AVHV attribute access test
107my B2 $obj1 = D3->new;
108$obj1->{b1} = "B2";
109my D3 $obj2 = $obj1;
110$obj2->{b1} = "D3";
111
112print "not " unless $obj1->[2] eq "B2" && $obj1->[5] eq "D3";
113print "ok ", ++$testno, "\n";
114
115# We should get compile time failures field name typos
116eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
ae9a5a84 117print "not " unless $@ && $@ =~ /^No such pseudo-hash field "notthere"/;
f1192cee 118print "ok ", ++$testno, "\n";
119
345599ca 120# Slices
121@$obj1{"_b1", "b1"} = (17, 29);
122print "not " unless "@$obj1[1,2]" eq "17 29";
123print "ok ", ++$testno, "\n";
124@$obj1[1,2] = (44,28);
125print "not " unless "@$obj1{'b1','_b1','b1'}" eq "28 44 28";
126print "ok ", ++$testno, "\n";
127
479ba383 128my $ph = fields::phash(a => 1, b => 2, c => 3);
129print "not " unless fstr($ph) eq 'a:1,b:2,c:3';
130print "ok ", ++$testno, "\n";
131
132$ph = fields::phash([qw/a b c/], [1, 2, 3]);
133print "not " unless fstr($ph) eq 'a:1,b:2,c:3';
134print "ok ", ++$testno, "\n";
135
136$ph = fields::phash([qw/a b c/], [1]);
137print "not " if exists $ph->{b} or exists $ph->{c} or !exists $ph->{a};
138print "ok ", ++$testno, "\n";
139
140eval '$ph = fields::phash("odd")';
141print "not " unless $@ && $@ =~ /^Odd number of/;
142print "ok ", ++$testno, "\n";
143
f1192cee 144#fields::_dump();
377b21bb 145
479ba383 146# check if fields autovivify
377b21bb 147{
148 package Foo;
149 use fields qw(foo bar);
150 sub new { bless [], $_[0]; }
151
152 package main;
153 my Foo $a = Foo->new();
154 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
155 $a->{bar} = { A => 'ok ' . ++$testno };
156 print $a->{foo}[1], "\n";
157 print $a->{bar}->{A}, "\n";
158}
479ba383 159
160# check if fields autovivify
161{
162 package Bar;
163 use fields qw(foo bar);
164 sub new { return fields::new($_[0]) }
165
166 package main;
167 my Bar $a = Bar::->new();
168 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
169 $a->{bar} = { A => 'ok ' . ++$testno };
170 print $a->{foo}[1], "\n";
171 print $a->{bar}->{A}, "\n";
172}
2bc5db75 173
174
175# Test $VERSION bug
176package No::Version;
177
178use vars qw($Foo);
179sub VERSION { 42 }
180
181package Test::Version;
182
183use base qw(No::Version);
184print "not " unless $No::Version::VERSION =~ /set by base\.pm/;
185print "ok ", ++$testno ,"\n";
e8f84f55 186
187# Test Inverse of $VERSION bug base.pm should not clobber existing $VERSION
188package Has::Version;
189
190BEGIN { $Has::Version::VERSION = '42' };
191
192package Test::Version2;
193
194use base qw(Has::Version);
195print "#$Has::Version::VERSION\nnot " unless $Has::Version::VERSION eq '42';
196print "ok ", ++$testno ,"\n";
197