github.com/Finschia/finschia-sdk@v0.48.1/codec/types/compat_test.go (about) 1 package types_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/suite" 7 amino "github.com/tendermint/go-amino" 8 9 "github.com/Finschia/finschia-sdk/codec/types" 10 "github.com/Finschia/finschia-sdk/testutil/testdata" 11 ) 12 13 type TypeWithInterface struct { 14 Animal testdata.Animal `json:"animal"` 15 X int64 `json:"x,omitempty"` 16 } 17 18 type Suite struct { 19 suite.Suite 20 cdc *amino.Codec 21 a TypeWithInterface 22 b testdata.HasAnimal 23 spot *testdata.Dog 24 } 25 26 func (s *Suite) SetupTest() { 27 s.cdc = amino.NewCodec() 28 s.cdc.RegisterInterface((*testdata.Animal)(nil), nil) 29 s.cdc.RegisterConcrete(&testdata.Dog{}, "testdata/Dog", nil) 30 31 s.spot = &testdata.Dog{Size_: "small", Name: "Spot"} 32 s.a = TypeWithInterface{Animal: s.spot} 33 34 any, err := types.NewAnyWithValue(s.spot) 35 s.Require().NoError(err) 36 s.b = testdata.HasAnimal{Animal: any} 37 } 38 39 func (s *Suite) TestAminoBinary() { 40 bz, err := s.cdc.MarshalBinaryBare(s.a) 41 s.Require().NoError(err) 42 43 // expect plain amino marshal to fail 44 _, err = s.cdc.MarshalBinaryBare(s.b) 45 s.Require().Error(err) 46 47 // expect unpack interfaces before amino marshal to succeed 48 err = types.UnpackInterfaces(s.b, types.AminoPacker{Cdc: s.cdc}) 49 s.Require().NoError(err) 50 bz2, err := s.cdc.MarshalBinaryBare(s.b) 51 s.Require().NoError(err) 52 s.Require().Equal(bz, bz2) 53 54 var c testdata.HasAnimal 55 err = s.cdc.UnmarshalBinaryBare(bz, &c) 56 s.Require().NoError(err) 57 err = types.UnpackInterfaces(c, types.AminoUnpacker{Cdc: s.cdc}) 58 s.Require().NoError(err) 59 s.Require().Equal(s.spot, c.Animal.GetCachedValue()) 60 } 61 62 func (s *Suite) TestAminoJSON() { 63 bz, err := s.cdc.MarshalJSON(s.a) 64 s.Require().NoError(err) 65 66 // expect plain amino marshal to fail 67 _, err = s.cdc.MarshalJSON(s.b) 68 s.Require().Error(err) 69 70 // expect unpack interfaces before amino marshal to succeed 71 err = types.UnpackInterfaces(s.b, types.AminoJSONPacker{Cdc: s.cdc}) 72 s.Require().NoError(err) 73 bz2, err := s.cdc.MarshalJSON(s.b) 74 s.Require().NoError(err) 75 s.Require().Equal(string(bz), string(bz2)) 76 77 var c testdata.HasAnimal 78 err = s.cdc.UnmarshalJSON(bz, &c) 79 s.Require().NoError(err) 80 err = types.UnpackInterfaces(c, types.AminoJSONUnpacker{Cdc: s.cdc}) 81 s.Require().NoError(err) 82 s.Require().Equal(s.spot, c.Animal.GetCachedValue()) 83 } 84 85 func (s *Suite) TestNested() { 86 s.cdc.RegisterInterface((*testdata.HasAnimalI)(nil), nil) 87 s.cdc.RegisterInterface((*testdata.HasHasAnimalI)(nil), nil) 88 s.cdc.RegisterConcrete(&testdata.HasAnimal{}, "testdata/HasAnimal", nil) 89 s.cdc.RegisterConcrete(&testdata.HasHasAnimal{}, "testdata/HasHasAnimal", nil) 90 s.cdc.RegisterConcrete(&testdata.HasHasHasAnimal{}, "testdata/HasHasHasAnimal", nil) 91 92 any, err := types.NewAnyWithValue(&s.b) 93 s.Require().NoError(err) 94 hha := testdata.HasHasAnimal{HasAnimal: any} 95 any2, err := types.NewAnyWithValue(&hha) 96 s.Require().NoError(err) 97 hhha := testdata.HasHasHasAnimal{HasHasAnimal: any2} 98 99 // marshal 100 err = types.UnpackInterfaces(hhha, types.AminoPacker{Cdc: s.cdc}) 101 s.Require().NoError(err) 102 bz, err := s.cdc.MarshalBinaryBare(hhha) 103 s.Require().NoError(err) 104 105 // unmarshal 106 var hhha2 testdata.HasHasHasAnimal 107 err = s.cdc.UnmarshalBinaryBare(bz, &hhha2) 108 s.Require().NoError(err) 109 err = types.UnpackInterfaces(hhha2, types.AminoUnpacker{Cdc: s.cdc}) 110 s.Require().NoError(err) 111 112 s.Require().Equal(s.spot, hhha2.TheHasHasAnimal().TheHasAnimal().TheAnimal()) 113 114 // json marshal 115 err = types.UnpackInterfaces(hhha, types.AminoJSONPacker{Cdc: s.cdc}) 116 s.Require().NoError(err) 117 jsonBz, err := s.cdc.MarshalJSON(hhha) 118 s.Require().NoError(err) 119 120 // json unmarshal 121 var hhha3 testdata.HasHasHasAnimal 122 err = s.cdc.UnmarshalJSON(jsonBz, &hhha3) 123 s.Require().NoError(err) 124 err = types.UnpackInterfaces(hhha3, types.AminoJSONUnpacker{Cdc: s.cdc}) 125 s.Require().NoError(err) 126 127 s.Require().Equal(s.spot, hhha3.TheHasHasAnimal().TheHasAnimal().TheAnimal()) 128 } 129 130 func TestSuite(t *testing.T) { 131 suite.Run(t, &Suite{}) 132 }