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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
use Test2::Bundle::Extended -target => 'Test2::Compare::Hash';
use lib 't/lib';
subtest simple => sub {
my $one = $CLASS->new();
isa_ok($one, $CLASS, 'Test2::Compare::Base');
is($one->name, '<HASH>', "name is <HASH>");
};
subtest verify => sub {
my $one = $CLASS->new();
ok(!$one->verify(exists => 0), "nothing to verify");
ok(!$one->verify(exists => 1, got => undef), "undef is not a hashref");
ok(!$one->verify(exists => 1, got => 1), "1 is not a hashref");
ok(!$one->verify(exists => 1, got => []), "An arrayref is not a hashref");
ok($one->verify(exists => 1, got => {}), "got a hashref");
};
subtest init => sub {
my $one = $CLASS->new();
ok( defined $one, "args are not required");
is($one->items, {}, "got the items hash");
is($one->order, [], "got order array");
$one = $CLASS->new(inref => { a => 1, b => 2 });
is($one->items, {a => 1, b => 2}, "got the items hash");
is($one->order, ['a', 'b'], "generated order (ascii sort)");
$one = $CLASS->new(items => { a => 1, b => 2 }, order => [ 'b', 'a' ]);
is($one->items, {a => 1, b => 2}, "got the items hash");
is($one->order, ['b', 'a'], "got specified order");
$one = $CLASS->new(items => { a => 1, b => 2 });
is($one->items, {a => 1, b => 2}, "got the items hash");
is($one->order, ['a', 'b'], "generated order (ascii sort)");
like(
dies { $CLASS->new(inref => {a => 1}, items => {a => 1}) },
qr/Cannot specify both 'inref' and 'items'/,
"inref and items are exclusive"
);
like(
dies { $CLASS->new(inref => {a => 1}, order => ['a']) },
qr/Cannot specify both 'inref' and 'order'/,
"inref and order are exclusive"
);
like(
dies { $CLASS->new(items => { a => 1, b => 2, c => 3 }, order => ['a']) },
qr/Keys are missing from the 'order' array: b, c/,
"Missing fields in order"
);
};
subtest add_field => sub {
my $one = $CLASS->new();
$one->add_field(a => 1);
$one->add_field(c => 3);
$one->add_field(b => 2);
like(
dies { $one->add_field(undef, 'x') },
qr/field name is required/,
"Must specify a field name"
);
like(
dies { $one->add_field(a => 1) },
qr/field 'a' has already been specified/,
"Cannot add field twice"
);
is($one->items, { a => 1, b => 2, c => 3 }, "added items");
is($one->order, [ 'a', 'c', 'b' ], "order preserved");
};
subtest deltas => sub {
my $convert = Test2::Compare->can('strict_convert');
my %params = (exists => 1, convert => $convert, seen => {});
my $one = $CLASS->new(inref => {a => 1, b => 2, c => 3, x => DNE()});
is(
[$one->deltas(got => {a => 1, b => 2, c => 3}, %params)],
[],
"No deltas, perfect match"
);
is(
[$one->deltas(got => {a => 1, b => 2, c => 3, e => 4, f => 5}, %params)],
[],
"No deltas, extra items are ok"
);
$one->set_ending(1);
is(
[$one->deltas(got => {a => 1, b => 2, c => 3, e => 4, f => 5}, %params)],
[
{
dne => 'check',
verified => F(),
id => [HASH => 'e'],
got => 4,
chk => F(),
},
{
dne => 'check',
verified => F(),
id => [HASH => 'f'],
got => 5,
chk => F(),
},
],
"Extra items are no longer ok, problem"
);
is(
[$one->deltas(got => {a => 1}, %params)],
[
{
children => [],
dne => 'got',
verified => F(),
id => [HASH => 'b'],
got => F(),
chk => T(),
},
{
children => [],
dne => 'got',
verified => F(),
id => [HASH => 'c'],
got => F(),
chk => T(),
},
],
"Missing items"
);
is(
[$one->deltas(got => {a => 1, b => 1, c => 1}, %params)],
[
{
children => [],
verified => F(),
id => [HASH => 'b'],
got => 1,
chk => T(),
},
{
children => [],
verified => F(),
id => [HASH => 'c'],
got => 1,
chk => T(),
},
],
"Items are wrong"
);
like(
[$one->deltas(got => {a => 1, b => 2, c => 3, x => 'oops'}, %params)],
[
{
verified => F(),
id => [HASH => 'x'],
got => 'oops',
check => DNE(),
},
],
"Items are wrong"
);
};
subtest add_prop => sub {
my $one = $CLASS->new();
ok(!$one->meta, "no meta yet");
$one->add_prop('size' => 1);
isa_ok($one->meta, 'Test2::Compare::Meta');
is(@{$one->meta->items}, 1, "1 item");
$one->add_prop('reftype' => 'HASH');
is(@{$one->meta->items}, 2, "2 items");
};
{
package Foo::Hash;
use base 'MyTest::Target';
sub new {
my $class = shift;
bless { @_ } , $class;
}
}
subtest objects_with_hashes => sub {
my $o1 = Foo::Hash->new( b => { foo => 2 } ) ;
my $o2 = Foo::Hash->new( b => { foo => 2 } ) ;
is ( $o1, $o2, "same" );
};
done_testing;
|