This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: perl@21233 looks ok on VMS 7.3-1
[perl5.git] / lib / 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;
23use Test::More tests => 29;
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
85package main;
86
87my %EXPECT = (
88 B1 => [qw(b1 b2 b3)],
89 D1 => [qw(b1 b2 b3 d1 d2 d3)],
90 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)],
91
92 M => [qw()],
93 B2 => [qw(_b1 b1 _b2 b2)],
94 D3 => [(undef,undef,undef,
95 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden
96 D4 => [(undef,undef,undef,
97 qw(b2 b1 d1),undef,undef,qw(_d3 d3))],
98 D5 => [undef, 'b1', undef, 'b2'],
99
100 B3 => [qw(b4 _b5 b6 _b7)],
101
102 'Foo::Bar' => [qw(b1 b2 b3)],
103 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)],
104 );
105
106while(my($class, $efields) = each %EXPECT) {
107 no strict 'refs';
108 my %fields = %{$class.'::FIELDS'};
109 my %expected_fields;
110 foreach my $idx (1..@$efields) {
111 my $key = $efields->[$idx-1];
112 next unless $key;
113 $expected_fields{$key} = $idx;
114 }
115
116 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class");
117}
118
119# Did we get the appropriate amount of warnings?
120is( $W, 1, 'right warnings' );
121
122
123# A simple object creation and attribute access test
124my B2 $obj1 = D3->new;
125$obj1->{b1} = "B2";
126my D3 $obj2 = $obj1;
127$obj2->{b1} = "D3";
128
129# We should get compile time failures field name typos
130eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
131if( $Has_PH ) {
132 like $@,
133 qr/^No such pseudo-hash field "notthere" in variable \$obj3 of type D3/;
134}
135else {
136 like $@,
137 qr/^Attempt to access disallowed key 'notthere' in a restricted hash/;
138}
139
140# Slices
141@$obj1{"_b1", "b1"} = (17, 29);
142is( $obj1->{_b1}, 17 );
143is( $obj1->{b1}, 29 );
144
145@$obj1{'_b1', 'b1'} = (44,28);
146is( $obj1->{_b1}, 44 );
147is( $obj1->{b1}, 28 );
148
149
150
151# Break multiple inheritance with a field name clash.
152package E1;
153use fields qw(yo this _lah meep 42);
154
155package E2;
156use fields qw(_yo ahhh this);
157
158eval {
159 package Broken;
160
161 # The error must occur at run time for the eval to catch it.
162 require base;
163 'base'->import(qw(E1 E2));
164};
165::like( $@, qr/Can't multiply inherit %FIELDS/i, 'Again, no multi inherit' );
166
167
168package No::Version;
169
170use vars qw($Foo);
171sub VERSION { 42 }
172
173package Test::Version;
174
175use base qw(No::Version);
176::ok( $No::Version::VERSION =~ /set by base\.pm/, '$VERSION bug' );
177
178# Test Inverse of $VERSION bug base.pm should not clobber existing $VERSION
179package Has::Version;
180
181BEGIN { $Has::Version::VERSION = '42' };
182
183package Test::Version2;
184
185use base qw(Has::Version);
186::is( $Has::Version::VERSION, 42 );
187
188package main;
189
190our $eval1 = q{
191 {
192 package Eval1;
193 {
194 package Eval2;
195 use base 'Eval1';
196 $Eval2::VERSION = "1.02";
197 }
198 $Eval1::VERSION = "1.01";
199 }
200};
201
202eval $eval1;
203is( $@, '' );
204
205is( $Eval1::VERSION, 1.01 );
206
207is( $Eval2::VERSION, 1.02 );
208
209
210eval q{use base 'reallyReAlLyNotexists'};
211like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
212 'base with empty package');
213
214eval q{use base 'reallyReAlLyNotexists'};
215like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
216 ' still empty on 2nd load');
217
218BEGIN { $Has::Version_0::VERSION = 0 }
219
220package Test::Version3;
221
222use base qw(Has::Version_0);
223::is( $Has::Version_0::VERSION, 0, '$VERSION==0 preserved' );
224
225
226package Test::SIGDIE;
227
228{
229 local $SIG{__DIE__} = sub {
230 ::fail('sigdie not caught, this test should not run')
231 };
232 eval {
233 'base'->import(qw(Huh::Boo));
234 };
235
236 ::like($@, qr/^Base class package "Huh::Boo" is empty/,
237 'Base class empty error message');
238
239}