Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080)
* Changelog update * Rename codec.MarshalAny * move codec.MarshalInterface to codec.Marshaler * fix tests * Update amino_codec for compliance with MarshalerInterface * update tests and comments * add tests * change order of args in UnmarshalInterface to a canonical one * uplift MarshalInterface to take ProtoMessage as an argument * wip * add nil check * make tests working * tests cleanup * add support for *JSON methods * Update changelog * linter fixes * fix test types * update evidence genesis_test * adding test * review updates Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot]
parent
da6b7f7755
commit
b219c54c2d
+10
-95
@@ -24,102 +24,17 @@ func createTestInterfaceRegistry() types.InterfaceRegistry {
|
||||
return interfaceRegistry
|
||||
}
|
||||
|
||||
func TestProtoMarsharlInterface(t *testing.T) {
|
||||
cdc := codec.NewProtoCodec(createTestInterfaceRegistry())
|
||||
m := interfaceMarshaler{cdc.MarshalInterface, cdc.UnmarshalInterface}
|
||||
testInterfaceMarshaling(require.New(t), m, false)
|
||||
m = interfaceMarshaler{cdc.MarshalInterfaceJSON, cdc.UnmarshalInterfaceJSON}
|
||||
testInterfaceMarshaling(require.New(t), m, false)
|
||||
}
|
||||
|
||||
func TestProtoCodec(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
codec codec.Marshaler
|
||||
input codec.ProtoMarshaler
|
||||
recv codec.ProtoMarshaler
|
||||
marshalErr bool
|
||||
unmarshalErr bool
|
||||
}{
|
||||
{
|
||||
"valid encoding and decoding",
|
||||
codec.NewProtoCodec(createTestInterfaceRegistry()),
|
||||
&testdata.Dog{Name: "rufus"},
|
||||
&testdata.Dog{},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid decode type",
|
||||
codec.NewProtoCodec(createTestInterfaceRegistry()),
|
||||
&testdata.Dog{Name: "rufus"},
|
||||
&testdata.Cat{},
|
||||
false,
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
bz, err := tc.codec.MarshalBinaryBare(tc.input)
|
||||
|
||||
if tc.marshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustMarshalBinaryBare(tc.input) })
|
||||
} else {
|
||||
var bz2 []byte
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { bz2 = tc.codec.MustMarshalBinaryBare(tc.input) })
|
||||
require.Equal(t, bz, bz2)
|
||||
|
||||
err := tc.codec.UnmarshalBinaryBare(bz, tc.recv)
|
||||
if tc.unmarshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustUnmarshalBinaryBare(bz, tc.recv) })
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { tc.codec.MustUnmarshalBinaryBare(bz, tc.recv) })
|
||||
require.Equal(t, tc.input, tc.recv)
|
||||
}
|
||||
}
|
||||
|
||||
bz, err = tc.codec.MarshalBinaryLengthPrefixed(tc.input)
|
||||
if tc.marshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustMarshalBinaryLengthPrefixed(tc.input) })
|
||||
} else {
|
||||
var bz2 []byte
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { bz2 = tc.codec.MustMarshalBinaryLengthPrefixed(tc.input) })
|
||||
require.Equal(t, bz, bz2)
|
||||
|
||||
err := tc.codec.UnmarshalBinaryLengthPrefixed(bz, tc.recv)
|
||||
if tc.unmarshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustUnmarshalBinaryLengthPrefixed(bz, tc.recv) })
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { tc.codec.MustUnmarshalBinaryLengthPrefixed(bz, tc.recv) })
|
||||
require.Equal(t, tc.input, tc.recv)
|
||||
}
|
||||
}
|
||||
|
||||
bz, err = tc.codec.MarshalJSON(tc.input)
|
||||
if tc.marshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustMarshalJSON(tc.input) })
|
||||
} else {
|
||||
var bz2 []byte
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { bz2 = tc.codec.MustMarshalJSON(tc.input) })
|
||||
require.Equal(t, bz, bz2)
|
||||
|
||||
err := tc.codec.UnmarshalJSON(bz, tc.recv)
|
||||
if tc.unmarshalErr {
|
||||
require.Error(t, err)
|
||||
require.Panics(t, func() { tc.codec.MustUnmarshalJSON(bz, tc.recv) })
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotPanics(t, func() { tc.codec.MustUnmarshalJSON(bz, tc.recv) })
|
||||
require.Equal(t, tc.input, tc.recv)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
cdc := codec.NewProtoCodec(createTestInterfaceRegistry())
|
||||
testMarshaling(t, cdc)
|
||||
}
|
||||
|
||||
type lyingProtoMarshaler struct {
|
||||
|
||||
Reference in New Issue
Block a user