diff --git a/util/proto/marshal_test.go b/util/proto/marshal_test.go index e565b7c..d5758f2 100644 --- a/util/proto/marshal_test.go +++ b/util/proto/marshal_test.go @@ -8,6 +8,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test" generated "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test/custom" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/encoding/protojson" goproto "google.golang.org/protobuf/proto" ) @@ -51,33 +52,64 @@ func TestStableMarshalSingle(t *testing.T) { } for _, tc := range marshalCases { t.Run(tc.name, func(t *testing.T) { - r := tc.input.MarshalProtobuf(nil) - require.Equal(t, len(r), tc.input.StableSize()) - require.NotEmpty(t, r) + t.Run("proto", func(t *testing.T) { + r := tc.input.MarshalProtobuf(nil) + require.Equal(t, len(r), tc.input.StableSize()) + require.NotEmpty(t, r) - var actual test.Primitives - require.NoError(t, goproto.Unmarshal(r, &actual)) + var actual test.Primitives + require.NoError(t, goproto.Unmarshal(r, &actual)) - var actualFrostfs generated.Primitives - require.NoError(t, actualFrostfs.UnmarshalProtobuf(r)) - require.Equal(t, tc.input, &actualFrostfs) + var actualFrostfs generated.Primitives + require.NoError(t, actualFrostfs.UnmarshalProtobuf(r)) + require.Equal(t, tc.input, &actualFrostfs) - // Compare each field directly, because proto-generated code has private fields. - require.Equal(t, tc.input.FieldA, actual.FieldA) - require.Equal(t, tc.input.FieldB, actual.FieldB) - require.Equal(t, tc.input.FieldC, actual.FieldC) - require.Equal(t, tc.input.FieldD, actual.FieldD) - require.Equal(t, tc.input.FieldE, actual.FieldE) - require.Equal(t, tc.input.FieldF, actual.FieldF) - require.Equal(t, tc.input.FieldG, actual.FieldG) - require.Equal(t, tc.input.FieldI, actual.FieldI) - require.Equal(t, tc.input.FieldJ, actual.FieldJ) - require.Equal(t, tc.input.FieldK, actual.FieldK) - require.EqualValues(t, tc.input.FieldH, actual.FieldH) + primitivesEqual(t, tc.input, &actual) + }) + t.Run("json", func(t *testing.T) { + r, err := tc.input.MarshalJSON() + require.NoError(t, err) + require.NotEmpty(t, r) + + var actual test.Primitives + require.NoError(t, protojson.Unmarshal(r, &actual)) + + var actualFrostfs generated.Primitives + require.NoError(t, actualFrostfs.UnmarshalJSON(r)) + require.Equal(t, tc.input, &actualFrostfs) + + primitivesEqual(t, tc.input, &actual) + }) }) } } +func primitivesEqual(t *testing.T, a *generated.Primitives, b *test.Primitives) { + // Compare each field directly, because proto-generated code has private fields. + require.Equal(t, a.FieldA, b.FieldA) + require.Equal(t, a.FieldB, b.FieldB) + require.Equal(t, a.FieldC, b.FieldC) + require.Equal(t, a.FieldD, b.FieldD) + require.Equal(t, a.FieldE, b.FieldE) + require.Equal(t, a.FieldF, b.FieldF) + require.Equal(t, a.FieldG, b.FieldG) + require.Equal(t, a.FieldI, b.FieldI) + require.Equal(t, a.FieldJ, b.FieldJ) + require.Equal(t, a.FieldK, b.FieldK) + require.EqualValues(t, a.FieldH, b.FieldH) +} + +func repPrimitivesEqual(t *testing.T, a *generated.RepPrimitives, b *test.RepPrimitives) { + // Compare each field directly, because proto-generated code has private fields. + require.Equal(t, a.FieldA, b.FieldA) + require.Equal(t, a.FieldB, b.FieldB) + require.Equal(t, a.FieldC, b.FieldC) + require.Equal(t, a.FieldD, b.FieldD) + require.Equal(t, a.FieldE, b.FieldE) + require.Equal(t, a.FieldF, b.FieldF) + require.Equal(t, a.FieldFu, b.FieldFu) +} + func randIntSlice[T protoInt](n int, includeZero bool) []T { r := make([]T, n) if n == 0 { @@ -142,21 +174,24 @@ func TestStableMarshalRep(t *testing.T) { } for _, tc := range marshalCases { t.Run(tc.name, func(t *testing.T) { - r := tc.input.MarshalProtobuf(nil) - require.Equal(t, len(r), tc.input.StableSize()) - require.NotEmpty(t, r) + t.Run("proto", func(t *testing.T) { + r := tc.input.MarshalProtobuf(nil) + require.Equal(t, len(r), tc.input.StableSize()) + require.NotEmpty(t, r) - var actual test.RepPrimitives - require.NoError(t, goproto.Unmarshal(r, &actual)) + var actual test.RepPrimitives + require.NoError(t, goproto.Unmarshal(r, &actual)) + repPrimitivesEqual(t, tc.input, &actual) + }) + t.Run("json", func(t *testing.T) { + r, err := tc.input.MarshalJSON() + require.NoError(t, err) + require.NotEmpty(t, r) - // Compare each field directly, because proto-generated code has private fields. - require.Equal(t, tc.input.FieldA, actual.FieldA) - require.Equal(t, tc.input.FieldB, actual.FieldB) - require.Equal(t, tc.input.FieldC, actual.FieldC) - require.Equal(t, tc.input.FieldD, actual.FieldD) - require.Equal(t, tc.input.FieldE, actual.FieldE) - require.Equal(t, tc.input.FieldF, actual.FieldF) - require.Equal(t, tc.input.FieldFu, actual.FieldFu) + var actual test.RepPrimitives + require.NoError(t, protojson.Unmarshal(r, &actual)) + repPrimitivesEqual(t, tc.input, &actual) + }) }) } }