github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/list_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 TestList(t *testing.T) {
    13  	dyn := dynamicpb.NewMessage(md_A)
    14  	msg := (&A{}).ProtoReflect()
    15  
    16  	dynList := dyn.Mutable(fd_A_LIST).List()
    17  	list := msg.Mutable(fd_A_LIST).List()
    18  
    19  	t.Run("set", func(t *testing.T) {
    20  		// extend list without using the mutable variable
    21  		dynList.AppendMutable()
    22  		list.AppendMutable()
    23  
    24  		dynElem := dynList.NewElement()
    25  		dynElem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    26  
    27  		elem := list.NewElement()
    28  		elem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    29  
    30  		dynList.Set(0, protoreflect.ValueOfMessage(dynElem.Message()))
    31  		list.Set(0, protoreflect.ValueOfMessage(elem.Message()))
    32  
    33  		require.True(t, proto.Equal(dyn, msg.Interface()))
    34  	})
    35  
    36  	t.Run("append mutable", func(t *testing.T) {
    37  		dynElem := dynList.AppendMutable()
    38  		dynElem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    39  
    40  		elem := list.AppendMutable()
    41  		elem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    42  
    43  		require.True(t, proto.Equal(dyn, msg.Interface()))
    44  	})
    45  
    46  	t.Run("new element - append", func(t *testing.T) {
    47  		dynElem := dynList.NewElement().Message()
    48  		dynElem.Set(fd_B_x, protoreflect.ValueOfString("test"))
    49  
    50  		elem := list.NewElement().Message()
    51  		elem.Set(fd_B_x, protoreflect.ValueOfString("test"))
    52  
    53  		dynList.Append(protoreflect.ValueOfMessage(dynElem))
    54  		list.Append(protoreflect.ValueOfMessage(elem))
    55  
    56  		require.True(t, proto.Equal(dyn, msg.Interface()))
    57  
    58  	})
    59  
    60  	// we reset everything in case the following tests are run separate to the others above
    61  	dyn.Clear(fd_A_LIST)
    62  	msg.Clear(fd_A_LIST)
    63  
    64  	dynList = dyn.Mutable(fd_A_LIST).List()
    65  	list = msg.Mutable(fd_A_LIST).List()
    66  
    67  	dynElem := dynList.AppendMutable()
    68  	dynElem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    69  
    70  	elem := list.AppendMutable()
    71  	elem.Message().Set(fd_B_x, protoreflect.ValueOfString("test"))
    72  
    73  	t.Run("len", func(t *testing.T) {
    74  		require.Equal(t, dynList.Len(), list.Len())
    75  	})
    76  
    77  	t.Run("get", func(t *testing.T) {
    78  		dynElem := dynList.Get(0)
    79  		elem := list.Get(0)
    80  
    81  		require.True(t, proto.Equal(dynElem.Message().Interface(), elem.Message().Interface()))
    82  	})
    83  
    84  	t.Run("validity", func(t *testing.T) {
    85  		require.Equal(t, dynList.IsValid(), list.IsValid())
    86  	})
    87  
    88  	t.Run("truncate", func(t *testing.T) {
    89  		dynList.Truncate(0)
    90  		list.Truncate(0)
    91  
    92  		require.Equal(t, dynList.Len(), list.Len())
    93  	})
    94  }