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; |
e75d1f10 |
24 | use Test::More tests => 28; |
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)); }; |
67 | ::like($@, qr/can't multiply inherit %FIELDS/i, |
68 | 'No multiple field inheritance'); |
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 | |
165 | # Slices |
e75d1f10 |
166 | # We should get compile time failures field name typos |
167 | eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ()); |
168 | like $@, |
169 | qr/^No such $Field "notthere" in variable \$obj3 of type D3/, |
170 | "Compile failure of undeclared fields (hslice)"; |
171 | eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ()); |
172 | like |
173 | $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/, |
174 | "Compile failure of undeclared fields (hslice (block form))"; |
175 | |
33d611b9 |
176 | @$obj1{"_b1", "b1"} = (17, 29); |
177 | is( $obj1->{_b1}, 17 ); |
178 | is( $obj1->{b1}, 29 ); |
179 | |
180 | @$obj1{'_b1', 'b1'} = (44,28); |
181 | is( $obj1->{_b1}, 44 ); |
182 | is( $obj1->{b1}, 28 ); |
183 | |
184 | |
185 | |
186 | # Break multiple inheritance with a field name clash. |
187 | package E1; |
188 | use fields qw(yo this _lah meep 42); |
189 | |
190 | package E2; |
191 | use fields qw(_yo ahhh this); |
192 | |
193 | eval { |
194 | package Broken; |
195 | |
196 | # The error must occur at run time for the eval to catch it. |
197 | require base; |
198 | 'base'->import(qw(E1 E2)); |
199 | }; |
200 | ::like( $@, qr/Can't multiply inherit %FIELDS/i, 'Again, no multi inherit' ); |
201 | |
202 | |
8731c5d9 |
203 | # Test that a package with no fields can inherit from a package with |
204 | # fields, and that pseudohash messages don't show up |
205 | |
206 | package B9; |
207 | use fields qw(b1); |
208 | |
209 | sub _mk_obj { fields::new($_[0])->{'b1'} }; |
210 | |
211 | package D9; |
212 | use base qw(B9); |
213 | |
214 | package main; |
215 | |
216 | { |
217 | my $w = 0; |
218 | local $SIG{__WARN__} = sub { $w++ }; |
219 | |
220 | B9->_mk_obj(); |
221 | # used tp emit a warning that pseudohashes are deprecated, because |
222 | # %FIELDS wasn't blessed. |
223 | D9->_mk_obj(); |
224 | |
225 | is ($w, 0, "pseudohash warnings in derived class with no fields of it's own"); |
226 | } |