github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/has_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  	"google.golang.org/protobuf/types/known/anypb"
    11  )
    12  
    13  func TestHas(t *testing.T) {
    14  	t.Run("has all set", func(t *testing.T) {
    15  		m := &A{
    16  			Enum:        Enumeration_Two,
    17  			SomeBoolean: true,
    18  			INT32:       1,
    19  			SINT32:      2,
    20  			UINT32:      3,
    21  			INT64:       4,
    22  			SING64:      5,
    23  			UINT64:      6,
    24  			SFIXED32:    7,
    25  			FIXED32:     8,
    26  			FLOAT:       9,
    27  			SFIXED64:    10,
    28  			FIXED64:     11,
    29  			DOUBLE:      12,
    30  			STRING:      "a string",
    31  			BYTES:       []byte("test bytes"),
    32  			MESSAGE: &B{
    33  				X: "something else",
    34  			},
    35  			MAP:       map[string]*B{"item": {X: "inside_map_item"}},
    36  			LIST:      []*B{{X: "part of list"}},
    37  			ONEOF:     &A_ONEOF_B{ONEOF_B: &B{X: "1"}},
    38  			LIST_ENUM: []Enumeration{Enumeration_One, Enumeration_One},
    39  		}
    40  
    41  		dyn := dynamicpb.NewMessage(m.ProtoReflect().Descriptor())
    42  
    43  		b, err := proto.MarshalOptions{}.Marshal(m)
    44  		require.NoError(t, err)
    45  		require.NoError(t, proto.Unmarshal(b, dyn))
    46  
    47  		for i := 0; i < dyn.Descriptor().Fields().Len(); i++ {
    48  			fd := dyn.Descriptor().Fields().Get(i)
    49  
    50  			require.Equal(t, dyn.Has(fd), m.ProtoReflect().Has(fd), fd.FullName())
    51  		}
    52  	})
    53  
    54  	t.Run("has all unset", func(t *testing.T) {
    55  		m := &A{}
    56  		dyn := dynamicpb.NewMessage(m.ProtoReflect().Descriptor())
    57  
    58  		for i := 0; i < dyn.Descriptor().Fields().Len(); i++ {
    59  			fd := dyn.Descriptor().Fields().Get(i)
    60  
    61  			require.Equal(t, dyn.Has(fd), m.ProtoReflect().Has(fd), fd.FullName())
    62  		}
    63  	})
    64  
    65  	t.Run("oneof field is set but the value is the default one", func(t *testing.T) {
    66  		dyn := dynamicpb.NewMessage(md_A)
    67  		dyn.Set(md_A.Fields().ByName("ONEOF_STRING"), protoreflect.ValueOfString(""))
    68  		require.True(t, dyn.Has(md_A.Fields().ByName("ONEOF_STRING")))
    69  
    70  		m := &A{ONEOF: &A_ONEOF_STRING{ONEOF_STRING: ""}}
    71  		require.True(t, m.ProtoReflect().Has(md_A.Fields().ByName("ONEOF_STRING")))
    72  	})
    73  
    74  	t.Run("nil bytes", func(t *testing.T) {
    75  		fd := (&A{}).ProtoReflect().Descriptor().Fields().ByName("BYTES")
    76  		m := &A{BYTES: nil}
    77  		require.False(t, m.slowProtoReflect().Has(fd))
    78  		require.False(t, m.ProtoReflect().Has(fd))
    79  	})
    80  
    81  	t.Run("0 len bytes", func(t *testing.T) {
    82  		fd := (&A{}).ProtoReflect().Descriptor().Fields().ByName("BYTES")
    83  		m := &A{BYTES: []byte{}}
    84  
    85  		require.False(t, m.slowProtoReflect().Has(fd))
    86  		require.False(t, m.ProtoReflect().Has(fd))
    87  	})
    88  
    89  	t.Run("invalid fd", func(t *testing.T) {
    90  		invalidFd := (&anypb.Any{}).ProtoReflect().Descriptor().Fields().ByName("value")
    91  
    92  		require.Panics(t, func() {
    93  			(&A{}).ProtoReflect().Has(invalidFd)
    94  		})
    95  	})
    96  }