github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/set_test.go (about) 1 package testpb 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 "google.golang.org/protobuf/reflect/protoreflect" 8 ) 9 10 func TestSet(t *testing.T) { 11 t.Run("extension panics", func(t *testing.T) { 12 // TODO(fdymylja): mock extensions fd 13 }) 14 15 t.Run("set scalar types and message types", func(t *testing.T) { 16 msg := &A{ 17 Enum: Enumeration_Two, 18 SomeBoolean: true, 19 INT32: 1, 20 SINT32: 2, 21 UINT32: 3, 22 INT64: 4, 23 SING64: 5, 24 UINT64: 6, 25 SFIXED32: 7, 26 FIXED32: 8, 27 FLOAT: 9, 28 SFIXED64: 10, 29 FIXED64: 11, 30 DOUBLE: 12, 31 STRING: "a string", 32 BYTES: []byte("test bytes"), 33 MESSAGE: &B{ 34 X: "something else", 35 }, 36 37 ONEOF: &A_ONEOF_STRING{ONEOF_STRING: "test"}, 38 } 39 40 m := &A{} 41 42 msg.slowProtoReflect().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { 43 m.ProtoReflect().Set(fd, v) // set the field 44 45 gotValue := m.ProtoReflect().Get(fd) // get the field which was set 46 47 require.Equal(t, v, gotValue) // and assert it was equal to the provided field of the default proto impl 48 49 return true 50 }) 51 }) 52 53 // NOTE(fdymylja): mutability of composite types is not tested 54 // because it is not part of the specification. 55 56 t.Run("set list type", func(t *testing.T) { 57 m := &A{} 58 fd := m.ProtoReflect().Descriptor().Fields().ByName("LIST") 59 v := m.ProtoReflect().NewField(fd).List() // no mutability with NewField 60 61 el1 := &B{X: "1"} 62 el2 := &B{X: "2"} 63 v.Append(protoreflect.ValueOfMessage(el1.ProtoReflect())) 64 v.Append(protoreflect.ValueOfMessage(el2.ProtoReflect())) 65 66 m.ProtoReflect().Set(fd, protoreflect.ValueOfList(v)) 67 68 require.Len(t, m.LIST, 2) 69 require.Equal(t, m.LIST[0], el1) 70 require.Equal(t, m.LIST[1], el2) 71 }) 72 73 t.Run("set map type", func(t *testing.T) { 74 m := &A{} 75 fd := m.ProtoReflect().Descriptor().Fields().ByName("MAP") 76 v := m.ProtoReflect().NewField(fd).Map() 77 78 mk1, mk2 := "1", "2" 79 mv1, mv2 := &B{X: "1"}, &B{X: "2"} 80 81 v.Set((protoreflect.MapKey)(protoreflect.ValueOfString(mk1)), protoreflect.ValueOfMessage(mv1.ProtoReflect())) 82 v.Set((protoreflect.MapKey)(protoreflect.ValueOfString(mk2)), protoreflect.ValueOfMessage(mv2.ProtoReflect())) 83 84 m.ProtoReflect().Set(fd, protoreflect.ValueOfMap(v)) 85 86 require.Len(t, m.MAP, 2) 87 require.Equal(t, m.MAP[mk1], mv1) 88 require.Equal(t, m.MAP[mk2], mv2) 89 }) 90 }