Resync with mainline
[p5sagit/p5-mst-13.2.git] / t / lib / fields.t
1 #!./perl -w
2
3 my $w;
4
5 BEGIN {
6    chdir 't' if -d 't';
7    unshift @INC, '../lib' if -d '../lib';
8    $SIG{__WARN__} = sub {
9        if ($_[0] =~ /^Hides field 'b1' in base class/) {
10            $w++;
11            return;
12        }
13        print $_[0];
14    };
15 }
16
17 use strict;
18 use vars qw($DEBUG);
19
20 package B1;
21 use fields qw(b1 b2 b3);
22
23 package B2;
24 use fields '_b1';
25 use fields qw(b1 _b2 b2);
26
27 sub new { bless [], shift }
28
29 package D1;
30 use base 'B1';
31 use fields qw(d1 d2 d3);
32
33 package D2;
34 use base 'B1';
35 use fields qw(_d1 _d2);
36 use fields qw(d1 d2);
37
38 package D3;
39 use base 'B2';
40 use fields qw(b1 d1 _b1 _d1);  # hide b1
41
42 package D4;
43 use base 'D3';
44 use fields qw(_d3 d3);
45
46 package M;
47 sub m {}
48
49 package D5;
50 use base qw(M B2);
51
52 package Foo::Bar;
53 use base 'B1';
54
55 package Foo::Bar::Baz;
56 use base 'Foo::Bar';
57 use fields qw(foo bar baz);
58
59 # Test repeatability for when modules get reloaded.
60 package B1;
61 use fields qw(b1 b2 b3);
62
63 package D3;
64 use base 'B2';
65 use fields qw(b1 d1 _b1 _d1);  # hide b1
66
67 package main;
68
69 sub fstr
70 {
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
82 my %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
93 print "1..", int(keys %expect)+5, "\n";
94 my $testno = 0;
95 while (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?
103 print "not " unless $w == 1;
104 print "ok ", ++$testno, "\n";
105
106 # A simple object creation and AVHV attribute access test
107 my B2 $obj1 = D3->new;
108 $obj1->{b1} = "B2";
109 my D3 $obj2 = $obj1;
110 $obj2->{b1} = "D3";
111
112 print "not " unless $obj1->[2] eq "B2" && $obj1->[5] eq "D3";
113 print "ok ", ++$testno, "\n";
114
115 # We should get compile time failures field name typos
116 eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
117 print "not " unless $@ && $@ =~ /^No such pseudo-hash field "notthere"/;
118 print "ok ", ++$testno, "\n";
119
120 #fields::_dump();
121
122 # check if 
123 {
124     package Foo;
125     use fields qw(foo bar);
126     sub new { bless [], $_[0]; }
127
128     package main;
129     my Foo $a = Foo->new();
130     $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
131     $a->{bar} = { A => 'ok ' . ++$testno };
132     print $a->{foo}[1], "\n";
133     print $a->{bar}->{A}, "\n";
134 }