github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/codec_test.go (about)

     1  package testpb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"google.golang.org/protobuf/proto"
     8  	"google.golang.org/protobuf/reflect/protoreflect"
     9  	"google.golang.org/protobuf/types/dynamicpb"
    10  )
    11  
    12  func TestMarshal(t *testing.T) {
    13  	msg := &A{
    14  		Enum:        Enumeration_Two,
    15  		SomeBoolean: true,
    16  		INT32:       2,
    17  		SINT32:      3,
    18  		UINT32:      4,
    19  		INT64:       5,
    20  		SING64:      6,
    21  		UINT64:      7,
    22  		SFIXED32:    8,
    23  		FIXED32:     9,
    24  		FLOAT:       10.1,
    25  		SFIXED64:    11,
    26  		FIXED64:     12,
    27  		DOUBLE:      13,
    28  		STRING:      "fourteen",
    29  		BYTES:       []byte("fifteen"),
    30  		MESSAGE:     &B{X: "something"},
    31  		MAP:         map[string]*B{"a": &B{X: "aa"}},
    32  		LIST:        []*B{{X: "list"}},
    33  		ONEOF:       &A_ONEOF_B{ONEOF_B: &B{X: "ONEOF"}},
    34  		LIST_ENUM:   []Enumeration{Enumeration_One},
    35  	}
    36  
    37  	dynA := dynamicpb.NewMessage(md_A)
    38  	// dynB := dynamicpb.NewMessage(md_B)
    39  
    40  	dynA.Set(fd_A_enum, protoreflect.ValueOfEnum(protoreflect.EnumNumber(msg.Enum)))
    41  	dynA.Set(fd_A_some_boolean, protoreflect.ValueOfBool(msg.SomeBoolean))
    42  	dynA.Set(fd_A_INT32, protoreflect.ValueOfInt32(msg.INT32))
    43  	dynA.Set(fd_A_SINT32, protoreflect.ValueOfInt32(msg.SINT32))
    44  	dynA.Set(fd_A_UINT32, protoreflect.ValueOfUint32(msg.UINT32))
    45  	dynA.Set(fd_A_INT64, protoreflect.ValueOfInt64(msg.INT64))
    46  	dynA.Set(fd_A_SING64, protoreflect.ValueOfInt64(msg.SING64))
    47  	dynA.Set(fd_A_UINT64, protoreflect.ValueOfUint64(msg.UINT64))
    48  	dynA.Set(fd_A_SFIXED32, protoreflect.ValueOfInt32(msg.SFIXED32))
    49  	dynA.Set(fd_A_FIXED32, protoreflect.ValueOfUint32(msg.FIXED32))
    50  	dynA.Set(fd_A_FLOAT, protoreflect.ValueOfFloat32(msg.FLOAT))
    51  	dynA.Set(fd_A_SFIXED64, protoreflect.ValueOfInt64(msg.SFIXED64))
    52  	dynA.Set(fd_A_FIXED64, protoreflect.ValueOfUint64(msg.FIXED64))
    53  	dynA.Set(fd_A_DOUBLE, protoreflect.ValueOfFloat64(msg.DOUBLE))
    54  	dynA.Set(fd_A_STRING, protoreflect.ValueOfString(msg.STRING))
    55  	dynA.Set(fd_A_BYTES, protoreflect.ValueOfBytes(msg.BYTES))
    56  	dynA.Set(fd_A_MESSAGE, protoreflect.ValueOfMessage(msg.MESSAGE.ProtoReflect()))
    57  	dynMap := dynA.Mutable(fd_A_MAP).Map()
    58  	dynMap.Set(protoreflect.MapKey(protoreflect.ValueOfString("a")), protoreflect.ValueOfMessage(msg.MAP["a"].ProtoReflect()))
    59  	dynA.Mutable(fd_A_LIST).List().AppendMutable().Message().Set(fd_B_x, protoreflect.ValueOfString(msg.LIST[0].X))
    60  	dynA.Set(fd_A_ONEOF_B, protoreflect.ValueOfMessage(msg.ONEOF.(*A_ONEOF_B).ONEOF_B.ProtoReflect()))
    61  	dynA.Mutable(fd_A_LIST_ENUM).List().Append(protoreflect.ValueOfEnum((protoreflect.EnumNumber)(Enumeration_One)))
    62  
    63  	got, err := proto.MarshalOptions{Deterministic: true}.Marshal(msg)
    64  	require.NoError(t, err)
    65  
    66  	expected, err := proto.MarshalOptions{Deterministic: true}.Marshal(dynA)
    67  	require.NoError(t, err)
    68  
    69  	require.Equal(t, expected, got)
    70  }