Commit | Line | Data |
33d611b9 |
1 | #!/usr/bin/perl -w |
2 | |
e75d1f10 |
3 | my ($Has_PH, $Field); |
33d611b9 |
4 | BEGIN { |
5 | $Has_PH = $] < 5.009; |
e75d1f10 |
6 | $Field = $Has_PH ? "pseudo-hash field" : "class field"; |
33d611b9 |
7 | } |
8 | |
9 | my $W; |
10 | |
11 | BEGIN { |
12 | $W = 0; |
13 | $SIG{__WARN__} = sub { |
14 | if ($_[0] =~ /^Hides field '.*?' in base class/) { |
15 | $W++; |
16 | } |
17 | else { |
18 | warn $_[0]; |
19 | } |
20 | }; |
21 | } |
22 | |
23 | use strict; |
85be41dd |
24 | use Test::More tests => 29; |
33d611b9 |
25 | |
26 | BEGIN { use_ok('base'); } |
27 | |
28 | package B1; |
29 | use fields qw(b1 b2 b3); |
30 | |
31 | package B2; |
32 | use fields '_b1'; |
33 | use fields qw(b1 _b2 b2); |
34 | |
35 | sub new { fields::new(shift) } |
36 | |
37 | package B3; |
38 | use fields qw(b4 _b5 b6 _b7); |
39 | |
40 | package D1; |
41 | use base 'B1'; |
42 | use fields qw(d1 d2 d3); |
43 | |
44 | package D2; |
45 | use base 'B1'; |
46 | use fields qw(_d1 _d2); |
47 | use fields qw(d1 d2); |
48 | |
49 | |
50 | package D3; |
51 | use base 'B2'; |
52 | use fields qw(b1 d1 _b1 _d1); # hide b1 |
53 | |
54 | package D4; |
55 | use base 'D3'; |
56 | use fields qw(_d3 d3); |
57 | |
58 | package M; |
59 | sub m {} |
60 | |
61 | package D5; |
62 | use base qw(M B2); |
63 | |
64 | # Test that multiple inheritance fails. |
65 | package D6; |
66 | eval { 'base'->import(qw(B2 M B3)); }; |
9e998a43 |
67 | ::like($@, qr/can't multiply inherit fields/i, |
68 | 'No multiple field inheritance'); |
33d611b9 |
69 | |
70 | package Foo::Bar; |
71 | use base 'B1'; |
72 | |
73 | package Foo::Bar::Baz; |
74 | use base 'Foo::Bar'; |
75 | use fields qw(foo bar baz); |
76 | |
77 | # Test repeatability for when modules get reloaded. |
78 | package B1; |
79 | use fields qw(b1 b2 b3); |
80 | |
81 | package D3; |
82 | use base 'B2'; |
83 | use fields qw(b1 d1 _b1 _d1); # hide b1 |
84 | |
85 | |
864f8ab4 |
86 | # Test that a package with only private fields gets inherited properly |
87 | package B7; |
88 | use fields qw(_b1); |
89 | |
90 | package D7; |
91 | use base qw(B7); |
92 | use fields qw(b1); |
93 | |
94 | |
95 | # Test that an intermediate package with no fields doesn't cause a problem. |
96 | package B8; |
97 | use fields qw(_b1); |
98 | |
99 | package D8; |
100 | use base qw(B8); |
101 | |
102 | package D8A; |
103 | use base qw(D8); |
104 | use fields qw(b1); |
105 | |
106 | |
33d611b9 |
107 | package main; |
108 | |
109 | my %EXPECT = ( |
110 | B1 => [qw(b1 b2 b3)], |
111 | D1 => [qw(b1 b2 b3 d1 d2 d3)], |
112 | D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)], |
113 | |
114 | M => [qw()], |
115 | B2 => [qw(_b1 b1 _b2 b2)], |
116 | D3 => [(undef,undef,undef, |
117 | qw(b2 b1 d1 _b1 _d1))], # b1 is hidden |
118 | D4 => [(undef,undef,undef, |
119 | qw(b2 b1 d1),undef,undef,qw(_d3 d3))], |
864f8ab4 |
120 | |
33d611b9 |
121 | D5 => [undef, 'b1', undef, 'b2'], |
122 | |
123 | B3 => [qw(b4 _b5 b6 _b7)], |
124 | |
864f8ab4 |
125 | B7 => [qw(_b1)], |
126 | D7 => [undef, 'b1'], |
127 | |
128 | B8 => [qw(_b1)], |
129 | D8 => [undef], |
130 | D8A => [undef, 'b1'], |
131 | |
33d611b9 |
132 | 'Foo::Bar' => [qw(b1 b2 b3)], |
133 | 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)], |
134 | ); |
135 | |
136 | while(my($class, $efields) = each %EXPECT) { |
137 | no strict 'refs'; |
138 | my %fields = %{$class.'::FIELDS'}; |
139 | my %expected_fields; |
140 | foreach my $idx (1..@$efields) { |
141 | my $key = $efields->[$idx-1]; |
142 | next unless $key; |
143 | $expected_fields{$key} = $idx; |
144 | } |
145 | |
146 | ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class"); |
147 | } |
148 | |
149 | # Did we get the appropriate amount of warnings? |
150 | is( $W, 1, 'right warnings' ); |
151 | |
152 | |
153 | # A simple object creation and attribute access test |
154 | my B2 $obj1 = D3->new; |
155 | $obj1->{b1} = "B2"; |
156 | my D3 $obj2 = $obj1; |
157 | $obj2->{b1} = "D3"; |
158 | |
159 | # We should get compile time failures field name typos |
e75d1f10 |
160 | eval q(return; my D3 $obj3 = $obj2; $obj3->{notthere} = ""); |
161 | like $@, |
162 | qr/^No such $Field "notthere" in variable \$obj3 of type D3/, |
163 | "Compile failure of undeclared fields (helem)"; |
33d611b9 |
164 | |
c63d38c6 |
165 | SKIP: { |
166 | # Slices |
167 | # We should get compile time failures field name typos |
168 | skip "Doesn't work before 5.9", 2 if $] < 5.009; |
169 | eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ()); |
170 | like $@, |
171 | qr/^No such $Field "notthere" in variable \$obj3 of type D3/, |
172 | "Compile failure of undeclared fields (hslice)"; |
173 | eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ()); |
174 | like |
175 | $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/, |
176 | "Compile failure of undeclared fields (hslice (block form))"; |
177 | } |
e75d1f10 |
178 | |
33d611b9 |
179 | @$obj1{"_b1", "b1"} = (17, 29); |
180 | is( $obj1->{_b1}, 17 ); |
181 | is( $obj1->{b1}, 29 ); |
182 | |
183 | @$obj1{'_b1', 'b1'} = (44,28); |
184 | is( $obj1->{_b1}, 44 ); |
185 | is( $obj1->{b1}, 28 ); |
186 | |
187 | |
188 | |
189 | # Break multiple inheritance with a field name clash. |
190 | package E1; |
191 | use fields qw(yo this _lah meep 42); |
192 | |
193 | package E2; |
194 | use fields qw(_yo ahhh this); |
195 | |
196 | eval { |
197 | package Broken; |
198 | |
199 | # The error must occur at run time for the eval to catch it. |
200 | require base; |
201 | 'base'->import(qw(E1 E2)); |
202 | }; |
9e998a43 |
203 | ::like( $@, qr/Can't multiply inherit fields/i, 'Again, no multi inherit' ); |
33d611b9 |
204 | |
205 | |
8731c5d9 |
206 | # Test that a package with no fields can inherit from a package with |
207 | # fields, and that pseudohash messages don't show up |
208 | |
209 | package B9; |
210 | use fields qw(b1); |
211 | |
212 | sub _mk_obj { fields::new($_[0])->{'b1'} }; |
213 | |
214 | package D9; |
215 | use base qw(B9); |
216 | |
217 | package main; |
218 | |
219 | { |
220 | my $w = 0; |
221 | local $SIG{__WARN__} = sub { $w++ }; |
222 | |
223 | B9->_mk_obj(); |
224 | # used tp emit a warning that pseudohashes are deprecated, because |
225 | # %FIELDS wasn't blessed. |
226 | D9->_mk_obj(); |
227 | |
228 | is ($w, 0, "pseudohash warnings in derived class with no fields of it's own"); |
229 | } |
85be41dd |
230 | |
231 | # [perl #31078] an intermediate class with no additional fields caused |
232 | # hidden fields in base class to get stomped on |
233 | |
234 | { |
235 | package X; |
236 | use fields qw(X1 _X2); |
237 | sub new { |
238 | my X $self = shift; |
239 | $self = fields::new($self) unless ref $self; |
240 | $self->{X1} = "x1"; |
4946def6 |
241 | # FIXME. This code is dead on blead becase the test is skipped. |
242 | # The test states that it's being skipped becaues restricted hashes |
243 | # don't support a feature. Presumably we need to make that feature |
244 | # supported. Bah. |
245 | # use Devel::Peek; Dump($self); |
85be41dd |
246 | $self->{_X2} = "_x2"; |
247 | return $self; |
248 | } |
249 | sub get_X2 { my X $self = shift; $self->{_X2} } |
250 | |
251 | package Y; |
252 | use base qw(X); |
253 | |
254 | sub new { |
255 | my Y $self = shift; |
256 | $self = fields::new($self) unless ref $self; |
257 | $self->SUPER::new(); |
258 | return $self; |
259 | } |
260 | |
261 | |
262 | package Z; |
263 | use base qw(Y); |
264 | use fields qw(Z1); |
265 | |
266 | sub new { |
267 | my Z $self = shift; |
268 | $self = fields::new($self) unless ref $self; |
269 | $self->SUPER::new(); |
270 | $self->{Z1} = 'z1'; |
271 | return $self; |
272 | } |
273 | |
274 | package main; |
275 | |
276 | if ($Has_PH) { |
277 | my Z $c = Z->new(); |
278 | is($c->get_X2, '_x2', "empty intermediate class"); |
279 | } |
280 | else { |
281 | SKIP: { |
282 | skip "restricted hashes don't support private fields properly", 1; |
283 | } |
284 | } |
285 | } |