github.com/cosmos/cosmos-sdk@v0.50.10/codec/yaml_test.go (about)

     1  package codec_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/cosmos/cosmos-sdk/codec"
     9  	"github.com/cosmos/cosmos-sdk/codec/types"
    10  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    11  )
    12  
    13  func TestMarshalYAML(t *testing.T) {
    14  	dog := &testdata.Dog{
    15  		Size_: "small",
    16  		Name:  "Spot",
    17  	}
    18  	any, err := types.NewAnyWithValue(dog)
    19  	require.NoError(t, err)
    20  	hasAnimal := &testdata.HasAnimal{
    21  		Animal: any,
    22  		X:      0,
    23  	}
    24  
    25  	// proto
    26  	protoCdc := codec.NewProtoCodec(NewTestInterfaceRegistry())
    27  	bz, err := codec.MarshalYAML(protoCdc, hasAnimal)
    28  	require.NoError(t, err)
    29  	require.Equal(t, `animal:
    30    '@type': /testpb.Dog
    31    name: Spot
    32    size: small
    33  x: "0"
    34  `, string(bz))
    35  
    36  	// amino
    37  	aminoCdc := codec.NewAminoCodec(&codec.LegacyAmino{testdata.NewTestAmino()})
    38  	bz, err = codec.MarshalYAML(aminoCdc, hasAnimal)
    39  	require.NoError(t, err)
    40  	require.Equal(t, `type: testpb/HasAnimal
    41  value:
    42    animal:
    43      type: testpb/Dog
    44      value:
    45        name: Spot
    46        size: small
    47  `, string(bz))
    48  }