This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid initializing GvCV slot for autovivified filehandles
[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
59package main;
60
61sub fstr
62{
63 my $h = shift;
64 my @tmp;
65 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
66 my $v = $h->{$k};
67 push(@tmp, "$k:$v");
68 }
69 my $str = join(",", @tmp);
70 print "$h => $str\n" if $DEBUG;
71 $str;
72}
73
74my %expect = (
75 B1 => "b1:1,b2:2,b3:3",
76 B2 => "_b1:1,b1:2,_b2:3,b2:4",
77 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
78 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
79 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
80 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
81 D5 => "b1:2,b2:4",
82 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
83);
84
377b21bb 85print "1..", int(keys %expect)+5, "\n";
f1192cee
GA
86my $testno = 0;
87while (my($class, $exp) = each %expect) {
88 no strict 'refs';
89 my $fstr = fstr(\%{$class."::FIELDS"});
90 print "EXP: $exp\nGOT: $fstr\nnot " unless $fstr eq $exp;
91 print "ok ", ++$testno, "\n";
92}
93
94# Did we get the appropriate amount of warnings?
95print "not " unless $w == 1;
96print "ok ", ++$testno, "\n";
97
98# A simple object creation and AVHV attribute access test
99my B2 $obj1 = D3->new;
100$obj1->{b1} = "B2";
101my D3 $obj2 = $obj1;
102$obj2->{b1} = "D3";
103
104print "not " unless $obj1->[2] eq "B2" && $obj1->[5] eq "D3";
105print "ok ", ++$testno, "\n";
106
107# We should get compile time failures field name typos
108eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
ae9a5a84 109print "not " unless $@ && $@ =~ /^No such pseudo-hash field "notthere"/;
f1192cee
GA
110print "ok ", ++$testno, "\n";
111
112#fields::_dump();
377b21bb
GS
113
114# check if
115{
116 package Foo;
117 use fields qw(foo bar);
118 sub new { bless [], $_[0]; }
119
120 package main;
121 my Foo $a = Foo->new();
122 $a->{foo} = ['a', 'ok ' . ++$testno, 'c'];
123 $a->{bar} = { A => 'ok ' . ++$testno };
124 print $a->{foo}[1], "\n";
125 print $a->{bar}->{A}, "\n";
126}