This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate cfgperl and vmsperl contents into mainline
[perl5.git] / t / lib / fields.t
CommitLineData
f1192cee
GA
1#!./perl -w
2
f1192cee
GA
3my $w;
4
5BEGIN {
11162842 6 chdir 't' if -d 't';
93430cb4 7 unshift @INC, '../lib' if -d '../lib';
f1192cee
GA
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
GS
17use strict;
18use vars qw($DEBUG);
19
f1192cee
GA
20package B1;
21use fields qw(b1 b2 b3);
22
23package B2;
24use fields '_b1';
25use fields qw(b1 _b2 b2);
26
27sub new { bless [], shift }
28
29package D1;
30use base 'B1';
31use fields qw(d1 d2 d3);
32
33package D2;
34use base 'B1';
35use fields qw(_d1 _d2);
36use fields qw(d1 d2);
37
38package D3;
39use base 'B2';
40use fields qw(b1 d1 _b1 _d1); # hide b1
41
42package D4;
43use base 'D3';
44use fields qw(_d3 d3);
45
46package M;
47sub m {}
48
49package D5;
50use base qw(M B2);
51
52package Foo::Bar;
53use base 'B1';
54
55package Foo::Bar::Baz;
56use base 'Foo::Bar';
57use fields qw(foo bar baz);
58
f30a1143
JT
59# Test repeatability for when modules get reloaded.
60package B1;
61use fields qw(b1 b2 b3);
62
63package D3;
64use base 'B2';
65use fields qw(b1 d1 _b1 _d1); # hide b1
66
f1192cee
GA
67package main;
68
69sub 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
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
345599ca 93print "1..", int(keys %expect)+7, "\n";
f1192cee
GA
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
GA
118print "ok ", ++$testno, "\n";
119
345599ca
GS
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
f1192cee 128#fields::_dump();
377b21bb
GS
129
130# check if
131{
132 package Foo;
133 use fields qw(foo bar);
134 sub new { bless [], $_[0]; }
135
136 package main;
137 my Foo $a = Foo->new();
138 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
139 $a->{bar} = { A => 'ok ' . ++$testno };
140 print $a->{foo}[1], "\n";
141 print $a->{bar}->{A}, "\n";
142}