This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
VMS::Stdio test tweak
[perl5.git] / lib / fields.t
CommitLineData
f1192cee
GA
1#!./perl -w
2
f1192cee
GA
3my $w;
4
5BEGIN {
11162842 6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
f1192cee
GA
8 $SIG{__WARN__} = sub {
9 if ($_[0] =~ /^Hides field 'b1' in base class/) {
10 $w++;
11 return;
12 }
803b07a7 13 print STDERR $_[0];
f1192cee
GA
14 };
15}
16
b47ba5cf 17use strict;
9f1b1f2d 18use warnings;
b47ba5cf
GS
19use vars qw($DEBUG);
20
6d822dc4
MS
21use Test::More;
22
23
f1192cee
GA
24package B1;
25use fields qw(b1 b2 b3);
26
27package B2;
28use fields '_b1';
29use fields qw(b1 _b2 b2);
30
6d822dc4 31sub new { fields::new(shift); }
f1192cee
GA
32
33package D1;
34use base 'B1';
35use fields qw(d1 d2 d3);
36
37package D2;
38use base 'B1';
39use fields qw(_d1 _d2);
40use fields qw(d1 d2);
41
42package D3;
43use base 'B2';
44use fields qw(b1 d1 _b1 _d1); # hide b1
45
46package D4;
47use base 'D3';
48use fields qw(_d3 d3);
49
50package M;
51sub m {}
52
53package D5;
54use base qw(M B2);
55
56package Foo::Bar;
57use base 'B1';
58
59package Foo::Bar::Baz;
60use base 'Foo::Bar';
61use fields qw(foo bar baz);
62
f30a1143
JT
63# Test repeatability for when modules get reloaded.
64package B1;
65use fields qw(b1 b2 b3);
66
67package D3;
68use base 'B2';
69use fields qw(b1 d1 _b1 _d1); # hide b1
70
f1192cee
GA
71package main;
72
479ba383 73sub fstr {
f1192cee
GA
74 my $h = shift;
75 my @tmp;
76 for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) {
77 my $v = $h->{$k};
78 push(@tmp, "$k:$v");
79 }
80 my $str = join(",", @tmp);
81 print "$h => $str\n" if $DEBUG;
82 $str;
83}
84
85my %expect = (
86 B1 => "b1:1,b2:2,b3:3",
87 B2 => "_b1:1,b1:2,_b2:3,b2:4",
88 D1 => "b1:1,b2:2,b3:3,d1:4,d2:5,d3:6",
89 D2 => "b1:1,b2:2,b3:3,_d1:4,_d2:5,d1:6,d2:7",
90 D3 => "b2:4,b1:5,d1:6,_b1:7,_d1:8",
91 D4 => "b2:4,b1:5,d1:6,_d3:9,d3:10",
92 D5 => "b1:2,b2:4",
93 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6',
94);
95
6d822dc4 96plan tests => keys(%expect) + 17;
f1192cee
GA
97my $testno = 0;
98while (my($class, $exp) = each %expect) {
99 no strict 'refs';
100 my $fstr = fstr(\%{$class."::FIELDS"});
6d822dc4 101 is( $fstr, $exp, "\%FIELDS check for $class" );
f1192cee
GA
102}
103
104# Did we get the appropriate amount of warnings?
6d822dc4 105is( $w, 1 );
f1192cee
GA
106
107# A simple object creation and AVHV attribute access test
108my B2 $obj1 = D3->new;
109$obj1->{b1} = "B2";
110my D3 $obj2 = $obj1;
111$obj2->{b1} = "D3";
112
f1192cee
GA
113# We should get compile time failures field name typos
114eval q(my D3 $obj3 = $obj2; $obj3->{notthere} = "");
6d822dc4 115like $@, qr/^Attempt to access disallowed key 'notthere' in a restricted hash/;
f1192cee 116
345599ca
GS
117# Slices
118@$obj1{"_b1", "b1"} = (17, 29);
6d822dc4 119is_deeply($obj1, { b1 => 29, _b1 => 17 });
479ba383 120
6d822dc4
MS
121@$obj1{'_b1', 'b1'} = (44,28);
122is_deeply($obj1, { b1 => 28, _b1 => 44 });
479ba383 123
6d822dc4
MS
124eval { fields::phash };
125like $@, qr/^Pseudo-hashes have been removed from Perl/;
479ba383 126
f1192cee 127#fields::_dump();
377b21bb 128
479ba383 129# check if fields autovivify
377b21bb
GS
130{
131 package Foo;
132 use fields qw(foo bar);
6d822dc4 133 sub new { fields::new($_[0]) }
377b21bb
GS
134
135 package main;
136 my Foo $a = Foo->new();
6d822dc4
MS
137 $a->{foo} = ['a', 'ok', 'c'];
138 $a->{bar} = { A => 'ok' };
139 is( $a->{foo}[1], 'ok' );
140 is( $a->{bar}->{A},, 'ok' );
377b21bb 141}
479ba383
GS
142
143# check if fields autovivify
144{
145 package Bar;
146 use fields qw(foo bar);
147 sub new { return fields::new($_[0]) }
148
149 package main;
150 my Bar $a = Bar::->new();
6d822dc4
MS
151 $a->{foo} = ['a', 'ok', 'c'];
152 $a->{bar} = { A => 'ok' };
153 is( $a->{foo}[1], 'ok' );
154 is( $a->{bar}->{A}, 'ok' );
479ba383 155}
2bc5db75
MS
156
157
158# Test $VERSION bug
159package No::Version;
160
161use vars qw($Foo);
162sub VERSION { 42 }
163
164package Test::Version;
165
166use base qw(No::Version);
6d822dc4 167::like( $No::Version::VERSION, qr/set by base.pm/ );
e8f84f55
NIS
168
169# Test Inverse of $VERSION bug base.pm should not clobber existing $VERSION
170package Has::Version;
171
172BEGIN { $Has::Version::VERSION = '42' };
173
174package Test::Version2;
175
176use base qw(Has::Version);
6d822dc4 177::is( $Has::Version::VERSION, 42 );
b94834e7
A
178
179package main;
180
181our $eval1 = q{
182 {
183 package Eval1;
184 {
185 package Eval2;
186 use base 'Eval1';
187 $Eval2::VERSION = "1.02";
188 }
189 $Eval1::VERSION = "1.01";
190 }
191};
192
193eval $eval1;
6d822dc4 194is( $@, '' );
b94834e7 195
6d822dc4 196is( $Eval1::VERSION, 1.01 );
b94834e7 197
6d822dc4 198is( $Eval2::VERSION, 1.02 );
b94834e7
A
199
200
7678c486 201eval q{use base 'reallyReAlLyNotexists'};
6d822dc4
MS
202like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
203 'base with empty package');
b94834e7 204
7678c486 205eval q{use base 'reallyReAlLyNotexists'};
6d822dc4
MS
206like( $@, qr/^Base class package "reallyReAlLyNotexists" is empty./,
207 ' still empty on 2nd load');
b94834e7
A
208
209BEGIN { $Has::Version_0::VERSION = 0 }
210
211package Test::Version3;
212
213use base qw(Has::Version_0);
6d822dc4 214::is( $Has::Version_0::VERSION, 0, '$VERSION==0 preserved' );
e8f84f55 215