Skip to content

Commit 4515637

Browse files
committed
fix: Discover and document the contains/unordered tradeoff
1 parent 287b375 commit 4515637

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

array.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ func (a *Asserter) checkArrayOrdered(path string, act, exp []interface{}) {
8282
func (a *Asserter) checkContainsArray(path string, act, exp []interface{}) {
8383
a.tt.Helper()
8484

85-
var unordered bool
8685
if len(exp) > 0 && exp[0] == "<<UNORDERED>>" {
87-
unordered = true
8886
exp = exp[1:]
8987
}
9088

@@ -95,13 +93,7 @@ func (a *Asserter) checkContainsArray(path string, act, exp []interface{}) {
9593
return
9694
}
9795

98-
if unordered {
99-
a.checkContainsUnorderedArray(path, act, exp)
100-
return
101-
}
102-
for i := range exp {
103-
a.pathContainsf(fmt.Sprintf("%s[%d]", path, i), serialize(act[i]), serialize(exp[i]))
104-
}
96+
a.checkContainsUnorderedArray(path, act, exp)
10597
}
10698

10799
func (a *Asserter) checkContainsUnorderedArray(path string, act, exp []interface{}) {
@@ -113,9 +105,7 @@ func (a *Asserter) checkContainsUnorderedArray(path string, act, exp []interface
113105
ap := arrayPrinter{}
114106
serializedAct := serialize(act[j])
115107
New(&ap).pathContainsf("", serializedAct, serializedExp)
116-
if len(ap) == 0 {
117-
found = true
118-
}
108+
found = found || len(ap) == 0
119109
}
120110
if !found {
121111
mismatchedExpPaths[fmt.Sprintf("%s[%d]", path, i+1)] = serializedExp // + 1 because 0th element is "<<UNORDERED>>"

integration_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,19 +492,28 @@ not found in
492492
["alpha","beta","gamma"]`,
493493
}},
494494
"multi-element array contains itself": {`["alpha", "beta"]`, `["alpha", "beta"]`, nil},
495-
"multi-element array does not contain itself permuted": {`["alpha", "beta"]`, `["beta" ,"alpha"]`, []string{
496-
"expected string at '$[0]' to be 'beta' but was 'alpha'",
497-
"expected string at '$[1]' to be 'alpha' but was 'beta'",
498-
}},
495+
// NOTE: There's an important design decision to be made here.
496+
// Currently, in the case of "Containsf" there's an implicit "<<UNORDERED>>" (if it's explicitly written it will be ignored)
497+
// This is so that nested arrays don't have to repeatedly say "<<UNORDERED">> assuming the user just wants to check for the existence of some element of an array.
498+
// However, this makes jsonassert useless for cases where you want to partially assert that an ordered array exists.
499+
// Ideally this package should be able to support both nicely.
500+
"multi-element array does contain itself permuted": {`["alpha", "beta"]`, `["beta" ,"alpha"]`, []string{}},
499501
// Allow users to test against a subset of the payload without erroring out.
500502
// This is to avoid the frustraion and unintuitive solution of adding "<<UNORDERED>>" in order to "enable" subsetting,
501503
// which is really implied with the `contains` part of the API name.
502-
"multi-element array does contain its subset": {`["alpha", "beta"]`, `["alpha"]`, []string{}},
504+
"multi-element array does contain its subset": {`["alpha", "beta"]`, `["beta"]`, []string{}},
503505
"multi-element array does not contain its superset": {`["alpha", "beta"]`, `["alpha", "beta", "gamma"]`, []string{"length of expected array at '$' was longer (length 3) than the actual array (length 2)", `actual JSON at '$' was: ["alpha","beta"], but expected JSON to contain: ["alpha","beta","gamma"]`}},
504506
"expected and actual have different types": {`{"foo": "bar"}`, `null`, []string{"actual JSON (object) and expected JSON (null) were of different types at '$'"}},
505507
"expected any value but got null": {`{"foo": null}`, `{"foo": "<<PRESENCE>>"}`, []string{"expected the presence of any value at '$.foo', but was absent"}},
506508
"unordered multi-element array of different types contains subset": {`["alpha", 5, false, ["foo"], {"bar": "baz"}]`, `["<<UNORDERED>>", 5, "alpha", {"bar": "baz"}]`, nil},
507509
"object contains its subset": {`{"foo": "bar", "alpha": "omega"}`, `{"alpha": "omega"}`, nil},
510+
/*
511+
"array inside object": {
512+
`{ "arr": [ { "fork": { "start": "stop" }, "nested": ["really", "fast"] } ] }`,
513+
`{ "arr": [ "<<UNORDERED>>", { "fork": { "start": "stop" }, "nested": ["<<UNORDERED>>", "fast"] } ] }`,
514+
nil,
515+
},
516+
*/
508517
}
509518
for name, tc := range tt {
510519
tc := tc

0 commit comments

Comments
 (0)