Skip to content

Commit 4315d7d

Browse files
authored
fix: Unordered arrays of objects with PRESENCE fix (#40)
1 parent 47f648f commit 4315d7d

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

array.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (a *Asserter) checkArrayUnordered(path string, act, exp []interface{}) {
4949
for i, expEl := range exp {
5050
found := false
5151
for _, actEl := range act {
52-
found = found || a.deepEqual(expEl, actEl)
52+
found = found || a.deepEqual(actEl, expEl)
5353
}
5454
if !found {
5555
serializedEl := serialize(expEl)
@@ -62,16 +62,6 @@ func (a *Asserter) checkArrayUnordered(path string, act, exp []interface{}) {
6262
}
6363
}
6464

65-
func (a *Asserter) deepEqual(act, exp interface{}) bool {
66-
// There's a non-zero chance that JSON serialization will *not* be
67-
// deterministic in the future like it is in v1.16.
68-
// However, until this is the case, I can't seem to find a test case that
69-
// makes this evaluation return a false positive.
70-
// The benefit is a lot of simplicity and considerable performance benefits
71-
// for large nested structures.
72-
return serialize(act) == serialize(exp)
73-
}
74-
7565
func (a *Asserter) checkArrayOrdered(path string, act, exp []interface{}) {
7666
a.tt.Helper()
7767
if len(act) != len(exp) {

helper.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ type noopHelperTT struct {
1212
func (*noopHelperTT) Helper() {
1313
// Intentional NOOP
1414
}
15+
16+
// deepEqualityPrinter is a utility Printer that lets the jsonassert package
17+
// verify equality internally without affecting the external facing output,
18+
// e.g. to verify where one potential candidate is matching or not.
19+
type deepEqualityPrinter struct{ count int }
20+
21+
func (p *deepEqualityPrinter) Errorf(msg string, args ...interface{}) { p.count++ }
22+
func (p *deepEqualityPrinter) Helper() { /* Intentional NOOP */ }
23+
24+
func (a *Asserter) deepEqual(act, exp interface{}) bool {
25+
p := &deepEqualityPrinter{count: 0}
26+
deepEqualityAsserter := &Asserter{tt: p}
27+
deepEqualityAsserter.pathassertf("", serialize(act), serialize(exp))
28+
return p.count == 0
29+
}

integration_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ but expected JSON was:
306306
`["<<UNORDERED>>", {"1": 1}, {"2": 2}, {"3": 3}, {"4": 4}, {"5": 5}, {"6": 6}, {"7": 7}, {"8": 8}, {"9": 9}, {"10": 10}, {"11": 11}, {"12": 12}, {"13": 13}, {"14": 14}, {"15": 15}, {"16": 16}, {"17": 17}, {"18": 18}, {"19": 19}, {"20": 20}]`,
307307
nil,
308308
},
309+
"unordered array with objects with PRESENCE directives": {
310+
// Regression test for https://github.com/kinbiko/jsonassert/issues/39
311+
`{ "data": [ { "foo": 1, "bar": 2 }, { "foo": 11, "bar": 22 } ] }`,
312+
`{ "data": [ "<<UNORDERED>>", { "foo": 11, "bar": "<<PRESENCE>>" }, { "foo": 1, "bar": "<<PRESENCE>>" } ] }`,
313+
nil,
314+
},
309315
} {
310316
tc := tc
311317
t.Run(name, func(t *testing.T) { tc.check(t) })

0 commit comments

Comments
 (0)