github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/map_test.go (about) 1 package testpb 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 "google.golang.org/protobuf/proto" 9 "google.golang.org/protobuf/reflect/protoreflect" 10 "google.golang.org/protobuf/types/dynamicpb" 11 ) 12 13 func TestMap(t *testing.T) { 14 dyn := dynamicpb.NewMessage(md_A) 15 msg := (&A{}).ProtoReflect() 16 17 dynMap := dyn.Mutable(fd_A_MAP).Map() 18 mapv := msg.Mutable(fd_A_MAP).Map() 19 20 // we set a value in the map 21 dynValue := dynMap.NewValue().Message() 22 dynValue.Set(fd_B_x, protoreflect.ValueOfString("value")) 23 24 value := mapv.NewValue().Message() 25 value.Set(fd_B_x, protoreflect.ValueOfString("value")) 26 27 dynMap.Set((protoreflect.MapKey)(protoreflect.ValueOfString("key")), protoreflect.ValueOfMessage(dynValue)) 28 mapv.Set((protoreflect.MapKey)(protoreflect.ValueOfString("key")), protoreflect.ValueOfMessage(value)) 29 30 // test set 31 t.Run("set", func(t *testing.T) { 32 dynValue := dynMap.NewValue().Message() 33 dynValue.Set(fd_B_x, protoreflect.ValueOfString("something")) 34 35 value := mapv.NewValue().Message() 36 value.Set(fd_B_x, protoreflect.ValueOfString("something")) 37 38 dynMap.Set((protoreflect.MapKey)(protoreflect.ValueOfString("test")), protoreflect.ValueOfMessage(dynValue)) 39 mapv.Set((protoreflect.MapKey)(protoreflect.ValueOfString("test")), protoreflect.ValueOfMessage(value)) 40 41 require.True(t, proto.Equal(dyn, msg.Interface())) 42 43 }) 44 45 // test get 46 t.Run("get", func(t *testing.T) { 47 dynValue := dynMap.Get(protoreflect.MapKey(protoreflect.ValueOfString("key"))) 48 value := mapv.Get(protoreflect.MapKey(protoreflect.ValueOfString("key"))) 49 50 require.True(t, proto.Equal(dynValue.Message().Interface(), value.Message().Interface())) 51 }) 52 53 // test len 54 t.Run("len", func(t *testing.T) { 55 require.Equal(t, dynMap.Len(), mapv.Len()) 56 }) 57 58 // test has 59 t.Run("has", func(t *testing.T) { 60 // case exists 61 require.Equal(t, dynMap.Has(protoreflect.MapKey(protoreflect.ValueOfString("key"))), mapv.Has(protoreflect.MapKey(protoreflect.ValueOfString("key")))) 62 // case not exists 63 require.Equal(t, dynMap.Has(protoreflect.MapKey(protoreflect.ValueOfString("not-exist"))), mapv.Has(protoreflect.MapKey(protoreflect.ValueOfString("not-exist")))) 64 }) 65 66 // test clear 67 t.Run("clear", func(t *testing.T) { 68 dynMap.Clear(protoreflect.MapKey(protoreflect.ValueOfString("key"))) 69 mapv.Clear(protoreflect.MapKey(protoreflect.ValueOfString("key"))) 70 71 require.True(t, proto.Equal(dyn, msg.Interface())) 72 }) 73 74 // test range with mutable 75 t.Run("range mutable", func(t *testing.T) { 76 dyn.Clear(fd_A_MAP) 77 msg.Clear(fd_A_MAP) 78 79 dynMap = dyn.Mutable(fd_A_MAP).Map() 80 mapv = msg.Mutable(fd_A_MAP).Map() 81 82 insert := func(m protoreflect.Map, n int) { 83 mutableMsg := m.Mutable(protoreflect.MapKey(protoreflect.ValueOfString(fmt.Sprintf("%d", n)))).Message() 84 mutableMsg.Set(fd_B_x, protoreflect.ValueOfString(fmt.Sprintf("%d", n))) 85 } 86 87 nElems := 10 88 89 for i := 0; i < nElems; i++ { 90 insert(dynMap, i) 91 insert(mapv, i) 92 } 93 94 dynM := make(map[string]protoreflect.Message) 95 msgM := make(map[string]protoreflect.Message) 96 97 dynMap.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool { 98 dynM[key.String()] = value.Message() 99 return true 100 }) 101 102 mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool { 103 msgM[key.String()] = value.Message() 104 return true 105 }) 106 107 require.Equal(t, dynMap.Len(), nElems) 108 require.Equal(t, dynMap.Len(), mapv.Len()) 109 110 for k, v := range dynM { 111 vm, ok := msgM[k] 112 require.True(t, ok, "map key ", k, "does not exist") 113 114 require.True(t, proto.Equal(v.Interface(), vm.Interface())) 115 } 116 }) 117 118 // test validity 119 t.Run("valid", func(t *testing.T) { 120 require.Equal(t, dynMap.IsValid(), mapv.IsValid()) 121 }) 122 123 }