This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #24942] fields::inherit doesn't bless derived
[perl5.git] / lib / base / t / fields-base.t
CommitLineData
33d611b9
JH
1#!/usr/bin/perl -w
2
3my $Has_PH;
4BEGIN {
5 $Has_PH = $] < 5.009;
6}
7
8my $W;
9
10BEGIN {
11 $W = 0;
12 $SIG{__WARN__} = sub {
13 if ($_[0] =~ /^Hides field '.*?' in base class/) {
14 $W++;
15 }
16 else {
17 warn $_[0];
18 }
19 };
20}
21
22use strict;
8731c5d9 23use Test::More tests => 26;
33d611b9
JH
24
25BEGIN { use_ok('base'); }
26
27package B1;
28use fields qw(b1 b2 b3);
29
30package B2;
31use fields '_b1';
32use fields qw(b1 _b2 b2);
33
34sub new { fields::new(shift) }
35
36package B3;
37use fields qw(b4 _b5 b6 _b7);
38
39package D1;
40use base 'B1';
41use fields qw(d1 d2 d3);
42
43package D2;
44use base 'B1';
45use fields qw(_d1 _d2);
46use fields qw(d1 d2);
47
48
49package D3;
50use base 'B2';
51use fields qw(b1 d1 _b1 _d1); # hide b1
52
53package D4;
54use base 'D3';
55use fields qw(_d3 d3);
56
57package M;
58sub m {}
59
60package D5;
61use base qw(M B2);
62
63# Test that multiple inheritance fails.
64package D6;
65eval { 'base'->import(qw(B2 M B3)); };
66::like($@, qr/can't multiply inherit %FIELDS/i,
67 'No multiple field inheritance');
68
69package Foo::Bar;
70use base 'B1';
71
72package Foo::Bar::Baz;
73use base 'Foo::Bar';
74use fields qw(foo bar baz);
75
76# Test repeatability for when modules get reloaded.
77package B1;
78use fields qw(b1 b2 b3);
79
80package D3;
81use base 'B2';
82use fields qw(b1 d1 _b1 _d1); # hide b1
83
84
864f8ab4
JH
85# Test that a package with only private fields gets inherited properly
86package B7;
87use fields qw(_b1);
88
89package D7;
90use base qw(B7);
91use fields qw(b1);
92
93
94# Test that an intermediate package with no fields doesn't cause a problem.
95package B8;
96use fields qw(_b1);
97
98package D8;
99use base qw(B8);
100
101package D8A;
102use base qw(D8);
103use fields qw(b1);
104
105
33d611b9
JH
106package main;
107
108my %EXPECT = (
109 B1 => [qw(b1 b2 b3)],
110 D1 => [qw(b1 b2 b3 d1 d2 d3)],
111 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)],
112
113 M => [qw()],
114 B2 => [qw(_b1 b1 _b2 b2)],
115 D3 => [(undef,undef,undef,
116 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden
117 D4 => [(undef,undef,undef,
118 qw(b2 b1 d1),undef,undef,qw(_d3 d3))],
864f8ab4 119
33d611b9
JH
120 D5 => [undef, 'b1', undef, 'b2'],
121
122 B3 => [qw(b4 _b5 b6 _b7)],
123
864f8ab4
JH
124 B7 => [qw(_b1)],
125 D7 => [undef, 'b1'],
126
127 B8 => [qw(_b1)],
128 D8 => [undef],
129 D8A => [undef, 'b1'],
130
33d611b9
JH
131 'Foo::Bar' => [qw(b1 b2 b3)],
132 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)],
133 );
134
135while(my($class, $efields) = each %EXPECT) {
136 no strict 'refs';
137 my %fields = %{$class.'::FIELDS'};
138 my %expected_fields;
139 foreach my $idx (1..@$efields) {
140 my $key = $efields->[$idx-1];
141 next unless $key;
142 $expected_fields{$key} = $idx;
143 }
144
145 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class");
146}
147
148# Did we get the appropriate amount of warnings?
149is( $W, 1, 'right warnings' );
150
151
152# A simple object creation and attribute access test
153my B2 $obj1 = D3->new;
154$obj1->{b1} = "B2";
155my D3 $obj2 = $obj1;
156$obj2->{b1} = "D3";
157
158# We should get compile time failures field name typos
159eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
160if( $Has_PH ) {
161 like $@,
162 qr/^No such pseudo-hash field "notthere" in variable \$obj3 of type D3/;
163}
164else {
165 like $@,
166 qr/^Attempt to access disallowed key 'notthere' in a restricted hash/;
167}
168
169# Slices
170@$obj1{"_b1", "b1"} = (17, 29);
171is( $obj1->{_b1}, 17 );
172is( $obj1->{b1}, 29 );
173
174@$obj1{'_b1', 'b1'} = (44,28);
175is( $obj1->{_b1}, 44 );
176is( $obj1->{b1}, 28 );
177
178
179
180# Break multiple inheritance with a field name clash.
181package E1;
182use fields qw(yo this _lah meep 42);
183
184package E2;
185use fields qw(_yo ahhh this);
186
187eval {
188 package Broken;
189
190 # The error must occur at run time for the eval to catch it.
191 require base;
192 'base'->import(qw(E1 E2));
193};
194::like( $@, qr/Can't multiply inherit %FIELDS/i, 'Again, no multi inherit' );
195
196
8731c5d9 197# Test that a package with no fields can inherit from a package with
198# fields, and that pseudohash messages don't show up
199
200package B9;
201use fields qw(b1);
202
203sub _mk_obj { fields::new($_[0])->{'b1'} };
204
205package D9;
206use base qw(B9);
207
208package main;
209
210{
211 my $w = 0;
212 local $SIG{__WARN__} = sub { $w++ };
213
214 B9->_mk_obj();
215 # used tp emit a warning that pseudohashes are deprecated, because
216 # %FIELDS wasn't blessed.
217 D9->_mk_obj();
218
219 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own");
220}