blob: ef71ca1e9cf88404d00df9aa756c7a7289d22981 [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// 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 Tsai08e00302018-11-26 22:32:06 -08005package impl_test
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7import (
Joe Tsai91e14662018-09-13 13:24:35 -07008 "fmt"
9 "math"
Damien Neil8012b442019-01-18 09:32:24 -080010 "reflect"
Joe Tsai91e14662018-09-13 13:24:35 -070011 "strings"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070012 "testing"
13
Damien Neil204f1c02018-10-23 15:03:38 -070014 protoV1 "github.com/golang/protobuf/proto"
Joe Tsai08e00302018-11-26 22:32:06 -080015 pimpl "github.com/golang/protobuf/v2/internal/impl"
Joe Tsai990b9f52019-03-13 12:56:39 -070016 ptype "github.com/golang/protobuf/v2/internal/prototype"
Joe Tsai009e0672018-11-27 18:45:07 -080017 scalar "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsai08e00302018-11-26 22:32:06 -080018 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai01ab2962018-09-21 17:44:00 -070019 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai87b955b2018-11-14 21:59:49 -080020 cmp "github.com/google/go-cmp/cmp"
21 cmpopts "github.com/google/go-cmp/cmp/cmpopts"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070022
Joe Tsai08e00302018-11-26 22:32:06 -080023 // 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 Tsai87b955b2018-11-14 21:59:49 -080027 proto2_20180125 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsaie1f8d502018-11-26 18:55:29 -080028 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070029)
30
Joe Tsai4b7aff62018-11-14 14:05:19 -080031// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070032type (
Joe Tsai87b955b2018-11-14 21:59:49 -080033 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070034 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070035
Joe Tsai87b955b2018-11-14 21:59:49 -080036 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080037 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070038
Joe Tsai87b955b2018-11-14 21:59:49 -080039 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000040 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070041)
42
43// Test operations performed on a message.
44type (
Joe Tsai87b955b2018-11-14 21:59:49 -080045 // 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
Joe Tsai4ec39c72019-04-03 13:40:53 -070055 // check for the presence of specific oneof member fields.
56 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080057 // apply messageOps on each specified message field
Joe Tsai91e14662018-09-13 13:24:35 -070058 messageFields map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080059 // apply listOps on each specified list field
60 listFields map[pref.FieldNumber]listOps
61 // apply mapOps on each specified map fields
62 mapFields map[pref.FieldNumber]mapOps
63 // range through all fields and check that they match
64 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070065)
66
Joe Tsai87b955b2018-11-14 21:59:49 -080067func (equalMessage) isMessageOp() {}
68func (hasFields) isMessageOp() {}
69func (getFields) isMessageOp() {}
70func (setFields) isMessageOp() {}
71func (clearFields) isMessageOp() {}
Joe Tsai4ec39c72019-04-03 13:40:53 -070072func (whichOneofs) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080073func (messageFields) isMessageOp() {}
74func (listFields) isMessageOp() {}
75func (mapFields) isMessageOp() {}
76func (rangeFields) isMessageOp() {}
77
Joe Tsai4b7aff62018-11-14 14:05:19 -080078// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070079type (
Joe Tsai87b955b2018-11-14 21:59:49 -080080 // check that the list contents match
81 equalList struct{ pref.List }
82 // check that list length matches
83 lenList int
84 // check that specific list entries match
85 getList map[int]pref.Value
86 // set specific list entries
87 setList map[int]pref.Value
88 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080089 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080090 // apply messageOps on a newly appended message
91 appendMessageList messageOps
92 // truncate the list to the specified length
93 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070094)
95
Joe Tsai87b955b2018-11-14 21:59:49 -080096func (equalList) isListOp() {}
97func (lenList) isListOp() {}
98func (getList) isListOp() {}
99func (setList) isListOp() {}
100func (appendList) isListOp() {}
101func (appendMessageList) isListOp() {}
102func (truncList) isListOp() {}
103
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000104// Test operations performed on a map.
105type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800106 // check that the map contents match
107 equalMap struct{ pref.Map }
108 // check that map length matches
109 lenMap int
110 // check presence for specific entries in the map
111 hasMap map[interface{}]bool
112 // check that specific map entries match
113 getMap map[interface{}]pref.Value
114 // set specific map entries
115 setMap map[interface{}]pref.Value
116 // clear specific entries in the map
117 clearMap []interface{}
118 // apply messageOps on each specified message entry
119 messageMap map[interface{}]messageOps
120 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000121 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000122)
123
Joe Tsai87b955b2018-11-14 21:59:49 -0800124func (equalMap) isMapOp() {}
125func (lenMap) isMapOp() {}
126func (hasMap) isMapOp() {}
127func (getMap) isMapOp() {}
128func (setMap) isMapOp() {}
129func (clearMap) isMapOp() {}
130func (messageMap) isMapOp() {}
131func (rangeMap) isMapOp() {}
132
Joe Tsaice6edd32018-10-19 16:27:46 -0700133type ScalarProto2 struct {
134 Bool *bool `protobuf:"1"`
135 Int32 *int32 `protobuf:"2"`
136 Int64 *int64 `protobuf:"3"`
137 Uint32 *uint32 `protobuf:"4"`
138 Uint64 *uint64 `protobuf:"5"`
139 Float32 *float32 `protobuf:"6"`
140 Float64 *float64 `protobuf:"7"`
141 String *string `protobuf:"8"`
142 StringA []byte `protobuf:"9"`
143 Bytes []byte `protobuf:"10"`
144 BytesA *string `protobuf:"11"`
145
146 MyBool *MyBool `protobuf:"12"`
147 MyInt32 *MyInt32 `protobuf:"13"`
148 MyInt64 *MyInt64 `protobuf:"14"`
149 MyUint32 *MyUint32 `protobuf:"15"`
150 MyUint64 *MyUint64 `protobuf:"16"`
151 MyFloat32 *MyFloat32 `protobuf:"17"`
152 MyFloat64 *MyFloat64 `protobuf:"18"`
153 MyString *MyString `protobuf:"19"`
154 MyStringA MyBytes `protobuf:"20"`
155 MyBytes MyBytes `protobuf:"21"`
156 MyBytesA *MyString `protobuf:"22"`
157}
158
Joe Tsai87b955b2018-11-14 21:59:49 -0800159func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
160 ed, err := ptype.NewEnum(&t)
161 if err != nil {
162 panic(err)
163 }
164 return ed
165}
166
167func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
168 md, err := ptype.NewMessage(&t)
169 if err != nil {
170 panic(err)
171 }
172 return md
173}
174
175var V = pref.ValueOf
176var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
177
178type (
179 MyBool bool
180 MyInt32 int32
181 MyInt64 int64
182 MyUint32 uint32
183 MyUint64 uint64
184 MyFloat32 float32
185 MyFloat64 float64
186 MyString string
187 MyBytes []byte
188
189 ListStrings []MyString
190 ListBytes []MyBytes
191
192 MapStrings map[MyString]MyString
193 MapBytes map[MyString]MyBytes
194)
195
Damien Neil8012b442019-01-18 09:32:24 -0800196var scalarProto2Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800197 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700198 Syntax: pref.Proto2,
199 FullName: "ScalarProto2",
200 Fields: []ptype.Field{
201 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
202 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
203 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
204 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
205 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
206 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
207 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
208 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
209 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
210 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
211 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
212
213 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
214 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
215 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
216 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
217 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
218 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
219 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
220 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
221 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
222 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
223 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
224 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800225 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800226 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800227 return new(ScalarProto2)
228 },
229)}
Joe Tsai91e14662018-09-13 13:24:35 -0700230
Damien Neil374cdb82019-01-29 16:45:30 -0800231func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.PBType }
232func (m *ScalarProto2) KnownFields() pref.KnownFields {
233 return scalarProto2Type.MessageOf(m).KnownFields()
234}
235func (m *ScalarProto2) UnknownFields() pref.UnknownFields {
236 return scalarProto2Type.MessageOf(m).UnknownFields()
237}
238func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
239func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800240
241func TestScalarProto2(t *testing.T) {
242 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700243 hasFields{
244 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
245 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
246 },
247 getFields{
248 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")),
249 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")),
250 },
251 setFields{
252 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)),
253 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)),
254 },
255 hasFields{
256 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
257 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
258 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800259 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700260 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
261 new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
Joe Tsai87b955b2018-11-14 21:59:49 -0800262 }},
263 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
264 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700265 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800266
267 // Test read-only operations on nil message.
268 testMessage(t, nil, (*ScalarProto2)(nil), messageOps{
269 hasFields{
270 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
271 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
272 },
273 getFields{
274 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")),
275 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")),
276 },
277 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700278}
279
Joe Tsaice6edd32018-10-19 16:27:46 -0700280type ScalarProto3 struct {
281 Bool bool `protobuf:"1"`
282 Int32 int32 `protobuf:"2"`
283 Int64 int64 `protobuf:"3"`
284 Uint32 uint32 `protobuf:"4"`
285 Uint64 uint64 `protobuf:"5"`
286 Float32 float32 `protobuf:"6"`
287 Float64 float64 `protobuf:"7"`
288 String string `protobuf:"8"`
289 StringA []byte `protobuf:"9"`
290 Bytes []byte `protobuf:"10"`
291 BytesA string `protobuf:"11"`
292
293 MyBool MyBool `protobuf:"12"`
294 MyInt32 MyInt32 `protobuf:"13"`
295 MyInt64 MyInt64 `protobuf:"14"`
296 MyUint32 MyUint32 `protobuf:"15"`
297 MyUint64 MyUint64 `protobuf:"16"`
298 MyFloat32 MyFloat32 `protobuf:"17"`
299 MyFloat64 MyFloat64 `protobuf:"18"`
300 MyString MyString `protobuf:"19"`
301 MyStringA MyBytes `protobuf:"20"`
302 MyBytes MyBytes `protobuf:"21"`
303 MyBytesA MyString `protobuf:"22"`
304}
305
Damien Neil8012b442019-01-18 09:32:24 -0800306var scalarProto3Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800307 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700308 Syntax: pref.Proto3,
309 FullName: "ScalarProto3",
310 Fields: []ptype.Field{
311 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
312 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
313 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
314 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
315 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
316 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
317 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
318 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
319 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
320 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
321 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700322
Joe Tsai91e14662018-09-13 13:24:35 -0700323 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
324 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
325 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
326 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
327 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
328 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
329 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
330 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
331 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
332 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
333 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
334 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800335 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800336 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800337 return new(ScalarProto3)
338 },
339)}
Joe Tsai91e14662018-09-13 13:24:35 -0700340
Damien Neil374cdb82019-01-29 16:45:30 -0800341func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.PBType }
342func (m *ScalarProto3) KnownFields() pref.KnownFields {
343 return scalarProto3Type.MessageOf(m).KnownFields()
344}
345func (m *ScalarProto3) UnknownFields() pref.UnknownFields {
346 return scalarProto3Type.MessageOf(m).UnknownFields()
347}
348func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
349func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800350
351func TestScalarProto3(t *testing.T) {
352 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700353 hasFields{
354 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
355 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
356 },
357 getFields{
358 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)),
359 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)),
360 },
361 setFields{
362 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)),
363 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)),
364 },
365 hasFields{
366 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
367 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
368 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800369 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700370 setFields{
371 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")),
372 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")),
373 },
374 hasFields{
375 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
376 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
377 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800378 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700379 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
380 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800381 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800382 setFields{
383 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
384 },
385 hasFields{
386 2: true, 3: true, 6: true, 7: true,
387 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800388 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
389 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700390 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800391
392 // Test read-only operations on nil message.
393 testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
394 hasFields{
395 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
396 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
397 },
398 getFields{
399 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)),
400 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)),
401 },
402 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700403}
404
Joe Tsaif0c01e42018-11-06 13:05:20 -0800405type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700406 Bools []bool `protobuf:"1"`
407 Int32s []int32 `protobuf:"2"`
408 Int64s []int64 `protobuf:"3"`
409 Uint32s []uint32 `protobuf:"4"`
410 Uint64s []uint64 `protobuf:"5"`
411 Float32s []float32 `protobuf:"6"`
412 Float64s []float64 `protobuf:"7"`
413 Strings []string `protobuf:"8"`
414 StringsA [][]byte `protobuf:"9"`
415 Bytes [][]byte `protobuf:"10"`
416 BytesA []string `protobuf:"11"`
417
418 MyStrings1 []MyString `protobuf:"12"`
419 MyStrings2 []MyBytes `protobuf:"13"`
420 MyBytes1 []MyBytes `protobuf:"14"`
421 MyBytes2 []MyString `protobuf:"15"`
422
Joe Tsai4b7aff62018-11-14 14:05:19 -0800423 MyStrings3 ListStrings `protobuf:"16"`
424 MyStrings4 ListBytes `protobuf:"17"`
425 MyBytes3 ListBytes `protobuf:"18"`
426 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700427}
428
Damien Neil8012b442019-01-18 09:32:24 -0800429var listScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(ListScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800430 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700431 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800432 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700433 Fields: []ptype.Field{
434 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
435 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
436 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
437 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
438 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
439 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
440 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
441 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
442 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
443 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
444 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700445
Joe Tsai91e14662018-09-13 13:24:35 -0700446 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
447 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
448 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
449 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
450
451 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
452 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
453 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
454 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700455 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800456 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800457 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800458 return new(ListScalars)
459 },
460)}
Joe Tsai91e14662018-09-13 13:24:35 -0700461
Damien Neil374cdb82019-01-29 16:45:30 -0800462func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType }
463func (m *ListScalars) KnownFields() pref.KnownFields {
464 return listScalarsType.MessageOf(m).KnownFields()
465}
466func (m *ListScalars) UnknownFields() pref.UnknownFields {
467 return listScalarsType.MessageOf(m).UnknownFields()
468}
469func (m *ListScalars) Interface() pref.ProtoMessage { return m }
470func (m *ListScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800471
472func TestListScalars(t *testing.T) {
473 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700474 emptyFS := empty.KnownFields()
475
Joe Tsaif0c01e42018-11-06 13:05:20 -0800476 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700477 Bools: []bool{true, false, true},
478 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
479 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
480 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
481 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
482 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
483 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
484 Strings: []string{"8", "", "eight"},
485 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
486 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
487 BytesA: []string{"11", "", "eleven"},
488
489 MyStrings1: []MyString{"12", "", "twelve"},
490 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
491 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
492 MyBytes2: []MyString{"15", "", "fifteen"},
493
Joe Tsai4b7aff62018-11-14 14:05:19 -0800494 MyStrings3: ListStrings{"16", "", "sixteen"},
495 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
496 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
497 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800498 }
Joe Tsai91e14662018-09-13 13:24:35 -0700499 wantFS := want.KnownFields()
500
Joe Tsaif0c01e42018-11-06 13:05:20 -0800501 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700502 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},
503 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)},
504 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 Tsai4b7aff62018-11-14 14:05:19 -0800505 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700506 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800507 lenList(0),
508 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
509 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800510 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700511 },
512 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800513 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
514 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
515 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700516 },
517 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800518 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800519 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700520 },
521 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800522 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
523 lenList(6),
524 setList{0: V("8"), 2: V("eight")},
525 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800526 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700527 },
528 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800529 appendList{V([]byte(nil)), V([]byte(nil))},
530 setList{0: V([]byte("10"))},
531 appendList{V([]byte("wrong"))},
532 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800533 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700534 },
535 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800536 appendList{V("12"), V("wrong"), V("twelve")},
537 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800538 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700539 },
540 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800541 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800542 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700543 },
544 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800545 appendList{V("16"), V(""), V("sixteen"), V("extra")},
546 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800547 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700548 },
549 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800550 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800551 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700552 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700553 },
Joe Tsai91e14662018-09-13 13:24:35 -0700554 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 Tsai87b955b2018-11-14 21:59:49 -0800555 equalMessage{want},
556 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
557 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000558 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800559
560 // Test read-only operations on nil message.
561 testMessage(t, nil, (*ListScalars)(nil), messageOps{
562 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},
563 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)}},
564 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000565}
566
Joe Tsaice6edd32018-10-19 16:27:46 -0700567type MapScalars struct {
568 KeyBools map[bool]string `protobuf:"1"`
569 KeyInt32s map[int32]string `protobuf:"2"`
570 KeyInt64s map[int64]string `protobuf:"3"`
571 KeyUint32s map[uint32]string `protobuf:"4"`
572 KeyUint64s map[uint64]string `protobuf:"5"`
573 KeyStrings map[string]string `protobuf:"6"`
574
575 ValBools map[string]bool `protobuf:"7"`
576 ValInt32s map[string]int32 `protobuf:"8"`
577 ValInt64s map[string]int64 `protobuf:"9"`
578 ValUint32s map[string]uint32 `protobuf:"10"`
579 ValUint64s map[string]uint64 `protobuf:"11"`
580 ValFloat32s map[string]float32 `protobuf:"12"`
581 ValFloat64s map[string]float64 `protobuf:"13"`
582 ValStrings map[string]string `protobuf:"14"`
583 ValStringsA map[string][]byte `protobuf:"15"`
584 ValBytes map[string][]byte `protobuf:"16"`
585 ValBytesA map[string]string `protobuf:"17"`
586
587 MyStrings1 map[MyString]MyString `protobuf:"18"`
588 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
589 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
590 MyBytes2 map[MyString]MyString `protobuf:"21"`
591
592 MyStrings3 MapStrings `protobuf:"22"`
593 MyStrings4 MapBytes `protobuf:"23"`
594 MyBytes3 MapBytes `protobuf:"24"`
595 MyBytes4 MapStrings `protobuf:"25"`
596}
597
Joe Tsaif0c01e42018-11-06 13:05:20 -0800598func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
599 return ptype.Field{
600 Name: pref.Name(fmt.Sprintf("f%d", n)),
601 Number: n,
602 Cardinality: pref.Repeated,
603 Kind: pref.MessageKind,
604 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
605 Syntax: pref.Proto2,
606 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
607 Fields: []ptype.Field{
608 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
609 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
610 },
Damien Neil232ea152018-12-10 15:14:36 -0800611 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
612 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800613 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000614 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800615}
616
Damien Neil8012b442019-01-18 09:32:24 -0800617var mapScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(MapScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800618 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000619 Syntax: pref.Proto2,
620 FullName: "MapScalars",
621 Fields: []ptype.Field{
622 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
623 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
624 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
625 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
626 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
627 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
628
629 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
630 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
631 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
632 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
633 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
634 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
635 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
636 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
637 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
638 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
639 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
640
641 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
642 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
643 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
644 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
645
646 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
647 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
648 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
649 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
650 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800651 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800652 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800653 return new(MapScalars)
654 },
655)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000656
Damien Neil374cdb82019-01-29 16:45:30 -0800657func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType }
658func (m *MapScalars) KnownFields() pref.KnownFields {
659 return mapScalarsType.MessageOf(m).KnownFields()
660}
661func (m *MapScalars) UnknownFields() pref.UnknownFields {
662 return mapScalarsType.MessageOf(m).UnknownFields()
663}
664func (m *MapScalars) Interface() pref.ProtoMessage { return m }
665func (m *MapScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800666
667func TestMapScalars(t *testing.T) {
668 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000669 emptyFS := empty.KnownFields()
670
Joe Tsaif0c01e42018-11-06 13:05:20 -0800671 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000672 KeyBools: map[bool]string{true: "true", false: "false"},
673 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
674 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
675 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
676 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
677 KeyStrings: map[string]string{"": "", "foo": "bar"},
678
679 ValBools: map[string]bool{"true": true, "false": false},
680 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
681 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
682 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
683 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
684 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
685 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
686 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
687 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
688 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
689 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
690
691 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
692 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
693 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
694 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
695
696 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
697 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
698 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
699 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800700 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000701 wantFS := want.KnownFields()
702
Joe Tsaif0c01e42018-11-06 13:05:20 -0800703 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000704 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},
705 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)},
706 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)},
707 mapFields{
708 2: {
709 lenMap(0),
710 hasMap{int32(0): false, int32(-1): false, int32(2): false},
711 setMap{int32(0): V("zero")},
712 lenMap(1),
713 hasMap{int32(0): true, int32(-1): false, int32(2): false},
714 setMap{int32(-1): V("one")},
715 lenMap(2),
716 hasMap{int32(0): true, int32(-1): true, int32(2): false},
717 setMap{int32(2): V("two")},
718 lenMap(3),
719 hasMap{int32(0): true, int32(-1): true, int32(2): true},
720 },
721 4: {
722 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800723 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000724 },
725 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800726 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000727 setMap{"foo": V("bar")},
728 setMap{"": V("empty")},
729 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
730 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800731 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 },
733 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800734 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000735 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
736 },
737 10: {
738 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
739 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800740 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000741 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
742 },
743 12: {
744 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800745 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000746 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
747 },
748 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800749 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000750 setMap{"s1": V("s1"), "s2": V("s2")},
751 },
752 16: {
753 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800754 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000755 },
756 18: {
757 hasMap{"s1": false, "s2": false, "s3": false},
758 setMap{"s1": V("s1"), "s2": V("s2")},
759 hasMap{"s1": true, "s2": true, "s3": false},
760 },
761 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800762 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000763 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
764 },
765 22: {
766 rangeMap{},
767 setMap{"s1": V("s1"), "s2": V("s2")},
768 rangeMap{"s1": V("s1"), "s2": V("s2")},
769 lenMap(2),
770 },
771 24: {
772 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800773 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000774 },
775 },
776 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 Tsai87b955b2018-11-14 21:59:49 -0800777 equalMessage{want},
778 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},
779 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700780 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800781
782 // Test read-only operations on nil message.
783 testMessage(t, nil, (*MapScalars)(nil), messageOps{
784 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},
785 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)}},
786 })
Joe Tsai91e14662018-09-13 13:24:35 -0700787}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700788
Joe Tsai87b955b2018-11-14 21:59:49 -0800789type OneofScalars struct {
790 Union isOneofScalars_Union `protobuf_oneof:"union"`
791}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700792
Damien Neil8012b442019-01-18 09:32:24 -0800793var oneofScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(OneofScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800794 mustMakeMessageDesc(ptype.StandaloneMessage{
795 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800796 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800797 Fields: []ptype.Field{
798 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
799 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
800 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
801 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
802 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
803 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
804 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
805 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
806 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
807 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
808 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
809 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
810 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
811 },
812 Oneofs: []ptype.Oneof{{Name: "union"}},
813 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800814 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800815 return new(OneofScalars)
816 },
817)}
818
Damien Neil374cdb82019-01-29 16:45:30 -0800819func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType }
820func (m *OneofScalars) KnownFields() pref.KnownFields {
821 return oneofScalarsType.MessageOf(m).KnownFields()
822}
823func (m *OneofScalars) UnknownFields() pref.UnknownFields {
824 return oneofScalarsType.MessageOf(m).UnknownFields()
825}
826func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
827func (m *OneofScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800828
Joe Tsaif18ab532018-11-27 17:25:04 -0800829func (*OneofScalars) XXX_OneofWrappers() []interface{} {
830 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700831 (*OneofScalars_Bool)(nil),
832 (*OneofScalars_Int32)(nil),
833 (*OneofScalars_Int64)(nil),
834 (*OneofScalars_Uint32)(nil),
835 (*OneofScalars_Uint64)(nil),
836 (*OneofScalars_Float32)(nil),
837 (*OneofScalars_Float64)(nil),
838 (*OneofScalars_String)(nil),
839 (*OneofScalars_StringA)(nil),
840 (*OneofScalars_StringB)(nil),
841 (*OneofScalars_Bytes)(nil),
842 (*OneofScalars_BytesA)(nil),
843 (*OneofScalars_BytesB)(nil),
844 }
845}
846
Joe Tsai87b955b2018-11-14 21:59:49 -0800847type (
848 isOneofScalars_Union interface {
849 isOneofScalars_Union()
850 }
851 OneofScalars_Bool struct {
852 Bool bool `protobuf:"1"`
853 }
854 OneofScalars_Int32 struct {
855 Int32 MyInt32 `protobuf:"2"`
856 }
857 OneofScalars_Int64 struct {
858 Int64 int64 `protobuf:"3"`
859 }
860 OneofScalars_Uint32 struct {
861 Uint32 MyUint32 `protobuf:"4"`
862 }
863 OneofScalars_Uint64 struct {
864 Uint64 uint64 `protobuf:"5"`
865 }
866 OneofScalars_Float32 struct {
867 Float32 MyFloat32 `protobuf:"6"`
868 }
869 OneofScalars_Float64 struct {
870 Float64 float64 `protobuf:"7"`
871 }
872 OneofScalars_String struct {
873 String string `protobuf:"8"`
874 }
875 OneofScalars_StringA struct {
876 StringA []byte `protobuf:"9"`
877 }
878 OneofScalars_StringB struct {
879 StringB MyString `protobuf:"10"`
880 }
881 OneofScalars_Bytes struct {
882 Bytes []byte `protobuf:"11"`
883 }
884 OneofScalars_BytesA struct {
885 BytesA string `protobuf:"12"`
886 }
887 OneofScalars_BytesB struct {
888 BytesB MyBytes `protobuf:"13"`
889 }
890)
891
Joe Tsai2c870bb2018-10-17 11:46:52 -0700892func (*OneofScalars_Bool) isOneofScalars_Union() {}
893func (*OneofScalars_Int32) isOneofScalars_Union() {}
894func (*OneofScalars_Int64) isOneofScalars_Union() {}
895func (*OneofScalars_Uint32) isOneofScalars_Union() {}
896func (*OneofScalars_Uint64) isOneofScalars_Union() {}
897func (*OneofScalars_Float32) isOneofScalars_Union() {}
898func (*OneofScalars_Float64) isOneofScalars_Union() {}
899func (*OneofScalars_String) isOneofScalars_Union() {}
900func (*OneofScalars_StringA) isOneofScalars_Union() {}
901func (*OneofScalars_StringB) isOneofScalars_Union() {}
902func (*OneofScalars_Bytes) isOneofScalars_Union() {}
903func (*OneofScalars_BytesA) isOneofScalars_Union() {}
904func (*OneofScalars_BytesB) isOneofScalars_Union() {}
905
906func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800907 empty := &OneofScalars{}
908 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
909 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
910 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
911 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
912 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
913 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
914 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
915 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
916 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
917 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
918 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
919 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
920 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700921
Joe Tsaif0c01e42018-11-06 13:05:20 -0800922 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700923 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},
924 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"))},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700925 whichOneofs{"union": 0, "Union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700926
Joe Tsai87b955b2018-11-14 21:59:49 -0800927 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
928 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
929 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
930 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
931 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
932 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
933 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700934
935 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: true, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
936 whichOneofs{"union": 7, "Union": 0},
937
Joe Tsai87b955b2018-11-14 21:59:49 -0800938 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
939 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
940 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
941 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
942 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
943 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700944
945 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},
946 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 Tsai87b955b2018-11-14 21:59:49 -0800947 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700948 whichOneofs{"union": 13, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800949 equalMessage{want13},
950 clearFields{13},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700951 whichOneofs{"union": 0, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800952 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700953 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800954
955 // Test read-only operations on nil message.
956 testMessage(t, nil, (*OneofScalars)(nil), messageOps{
957 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},
958 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"))},
959 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700960}
961
Joe Tsai87b955b2018-11-14 21:59:49 -0800962type EnumProto2 int32
963
964var enumProto2Type = ptype.GoEnum(
965 mustMakeEnumDesc(ptype.StandaloneEnum{
966 Syntax: pref.Proto2,
967 FullName: "EnumProto2",
968 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
969 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800970 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800971 return EnumProto2(n)
972 },
973)
974
975func (e EnumProto2) Enum() *EnumProto2 { return &e }
976func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
977func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800978
979type EnumProto3 int32
980
981var enumProto3Type = ptype.GoEnum(
982 mustMakeEnumDesc(ptype.StandaloneEnum{
983 Syntax: pref.Proto3,
984 FullName: "EnumProto3",
985 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
986 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800987 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800988 return EnumProto3(n)
989 },
990)
991
992func (e EnumProto3) Enum() *EnumProto3 { return &e }
993func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
994func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800995
996type EnumMessages struct {
997 EnumP2 *EnumProto2 `protobuf:"1"`
998 EnumP3 *EnumProto3 `protobuf:"2"`
999 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
1000 MessageCycle *EnumMessages `protobuf:"4"`
1001 EnumList []EnumProto2 `protobuf:"5"`
1002 MessageList []*ScalarProto2 `protobuf:"6"`
1003 EnumMap map[string]EnumProto3 `protobuf:"7"`
1004 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
1005 Union isEnumMessages_Union `protobuf_oneof:"union"`
1006}
1007
Damien Neil8012b442019-01-18 09:32:24 -08001008var enumMessagesType = pimpl.MessageType{GoType: reflect.TypeOf(new(EnumMessages)), PBType: ptype.GoMessage(
Joe Tsai87b955b2018-11-14 21:59:49 -08001009 mustMakeMessageDesc(ptype.StandaloneMessage{
1010 Syntax: pref.Proto2,
1011 FullName: "EnumMessages",
1012 Fields: []ptype.Field{
1013 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
1014 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
Joe Tsai08e00302018-11-26 22:32:06 -08001015 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: pimpl.Export{}.MessageOf(new(proto2_20180125.Message)).Type()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001016 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
1017 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
Damien Neil8012b442019-01-18 09:32:24 -08001018 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001019 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
1020 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
1021 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
1022 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
Damien Neil8012b442019-01-18 09:32:24 -08001023 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType},
1024 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001025 },
1026 Oneofs: []ptype.Oneof{{Name: "union"}},
1027 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001028 func(pref.MessageType) pref.Message {
Joe Tsai87b955b2018-11-14 21:59:49 -08001029 return new(EnumMessages)
1030 },
1031)}
1032
1033var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1034 Syntax: pref.Proto2,
1035 FullName: "EnumMessages.F7Entry",
1036 Fields: []ptype.Field{
1037 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
1038 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
1039 },
Damien Neil232ea152018-12-10 15:14:36 -08001040 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1041 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001042})
1043
1044var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1045 Syntax: pref.Proto2,
1046 FullName: "EnumMessages.F8Entry",
1047 Fields: []ptype.Field{
1048 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Damien Neil8012b442019-01-18 09:32:24 -08001049 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001050 },
Damien Neil232ea152018-12-10 15:14:36 -08001051 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1052 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001053})
1054
Damien Neil374cdb82019-01-29 16:45:30 -08001055func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType }
1056func (m *EnumMessages) KnownFields() pref.KnownFields {
1057 return enumMessagesType.MessageOf(m).KnownFields()
1058}
1059func (m *EnumMessages) UnknownFields() pref.UnknownFields {
1060 return enumMessagesType.MessageOf(m).UnknownFields()
1061}
1062func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
1063func (m *EnumMessages) ProtoReflect() pref.Message { return m }
Joe Tsai87b955b2018-11-14 21:59:49 -08001064
Joe Tsaif18ab532018-11-27 17:25:04 -08001065func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1066 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001067 (*EnumMessages_OneofE2)(nil),
1068 (*EnumMessages_OneofE3)(nil),
1069 (*EnumMessages_OneofM2)(nil),
1070 (*EnumMessages_OneofM3)(nil),
1071 }
1072}
1073
1074type (
1075 isEnumMessages_Union interface {
1076 isEnumMessages_Union()
1077 }
1078 EnumMessages_OneofE2 struct {
1079 OneofE2 EnumProto2 `protobuf:"9"`
1080 }
1081 EnumMessages_OneofE3 struct {
1082 OneofE3 EnumProto3 `protobuf:"10"`
1083 }
1084 EnumMessages_OneofM2 struct {
1085 OneofM2 *ScalarProto2 `protobuf:"11"`
1086 }
1087 EnumMessages_OneofM3 struct {
1088 OneofM3 *ScalarProto3 `protobuf:"12"`
1089 }
1090)
1091
1092func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1093func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1094func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1095func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1096
1097func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001098 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001099 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001100 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1101 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001102 wantM3a := &ScalarProto3{Float32: math.Pi}
1103 wantM3b := &ScalarProto3{Float32: math.Ln2}
1104
1105 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1106 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1107
1108 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1109 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1110
1111 testMessage(t, nil, &EnumMessages{}, messageOps{
1112 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 Tsaif6d4a422018-11-19 14:26:06 -08001113 getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001114
1115 // Test singular enums.
1116 setFields{1: VE(0xdead), 2: VE(0)},
1117 getFields{1: VE(0xdead), 2: VE(0)},
1118 hasFields{1: true, 2: true},
1119
1120 // Test singular messages.
1121 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1122 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1123 getFields{3: V(wantL), 4: V(wantM)},
1124 clearFields{3, 4},
1125 hasFields{3: false, 4: false},
1126 setFields{3: V(wantL), 4: V(wantM)},
1127 hasFields{3: true, 4: true},
1128
1129 // Test list of enums and messages.
1130 listFields{
1131 5: listOps{
1132 appendList{VE(111), VE(222)},
1133 setList{0: VE(333)},
1134 getList{0: VE(333), 1: VE(222)},
1135 lenList(2),
1136 },
1137 6: listOps{
1138 appendMessageList{setFields{4: V(uint32(1e6))}},
1139 appendMessageList{setFields{6: V(float32(math.Phi))}},
1140 setList{0: V(wantM2a)},
1141 getList{0: V(wantM2a), 1: V(wantM2b)},
1142 },
1143 },
1144 getFields{5: wantList5, 6: wantList6},
1145 hasFields{5: true, 6: true},
1146 listFields{5: listOps{truncList(0)}},
1147 hasFields{5: false, 6: true},
1148
1149 // Test maps of enums and messages.
1150 mapFields{
1151 7: mapOps{
1152 setMap{"one": VE(1), "two": VE(2)},
1153 hasMap{"one": true, "two": true, "three": false},
1154 lenMap(2),
1155 },
1156 8: mapOps{
1157 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1158 setMap{"ln2": V(wantM3b)},
1159 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1160 lenMap(2),
1161 },
1162 },
1163 getFields{7: wantMap7, 8: wantMap8},
1164 hasFields{7: true, 8: true},
1165 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1166 hasFields{7: true, 8: false},
1167
1168 // Test oneofs of enums and messages.
1169 setFields{9: VE(0xdead)},
1170 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1171 setFields{10: VE(0)},
1172 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1173 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1174 getFields{11: V(wantM2a)},
1175 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1176 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1177 getFields{12: V(wantM3a)},
1178 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1179
1180 // Check entire message.
1181 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1182 equalMessage{&EnumMessages{
1183 EnumP2: EnumProto2(0xdead).Enum(),
1184 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001185 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001186 MessageCycle: wantM,
1187 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1188 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1189 Union: &EnumMessages_OneofM3{wantM3a},
1190 }},
1191 clearFields{1, 2, 3, 4, 6, 7, 12},
1192 equalMessage{&EnumMessages{}},
1193 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001194
1195 // Test read-only operations on nil message.
1196 testMessage(t, nil, (*EnumMessages)(nil), messageOps{
1197 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},
1198 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)},
1199 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1200 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1201 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001202}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001203
Joe Tsai91e14662018-09-13 13:24:35 -07001204var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001205 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1206 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001207 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001208 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1209 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001210 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001211 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001212 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001213 }),
1214 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001215 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001216}
1217
1218func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1219 fs := m.KnownFields()
1220 for i, op := range tt {
1221 p.Push(i)
1222 switch op := op.(type) {
1223 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001224 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001225 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1226 }
1227 case hasFields:
1228 got := map[pref.FieldNumber]bool{}
1229 want := map[pref.FieldNumber]bool(op)
1230 for n := range want {
1231 got[n] = fs.Has(n)
1232 }
1233 if diff := cmp.Diff(want, got); diff != "" {
1234 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1235 }
1236 case getFields:
1237 got := map[pref.FieldNumber]pref.Value{}
1238 want := map[pref.FieldNumber]pref.Value(op)
1239 for n := range want {
1240 got[n] = fs.Get(n)
1241 }
1242 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1243 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1244 }
1245 case setFields:
1246 for n, v := range op {
1247 fs.Set(n, v)
1248 }
1249 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001250 for _, n := range op {
1251 fs.Clear(n)
1252 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001253 case whichOneofs:
1254 got := map[pref.Name]pref.FieldNumber{}
1255 want := map[pref.Name]pref.FieldNumber(op)
1256 for s := range want {
1257 got[s] = fs.WhichOneof(s)
1258 }
1259 if diff := cmp.Diff(want, got); diff != "" {
1260 t.Errorf("operation %v, KnownFields.WhichOneof mismatch (-want, +got):\n%s", p, diff)
1261 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001262 case messageFields:
1263 for n, tt := range op {
1264 p.Push(int(n))
Damien Neil97e7f572018-12-07 14:28:33 -08001265 if !fs.Has(n) {
1266 fs.Set(n, V(fs.NewMessage(n)))
1267 }
1268 testMessage(t, p, fs.Get(n).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001269 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001270 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001271 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001272 for n, tt := range op {
1273 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001274 testLists(t, p, fs.Get(n).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001275 p.Pop()
1276 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001277 case mapFields:
1278 for n, tt := range op {
1279 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001280 testMaps(t, p, fs.Get(n).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001281 p.Pop()
1282 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001283 case rangeFields:
1284 got := map[pref.FieldNumber]pref.Value{}
1285 want := map[pref.FieldNumber]pref.Value(op)
1286 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1287 got[n] = v
1288 return true
1289 })
1290 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1291 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1292 }
Joe Tsai91e14662018-09-13 13:24:35 -07001293 default:
1294 t.Fatalf("operation %v, invalid operation: %T", p, op)
1295 }
1296 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001297 }
1298}
Joe Tsai91e14662018-09-13 13:24:35 -07001299
Joe Tsai4b7aff62018-11-14 14:05:19 -08001300func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001301 for i, op := range tt {
1302 p.Push(i)
1303 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001304 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001305 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001306 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001307 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001308 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001309 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001310 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001311 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001312 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001313 got := map[int]pref.Value{}
1314 want := map[int]pref.Value(op)
1315 for n := range want {
1316 got[n] = v.Get(n)
1317 }
1318 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001319 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001320 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001321 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001322 for n, e := range op {
1323 v.Set(n, e)
1324 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001325 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001326 for _, e := range op {
1327 v.Append(e)
1328 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001329 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001330 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001331 v.Append(V(m))
1332 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001333 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001334 v.Truncate(int(op))
1335 default:
1336 t.Fatalf("operation %v, invalid operation: %T", p, op)
1337 }
1338 p.Pop()
1339 }
1340}
1341
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001342func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1343 for i, op := range tt {
1344 p.Push(i)
1345 switch op := op.(type) {
1346 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001347 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001348 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1349 }
1350 case lenMap:
1351 if got, want := m.Len(), int(op); got != want {
1352 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1353 }
1354 case hasMap:
1355 got := map[interface{}]bool{}
1356 want := map[interface{}]bool(op)
1357 for k := range want {
1358 got[k] = m.Has(V(k).MapKey())
1359 }
1360 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1361 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1362 }
1363 case getMap:
1364 got := map[interface{}]pref.Value{}
1365 want := map[interface{}]pref.Value(op)
1366 for k := range want {
1367 got[k] = m.Get(V(k).MapKey())
1368 }
1369 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1370 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1371 }
1372 case setMap:
1373 for k, v := range op {
1374 m.Set(V(k).MapKey(), v)
1375 }
1376 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001377 for _, k := range op {
1378 m.Clear(V(k).MapKey())
1379 }
1380 case messageMap:
1381 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001382 mk := V(k).MapKey()
1383 if !m.Has(mk) {
1384 m.Set(mk, V(m.NewMessage()))
1385 }
1386 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001387 }
1388 case rangeMap:
1389 got := map[interface{}]pref.Value{}
1390 want := map[interface{}]pref.Value(op)
1391 m.Range(func(k pref.MapKey, v pref.Value) bool {
1392 got[k.Interface()] = v
1393 return true
1394 })
1395 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1396 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1397 }
1398 default:
1399 t.Fatalf("operation %v, invalid operation: %T", p, op)
1400 }
1401 p.Pop()
1402 }
1403}
1404
Joe Tsai91e14662018-09-13 13:24:35 -07001405type path []int
1406
1407func (p *path) Push(i int) { *p = append(*p, i) }
1408func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1409func (p path) String() string {
1410 var ss []string
1411 for _, i := range p {
1412 ss = append(ss, fmt.Sprint(i))
1413 }
1414 return strings.Join(ss, ".")
1415}