This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #24942] fields::inherit doesn't bless derived
[perl5.git] / lib / base / t / fields.t
CommitLineData
33d611b9 1#!/usr/bin/perl -w
f1192cee 2
33d611b9 3my $Has_PH;
f1192cee 4BEGIN {
33d611b9 5 $Has_PH = $] < 5.009;
f1192cee
GA
6}
7
b47ba5cf 8use strict;
33d611b9 9use Test::More tests => 16;
f1192cee 10
33d611b9 11BEGIN { use_ok('fields'); }
f1192cee 12
f1192cee 13
33d611b9 14package Foo;
f1192cee 15
33d611b9
JH
16use fields qw(_no Pants who _up_yours);
17use fields qw(what);
f1192cee 18
33d611b9
JH
19sub new { fields::new(shift) }
20sub magic_new { bless [] } # Doesn't 100% work, perl's problem.
f30a1143 21
f1192cee
GA
22package main;
23
33d611b9
JH
24is_deeply( [sort keys %Foo::FIELDS],
25 [sort qw(_no Pants who _up_yours what)]
f1192cee
GA
26);
27
33d611b9
JH
28sub show_fields {
29 my($base, $mask) = @_;
30 no strict 'refs';
31 my $fields = \%{$base.'::FIELDS'};
32 return grep { ($fields::attr{$base}[$fields->{$_}] & $mask) == $mask}
33 keys %$fields;
34}
0ea4badc 35
33d611b9
JH
36is_deeply( [sort &show_fields('Foo', fields::PUBLIC)],
37 [sort qw(Pants who what)]);
38is_deeply( [sort &show_fields('Foo', fields::PRIVATE)],
39 [sort qw(_no _up_yours)]);
0ea4badc 40
33d611b9
JH
41# We should get compile time failures field name typos
42eval q(my Foo $obj = Foo->new; $obj->{notthere} = "");
f1192cee 43
33d611b9
JH
44my $error = $Has_PH ? 'No such(?: [\w-]+)? field "notthere"'
45 : q[Attempt to access disallowed key 'notthere' in a ].
46 q[restricted hash at ];
47ok( $@ && $@ =~ /^$error/i );
f1192cee 48
f1192cee 49
33d611b9
JH
50foreach (Foo->new) {
51 my Foo $obj = $_;
52 my %test = ( Pants => 'Whatever', _no => 'Yeah',
53 what => 'Ahh', who => 'Moo',
54 _up_yours => 'Yip' );
f1192cee 55
33d611b9
JH
56 $obj->{Pants} = 'Whatever';
57 $obj->{_no} = 'Yeah';
58 @{$obj}{qw(what who _up_yours)} = ('Ahh', 'Moo', 'Yip');
479ba383 59
33d611b9
JH
60 while(my($k,$v) = each %test) {
61 ok($obj->{$k} eq $v);
62 }
63}
479ba383 64
33d611b9 65{
864f8ab4
JH
66 local $SIG{__WARN__} = sub {
67 return if $_[0] =~ /^Pseudo-hashes are deprecated/
68 };
33d611b9
JH
69 my $phash;
70 eval { $phash = fields::phash(name => "Joe", rank => "Captain") };
71 if( $Has_PH ) {
72 is( $phash->{rank}, "Captain" );
73 }
74 else {
75 like $@, qr/^Pseudo-hashes have been removed from Perl/;
76 }
77}
479ba383 78
377b21bb 79
479ba383 80# check if fields autovivify
377b21bb 81{
33d611b9 82 package Foo::Autoviv;
377b21bb 83 use fields qw(foo bar);
6d822dc4 84 sub new { fields::new($_[0]) }
377b21bb
GS
85
86 package main;
33d611b9 87 my Foo::Autoviv $a = Foo::Autoviv->new();
6d822dc4
MS
88 $a->{foo} = ['a', 'ok', 'c'];
89 $a->{bar} = { A => 'ok' };
90 is( $a->{foo}[1], 'ok' );
91 is( $a->{bar}->{A},, 'ok' );
377b21bb 92}
479ba383 93
0ea4badc
JH
94package Test::FooBar;
95
96use fields qw(a b c);
97
98sub new {
99 my $self = fields::new(shift);
100 %$self = @_ if @_;
101 $self;
102}
103
104package main;
105
106{
107 my $x = Test::FooBar->new( a => 1, b => 2);
108
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');
0ea4badc 112}