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