* 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>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package codec_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
)
|
|
|
|
func NewTestInterfaceRegistry() types.InterfaceRegistry {
|
|
registry := types.NewInterfaceRegistry()
|
|
registry.RegisterInterface("Animal", (*testdata.Animal)(nil))
|
|
registry.RegisterImplementations(
|
|
(*testdata.Animal)(nil),
|
|
&testdata.Dog{},
|
|
&testdata.Cat{},
|
|
)
|
|
return registry
|
|
}
|
|
|
|
func TestMarshalAny(t *testing.T) {
|
|
registry := types.NewInterfaceRegistry()
|
|
|
|
cdc := codec.NewProtoCodec(registry)
|
|
|
|
kitty := &testdata.Cat{Moniker: "Kitty"}
|
|
bz, err := cdc.MarshalInterface(kitty)
|
|
require.NoError(t, err)
|
|
|
|
var animal testdata.Animal
|
|
|
|
// empty registry should fail
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.Error(t, err)
|
|
|
|
// wrong type registration should fail
|
|
registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{})
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.Error(t, err)
|
|
|
|
// should pass
|
|
registry = NewTestInterfaceRegistry()
|
|
cdc = codec.NewProtoCodec(registry)
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.NoError(t, err)
|
|
require.Equal(t, kitty, animal)
|
|
|
|
// nil should fail
|
|
registry = NewTestInterfaceRegistry()
|
|
err = cdc.UnmarshalInterface(bz, nil)
|
|
require.Error(t, err)
|
|
}
|