Skip to content

feat: ObjectsMatch and JsonContentsMatch (unordered nested slices)#1928

Open
sonnemusk wants to merge 6 commits into
stretchr:masterfrom
sonnemusk:feat/elements-match-deep
Open

feat: ObjectsMatch and JsonContentsMatch (unordered nested slices)#1928
sonnemusk wants to merge 6 commits into
stretchr:masterfrom
sonnemusk:feat/elements-match-deep

Conversation

@sonnemusk

Copy link
Copy Markdown

Summary

Adds deep equality helpers that treat every slice/array as an unordered multiset (like ElementsMatch, but recursive into structs and maps). Addresses #806 and the JSON follow-up discussed on that issue.

type T struct{ Names []string }
assert.ObjectsMatch(t,
    T{Names: []string{"Joe", "Rick"}},
    T{Names: []string{"Rick", "Joe"}},
)

assert.JsonContentsMatch(t,
    `{"participants":["Joe","Rick"],"event":"Birthday party"}`,
    `{"event":"Birthday party","participants":["Rick","Joe"]}`,
)

Details

  • Structs: exported fields only, field-by-field with the same rules
  • Maps: key-wise, values compared with the same rules
  • []byte compared as ordered bytes (not as a bag of bytes)
  • Also available on assert.Assertions, require, and *f format variants

Fixes #806

Deep equality that treats slices/arrays as unordered multisets at every
level (structs and maps recurse). JsonContentsMatch applies the same
rule to decoded JSON. Addresses stretchr#806.
Place forward/require wrappers alphabetically so .ci.gogenerate.sh
diff check can pass (codegen panics on local Go 1.23).
Use space after // in doc examples so .ci.gogenerate.sh is clean.
Reset generated files and re-run codegen so ObjectsMatch and
JsonContentsMatch wrappers match .ci.gogenerate.sh exactly.
Keep upstream generated files intact and only append ObjectsMatch /
JsonContentsMatch wrappers so gofmt and go generate stay stable on CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compare structs with unsorted slices

1 participant