Debian Codegraph @

cpan/Test2-Suite/t/modules/Compare/Hash.t

raw
1 use Test2::Bundle::Extended -target => 'Test2::Compare::Hash';
2
3 use lib 't/lib';
4
5 subtest simple => sub {
6 my $one = $CLASS->new();
7 isa_ok($one, $CLASS, 'Test2::Compare::Base');
8
9 is($one->name, '<HASH>', "name is <HASH>");
10 };
11
12 subtest verify => sub {
13 my $one = $CLASS->new();
14
15 ok(!$one->verify(exists => 0), "nothing to verify");
16 ok(!$one->verify(exists => 1, got => undef), "undef is not a hashref");
17 ok(!$one->verify(exists => 1, got => 1), "1 is not a hashref");
18 ok(!$one->verify(exists => 1, got => []), "An arrayref is not a hashref");
19
20 ok($one->verify(exists => 1, got => {}), "got a hashref");
21 };
22
23 subtest init => sub {
24 my $one = $CLASS->new();
25 ok( defined $one, "args are not required");
26 is($one->items, {}, "got the items hash");
27 is($one->order, [], "got order array");
28
29 $one = $CLASS->new(inref => { a => 1, b => 2 });
30 is($one->items, {a => 1, b => 2}, "got the items hash");
31 is($one->order, ['a', 'b'], "generated order (ascii sort)");
32
33 $one = $CLASS->new(items => { a => 1, b => 2 }, order => [ 'b', 'a' ]);
34 is($one->items, {a => 1, b => 2}, "got the items hash");
35 is($one->order, ['b', 'a'], "got specified order");
36
37 $one = $CLASS->new(items => { a => 1, b => 2 });
38 is($one->items, {a => 1, b => 2}, "got the items hash");
39 is($one->order, ['a', 'b'], "generated order (ascii sort)");
40
41 like(
42 dies { $CLASS->new(inref => {a => 1}, items => {a => 1}) },
43 qr/Cannot specify both 'inref' and 'items'/,
44 "inref and items are exclusive"
45 );
46
47 like(
48 dies { $CLASS->new(inref => {a => 1}, order => ['a']) },
49 qr/Cannot specify both 'inref' and 'order'/,
50 "inref and order are exclusive"
51 );
52
53 like(
54 dies { $CLASS->new(items => { a => 1, b => 2, c => 3 }, order => ['a']) },
55 qr/Keys are missing from the 'order' array: b, c/,
56 "Missing fields in order"
57 );
58 };
59
60 subtest add_field => sub {
61 my $one = $CLASS->new();
62
63 $one->add_field(a => 1);
64 $one->add_field(c => 3);
65 $one->add_field(b => 2);
66
67 like(
68 dies { $one->add_field(undef, 'x') },
69 qr/field name is required/,
70 "Must specify a field name"
71 );
72
73 like(
74 dies { $one->add_field(a => 1) },
75 qr/field 'a' has already been specified/,
76 "Cannot add field twice"
77 );
78
79 is($one->items, { a => 1, b => 2, c => 3 }, "added items");
80 is($one->order, [ 'a', 'c', 'b' ], "order preserved");
81 };
82
83 subtest deltas => sub {
84 my $convert = Test2::Compare->can('strict_convert');
85
86 my %params = (exists => 1, convert => $convert, seen => {});
87
88 my $one = $CLASS->new(inref => {a => 1, b => 2, c => 3, x => DNE()});
89
90 is(
91 [$one->deltas(got => {a => 1, b => 2, c => 3}, %params)],
92 [],
93 "No deltas, perfect match"
94 );
95
96 is(
97 [$one->deltas(got => {a => 1, b => 2, c => 3, e => 4, f => 5}, %params)],
98 [],
99 "No deltas, extra items are ok"
100 );
101
102 $one->set_ending(1);
103 is(
104 [$one->deltas(got => {a => 1, b => 2, c => 3, e => 4, f => 5}, %params)],
105 [
106 {
107 dne => 'check',
108 verified => F(),
109 id => [HASH => 'e'],
110 got => 4,
111 chk => F(),
112 },
113 {
114 dne => 'check',
115 verified => F(),
116 id => [HASH => 'f'],
117 got => 5,
118 chk => F(),
119 },
120 ],
121 "Extra items are no longer ok, problem"
122 );
123
124 is(
125 [$one->deltas(got => {a => 1}, %params)],
126 [
127 {
128 children => [],
129 dne => 'got',
130 verified => F(),
131 id => [HASH => 'b'],
132 got => F(),
133 chk => T(),
134 },
135 {
136 children => [],
137 dne => 'got',
138 verified => F(),
139 id => [HASH => 'c'],
140 got => F(),
141 chk => T(),
142 },
143 ],
144 "Missing items"
145 );
146
147 is(
148 [$one->deltas(got => {a => 1, b => 1, c => 1}, %params)],
149 [
150 {
151 children => [],
152 verified => F(),
153 id => [HASH => 'b'],
154 got => 1,
155 chk => T(),
156 },
157 {
158 children => [],
159 verified => F(),
160 id => [HASH => 'c'],
161 got => 1,
162 chk => T(),
163 },
164 ],
165 "Items are wrong"
166 );
167
168 like(
169 [$one->deltas(got => {a => 1, b => 2, c => 3, x => 'oops'}, %params)],
170 [
171 {
172 verified => F(),
173 id => [HASH => 'x'],
174 got => 'oops',
175 check => DNE(),
176 },
177 ],
178 "Items are wrong"
179 );
180
181 };
182
183 subtest add_prop => sub {
184 my $one = $CLASS->new();
185
186 ok(!$one->meta, "no meta yet");
187 $one->add_prop('size' => 1);
188 isa_ok($one->meta, 'Test2::Compare::Meta');
189 is(@{$one->meta->items}, 1, "1 item");
190
191 $one->add_prop('reftype' => 'HASH');
192 is(@{$one->meta->items}, 2, "2 items");
193 };
194
195 {
196 package Foo::Hash;
197
198 use base 'MyTest::Target';
199
200 sub new {
201 my $class = shift;
202 bless { @_ } , $class;
203 }
204 }
205
206 subtest objects_with_hashes => sub {
207
208 my $o1 = Foo::Hash->new( b => { foo => 2 } ) ;
209 my $o2 = Foo::Hash->new( b => { foo => 2 } ) ;
210
211 is ( $o1, $o2, "same" );
212 };
213
214
215 done_testing;