testutil: use cmp.Diff in protobuf json assertion (#5517)

This commit is contained in:
Denis Mishin 2025-03-07 20:20:27 -05:00 committed by GitHub
parent a55c144ca1
commit b86c9931b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
// 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()
formattedJSON := formattedProtoJSON(protoMsg)
return assert.Equal(t, reformatJSON(json.RawMessage(expected)), formattedJSON, msgAndArgs...)
var expected any
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 {