This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] Simplified magic_setisa() and improved fields.pm
[perl5.git] / t / lib / fields.t
CommitLineData
f1192cee
GA
1#!./perl -w
2
3use strict;
4use vars qw($DEBUG);
5
6my $w;
7
8BEGIN {
9 $SIG{__WARN__} = sub {
10 if ($_[0] =~ /^Hides field 'b1' in base class/) {
11 $w++;
12 return;
13 }
14 print $_[0];
15 };
16}
17
18package B1;
19use fields qw(b1 b2 b3);
20
21package B2;
22use fields '_b1';
23use fields qw(b1 _b2 b2);
24
25sub new { bless [], shift }
26
27package D1;
28use base 'B1';
29use fields qw(d1 d2 d3);
30
31package D2;
32use base 'B1';
33use fields qw(_d1 _d2);
34use fields qw(d1 d2);
35
36package D3;
37use base 'B2';
38use fields qw(b1 d1 _b1 _d1); # hide b1
39
40package D4;
41use base 'D3';
42use fields qw(_d3 d3);
43
44package M;
45sub m {}
46
47package D5;
48use base qw(M B2);
49
50package Foo::Bar;
51use base 'B1';
52
53package Foo::Bar::Baz;
54use base 'Foo::Bar';
55use fields qw(foo bar baz);
56
57package main;
58
59sub fstr
60{
61 my $h = shift;
62 my @tmp;
63 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
64 my $v = $h->{$k};
65 push(@tmp, "$k:$v");
66 }
67 my $str = join(",", @tmp);
68 print "$h => $str\n" if $DEBUG;
69 $str;
70}
71
72my %expect = (
73 B1 => "b1:1,b2:2,b3:3",
74 B2 => "_b1:1,b1:2,_b2:3,b2:4",
75 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
76 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
77 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
78 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
79 D5 => "b1:2,b2:4",
80 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
81);
82
83print "1..", int(keys %expect)+3, "\n";
84my $testno = 0;
85while (my($class, $exp) = each %expect) {
86 no strict 'refs';
87 my $fstr = fstr(\%{$class."::FIELDS"});
88 print "EXP: $exp\nGOT: $fstr\nnot " unless $fstr eq $exp;
89 print "ok ", ++$testno, "\n";
90}
91
92# Did we get the appropriate amount of warnings?
93print "not " unless $w == 1;
94print "ok ", ++$testno, "\n";
95
96# A simple object creation and AVHV attribute access test
97my B2 $obj1 = D3->new;
98$obj1->{b1} = "B2";
99my D3 $obj2 = $obj1;
100$obj2->{b1} = "D3";
101
102print "not " unless $obj1->[2] eq "B2" && $obj1->[5] eq "D3";
103print "ok ", ++$testno, "\n";
104
105# We should get compile time failures field name typos
106eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
107print "not " unless $@ && $@ =~ /^No such field "notthere"/;
108print "ok ", ++$testno, "\n";
109
110#fields::_dump();