9 use Test::More tests => 16;
11 BEGIN { use_ok('fields'); }
16 use fields qw(_no Pants who _up_yours);
19 sub new { fields::new(shift) }
20 sub magic_new { bless [] } # Doesn't 100% work, perl's problem.
24 is_deeply( [sort keys %Foo::FIELDS],
25 [sort qw(_no Pants who _up_yours what)]
29 my($base, $mask) = @_;
31 my $fields = \%{$base.'::FIELDS'};
32 return grep { ($fields::attr{$base}[$fields->{$_}] & $mask) == $mask}
36 is_deeply( [sort &show_fields('Foo', fields::PUBLIC)],
37 [sort qw(Pants who what)]);
38 is_deeply( [sort &show_fields('Foo', fields::PRIVATE)],
39 [sort qw(_no _up_yours)]);
41 # We should get compile time failures field name typos
42 eval q(my Foo $obj = Foo->new; $obj->{notthere} = "");
44 my $error = $Has_PH ? 'No such(?: [\w-]+)? field "notthere"'
45 : q[Attempt to access disallowed key 'notthere' in a ].
46 q[restricted hash at ];
47 ok( $@ && $@ =~ /^$error/i );
52 my %test = ( Pants => 'Whatever', _no => 'Yeah',
53 what => 'Ahh', who => 'Moo',
56 $obj->{Pants} = 'Whatever';
58 @{$obj}{qw(what who _up_yours)} = ('Ahh', 'Moo', 'Yip');
60 while(my($k,$v) = each %test) {
66 local $SIG{__WARN__} = sub {
67 return if $_[0] =~ /^Pseudo-hashes are deprecated/
70 eval { $phash = fields::phash(name => "Joe", rank => "Captain") };
72 is( $phash->{rank}, "Captain" );
75 like $@, qr/^Pseudo-hashes have been removed from Perl/;
80 # check if fields autovivify
83 use fields qw(foo bar);
84 sub new { fields::new($_[0]) }
87 my Foo::Autoviv $a = Foo::Autoviv->new();
88 $a->{foo} = ['a', 'ok', 'c'];
89 $a->{bar} = { A => 'ok' };
90 is( $a->{foo}[1], 'ok' );
91 is( $a->{bar}->{A},, 'ok' );
99 my $self = fields::new(shift);
107 my $x = Test::FooBar->new( a => 1, b => 2);
109 is(ref $x, 'Test::FooBar', 'x is a Test::FooBar');
110 ok(exists $x->{a}, 'x has a');
111 ok(exists $x->{b}, 'x has b');