File: 14_named.t

package info (click to toggle)
libhash-fieldhash-perl 0.15-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 1,249; ansic: 207; makefile: 10
file content (106 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download | duplicates (7)
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
#!perl -w

use strict;
use Test::More tests => 24;

sub XXX{
	require Data::Dumper;
	diag(Data::Dumper::Dumper(@_));
}

BEGIN{
	package InsideOut;
	use Hash::FieldHash qw(:all);

	fieldhash my %foo, 'foo';
	fieldhash my %bar, 'bar';

	sub new{
		my $class = shift;
		my $obj = bless do{ \my $o }, $class;
		return from_hash($obj, @_);
	}

	sub registry{
		[\(%foo, %bar)];
	}
}
my $registry = InsideOut->registry();

is_deeply $registry, [{}, {}];

{
	my $x = InsideOut->new();
	my $y = InsideOut->new();

	isa_ok $x, 'InsideOut';
	isa_ok $y, 'InsideOut';

	$x->foo(42);
	is $x->foo, 42 or XXX(InsideOut->registry);
	is $x->bar, undef;
	is $y->foo, undef;
	is $y->bar, undef;

	$x->foo('x.foo');
	$x->bar('x.bar');
	$y->foo('y.foo');
	$y->bar('y.bar');
	is $x->foo, 'x.foo';
	is $x->bar, 'x.bar';
	is $y->foo, 'y.foo';
	is $y->bar, 'y.bar';

	is_deeply $x->to_hash, { foo => 'x.foo', bar => 'x.bar' };
	is_deeply $y->to_hash, { foo => 'y.foo', bar => 'y.bar' };
}

{
	my $x = InsideOut->new(foo => 42, bar => 52);
	my $y = InsideOut->new(foo => 10, bar => 20);

	is_deeply $x->to_hash, { foo => 42, bar => 52 } or XXX($x->dump);
	is_deeply $y->to_hash, { foo => 10, bar => 20 } or XXX($x->dump);
}


is_deeply $registry, [{}, {}];


eval{
	InsideOut->new([]);
};
like $@, qr/must be a HASH reference/;

eval{
	InsideOut->new(1, 2, 3);
};
like $@, qr/Odd number of parameters/;

eval{
	InsideOut->new(xxx => 42);
};
like $@, qr/No such field "xxx"/;

eval{
	InsideOut->new({xxx => 42});
};
like $@, qr/No such field "xxx"/;

eval{
	InsideOut->foo;
};
like $@, qr/The foo\(\) method must be called as an instance method/;

eval{
	Hash::FieldHash::from_hash([]);
};

ok $@;

eval{
	Hash::FieldHash::to_hash([]);
};
ok $@;

is_deeply $registry, [{}, {}];