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