Debian Codegraph @

t/equality.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use utf8;
14 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
15
16 use Scalar::Util qw(dualvar isdual);
17 use lib 't/lib';
18 use Helper;
19 use JSON::Schema::Modern::Utilities qw(is_type get_type is_equal);
20
21 subtest 'equality, using inflated data' => sub {
22 foreach my $test (
23 [ undef, undef, true ],
24 [ undef, false, false, '', 'wrong type: null vs boolean' ],
25 [ undef, true , false, '', 'wrong type: null vs boolean' ],
26 [ undef, 1, false, '', 'wrong type: null vs integer' ],
27 [ undef, '1', false, '', 'wrong type: null vs string' ],
28 [ [qw(a b c)], [qw(a b c)], true ],
29 [ [qw(a b c)], [qw(a b)], false, '', 'element count differs: 3 vs 2' ],
30 [ [qw(a b)], [qw(b a)], false, '/0', 'strings not equal' ],
31 [ 1, 1, true ],
32 [ 1, 1.0, true ],
33 [ 1, '1.0', false, '', 'wrong type: integer vs string' ],
34 [ '1.1', 1.1, false, '', 'wrong type: string vs number' ],
35 [ '1', 1, false, '', 'wrong type: string vs integer' ],
36 [ '1.1', 1.1, false, '', 'wrong type: string vs number' ],
37 [ [1,2], [2,1], false, '/0', 'integers not equal' ],
38 [ { a => 1, b => 2 }, { b => 2, a => 1 }, true ],
39 [ { a => 1 }, { a => 1.0 }, true ],
40 [ [qw(école ಠ_ಠ)], ["\x{e9}cole", "\x{0ca0}_\x{0ca0}"], true ],
41 [ { a => 1, b => 2 }, { a => 1, b => 3 }, false, '/b', 'integers not equal' ],
42 [ { a => { b => 1, c => 2 }, d => { e => 3, f => 4 } },
43 { a => { b => 1, c => 2 }, d => { e => 3, f => 5 } }, false, '/d/f', 'integers not equal' ],
44 [ [ { a => 1 } ], [ { a => 1, b => 2 } ], false, '/0', 'property count differs: 1 vs 2' ],
45 [ [ { a => 1 } ], [ { b => 2 } ], false, '/0', 'property names differ starting at position 0 ("a" vs "b")' ],
46 [ { foo => [ [ 0 ] ] }, { foo => [ [ 0, 1 ] ] }, false, '/foo/0', 'element count differs: 1 vs 2' ],
47 ) {
48 my ($x, $y, $expected, $diff_path, $error) = @$test;
49 my @types = map get_type($_), $x, $y;
50 my $result = is_equal($x, $y, my $state = {});
51
52 ok(!($result xor $expected), json_sprintf('%s == %s is %s', $x, $y, $expected));
53 is($state->{path}, $diff_path // '', 'two instances differ at the expected place') if not $expected;
54 is($state->{error}, $error // '', 'error is correct') if not $expected;
55 is($state->{error}, undef, 'error is undefined') if $expected;
56 isnt($state->{error}, 'uh oh', 'no unexpected error encountered');
57
58 ok(is_type($types[0], $x), 'type of arg 0 was not mutated while making equality check');
59 ok(is_type($types[1], $y), 'type of arg 1 was not mutated while making equality check');
60
61 foreach my $idx (0, 1) {
62 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & B::SVf_POK), "arg $idx did not gain a POK")
63 if $types[$idx] eq 'integer' or $types[$idx] eq 'number';
64
65 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & (B::SVf_IOK | B::SVf_NOK)), "arg $idx did not gain an NOK or IOK")
66 if $types[$idx] eq 'string';
67 }
68
69 note '';
70 }
71 };
72
73 my $decoder = JSON::Schema::Modern::_JSON_BACKEND()->new->allow_nonref(1)->utf8(0);
74
75 subtest 'equality, using JSON strings' => sub {
76 foreach my $test (
77 [ 'null', 'null', true ],
78 [ 'null', 1, false ],
79 [ '["a","b","c"]', '["a","b","c"]', true ],
80 [ '["a","b","c"]', '["a","b"]', false ],
81 [ '["a","b"]', '["b","a"]', false, '/0' ],
82 [ '1', '1', true ],
83 [ '1', '1.0', true ],
84 [ '10', '1e1', true ],
85 [ '[1,2]', '[2,1]', false, '/0' ],
86 [ '{"a":1,"b":2}', '{"a":1,"b":2}', true ],
87 [ '{"a":1}', '{"a":1.0}', true ],
88 [ '["école","ಠ_ಠ"]', qq{["\x{e9}cole", "\x{0ca0}_\x{0ca0}"]}, true ],
89 [ '{"a":1,"b":2}', '{"b":3,"a":1}', false, '/b' ],
90 [ '{"a":{"b":1,"c":2},"d":{"e":3,"f":4}}',
91 '{"a":{"b":1,"c":2},"d":{"e":3,"f":5}}', false, '/d/f' ],
92 ) {
93 my ($x, $y, $expected, $diff_path) = @$test;
94 ($x, $y) = map $decoder->decode($_), $x, $y;
95
96 my @types = map get_type($_), $x, $y;
97 my $result = is_equal($x, $y, my $state = {});
98 ok(!($result xor $expected), json_sprintf('%s == %s is %s', $x, $y, $expected));
99 is($state->{path}, $diff_path // '', 'two instances differ at the expected place') if not $expected;
100 isnt($state->{error}, 'uh oh', 'no unexpected error encountered');
101
102 ok(is_type($types[0], $x), 'type of arg 0 was not mutated while making equality check');
103 ok(is_type($types[1], $y), 'type of arg 1 was not mutated while making equality check');
104
105 foreach my $idx (0, 1) {
106 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & B::SVf_POK), "arg $idx did not gain a POK")
107 if $types[$idx] eq 'integer' or $types[$idx] eq 'number';
108
109 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & (B::SVf_IOK | B::SVf_NOK)), "arg $idx did not gain an NOK or IOK")
110 if $types[$idx] eq 'string';
111 }
112
113 note '';
114 }
115 };
116
117 subtest 'equality, using scalarref_booleans' => sub {
118 foreach my $test (
119 [ \0, true, false ],
120 [ \1, true, true ],
121 [ \0, false, true ],
122 [ \1, false, false ],
123 [ undef, \0, false ],
124 [ undef, false, false ],
125 ) {
126 my ($x, $y, $expected, $diff_path) = @$test;
127 my @types = map get_type($_), $x, $y;
128 my $result = is_equal($x, $y, my $state = { scalarref_booleans => 1});
129
130 ok(!($result xor $expected), json_sprintf('%s == %s is %s', $x, $y, $expected));
131 is($state->{path}, $diff_path // '', 'two instances differ at the expected place') if not $expected;
132 isnt($state->{error}, 'uh oh', 'no unexpected error encountered');
133
134 ok(is_type($types[0], $x), 'type of arg 0 was not mutated while making equality check');
135 ok(is_type($types[1], $y), 'type of arg 1 was not mutated while making equality check');
136
137 foreach my $idx (0, 1) {
138 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & B::SVf_POK), "arg $idx did not gain a POK")
139 if $types[$idx] eq 'integer' or $types[$idx] eq 'number';
140
141 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & (B::SVf_IOK | B::SVf_NOK)), "arg $idx did not gain an NOK or IOK")
142 if $types[$idx] eq 'string';
143 }
144
145 note '';
146 }
147 };
148
149 subtest 'equality, using stringy_numbers' => sub {
150 foreach my $test (
151 [ 1, 1, true ],
152 [ 1, 1.0, true ],
153 [ 1, '1.0', true ],
154 [ '1.1', 1.1, true ],
155 [ '1', 1, true ],
156 [ '1.1', 1.1, true ],
157 [ '1', '1.00', true ],
158 [ '1.10', '1.1000', true ],
159 [ 'x', 'x', true ],
160 [ 'x', 'y', false ],
161 [ 'x', 0, false ],
162 [ 0, 'y', false ],
163 [ '5', dualvar(5, '5'), true ],
164 [ 5, dualvar(5, '5'), true ],
165 [ '5', dualvar(5, 'five'), false ],
166 [ 5, dualvar(5, 'five'), false ],
167 [ dualvar(5, 'five'), dualvar(5, 'five'), false ],
168 ) {
169 my ($x, $y, $expected, $diff_path) = @$test;
170 my @types = map get_type($_), $x, $y;
171 my $result = is_equal($x, $y, my $state = { stringy_numbers => 1 });
172
173 ok(!($result xor $expected), json_sprintf('%s == %s is %s', $x, $y, $expected));
174 is($state->{path}, $diff_path // '', 'two instances differ at the expected place') if not $expected;
175
176 isnt($state->{error}, 'uh oh', 'no unexpected error encountered');
177 is(get_type($x), $types[0], 'type of arg 0 was not mutated while making equality check (get_type returns '.$types[0].')');
178 is(get_type($y), $types[1], 'type of arg 1 was not mutated while making equality check (get_type returns '.$types[1].')');
179
180 ok(
181 is_type($types[0], $x),
182 "type of arg 0 was not mutated while making equality check (is_type('$types[0]') returns true)",
183 ) if $types[0] ne 'ambiguous type';
184 ok(
185 is_type($types[1], $y),
186 "type of arg 1 was not mutated while making equality check (is_type('$types[1]') returns true)",
187 ) if $types[1] ne 'ambiguous type';
188
189 foreach my $idx (0, 1) {
190 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & B::SVf_POK), "arg $idx did not gain a POK")
191 if $types[$idx] eq 'integer' or $types[$idx] eq 'number';
192
193 ok(!(B::svref_2object(\[$x, $y]->[$idx])->FLAGS & (B::SVf_IOK | B::SVf_NOK)), "arg $idx did not gain an NOK or IOK")
194 if not ($idx == 1 and isdual($y) and $types[1] ne 'ambiguous type') and $types[$idx] eq 'string';
195 }
196
197 note '';
198 }
199 };
200
201 done_testing;