github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/new_field_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 "google.golang.org/protobuf/types/dynamicpb" 9 "google.golang.org/protobuf/types/known/anypb" 10 ) 11 12 func TestNewField(t *testing.T) { 13 14 t.Run("matching values", func(t *testing.T) { 15 fds := (&A{}).slowProtoReflect().Descriptor().Fields() 16 17 dyn := dynamicpb.NewMessage((&A{}).slowProtoReflect().Descriptor()) 18 19 for i := 0; i < fds.Len(); i++ { 20 fd := fds.Get(i) 21 22 dynV := dyn.NewField(fd) 23 v := (&A{}).ProtoReflect().NewField(fd) 24 25 switch { 26 case fd.IsMap(): 27 // cast to map 28 require.NotPanics(t, func() { 29 v.Map() 30 dynV.Map() 31 }) 32 // validity 33 require.Equal(t, dynV.IsValid(), v.IsValid()) 34 case fd.IsList(): 35 // cast to list 36 require.NotPanics(t, func() { 37 v.List() 38 dynV.List() 39 }) 40 // validity 41 require.Equal(t, dynV.IsValid(), v.IsValid()) 42 case !fd.HasPresence(): 43 require.Equal(t, dynV, v) 44 case fd.Kind() == protoreflect.MessageKind: 45 require.NotPanics(t, func() { 46 v.Message() 47 dynV.Message() 48 49 require.Equal(t, dynV.IsValid(), v.IsValid()) 50 }) 51 } 52 } 53 }) 54 55 t.Run("invalid fd", func(t *testing.T) { 56 invalidFd := (&anypb.Any{}).ProtoReflect().Descriptor().Fields().ByName("value") 57 58 require.Panics(t, func() { 59 dynamicpb.NewMessage((&A{}).slowProtoReflect().Descriptor()).NewField(invalidFd) 60 }) 61 62 require.Panics(t, func() { 63 (&A{}).ProtoReflect().NewField(invalidFd) 64 }) 65 66 }) 67 68 t.Run("extensions panic", func(t *testing.T) { 69 // TODO(fdymylja) 70 }) 71 }