Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 5 | package impl_test |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 8 | "fmt" |
| 9 | "math" |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 10 | "reflect" |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 11 | "strings" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 12 | "testing" |
| 13 | |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 14 | protoV1 "github.com/golang/protobuf/proto" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 15 | pimpl "github.com/golang/protobuf/v2/internal/impl" |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 16 | scalar "github.com/golang/protobuf/v2/internal/scalar" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 17 | pvalue "github.com/golang/protobuf/v2/internal/value" |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 18 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 19 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 20 | cmp "github.com/google/go-cmp/cmp" |
| 21 | cmpopts "github.com/google/go-cmp/cmp/cmpopts" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 22 | |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 23 | // The legacy package must be imported prior to use of any legacy messages. |
| 24 | // TODO: Remove this when protoV1 registers these hooks for you. |
| 25 | _ "github.com/golang/protobuf/v2/internal/legacy" |
| 26 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 27 | proto2_20180125 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 28 | descriptorpb "github.com/golang/protobuf/v2/types/descriptor" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 31 | // List of test operations to perform on messages, lists, or maps. |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 32 | type ( |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 33 | messageOp interface{ isMessageOp() } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 34 | messageOps []messageOp |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 35 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 36 | listOp interface{ isListOp() } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 37 | listOps []listOp |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 38 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 39 | mapOp interface{ isMapOp() } |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 40 | mapOps []mapOp |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | // Test operations performed on a message. |
| 44 | type ( |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 45 | // check that the message contents match |
| 46 | equalMessage struct{ pref.Message } |
| 47 | // check presence for specific fields in the message |
| 48 | hasFields map[pref.FieldNumber]bool |
| 49 | // check that specific message fields match |
| 50 | getFields map[pref.FieldNumber]pref.Value |
| 51 | // set specific message fields |
| 52 | setFields map[pref.FieldNumber]pref.Value |
| 53 | // clear specific fields in the message |
| 54 | clearFields []pref.FieldNumber |
| 55 | // apply messageOps on each specified message field |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 56 | messageFields map[pref.FieldNumber]messageOps |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 57 | // apply listOps on each specified list field |
| 58 | listFields map[pref.FieldNumber]listOps |
| 59 | // apply mapOps on each specified map fields |
| 60 | mapFields map[pref.FieldNumber]mapOps |
| 61 | // range through all fields and check that they match |
| 62 | rangeFields map[pref.FieldNumber]pref.Value |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 63 | ) |
| 64 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 65 | func (equalMessage) isMessageOp() {} |
| 66 | func (hasFields) isMessageOp() {} |
| 67 | func (getFields) isMessageOp() {} |
| 68 | func (setFields) isMessageOp() {} |
| 69 | func (clearFields) isMessageOp() {} |
| 70 | func (messageFields) isMessageOp() {} |
| 71 | func (listFields) isMessageOp() {} |
| 72 | func (mapFields) isMessageOp() {} |
| 73 | func (rangeFields) isMessageOp() {} |
| 74 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 75 | // Test operations performed on a list. |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 76 | type ( |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 77 | // check that the list contents match |
| 78 | equalList struct{ pref.List } |
| 79 | // check that list length matches |
| 80 | lenList int |
| 81 | // check that specific list entries match |
| 82 | getList map[int]pref.Value |
| 83 | // set specific list entries |
| 84 | setList map[int]pref.Value |
| 85 | // append entries to the list |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 86 | appendList []pref.Value |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 87 | // apply messageOps on a newly appended message |
| 88 | appendMessageList messageOps |
| 89 | // truncate the list to the specified length |
| 90 | truncList int |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 91 | ) |
| 92 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 93 | func (equalList) isListOp() {} |
| 94 | func (lenList) isListOp() {} |
| 95 | func (getList) isListOp() {} |
| 96 | func (setList) isListOp() {} |
| 97 | func (appendList) isListOp() {} |
| 98 | func (appendMessageList) isListOp() {} |
| 99 | func (truncList) isListOp() {} |
| 100 | |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 101 | // Test operations performed on a map. |
| 102 | type ( |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 103 | // check that the map contents match |
| 104 | equalMap struct{ pref.Map } |
| 105 | // check that map length matches |
| 106 | lenMap int |
| 107 | // check presence for specific entries in the map |
| 108 | hasMap map[interface{}]bool |
| 109 | // check that specific map entries match |
| 110 | getMap map[interface{}]pref.Value |
| 111 | // set specific map entries |
| 112 | setMap map[interface{}]pref.Value |
| 113 | // clear specific entries in the map |
| 114 | clearMap []interface{} |
| 115 | // apply messageOps on each specified message entry |
| 116 | messageMap map[interface{}]messageOps |
| 117 | // range through all entries and check that they match |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 118 | rangeMap map[interface{}]pref.Value |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 119 | ) |
| 120 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 121 | func (equalMap) isMapOp() {} |
| 122 | func (lenMap) isMapOp() {} |
| 123 | func (hasMap) isMapOp() {} |
| 124 | func (getMap) isMapOp() {} |
| 125 | func (setMap) isMapOp() {} |
| 126 | func (clearMap) isMapOp() {} |
| 127 | func (messageMap) isMapOp() {} |
| 128 | func (rangeMap) isMapOp() {} |
| 129 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 130 | type ScalarProto2 struct { |
| 131 | Bool *bool `protobuf:"1"` |
| 132 | Int32 *int32 `protobuf:"2"` |
| 133 | Int64 *int64 `protobuf:"3"` |
| 134 | Uint32 *uint32 `protobuf:"4"` |
| 135 | Uint64 *uint64 `protobuf:"5"` |
| 136 | Float32 *float32 `protobuf:"6"` |
| 137 | Float64 *float64 `protobuf:"7"` |
| 138 | String *string `protobuf:"8"` |
| 139 | StringA []byte `protobuf:"9"` |
| 140 | Bytes []byte `protobuf:"10"` |
| 141 | BytesA *string `protobuf:"11"` |
| 142 | |
| 143 | MyBool *MyBool `protobuf:"12"` |
| 144 | MyInt32 *MyInt32 `protobuf:"13"` |
| 145 | MyInt64 *MyInt64 `protobuf:"14"` |
| 146 | MyUint32 *MyUint32 `protobuf:"15"` |
| 147 | MyUint64 *MyUint64 `protobuf:"16"` |
| 148 | MyFloat32 *MyFloat32 `protobuf:"17"` |
| 149 | MyFloat64 *MyFloat64 `protobuf:"18"` |
| 150 | MyString *MyString `protobuf:"19"` |
| 151 | MyStringA MyBytes `protobuf:"20"` |
| 152 | MyBytes MyBytes `protobuf:"21"` |
| 153 | MyBytesA *MyString `protobuf:"22"` |
| 154 | } |
| 155 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 156 | func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor { |
| 157 | ed, err := ptype.NewEnum(&t) |
| 158 | if err != nil { |
| 159 | panic(err) |
| 160 | } |
| 161 | return ed |
| 162 | } |
| 163 | |
| 164 | func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor { |
| 165 | md, err := ptype.NewMessage(&t) |
| 166 | if err != nil { |
| 167 | panic(err) |
| 168 | } |
| 169 | return md |
| 170 | } |
| 171 | |
| 172 | var V = pref.ValueOf |
| 173 | var VE = func(n pref.EnumNumber) pref.Value { return V(n) } |
| 174 | |
| 175 | type ( |
| 176 | MyBool bool |
| 177 | MyInt32 int32 |
| 178 | MyInt64 int64 |
| 179 | MyUint32 uint32 |
| 180 | MyUint64 uint64 |
| 181 | MyFloat32 float32 |
| 182 | MyFloat64 float64 |
| 183 | MyString string |
| 184 | MyBytes []byte |
| 185 | |
| 186 | ListStrings []MyString |
| 187 | ListBytes []MyBytes |
| 188 | |
| 189 | MapStrings map[MyString]MyString |
| 190 | MapBytes map[MyString]MyBytes |
| 191 | ) |
| 192 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 193 | var scalarProto2Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: ptype.GoMessage( |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 194 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 195 | Syntax: pref.Proto2, |
| 196 | FullName: "ScalarProto2", |
| 197 | Fields: []ptype.Field{ |
| 198 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))}, |
| 199 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))}, |
| 200 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))}, |
| 201 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))}, |
| 202 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))}, |
| 203 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))}, |
| 204 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))}, |
| 205 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))}, |
| 206 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))}, |
| 207 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))}, |
| 208 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))}, |
| 209 | |
| 210 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))}, |
| 211 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))}, |
| 212 | {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))}, |
| 213 | {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))}, |
| 214 | {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))}, |
| 215 | {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))}, |
| 216 | {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))}, |
| 217 | {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))}, |
| 218 | {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))}, |
| 219 | {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))}, |
| 220 | {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))}, |
| 221 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 222 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 223 | func(pref.MessageType) pref.Message { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 224 | return new(ScalarProto2) |
| 225 | }, |
| 226 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 227 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 228 | func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.PBType } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 229 | func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) } |
| 230 | func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) } |
| 231 | func (m *ScalarProto2) Interface() pref.ProtoMessage { return m } |
| 232 | func (m *ScalarProto2) ProtoReflect() pref.Message { return m } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 233 | |
| 234 | func TestScalarProto2(t *testing.T) { |
| 235 | testMessage(t, nil, &ScalarProto2{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 236 | hasFields{ |
| 237 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 238 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 239 | }, |
| 240 | getFields{ |
| 241 | 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")), |
| 242 | 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")), |
| 243 | }, |
| 244 | setFields{ |
| 245 | 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)), |
| 246 | 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)), |
| 247 | }, |
| 248 | hasFields{ |
| 249 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 250 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 251 | }, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 252 | equalMessage{&ScalarProto2{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 253 | new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string), |
| 254 | new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 255 | }}, |
| 256 | clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, |
| 257 | equalMessage{&ScalarProto2{}}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 258 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 259 | |
| 260 | // Test read-only operations on nil message. |
| 261 | testMessage(t, nil, (*ScalarProto2)(nil), messageOps{ |
| 262 | hasFields{ |
| 263 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 264 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 265 | }, |
| 266 | getFields{ |
| 267 | 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")), |
| 268 | 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")), |
| 269 | }, |
| 270 | }) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 273 | type ScalarProto3 struct { |
| 274 | Bool bool `protobuf:"1"` |
| 275 | Int32 int32 `protobuf:"2"` |
| 276 | Int64 int64 `protobuf:"3"` |
| 277 | Uint32 uint32 `protobuf:"4"` |
| 278 | Uint64 uint64 `protobuf:"5"` |
| 279 | Float32 float32 `protobuf:"6"` |
| 280 | Float64 float64 `protobuf:"7"` |
| 281 | String string `protobuf:"8"` |
| 282 | StringA []byte `protobuf:"9"` |
| 283 | Bytes []byte `protobuf:"10"` |
| 284 | BytesA string `protobuf:"11"` |
| 285 | |
| 286 | MyBool MyBool `protobuf:"12"` |
| 287 | MyInt32 MyInt32 `protobuf:"13"` |
| 288 | MyInt64 MyInt64 `protobuf:"14"` |
| 289 | MyUint32 MyUint32 `protobuf:"15"` |
| 290 | MyUint64 MyUint64 `protobuf:"16"` |
| 291 | MyFloat32 MyFloat32 `protobuf:"17"` |
| 292 | MyFloat64 MyFloat64 `protobuf:"18"` |
| 293 | MyString MyString `protobuf:"19"` |
| 294 | MyStringA MyBytes `protobuf:"20"` |
| 295 | MyBytes MyBytes `protobuf:"21"` |
| 296 | MyBytesA MyString `protobuf:"22"` |
| 297 | } |
| 298 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 299 | var scalarProto3Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: ptype.GoMessage( |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 300 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 301 | Syntax: pref.Proto3, |
| 302 | FullName: "ScalarProto3", |
| 303 | Fields: []ptype.Field{ |
| 304 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind}, |
| 305 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind}, |
| 306 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind}, |
| 307 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind}, |
| 308 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind}, |
| 309 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind}, |
| 310 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind}, |
| 311 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 312 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 313 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 314 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 315 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 316 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind}, |
| 317 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind}, |
| 318 | {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind}, |
| 319 | {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind}, |
| 320 | {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind}, |
| 321 | {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind}, |
| 322 | {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind}, |
| 323 | {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 324 | {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 325 | {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 326 | {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 327 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 328 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 329 | func(pref.MessageType) pref.Message { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 330 | return new(ScalarProto3) |
| 331 | }, |
| 332 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 333 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 334 | func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.PBType } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 335 | func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) } |
| 336 | func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) } |
| 337 | func (m *ScalarProto3) Interface() pref.ProtoMessage { return m } |
| 338 | func (m *ScalarProto3) ProtoReflect() pref.Message { return m } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 339 | |
| 340 | func TestScalarProto3(t *testing.T) { |
| 341 | testMessage(t, nil, &ScalarProto3{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 342 | hasFields{ |
| 343 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 344 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 345 | }, |
| 346 | getFields{ |
| 347 | 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)), |
| 348 | 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)), |
| 349 | }, |
| 350 | setFields{ |
| 351 | 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)), |
| 352 | 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)), |
| 353 | }, |
| 354 | hasFields{ |
| 355 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 356 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 357 | }, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 358 | equalMessage{&ScalarProto3{}}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 359 | setFields{ |
| 360 | 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")), |
| 361 | 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")), |
| 362 | }, |
| 363 | hasFields{ |
| 364 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 365 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 366 | }, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 367 | equalMessage{&ScalarProto3{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 368 | true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11", |
| 369 | true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22", |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 370 | }}, |
Joe Tsai | 44e389c | 2018-11-19 15:27:09 -0800 | [diff] [blame] | 371 | setFields{ |
| 372 | 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())), |
| 373 | }, |
| 374 | hasFields{ |
| 375 | 2: true, 3: true, 6: true, 7: true, |
| 376 | }, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 377 | clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, |
| 378 | equalMessage{&ScalarProto3{}}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 379 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 380 | |
| 381 | // Test read-only operations on nil message. |
| 382 | testMessage(t, nil, (*ScalarProto3)(nil), messageOps{ |
| 383 | hasFields{ |
| 384 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 385 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 386 | }, |
| 387 | getFields{ |
| 388 | 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)), |
| 389 | 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)), |
| 390 | }, |
| 391 | }) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 394 | type ListScalars struct { |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 395 | Bools []bool `protobuf:"1"` |
| 396 | Int32s []int32 `protobuf:"2"` |
| 397 | Int64s []int64 `protobuf:"3"` |
| 398 | Uint32s []uint32 `protobuf:"4"` |
| 399 | Uint64s []uint64 `protobuf:"5"` |
| 400 | Float32s []float32 `protobuf:"6"` |
| 401 | Float64s []float64 `protobuf:"7"` |
| 402 | Strings []string `protobuf:"8"` |
| 403 | StringsA [][]byte `protobuf:"9"` |
| 404 | Bytes [][]byte `protobuf:"10"` |
| 405 | BytesA []string `protobuf:"11"` |
| 406 | |
| 407 | MyStrings1 []MyString `protobuf:"12"` |
| 408 | MyStrings2 []MyBytes `protobuf:"13"` |
| 409 | MyBytes1 []MyBytes `protobuf:"14"` |
| 410 | MyBytes2 []MyString `protobuf:"15"` |
| 411 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 412 | MyStrings3 ListStrings `protobuf:"16"` |
| 413 | MyStrings4 ListBytes `protobuf:"17"` |
| 414 | MyBytes3 ListBytes `protobuf:"18"` |
| 415 | MyBytes4 ListStrings `protobuf:"19"` |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 418 | var listScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(ListScalars)), PBType: ptype.GoMessage( |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 419 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 420 | Syntax: pref.Proto2, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 421 | FullName: "ListScalars", |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 422 | Fields: []ptype.Field{ |
| 423 | {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind}, |
| 424 | {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind}, |
| 425 | {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind}, |
| 426 | {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind}, |
| 427 | {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind}, |
| 428 | {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind}, |
| 429 | {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind}, |
| 430 | {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 431 | {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 432 | {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 433 | {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 434 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 435 | {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 436 | {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 437 | {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 438 | {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 439 | |
| 440 | {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 441 | {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 442 | {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 443 | {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 444 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 445 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 446 | func(pref.MessageType) pref.Message { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 447 | return new(ListScalars) |
| 448 | }, |
| 449 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 450 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 451 | func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 452 | func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) } |
| 453 | func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) } |
| 454 | func (m *ListScalars) Interface() pref.ProtoMessage { return m } |
| 455 | func (m *ListScalars) ProtoReflect() pref.Message { return m } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 456 | |
| 457 | func TestListScalars(t *testing.T) { |
| 458 | empty := &ListScalars{} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 459 | emptyFS := empty.KnownFields() |
| 460 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 461 | want := &ListScalars{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 462 | Bools: []bool{true, false, true}, |
| 463 | Int32s: []int32{2, math.MinInt32, math.MaxInt32}, |
| 464 | Int64s: []int64{3, math.MinInt64, math.MaxInt64}, |
| 465 | Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32}, |
| 466 | Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64}, |
| 467 | Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32}, |
| 468 | Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64}, |
| 469 | Strings: []string{"8", "", "eight"}, |
| 470 | StringsA: [][]byte{[]byte("9"), nil, []byte("nine")}, |
| 471 | Bytes: [][]byte{[]byte("10"), nil, []byte("ten")}, |
| 472 | BytesA: []string{"11", "", "eleven"}, |
| 473 | |
| 474 | MyStrings1: []MyString{"12", "", "twelve"}, |
| 475 | MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")}, |
| 476 | MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")}, |
| 477 | MyBytes2: []MyString{"15", "", "fifteen"}, |
| 478 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 479 | MyStrings3: ListStrings{"16", "", "sixteen"}, |
| 480 | MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")}, |
| 481 | MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")}, |
| 482 | MyBytes4: ListStrings{"19", "", "nineteen"}, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 483 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 484 | wantFS := want.KnownFields() |
| 485 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 486 | testMessage(t, nil, &ListScalars{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 487 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false}, |
| 488 | getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19)}, |
| 489 | setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19)}, |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 490 | listFields{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 491 | 2: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 492 | lenList(0), |
| 493 | appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))}, |
| 494 | getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 495 | equalList{wantFS.Get(2).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 496 | }, |
| 497 | 4: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 498 | appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))}, |
| 499 | setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))}, |
| 500 | lenList(3), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 501 | }, |
| 502 | 6: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 503 | appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 504 | equalList{wantFS.Get(6).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 505 | }, |
| 506 | 8: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 507 | appendList{V(""), V(""), V(""), V(""), V(""), V("")}, |
| 508 | lenList(6), |
| 509 | setList{0: V("8"), 2: V("eight")}, |
| 510 | truncList(3), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 511 | equalList{wantFS.Get(8).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 512 | }, |
| 513 | 10: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 514 | appendList{V([]byte(nil)), V([]byte(nil))}, |
| 515 | setList{0: V([]byte("10"))}, |
| 516 | appendList{V([]byte("wrong"))}, |
| 517 | setList{2: V([]byte("ten"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 518 | equalList{wantFS.Get(10).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 519 | }, |
| 520 | 12: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 521 | appendList{V("12"), V("wrong"), V("twelve")}, |
| 522 | setList{1: V("")}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 523 | equalList{wantFS.Get(12).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 524 | }, |
| 525 | 14: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 526 | appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 527 | equalList{wantFS.Get(14).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 528 | }, |
| 529 | 16: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 530 | appendList{V("16"), V(""), V("sixteen"), V("extra")}, |
| 531 | truncList(3), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 532 | equalList{wantFS.Get(16).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 533 | }, |
| 534 | 18: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 535 | appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 536 | equalList{wantFS.Get(18).List()}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 537 | }, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 538 | }, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 539 | hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 540 | equalMessage{want}, |
| 541 | clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, |
| 542 | equalMessage{empty}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 543 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 544 | |
| 545 | // Test read-only operations on nil message. |
| 546 | testMessage(t, nil, (*ListScalars)(nil), messageOps{ |
| 547 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false}, |
| 548 | listFields{2: {lenList(0)}, 4: {lenList(0)}, 6: {lenList(0)}, 8: {lenList(0)}, 10: {lenList(0)}, 12: {lenList(0)}, 14: {lenList(0)}, 16: {lenList(0)}, 18: {lenList(0)}}, |
| 549 | }) |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 552 | type MapScalars struct { |
| 553 | KeyBools map[bool]string `protobuf:"1"` |
| 554 | KeyInt32s map[int32]string `protobuf:"2"` |
| 555 | KeyInt64s map[int64]string `protobuf:"3"` |
| 556 | KeyUint32s map[uint32]string `protobuf:"4"` |
| 557 | KeyUint64s map[uint64]string `protobuf:"5"` |
| 558 | KeyStrings map[string]string `protobuf:"6"` |
| 559 | |
| 560 | ValBools map[string]bool `protobuf:"7"` |
| 561 | ValInt32s map[string]int32 `protobuf:"8"` |
| 562 | ValInt64s map[string]int64 `protobuf:"9"` |
| 563 | ValUint32s map[string]uint32 `protobuf:"10"` |
| 564 | ValUint64s map[string]uint64 `protobuf:"11"` |
| 565 | ValFloat32s map[string]float32 `protobuf:"12"` |
| 566 | ValFloat64s map[string]float64 `protobuf:"13"` |
| 567 | ValStrings map[string]string `protobuf:"14"` |
| 568 | ValStringsA map[string][]byte `protobuf:"15"` |
| 569 | ValBytes map[string][]byte `protobuf:"16"` |
| 570 | ValBytesA map[string]string `protobuf:"17"` |
| 571 | |
| 572 | MyStrings1 map[MyString]MyString `protobuf:"18"` |
| 573 | MyStrings2 map[MyString]MyBytes `protobuf:"19"` |
| 574 | MyBytes1 map[MyString]MyBytes `protobuf:"20"` |
| 575 | MyBytes2 map[MyString]MyString `protobuf:"21"` |
| 576 | |
| 577 | MyStrings3 MapStrings `protobuf:"22"` |
| 578 | MyStrings4 MapBytes `protobuf:"23"` |
| 579 | MyBytes3 MapBytes `protobuf:"24"` |
| 580 | MyBytes4 MapStrings `protobuf:"25"` |
| 581 | } |
| 582 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 583 | func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field { |
| 584 | return ptype.Field{ |
| 585 | Name: pref.Name(fmt.Sprintf("f%d", n)), |
| 586 | Number: n, |
| 587 | Cardinality: pref.Repeated, |
| 588 | Kind: pref.MessageKind, |
| 589 | MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 590 | Syntax: pref.Proto2, |
| 591 | FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)), |
| 592 | Fields: []ptype.Field{ |
| 593 | {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind}, |
| 594 | {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind}, |
| 595 | }, |
Damien Neil | 232ea15 | 2018-12-10 15:14:36 -0800 | [diff] [blame] | 596 | Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)}, |
| 597 | IsMapEntry: true, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 598 | }), |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 599 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 602 | var mapScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(MapScalars)), PBType: ptype.GoMessage( |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 603 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 604 | Syntax: pref.Proto2, |
| 605 | FullName: "MapScalars", |
| 606 | Fields: []ptype.Field{ |
| 607 | mustMakeMapEntry(1, pref.BoolKind, pref.StringKind), |
| 608 | mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind), |
| 609 | mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind), |
| 610 | mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind), |
| 611 | mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind), |
| 612 | mustMakeMapEntry(6, pref.StringKind, pref.StringKind), |
| 613 | |
| 614 | mustMakeMapEntry(7, pref.StringKind, pref.BoolKind), |
| 615 | mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind), |
| 616 | mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind), |
| 617 | mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind), |
| 618 | mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind), |
| 619 | mustMakeMapEntry(12, pref.StringKind, pref.FloatKind), |
| 620 | mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind), |
| 621 | mustMakeMapEntry(14, pref.StringKind, pref.StringKind), |
| 622 | mustMakeMapEntry(15, pref.StringKind, pref.StringKind), |
| 623 | mustMakeMapEntry(16, pref.StringKind, pref.BytesKind), |
| 624 | mustMakeMapEntry(17, pref.StringKind, pref.BytesKind), |
| 625 | |
| 626 | mustMakeMapEntry(18, pref.StringKind, pref.StringKind), |
| 627 | mustMakeMapEntry(19, pref.StringKind, pref.StringKind), |
| 628 | mustMakeMapEntry(20, pref.StringKind, pref.BytesKind), |
| 629 | mustMakeMapEntry(21, pref.StringKind, pref.BytesKind), |
| 630 | |
| 631 | mustMakeMapEntry(22, pref.StringKind, pref.StringKind), |
| 632 | mustMakeMapEntry(23, pref.StringKind, pref.StringKind), |
| 633 | mustMakeMapEntry(24, pref.StringKind, pref.BytesKind), |
| 634 | mustMakeMapEntry(25, pref.StringKind, pref.BytesKind), |
| 635 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 636 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 637 | func(pref.MessageType) pref.Message { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 638 | return new(MapScalars) |
| 639 | }, |
| 640 | )} |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 641 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 642 | func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 643 | func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) } |
| 644 | func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) } |
| 645 | func (m *MapScalars) Interface() pref.ProtoMessage { return m } |
| 646 | func (m *MapScalars) ProtoReflect() pref.Message { return m } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 647 | |
| 648 | func TestMapScalars(t *testing.T) { |
| 649 | empty := &MapScalars{} |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 650 | emptyFS := empty.KnownFields() |
| 651 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 652 | want := &MapScalars{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 653 | KeyBools: map[bool]string{true: "true", false: "false"}, |
| 654 | KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"}, |
| 655 | KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"}, |
| 656 | KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"}, |
| 657 | KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"}, |
| 658 | KeyStrings: map[string]string{"": "", "foo": "bar"}, |
| 659 | |
| 660 | ValBools: map[string]bool{"true": true, "false": false}, |
| 661 | ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3}, |
| 662 | ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30}, |
| 663 | ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead}, |
| 664 | ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead}, |
| 665 | ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)}, |
| 666 | ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)}, |
| 667 | ValStrings: map[string]string{"s1": "s1", "s2": "s2"}, |
| 668 | ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 669 | ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 670 | ValBytesA: map[string]string{"s1": "s1", "s2": "s2"}, |
| 671 | |
| 672 | MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"}, |
| 673 | MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 674 | MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 675 | MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"}, |
| 676 | |
| 677 | MyStrings3: MapStrings{"s1": "s1", "s2": "s2"}, |
| 678 | MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 679 | MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 680 | MyBytes4: MapStrings{"s1": "s1", "s2": "s2"}, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 681 | } |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 682 | wantFS := want.KnownFields() |
| 683 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 684 | testMessage(t, nil, &MapScalars{}, messageOps{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 685 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false}, |
| 686 | getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19), 21: emptyFS.Get(21), 23: emptyFS.Get(23), 25: emptyFS.Get(25)}, |
| 687 | setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19), 21: wantFS.Get(21), 23: wantFS.Get(23), 25: wantFS.Get(25)}, |
| 688 | mapFields{ |
| 689 | 2: { |
| 690 | lenMap(0), |
| 691 | hasMap{int32(0): false, int32(-1): false, int32(2): false}, |
| 692 | setMap{int32(0): V("zero")}, |
| 693 | lenMap(1), |
| 694 | hasMap{int32(0): true, int32(-1): false, int32(2): false}, |
| 695 | setMap{int32(-1): V("one")}, |
| 696 | lenMap(2), |
| 697 | hasMap{int32(0): true, int32(-1): true, int32(2): false}, |
| 698 | setMap{int32(2): V("two")}, |
| 699 | lenMap(3), |
| 700 | hasMap{int32(0): true, int32(-1): true, int32(2): true}, |
| 701 | }, |
| 702 | 4: { |
| 703 | setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 704 | equalMap{wantFS.Get(4).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 705 | }, |
| 706 | 6: { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 707 | clearMap{"noexist"}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 708 | setMap{"foo": V("bar")}, |
| 709 | setMap{"": V("empty")}, |
| 710 | getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)}, |
| 711 | setMap{"": V(""), "extra": V("extra")}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 712 | clearMap{"extra", "noexist"}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 713 | }, |
| 714 | 8: { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 715 | equalMap{emptyFS.Get(8).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 716 | setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))}, |
| 717 | }, |
| 718 | 10: { |
| 719 | setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))}, |
| 720 | lenMap(3), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 721 | equalMap{wantFS.Get(10).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 722 | getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)}, |
| 723 | }, |
| 724 | 12: { |
| 725 | setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 726 | clearMap{"e", "phi"}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 727 | rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))}, |
| 728 | }, |
| 729 | 14: { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 730 | equalMap{emptyFS.Get(14).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 731 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 732 | }, |
| 733 | 16: { |
| 734 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 735 | equalMap{wantFS.Get(16).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 736 | }, |
| 737 | 18: { |
| 738 | hasMap{"s1": false, "s2": false, "s3": false}, |
| 739 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 740 | hasMap{"s1": true, "s2": true, "s3": false}, |
| 741 | }, |
| 742 | 20: { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 743 | equalMap{emptyFS.Get(20).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 744 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
| 745 | }, |
| 746 | 22: { |
| 747 | rangeMap{}, |
| 748 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 749 | rangeMap{"s1": V("s1"), "s2": V("s2")}, |
| 750 | lenMap(2), |
| 751 | }, |
| 752 | 24: { |
| 753 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 754 | equalMap{wantFS.Get(24).Map()}, |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 755 | }, |
| 756 | }, |
| 757 | hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, 23: true, 24: true, 25: true}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 758 | equalMessage{want}, |
| 759 | clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, |
| 760 | equalMessage{empty}, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 761 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 762 | |
| 763 | // Test read-only operations on nil message. |
| 764 | testMessage(t, nil, (*MapScalars)(nil), messageOps{ |
| 765 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false}, |
| 766 | mapFields{2: {lenMap(0)}, 4: {lenMap(0)}, 6: {lenMap(0)}, 8: {lenMap(0)}, 10: {lenMap(0)}, 12: {lenMap(0)}, 14: {lenMap(0)}, 16: {lenMap(0)}, 18: {lenMap(0)}, 20: {lenMap(0)}, 22: {lenMap(0)}, 24: {lenMap(0)}}, |
| 767 | }) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 768 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 769 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 770 | type OneofScalars struct { |
| 771 | Union isOneofScalars_Union `protobuf_oneof:"union"` |
| 772 | } |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 773 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 774 | var oneofScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(OneofScalars)), PBType: ptype.GoMessage( |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 775 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 776 | Syntax: pref.Proto2, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 777 | FullName: "OneofScalars", |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 778 | Fields: []ptype.Field{ |
| 779 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"}, |
| 780 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"}, |
| 781 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"}, |
| 782 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"}, |
| 783 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"}, |
| 784 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"}, |
| 785 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"}, |
| 786 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"}, |
| 787 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"}, |
| 788 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"}, |
| 789 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"}, |
| 790 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"}, |
| 791 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"}, |
| 792 | }, |
| 793 | Oneofs: []ptype.Oneof{{Name: "union"}}, |
| 794 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 795 | func(pref.MessageType) pref.Message { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 796 | return new(OneofScalars) |
| 797 | }, |
| 798 | )} |
| 799 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 800 | func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 801 | func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) } |
| 802 | func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) } |
| 803 | func (m *OneofScalars) Interface() pref.ProtoMessage { return m } |
| 804 | func (m *OneofScalars) ProtoReflect() pref.Message { return m } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 805 | |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 806 | func (*OneofScalars) XXX_OneofWrappers() []interface{} { |
| 807 | return []interface{}{ |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 808 | (*OneofScalars_Bool)(nil), |
| 809 | (*OneofScalars_Int32)(nil), |
| 810 | (*OneofScalars_Int64)(nil), |
| 811 | (*OneofScalars_Uint32)(nil), |
| 812 | (*OneofScalars_Uint64)(nil), |
| 813 | (*OneofScalars_Float32)(nil), |
| 814 | (*OneofScalars_Float64)(nil), |
| 815 | (*OneofScalars_String)(nil), |
| 816 | (*OneofScalars_StringA)(nil), |
| 817 | (*OneofScalars_StringB)(nil), |
| 818 | (*OneofScalars_Bytes)(nil), |
| 819 | (*OneofScalars_BytesA)(nil), |
| 820 | (*OneofScalars_BytesB)(nil), |
| 821 | } |
| 822 | } |
| 823 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 824 | type ( |
| 825 | isOneofScalars_Union interface { |
| 826 | isOneofScalars_Union() |
| 827 | } |
| 828 | OneofScalars_Bool struct { |
| 829 | Bool bool `protobuf:"1"` |
| 830 | } |
| 831 | OneofScalars_Int32 struct { |
| 832 | Int32 MyInt32 `protobuf:"2"` |
| 833 | } |
| 834 | OneofScalars_Int64 struct { |
| 835 | Int64 int64 `protobuf:"3"` |
| 836 | } |
| 837 | OneofScalars_Uint32 struct { |
| 838 | Uint32 MyUint32 `protobuf:"4"` |
| 839 | } |
| 840 | OneofScalars_Uint64 struct { |
| 841 | Uint64 uint64 `protobuf:"5"` |
| 842 | } |
| 843 | OneofScalars_Float32 struct { |
| 844 | Float32 MyFloat32 `protobuf:"6"` |
| 845 | } |
| 846 | OneofScalars_Float64 struct { |
| 847 | Float64 float64 `protobuf:"7"` |
| 848 | } |
| 849 | OneofScalars_String struct { |
| 850 | String string `protobuf:"8"` |
| 851 | } |
| 852 | OneofScalars_StringA struct { |
| 853 | StringA []byte `protobuf:"9"` |
| 854 | } |
| 855 | OneofScalars_StringB struct { |
| 856 | StringB MyString `protobuf:"10"` |
| 857 | } |
| 858 | OneofScalars_Bytes struct { |
| 859 | Bytes []byte `protobuf:"11"` |
| 860 | } |
| 861 | OneofScalars_BytesA struct { |
| 862 | BytesA string `protobuf:"12"` |
| 863 | } |
| 864 | OneofScalars_BytesB struct { |
| 865 | BytesB MyBytes `protobuf:"13"` |
| 866 | } |
| 867 | ) |
| 868 | |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 869 | func (*OneofScalars_Bool) isOneofScalars_Union() {} |
| 870 | func (*OneofScalars_Int32) isOneofScalars_Union() {} |
| 871 | func (*OneofScalars_Int64) isOneofScalars_Union() {} |
| 872 | func (*OneofScalars_Uint32) isOneofScalars_Union() {} |
| 873 | func (*OneofScalars_Uint64) isOneofScalars_Union() {} |
| 874 | func (*OneofScalars_Float32) isOneofScalars_Union() {} |
| 875 | func (*OneofScalars_Float64) isOneofScalars_Union() {} |
| 876 | func (*OneofScalars_String) isOneofScalars_Union() {} |
| 877 | func (*OneofScalars_StringA) isOneofScalars_Union() {} |
| 878 | func (*OneofScalars_StringB) isOneofScalars_Union() {} |
| 879 | func (*OneofScalars_Bytes) isOneofScalars_Union() {} |
| 880 | func (*OneofScalars_BytesA) isOneofScalars_Union() {} |
| 881 | func (*OneofScalars_BytesB) isOneofScalars_Union() {} |
| 882 | |
| 883 | func TestOneofs(t *testing.T) { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 884 | empty := &OneofScalars{} |
| 885 | want1 := &OneofScalars{Union: &OneofScalars_Bool{true}} |
| 886 | want2 := &OneofScalars{Union: &OneofScalars_Int32{20}} |
| 887 | want3 := &OneofScalars{Union: &OneofScalars_Int64{30}} |
| 888 | want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}} |
| 889 | want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}} |
| 890 | want6 := &OneofScalars{Union: &OneofScalars_Float32{60}} |
| 891 | want7 := &OneofScalars{Union: &OneofScalars_Float64{70}} |
| 892 | want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}} |
| 893 | want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}} |
| 894 | want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}} |
| 895 | want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}} |
| 896 | want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}} |
| 897 | want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}} |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 898 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 899 | testMessage(t, nil, &OneofScalars{}, messageOps{ |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 900 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false}, |
| 901 | getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))}, |
| 902 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 903 | setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1}, |
| 904 | setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2}, |
| 905 | setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3}, |
| 906 | setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4}, |
| 907 | setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5}, |
| 908 | setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6}, |
| 909 | setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7}, |
| 910 | setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8}, |
| 911 | setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9}, |
| 912 | setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10}, |
| 913 | setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11}, |
| 914 | setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12}, |
| 915 | setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13}, |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 916 | |
| 917 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: true}, |
| 918 | getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("130"))}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 919 | clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, |
| 920 | equalMessage{want13}, |
| 921 | clearFields{13}, |
| 922 | equalMessage{empty}, |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 923 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 924 | |
| 925 | // Test read-only operations on nil message. |
| 926 | testMessage(t, nil, (*OneofScalars)(nil), messageOps{ |
| 927 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false}, |
| 928 | getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))}, |
| 929 | }) |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 932 | type EnumProto2 int32 |
| 933 | |
| 934 | var enumProto2Type = ptype.GoEnum( |
| 935 | mustMakeEnumDesc(ptype.StandaloneEnum{ |
| 936 | Syntax: pref.Proto2, |
| 937 | FullName: "EnumProto2", |
| 938 | Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}}, |
| 939 | }), |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 940 | func(_ pref.EnumType, n pref.EnumNumber) pref.Enum { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 941 | return EnumProto2(n) |
| 942 | }, |
| 943 | ) |
| 944 | |
| 945 | func (e EnumProto2) Enum() *EnumProto2 { return &e } |
| 946 | func (e EnumProto2) Type() pref.EnumType { return enumProto2Type } |
| 947 | func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 948 | |
| 949 | type EnumProto3 int32 |
| 950 | |
| 951 | var enumProto3Type = ptype.GoEnum( |
| 952 | mustMakeEnumDesc(ptype.StandaloneEnum{ |
| 953 | Syntax: pref.Proto3, |
| 954 | FullName: "EnumProto3", |
| 955 | Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}}, |
| 956 | }), |
Damien Neil | a8593ba | 2019-01-08 16:18:07 -0800 | [diff] [blame] | 957 | func(_ pref.EnumType, n pref.EnumNumber) pref.Enum { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 958 | return EnumProto3(n) |
| 959 | }, |
| 960 | ) |
| 961 | |
| 962 | func (e EnumProto3) Enum() *EnumProto3 { return &e } |
| 963 | func (e EnumProto3) Type() pref.EnumType { return enumProto3Type } |
| 964 | func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 965 | |
| 966 | type EnumMessages struct { |
| 967 | EnumP2 *EnumProto2 `protobuf:"1"` |
| 968 | EnumP3 *EnumProto3 `protobuf:"2"` |
| 969 | MessageLegacy *proto2_20180125.Message `protobuf:"3"` |
| 970 | MessageCycle *EnumMessages `protobuf:"4"` |
| 971 | EnumList []EnumProto2 `protobuf:"5"` |
| 972 | MessageList []*ScalarProto2 `protobuf:"6"` |
| 973 | EnumMap map[string]EnumProto3 `protobuf:"7"` |
| 974 | MessageMap map[string]*ScalarProto3 `protobuf:"8"` |
| 975 | Union isEnumMessages_Union `protobuf_oneof:"union"` |
| 976 | } |
| 977 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 978 | var enumMessagesType = pimpl.MessageType{GoType: reflect.TypeOf(new(EnumMessages)), PBType: ptype.GoMessage( |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 979 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 980 | Syntax: pref.Proto2, |
| 981 | FullName: "EnumMessages", |
| 982 | Fields: []ptype.Field{ |
| 983 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type}, |
| 984 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type}, |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 985 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: pimpl.Export{}.MessageOf(new(proto2_20180125.Message)).Type()}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 986 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")}, |
| 987 | {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type}, |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 988 | {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 989 | {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc}, |
| 990 | {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc}, |
| 991 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type}, |
| 992 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type}, |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 993 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType}, |
| 994 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 995 | }, |
| 996 | Oneofs: []ptype.Oneof{{Name: "union"}}, |
| 997 | }), |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 998 | func(pref.MessageType) pref.Message { |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 999 | return new(EnumMessages) |
| 1000 | }, |
| 1001 | )} |
| 1002 | |
| 1003 | var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 1004 | Syntax: pref.Proto2, |
| 1005 | FullName: "EnumMessages.F7Entry", |
| 1006 | Fields: []ptype.Field{ |
| 1007 | {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 1008 | {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type}, |
| 1009 | }, |
Damien Neil | 232ea15 | 2018-12-10 15:14:36 -0800 | [diff] [blame] | 1010 | Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)}, |
| 1011 | IsMapEntry: true, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1012 | }) |
| 1013 | |
| 1014 | var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 1015 | Syntax: pref.Proto2, |
| 1016 | FullName: "EnumMessages.F8Entry", |
| 1017 | Fields: []ptype.Field{ |
| 1018 | {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind}, |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 1019 | {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1020 | }, |
Damien Neil | 232ea15 | 2018-12-10 15:14:36 -0800 | [diff] [blame] | 1021 | Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)}, |
| 1022 | IsMapEntry: true, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1023 | }) |
| 1024 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame^] | 1025 | func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1026 | func (m *EnumMessages) KnownFields() pref.KnownFields { return enumMessagesType.KnownFieldsOf(m) } |
| 1027 | func (m *EnumMessages) UnknownFields() pref.UnknownFields { return enumMessagesType.UnknownFieldsOf(m) } |
| 1028 | func (m *EnumMessages) Interface() pref.ProtoMessage { return m } |
| 1029 | func (m *EnumMessages) ProtoReflect() pref.Message { return m } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1030 | |
Joe Tsai | f18ab53 | 2018-11-27 17:25:04 -0800 | [diff] [blame] | 1031 | func (*EnumMessages) XXX_OneofWrappers() []interface{} { |
| 1032 | return []interface{}{ |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1033 | (*EnumMessages_OneofE2)(nil), |
| 1034 | (*EnumMessages_OneofE3)(nil), |
| 1035 | (*EnumMessages_OneofM2)(nil), |
| 1036 | (*EnumMessages_OneofM3)(nil), |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | type ( |
| 1041 | isEnumMessages_Union interface { |
| 1042 | isEnumMessages_Union() |
| 1043 | } |
| 1044 | EnumMessages_OneofE2 struct { |
| 1045 | OneofE2 EnumProto2 `protobuf:"9"` |
| 1046 | } |
| 1047 | EnumMessages_OneofE3 struct { |
| 1048 | OneofE3 EnumProto3 `protobuf:"10"` |
| 1049 | } |
| 1050 | EnumMessages_OneofM2 struct { |
| 1051 | OneofM2 *ScalarProto2 `protobuf:"11"` |
| 1052 | } |
| 1053 | EnumMessages_OneofM3 struct { |
| 1054 | OneofM3 *ScalarProto3 `protobuf:"12"` |
| 1055 | } |
| 1056 | ) |
| 1057 | |
| 1058 | func (*EnumMessages_OneofE2) isEnumMessages_Union() {} |
| 1059 | func (*EnumMessages_OneofE3) isEnumMessages_Union() {} |
| 1060 | func (*EnumMessages_OneofM2) isEnumMessages_Union() {} |
| 1061 | func (*EnumMessages_OneofM3) isEnumMessages_Union() {} |
| 1062 | |
| 1063 | func TestEnumMessages(t *testing.T) { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 1064 | wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)}) |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1065 | wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()} |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 1066 | wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)} |
| 1067 | wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)} |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1068 | wantM3a := &ScalarProto3{Float32: math.Pi} |
| 1069 | wantM3b := &ScalarProto3{Float32: math.Ln2} |
| 1070 | |
| 1071 | wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5) |
| 1072 | wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6) |
| 1073 | |
| 1074 | wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7) |
| 1075 | wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8) |
| 1076 | |
| 1077 | testMessage(t, nil, &EnumMessages{}, messageOps{ |
| 1078 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false}, |
Joe Tsai | f6d4a42 | 2018-11-19 14:26:06 -0800 | [diff] [blame] | 1079 | getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1)}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1080 | |
| 1081 | // Test singular enums. |
| 1082 | setFields{1: VE(0xdead), 2: VE(0)}, |
| 1083 | getFields{1: VE(0xdead), 2: VE(0)}, |
| 1084 | hasFields{1: true, 2: true}, |
| 1085 | |
| 1086 | // Test singular messages. |
| 1087 | messageFields{3: messageOps{setFields{109: V(float32(math.E))}}}, |
| 1088 | messageFields{4: messageOps{setFields{1: VE(1234)}}}, |
| 1089 | getFields{3: V(wantL), 4: V(wantM)}, |
| 1090 | clearFields{3, 4}, |
| 1091 | hasFields{3: false, 4: false}, |
| 1092 | setFields{3: V(wantL), 4: V(wantM)}, |
| 1093 | hasFields{3: true, 4: true}, |
| 1094 | |
| 1095 | // Test list of enums and messages. |
| 1096 | listFields{ |
| 1097 | 5: listOps{ |
| 1098 | appendList{VE(111), VE(222)}, |
| 1099 | setList{0: VE(333)}, |
| 1100 | getList{0: VE(333), 1: VE(222)}, |
| 1101 | lenList(2), |
| 1102 | }, |
| 1103 | 6: listOps{ |
| 1104 | appendMessageList{setFields{4: V(uint32(1e6))}}, |
| 1105 | appendMessageList{setFields{6: V(float32(math.Phi))}}, |
| 1106 | setList{0: V(wantM2a)}, |
| 1107 | getList{0: V(wantM2a), 1: V(wantM2b)}, |
| 1108 | }, |
| 1109 | }, |
| 1110 | getFields{5: wantList5, 6: wantList6}, |
| 1111 | hasFields{5: true, 6: true}, |
| 1112 | listFields{5: listOps{truncList(0)}}, |
| 1113 | hasFields{5: false, 6: true}, |
| 1114 | |
| 1115 | // Test maps of enums and messages. |
| 1116 | mapFields{ |
| 1117 | 7: mapOps{ |
| 1118 | setMap{"one": VE(1), "two": VE(2)}, |
| 1119 | hasMap{"one": true, "two": true, "three": false}, |
| 1120 | lenMap(2), |
| 1121 | }, |
| 1122 | 8: mapOps{ |
| 1123 | messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}}, |
| 1124 | setMap{"ln2": V(wantM3b)}, |
| 1125 | getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)}, |
| 1126 | lenMap(2), |
| 1127 | }, |
| 1128 | }, |
| 1129 | getFields{7: wantMap7, 8: wantMap8}, |
| 1130 | hasFields{7: true, 8: true}, |
| 1131 | mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}}, |
| 1132 | hasFields{7: true, 8: false}, |
| 1133 | |
| 1134 | // Test oneofs of enums and messages. |
| 1135 | setFields{9: VE(0xdead)}, |
| 1136 | hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false}, |
| 1137 | setFields{10: VE(0)}, |
| 1138 | hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false}, |
| 1139 | messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}}, |
| 1140 | getFields{11: V(wantM2a)}, |
| 1141 | hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false}, |
| 1142 | messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}}, |
| 1143 | getFields{12: V(wantM3a)}, |
| 1144 | hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true}, |
| 1145 | |
| 1146 | // Check entire message. |
| 1147 | rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)}, |
| 1148 | equalMessage{&EnumMessages{ |
| 1149 | EnumP2: EnumProto2(0xdead).Enum(), |
| 1150 | EnumP3: EnumProto3(0).Enum(), |
Joe Tsai | 009e067 | 2018-11-27 18:45:07 -0800 | [diff] [blame] | 1151 | MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)}, |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1152 | MessageCycle: wantM, |
| 1153 | MessageList: []*ScalarProto2{wantM2a, wantM2b}, |
| 1154 | EnumMap: map[string]EnumProto3{"one": 1, "two": 2}, |
| 1155 | Union: &EnumMessages_OneofM3{wantM3a}, |
| 1156 | }}, |
| 1157 | clearFields{1, 2, 3, 4, 6, 7, 12}, |
| 1158 | equalMessage{&EnumMessages{}}, |
| 1159 | }) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 1160 | |
| 1161 | // Test read-only operations on nil message. |
| 1162 | testMessage(t, nil, (*EnumMessages)(nil), messageOps{ |
| 1163 | hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false}, |
| 1164 | getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1), 11: V(nil), 12: V(nil)}, |
| 1165 | listFields{5: {lenList(0)}, 6: {lenList(0)}}, |
| 1166 | mapFields{7: {lenMap(0)}, 8: {lenMap(0)}}, |
| 1167 | }) |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1168 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 1169 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1170 | var cmpOpts = cmp.Options{ |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1171 | cmp.Comparer(func(x, y *proto2_20180125.Message) bool { |
| 1172 | return protoV1.Equal(x, y) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1173 | }), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1174 | cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} { |
| 1175 | return pv.Interface() |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1176 | }), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1177 | cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} { |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 1178 | return x.ProtoUnwrap() |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1179 | }), |
| 1180 | cmpopts.EquateNaNs(), |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1181 | cmpopts.EquateEmpty(), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) { |
| 1185 | fs := m.KnownFields() |
| 1186 | for i, op := range tt { |
| 1187 | p.Push(i) |
| 1188 | switch op := op.(type) { |
| 1189 | case equalMessage: |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1190 | if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" { |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1191 | t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff) |
| 1192 | } |
| 1193 | case hasFields: |
| 1194 | got := map[pref.FieldNumber]bool{} |
| 1195 | want := map[pref.FieldNumber]bool(op) |
| 1196 | for n := range want { |
| 1197 | got[n] = fs.Has(n) |
| 1198 | } |
| 1199 | if diff := cmp.Diff(want, got); diff != "" { |
| 1200 | t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff) |
| 1201 | } |
| 1202 | case getFields: |
| 1203 | got := map[pref.FieldNumber]pref.Value{} |
| 1204 | want := map[pref.FieldNumber]pref.Value(op) |
| 1205 | for n := range want { |
| 1206 | got[n] = fs.Get(n) |
| 1207 | } |
| 1208 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 1209 | t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff) |
| 1210 | } |
| 1211 | case setFields: |
| 1212 | for n, v := range op { |
| 1213 | fs.Set(n, v) |
| 1214 | } |
| 1215 | case clearFields: |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1216 | for _, n := range op { |
| 1217 | fs.Clear(n) |
| 1218 | } |
| 1219 | case messageFields: |
| 1220 | for n, tt := range op { |
| 1221 | p.Push(int(n)) |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 1222 | if !fs.Has(n) { |
| 1223 | fs.Set(n, V(fs.NewMessage(n))) |
| 1224 | } |
| 1225 | testMessage(t, p, fs.Get(n).Message(), tt) |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1226 | p.Pop() |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 1227 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1228 | case listFields: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1229 | for n, tt := range op { |
| 1230 | p.Push(int(n)) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 1231 | testLists(t, p, fs.Get(n).List(), tt) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1232 | p.Pop() |
| 1233 | } |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 1234 | case mapFields: |
| 1235 | for n, tt := range op { |
| 1236 | p.Push(int(n)) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 1237 | testMaps(t, p, fs.Get(n).Map(), tt) |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 1238 | p.Pop() |
| 1239 | } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1240 | case rangeFields: |
| 1241 | got := map[pref.FieldNumber]pref.Value{} |
| 1242 | want := map[pref.FieldNumber]pref.Value(op) |
| 1243 | fs.Range(func(n pref.FieldNumber, v pref.Value) bool { |
| 1244 | got[n] = v |
| 1245 | return true |
| 1246 | }) |
| 1247 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 1248 | t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff) |
| 1249 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1250 | default: |
| 1251 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 1252 | } |
| 1253 | p.Pop() |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1256 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1257 | func testLists(t *testing.T, p path, v pref.List, tt listOps) { |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1258 | for i, op := range tt { |
| 1259 | p.Push(i) |
| 1260 | switch op := op.(type) { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1261 | case equalList: |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1262 | if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1263 | t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1264 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1265 | case lenList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1266 | if got, want := v.Len(), int(op); got != want { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1267 | t.Errorf("operation %v, List.Len = %d, want %d", p, got, want) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1268 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1269 | case getList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1270 | got := map[int]pref.Value{} |
| 1271 | want := map[int]pref.Value(op) |
| 1272 | for n := range want { |
| 1273 | got[n] = v.Get(n) |
| 1274 | } |
| 1275 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1276 | t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1277 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1278 | case setList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1279 | for n, e := range op { |
| 1280 | v.Set(n, e) |
| 1281 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1282 | case appendList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1283 | for _, e := range op { |
| 1284 | v.Append(e) |
| 1285 | } |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1286 | case appendMessageList: |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 1287 | m := v.NewMessage() |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 1288 | v.Append(V(m)) |
| 1289 | testMessage(t, p, m, messageOps(op)) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 1290 | case truncList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1291 | v.Truncate(int(op)) |
| 1292 | default: |
| 1293 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 1294 | } |
| 1295 | p.Pop() |
| 1296 | } |
| 1297 | } |
| 1298 | |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 1299 | func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) { |
| 1300 | for i, op := range tt { |
| 1301 | p.Push(i) |
| 1302 | switch op := op.(type) { |
| 1303 | case equalMap: |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1304 | if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" { |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 1305 | t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff) |
| 1306 | } |
| 1307 | case lenMap: |
| 1308 | if got, want := m.Len(), int(op); got != want { |
| 1309 | t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want) |
| 1310 | } |
| 1311 | case hasMap: |
| 1312 | got := map[interface{}]bool{} |
| 1313 | want := map[interface{}]bool(op) |
| 1314 | for k := range want { |
| 1315 | got[k] = m.Has(V(k).MapKey()) |
| 1316 | } |
| 1317 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 1318 | t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff) |
| 1319 | } |
| 1320 | case getMap: |
| 1321 | got := map[interface{}]pref.Value{} |
| 1322 | want := map[interface{}]pref.Value(op) |
| 1323 | for k := range want { |
| 1324 | got[k] = m.Get(V(k).MapKey()) |
| 1325 | } |
| 1326 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 1327 | t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff) |
| 1328 | } |
| 1329 | case setMap: |
| 1330 | for k, v := range op { |
| 1331 | m.Set(V(k).MapKey(), v) |
| 1332 | } |
| 1333 | case clearMap: |
Joe Tsai | 87b955b | 2018-11-14 21:59:49 -0800 | [diff] [blame] | 1334 | for _, k := range op { |
| 1335 | m.Clear(V(k).MapKey()) |
| 1336 | } |
| 1337 | case messageMap: |
| 1338 | for k, tt := range op { |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 1339 | mk := V(k).MapKey() |
| 1340 | if !m.Has(mk) { |
| 1341 | m.Set(mk, V(m.NewMessage())) |
| 1342 | } |
| 1343 | testMessage(t, p, m.Get(mk).Message(), tt) |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 1344 | } |
| 1345 | case rangeMap: |
| 1346 | got := map[interface{}]pref.Value{} |
| 1347 | want := map[interface{}]pref.Value(op) |
| 1348 | m.Range(func(k pref.MapKey, v pref.Value) bool { |
| 1349 | got[k.Interface()] = v |
| 1350 | return true |
| 1351 | }) |
| 1352 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 1353 | t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff) |
| 1354 | } |
| 1355 | default: |
| 1356 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 1357 | } |
| 1358 | p.Pop() |
| 1359 | } |
| 1360 | } |
| 1361 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 1362 | type path []int |
| 1363 | |
| 1364 | func (p *path) Push(i int) { *p = append(*p, i) } |
| 1365 | func (p *path) Pop() { *p = (*p)[:len(*p)-1] } |
| 1366 | func (p path) String() string { |
| 1367 | var ss []string |
| 1368 | for _, i := range p { |
| 1369 | ss = append(ss, fmt.Sprint(i)) |
| 1370 | } |
| 1371 | return strings.Join(ss, ".") |
| 1372 | } |