github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/whichoneof_test.go (about) 1 package testpb 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 "google.golang.org/protobuf/types/known/structpb" 8 ) 9 10 func TestWhichOneof(t *testing.T) { 11 od := (&A{}).ProtoReflect().Descriptor().Oneofs().ByName("ONEOF") 12 t.Run("unknown oneof", func(t *testing.T) { 13 msg := &A{ 14 ONEOF: &A_ONEOF_B{ONEOF_B: &B{X: "allelujah"}}, 15 } 16 17 require.Panics(t, func() { 18 msg.ProtoReflect().WhichOneof((&structpb.Value{}).ProtoReflect().Descriptor().Oneofs().Get(0)) 19 }) 20 21 }) 22 23 t.Run("valid oneof", func(t *testing.T) { 24 msg := &A{ 25 ONEOF: &A_ONEOF_B{ONEOF_B: &B{X: "allelujah"}}, 26 } 27 28 require.Equal(t, msg.ProtoReflect().WhichOneof(od), msg.ProtoReflect().Descriptor().Fields().ByName("ONEOF_B")) 29 }) 30 }