Commit | Line | Data |
33d611b9 |
1 | #!/usr/bin/perl -w |
2 | |
3 | my $Has_PH; |
4 | BEGIN { |
5 | $Has_PH = $] < 5.009; |
6 | } |
7 | |
8 | my $W; |
9 | |
10 | BEGIN { |
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 | |
22 | use strict; |
8731c5d9 |
23 | use Test::More tests => 26; |
33d611b9 |
24 | |
25 | BEGIN { use_ok('base'); } |
26 | |
27 | package B1; |
28 | use fields qw(b1 b2 b3); |
29 | |
30 | package B2; |
31 | use fields '_b1'; |
32 | use fields qw(b1 _b2 b2); |
33 | |
34 | sub new { fields::new(shift) } |
35 | |
36 | package B3; |
37 | use fields qw(b4 _b5 b6 _b7); |
38 | |
39 | package D1; |
40 | use base 'B1'; |
41 | use fields qw(d1 d2 d3); |
42 | |
43 | package D2; |
44 | use base 'B1'; |
45 | use fields qw(_d1 _d2); |
46 | use fields qw(d1 d2); |
47 | |
48 | |
49 | package D3; |
50 | use base 'B2'; |
51 | use fields qw(b1 d1 _b1 _d1); # hide b1 |
52 | |
53 | package D4; |
54 | use base 'D3'; |
55 | use fields qw(_d3 d3); |
56 | |
57 | package M; |
58 | sub m {} |
59 | |
60 | package D5; |
61 | use base qw(M B2); |
62 | |
63 | # Test that multiple inheritance fails. |
64 | package D6; |
65 | eval { 'base'->import(qw(B2 M B3)); }; |
66 | ::like($@, qr/can't multiply inherit %FIELDS/i, |
67 | 'No multiple field inheritance'); |
68 | |
69 | package Foo::Bar; |
70 | use base 'B1'; |
71 | |
72 | package Foo::Bar::Baz; |
73 | use base 'Foo::Bar'; |
74 | use fields qw(foo bar baz); |
75 | |
76 | # Test repeatability for when modules get reloaded. |
77 | package B1; |
78 | use fields qw(b1 b2 b3); |
79 | |
80 | package D3; |
81 | use base 'B2'; |
82 | use fields qw(b1 d1 _b1 _d1); # hide b1 |
83 | |
84 | |
864f8ab4 |
85 | # Test that a package with only private fields gets inherited properly |
86 | package B7; |
87 | use fields qw(_b1); |
88 | |
89 | package D7; |
90 | use base qw(B7); |
91 | use fields qw(b1); |
92 | |
93 | |
94 | # Test that an intermediate package with no fields doesn't cause a problem. |
95 | package B8; |
96 | use fields qw(_b1); |
97 | |
98 | package D8; |
99 | use base qw(B8); |
100 | |
101 | package D8A; |
102 | use base qw(D8); |
103 | use fields qw(b1); |
104 | |
105 | |
33d611b9 |
106 | package main; |
107 | |
108 | my %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 |
120 | D5 => [undef, 'b1', undef, 'b2'], |
121 | |
122 | B3 => [qw(b4 _b5 b6 _b7)], |
123 | |
864f8ab4 |
124 | B7 => [qw(_b1)], |
125 | D7 => [undef, 'b1'], |
126 | |
127 | B8 => [qw(_b1)], |
128 | D8 => [undef], |
129 | D8A => [undef, 'b1'], |
130 | |
33d611b9 |
131 | 'Foo::Bar' => [qw(b1 b2 b3)], |
132 | 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)], |
133 | ); |
134 | |
135 | while(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? |
149 | is( $W, 1, 'right warnings' ); |
150 | |
151 | |
152 | # A simple object creation and attribute access test |
153 | my B2 $obj1 = D3->new; |
154 | $obj1->{b1} = "B2"; |
155 | my D3 $obj2 = $obj1; |
156 | $obj2->{b1} = "D3"; |
157 | |
158 | # We should get compile time failures field name typos |
159 | eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = ""); |
160 | if( $Has_PH ) { |
161 | like $@, |
162 | qr/^No such pseudo-hash field "notthere" in variable \$obj3 of type D3/; |
163 | } |
164 | else { |
165 | like $@, |
166 | qr/^Attempt to access disallowed key 'notthere' in a restricted hash/; |
167 | } |
168 | |
169 | # Slices |
170 | @$obj1{"_b1", "b1"} = (17, 29); |
171 | is( $obj1->{_b1}, 17 ); |
172 | is( $obj1->{b1}, 29 ); |
173 | |
174 | @$obj1{'_b1', 'b1'} = (44,28); |
175 | is( $obj1->{_b1}, 44 ); |
176 | is( $obj1->{b1}, 28 ); |
177 | |
178 | |
179 | |
180 | # Break multiple inheritance with a field name clash. |
181 | package E1; |
182 | use fields qw(yo this _lah meep 42); |
183 | |
184 | package E2; |
185 | use fields qw(_yo ahhh this); |
186 | |
187 | eval { |
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 | |
200 | package B9; |
201 | use fields qw(b1); |
202 | |
203 | sub _mk_obj { fields::new($_[0])->{'b1'} }; |
204 | |
205 | package D9; |
206 | use base qw(B9); |
207 | |
208 | package 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 | } |