Skip to content

Commit

Permalink
fix: Unordered arrays of objects with PRESENCE fix (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinbiko authored Aug 18, 2022
1 parent 47f648f commit 4315d7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
12 changes: 1 addition & 11 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *Asserter) checkArrayUnordered(path string, act, exp []interface{}) {
for i, expEl := range exp {
found := false
for _, actEl := range act {
found = found || a.deepEqual(expEl, actEl)
found = found || a.deepEqual(actEl, expEl)
}
if !found {
serializedEl := serialize(expEl)
Expand All @@ -62,16 +62,6 @@ func (a *Asserter) checkArrayUnordered(path string, act, exp []interface{}) {
}
}

func (a *Asserter) deepEqual(act, exp interface{}) bool {
// There's a non-zero chance that JSON serialization will *not* be
// deterministic in the future like it is in v1.16.
// However, until this is the case, I can't seem to find a test case that
// makes this evaluation return a false positive.
// The benefit is a lot of simplicity and considerable performance benefits
// for large nested structures.
return serialize(act) == serialize(exp)
}

func (a *Asserter) checkArrayOrdered(path string, act, exp []interface{}) {
a.tt.Helper()
if len(act) != len(exp) {
Expand Down
15 changes: 15 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ type noopHelperTT struct {
func (*noopHelperTT) Helper() {
// Intentional NOOP
}

// deepEqualityPrinter is a utility Printer that lets the jsonassert package
// verify equality internally without affecting the external facing output,
// e.g. to verify where one potential candidate is matching or not.
type deepEqualityPrinter struct{ count int }

func (p *deepEqualityPrinter) Errorf(msg string, args ...interface{}) { p.count++ }
func (p *deepEqualityPrinter) Helper() { /* Intentional NOOP */ }

func (a *Asserter) deepEqual(act, exp interface{}) bool {
p := &deepEqualityPrinter{count: 0}
deepEqualityAsserter := &Asserter{tt: p}
deepEqualityAsserter.pathassertf("", serialize(act), serialize(exp))
return p.count == 0
}
6 changes: 6 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ but expected JSON was:
`["<<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}]`,
nil,
},
"unordered array with objects with PRESENCE directives": {
// Regression test for https://github.com/kinbiko/jsonassert/issues/39
`{ "data": [ { "foo": 1, "bar": 2 }, { "foo": 11, "bar": 22 } ] }`,
`{ "data": [ "<<UNORDERED>>", { "foo": 11, "bar": "<<PRESENCE>>" }, { "foo": 1, "bar": "<<PRESENCE>>" } ] }`,
nil,
},
} {
tc := tc
t.Run(name, func(t *testing.T) { tc.check(t) })
Expand Down

0 comments on commit 4315d7d

Please sign in to comment.