mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-12 14:28:33 +02:00
testutil: use cmp.Diff in protobuf json assertion (#5517)
This commit is contained in:
parent
a55c144ca1
commit
b86c9931b1
1 changed files with 11 additions and 3 deletions
|
@ -29,10 +29,18 @@ func AssertProtoEqual(t *testing.T, expected, actual any, msgAndArgs ...any) boo
|
||||||
|
|
||||||
// AssertProtoJSONEqual asserts that a protobuf message matches the given JSON. The protoMsg can also be a slice
|
// AssertProtoJSONEqual asserts that a protobuf message matches the given JSON. The protoMsg can also be a slice
|
||||||
// of protobuf messages.
|
// of protobuf messages.
|
||||||
func AssertProtoJSONEqual(t *testing.T, expected string, protoMsg any, msgAndArgs ...any) bool {
|
func AssertProtoJSONEqual(t *testing.T, expectedJSON string, protoMsg any, msgAndArgs ...any) bool {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
formattedJSON := formattedProtoJSON(protoMsg)
|
var expected any
|
||||||
return assert.Equal(t, reformatJSON(json.RawMessage(expected)), formattedJSON, msgAndArgs...)
|
err := json.Unmarshal([]byte(expectedJSON), &expected)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var proto any
|
||||||
|
err = json.Unmarshal([]byte(formattedProtoJSON(protoMsg)), &proto)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
diff := cmp.Diff(expected, proto)
|
||||||
|
return assert.Empty(t, diff, msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formattedProtoJSON(protoMsg any) string {
|
func formattedProtoJSON(protoMsg any) string {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue