1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/perl -w
BEGIN {
if( $ENV{PERL_CORE} ) {
chdir 't' if -d 't';
@INC = qw(../lib);
}
}
my $Has_PH;
BEGIN {
$Has_PH = $] < 5.009;
}
use strict;
use Test::More tests => 16;
BEGIN { use_ok('fields'); }
package Foo;
use fields qw(_no Pants who _up_yours);
use fields qw(what);
sub new { fields::new(shift) }
sub magic_new { bless [] } # Doesn't 100% work, perl's problem.
package main;
is_deeply( [sort keys %Foo::FIELDS],
[sort qw(_no Pants who _up_yours what)]
);
sub show_fields {
my($base, $mask) = @_;
no strict 'refs';
my $fields = \%{$base.'::FIELDS'};
return grep { ($fields::attr{$base}[$fields->{$_}] & $mask) == $mask}
keys %$fields;
}
is_deeply( [sort &show_fields('Foo', fields::PUBLIC)],
[sort qw(Pants who what)]);
is_deeply( [sort &show_fields('Foo', fields::PRIVATE)],
[sort qw(_no _up_yours)]);
# We should get compile time failures field name typos
eval q(my Foo $obj = Foo->new; $obj->{notthere} = "");
like $@, qr/^No such .*field "notthere"/i;
foreach (Foo->new) {
my Foo $obj = $_;
my %test = ( Pants => 'Whatever', _no => 'Yeah',
what => 'Ahh', who => 'Moo',
_up_yours => 'Yip' );
$obj->{Pants} = 'Whatever';
$obj->{_no} = 'Yeah';
@{$obj}{qw(what who _up_yours)} = ('Ahh', 'Moo', 'Yip');
while(my($k,$v) = each %test) {
is($obj->{$k}, $v);
}
}
{
local $SIG{__WARN__} = sub {
return if $_[0] =~ /^Pseudo-hashes are deprecated/
};
my $phash;
eval { $phash = fields::phash(name => "Joe", rank => "Captain") };
if( $Has_PH ) {
is( $phash->{rank}, "Captain" );
}
else {
like $@, qr/^Pseudo-hashes have been removed from Perl/;
}
}
# check if fields autovivify
{
package Foo::Autoviv;
use fields qw(foo bar);
sub new { fields::new($_[0]) }
package main;
my Foo::Autoviv $a = Foo::Autoviv->new();
$a->{foo} = ['a', 'ok', 'c'];
$a->{bar} = { A => 'ok' };
is( $a->{foo}[1], 'ok' );
is( $a->{bar}->{A},, 'ok' );
}
package Test::FooBar;
use fields qw(a b c);
sub new {
my $self = fields::new(shift);
%$self = @_ if @_;
$self;
}
package main;
{
my $x = Test::FooBar->new( a => 1, b => 2);
is(ref $x, 'Test::FooBar', 'x is a Test::FooBar');
ok(exists $x->{a}, 'x has a');
ok(exists $x->{b}, 'x has b');
}
|