blob: 49e3a0ad784adaa6e0d8311300dfc96c46845607 [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
Joe Tsai0fc49f82019-05-01 12:29:25 -0700231// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800232func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700233func (m *ScalarProto2) Descriptor() pref.MessageDescriptor {
234 return scalarProto2Type.PBType.Descriptor()
235}
Damien Neil374cdb82019-01-29 16:45:30 -0800236func (m *ScalarProto2) KnownFields() pref.KnownFields {
237 return scalarProto2Type.MessageOf(m).KnownFields()
238}
239func (m *ScalarProto2) UnknownFields() pref.UnknownFields {
240 return scalarProto2Type.MessageOf(m).UnknownFields()
241}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700242func (m *ScalarProto2) New() pref.Message { return new(ScalarProto2) }
Damien Neil374cdb82019-01-29 16:45:30 -0800243func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
244func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800245
246func TestScalarProto2(t *testing.T) {
247 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700248 hasFields{
249 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
250 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
251 },
252 getFields{
253 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")),
254 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")),
255 },
256 setFields{
257 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)),
258 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)),
259 },
260 hasFields{
261 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
262 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
263 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800264 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700265 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
266 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 -0800267 }},
268 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
269 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700270 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800271
272 // Test read-only operations on nil message.
273 testMessage(t, nil, (*ScalarProto2)(nil), messageOps{
274 hasFields{
275 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
276 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
277 },
278 getFields{
279 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")),
280 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")),
281 },
282 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700283}
284
Joe Tsaice6edd32018-10-19 16:27:46 -0700285type ScalarProto3 struct {
286 Bool bool `protobuf:"1"`
287 Int32 int32 `protobuf:"2"`
288 Int64 int64 `protobuf:"3"`
289 Uint32 uint32 `protobuf:"4"`
290 Uint64 uint64 `protobuf:"5"`
291 Float32 float32 `protobuf:"6"`
292 Float64 float64 `protobuf:"7"`
293 String string `protobuf:"8"`
294 StringA []byte `protobuf:"9"`
295 Bytes []byte `protobuf:"10"`
296 BytesA string `protobuf:"11"`
297
298 MyBool MyBool `protobuf:"12"`
299 MyInt32 MyInt32 `protobuf:"13"`
300 MyInt64 MyInt64 `protobuf:"14"`
301 MyUint32 MyUint32 `protobuf:"15"`
302 MyUint64 MyUint64 `protobuf:"16"`
303 MyFloat32 MyFloat32 `protobuf:"17"`
304 MyFloat64 MyFloat64 `protobuf:"18"`
305 MyString MyString `protobuf:"19"`
306 MyStringA MyBytes `protobuf:"20"`
307 MyBytes MyBytes `protobuf:"21"`
308 MyBytesA MyString `protobuf:"22"`
309}
310
Damien Neil8012b442019-01-18 09:32:24 -0800311var scalarProto3Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800312 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700313 Syntax: pref.Proto3,
314 FullName: "ScalarProto3",
315 Fields: []ptype.Field{
316 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
317 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
318 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
319 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
320 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
321 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
322 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
323 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
324 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
325 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
326 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700327
Joe Tsai91e14662018-09-13 13:24:35 -0700328 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
329 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
330 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
331 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
332 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
333 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
334 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
335 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
336 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
337 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
338 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
339 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800340 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800341 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800342 return new(ScalarProto3)
343 },
344)}
Joe Tsai91e14662018-09-13 13:24:35 -0700345
Joe Tsai0fc49f82019-05-01 12:29:25 -0700346// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800347func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700348func (m *ScalarProto3) Descriptor() pref.MessageDescriptor {
349 return scalarProto3Type.PBType.Descriptor()
350}
Damien Neil374cdb82019-01-29 16:45:30 -0800351func (m *ScalarProto3) KnownFields() pref.KnownFields {
352 return scalarProto3Type.MessageOf(m).KnownFields()
353}
354func (m *ScalarProto3) UnknownFields() pref.UnknownFields {
355 return scalarProto3Type.MessageOf(m).UnknownFields()
356}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700357func (m *ScalarProto3) New() pref.Message { return new(ScalarProto3) }
Damien Neil374cdb82019-01-29 16:45:30 -0800358func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
359func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800360
361func TestScalarProto3(t *testing.T) {
362 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700363 hasFields{
364 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
365 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
366 },
367 getFields{
368 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)),
369 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)),
370 },
371 setFields{
372 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)),
373 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)),
374 },
375 hasFields{
376 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
377 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
378 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800379 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700380 setFields{
381 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")),
382 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")),
383 },
384 hasFields{
385 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
386 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
387 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800388 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700389 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
390 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800391 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800392 setFields{
393 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
394 },
395 hasFields{
396 2: true, 3: true, 6: true, 7: true,
397 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800398 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
399 equalMessage{&ScalarProto3{}},
Joe Tsai060cdac2019-04-22 11:44:49 -0700400
401 // Verify that -0 triggers proper Has behavior.
402 hasFields{6: false, 7: false},
403 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
404 hasFields{6: true, 7: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700405 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800406
407 // Test read-only operations on nil message.
408 testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
409 hasFields{
410 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
411 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
412 },
413 getFields{
414 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)),
415 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)),
416 },
417 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700418}
419
Joe Tsaif0c01e42018-11-06 13:05:20 -0800420type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700421 Bools []bool `protobuf:"1"`
422 Int32s []int32 `protobuf:"2"`
423 Int64s []int64 `protobuf:"3"`
424 Uint32s []uint32 `protobuf:"4"`
425 Uint64s []uint64 `protobuf:"5"`
426 Float32s []float32 `protobuf:"6"`
427 Float64s []float64 `protobuf:"7"`
428 Strings []string `protobuf:"8"`
429 StringsA [][]byte `protobuf:"9"`
430 Bytes [][]byte `protobuf:"10"`
431 BytesA []string `protobuf:"11"`
432
433 MyStrings1 []MyString `protobuf:"12"`
434 MyStrings2 []MyBytes `protobuf:"13"`
435 MyBytes1 []MyBytes `protobuf:"14"`
436 MyBytes2 []MyString `protobuf:"15"`
437
Joe Tsai4b7aff62018-11-14 14:05:19 -0800438 MyStrings3 ListStrings `protobuf:"16"`
439 MyStrings4 ListBytes `protobuf:"17"`
440 MyBytes3 ListBytes `protobuf:"18"`
441 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700442}
443
Damien Neil8012b442019-01-18 09:32:24 -0800444var listScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(ListScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800445 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700446 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800447 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700448 Fields: []ptype.Field{
449 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
450 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
451 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
452 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
453 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
454 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
455 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
456 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
457 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
458 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
459 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700460
Joe Tsai91e14662018-09-13 13:24:35 -0700461 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
462 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
463 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
464 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
465
466 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
467 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
468 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
469 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700470 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800471 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800472 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800473 return new(ListScalars)
474 },
475)}
Joe Tsai91e14662018-09-13 13:24:35 -0700476
Joe Tsai0fc49f82019-05-01 12:29:25 -0700477// TODO: Remove this.
478func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType }
479func (m *ListScalars) Descriptor() pref.MessageDescriptor { return listScalarsType.PBType.Descriptor() }
Damien Neil374cdb82019-01-29 16:45:30 -0800480func (m *ListScalars) KnownFields() pref.KnownFields {
481 return listScalarsType.MessageOf(m).KnownFields()
482}
483func (m *ListScalars) UnknownFields() pref.UnknownFields {
484 return listScalarsType.MessageOf(m).UnknownFields()
485}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700486func (m *ListScalars) New() pref.Message { return new(ListScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800487func (m *ListScalars) Interface() pref.ProtoMessage { return m }
488func (m *ListScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800489
490func TestListScalars(t *testing.T) {
491 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700492 emptyFS := empty.KnownFields()
493
Joe Tsaif0c01e42018-11-06 13:05:20 -0800494 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700495 Bools: []bool{true, false, true},
496 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
497 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
498 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
499 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
500 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
501 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
502 Strings: []string{"8", "", "eight"},
503 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
504 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
505 BytesA: []string{"11", "", "eleven"},
506
507 MyStrings1: []MyString{"12", "", "twelve"},
508 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
509 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
510 MyBytes2: []MyString{"15", "", "fifteen"},
511
Joe Tsai4b7aff62018-11-14 14:05:19 -0800512 MyStrings3: ListStrings{"16", "", "sixteen"},
513 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
514 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
515 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800516 }
Joe Tsai91e14662018-09-13 13:24:35 -0700517 wantFS := want.KnownFields()
518
Joe Tsaif0c01e42018-11-06 13:05:20 -0800519 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700520 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},
521 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)},
522 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 -0800523 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700524 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800525 lenList(0),
526 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
527 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800528 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700529 },
530 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800531 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
532 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
533 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700534 },
535 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800536 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800537 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700538 },
539 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800540 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
541 lenList(6),
542 setList{0: V("8"), 2: V("eight")},
543 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800544 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700545 },
546 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800547 appendList{V([]byte(nil)), V([]byte(nil))},
548 setList{0: V([]byte("10"))},
549 appendList{V([]byte("wrong"))},
550 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800551 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700552 },
553 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800554 appendList{V("12"), V("wrong"), V("twelve")},
555 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800556 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700557 },
558 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800559 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800560 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700561 },
562 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800563 appendList{V("16"), V(""), V("sixteen"), V("extra")},
564 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800565 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700566 },
567 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800568 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800569 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700570 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700571 },
Joe Tsai91e14662018-09-13 13:24:35 -0700572 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 -0800573 equalMessage{want},
574 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
575 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000576 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800577
578 // Test read-only operations on nil message.
579 testMessage(t, nil, (*ListScalars)(nil), messageOps{
580 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},
581 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)}},
582 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000583}
584
Joe Tsaice6edd32018-10-19 16:27:46 -0700585type MapScalars struct {
586 KeyBools map[bool]string `protobuf:"1"`
587 KeyInt32s map[int32]string `protobuf:"2"`
588 KeyInt64s map[int64]string `protobuf:"3"`
589 KeyUint32s map[uint32]string `protobuf:"4"`
590 KeyUint64s map[uint64]string `protobuf:"5"`
591 KeyStrings map[string]string `protobuf:"6"`
592
593 ValBools map[string]bool `protobuf:"7"`
594 ValInt32s map[string]int32 `protobuf:"8"`
595 ValInt64s map[string]int64 `protobuf:"9"`
596 ValUint32s map[string]uint32 `protobuf:"10"`
597 ValUint64s map[string]uint64 `protobuf:"11"`
598 ValFloat32s map[string]float32 `protobuf:"12"`
599 ValFloat64s map[string]float64 `protobuf:"13"`
600 ValStrings map[string]string `protobuf:"14"`
601 ValStringsA map[string][]byte `protobuf:"15"`
602 ValBytes map[string][]byte `protobuf:"16"`
603 ValBytesA map[string]string `protobuf:"17"`
604
605 MyStrings1 map[MyString]MyString `protobuf:"18"`
606 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
607 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
608 MyBytes2 map[MyString]MyString `protobuf:"21"`
609
610 MyStrings3 MapStrings `protobuf:"22"`
611 MyStrings4 MapBytes `protobuf:"23"`
612 MyBytes3 MapBytes `protobuf:"24"`
613 MyBytes4 MapStrings `protobuf:"25"`
614}
615
Joe Tsaif0c01e42018-11-06 13:05:20 -0800616func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
617 return ptype.Field{
618 Name: pref.Name(fmt.Sprintf("f%d", n)),
619 Number: n,
620 Cardinality: pref.Repeated,
621 Kind: pref.MessageKind,
622 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
623 Syntax: pref.Proto2,
624 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
625 Fields: []ptype.Field{
626 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
627 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
628 },
Damien Neil232ea152018-12-10 15:14:36 -0800629 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
630 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800631 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000632 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800633}
634
Damien Neil8012b442019-01-18 09:32:24 -0800635var mapScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(MapScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800636 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000637 Syntax: pref.Proto2,
638 FullName: "MapScalars",
639 Fields: []ptype.Field{
640 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
641 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
642 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
643 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
644 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
645 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
646
647 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
648 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
649 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
650 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
651 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
652 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
653 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
654 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
655 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
656 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
657 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
658
659 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
660 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
661 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
662 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
663
664 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
665 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
666 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
667 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
668 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800669 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800670 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800671 return new(MapScalars)
672 },
673)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000674
Joe Tsai0fc49f82019-05-01 12:29:25 -0700675// TODO: Remove this.
676func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType }
677func (m *MapScalars) Descriptor() pref.MessageDescriptor { return mapScalarsType.PBType.Descriptor() }
Damien Neil374cdb82019-01-29 16:45:30 -0800678func (m *MapScalars) KnownFields() pref.KnownFields {
679 return mapScalarsType.MessageOf(m).KnownFields()
680}
681func (m *MapScalars) UnknownFields() pref.UnknownFields {
682 return mapScalarsType.MessageOf(m).UnknownFields()
683}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700684func (m *MapScalars) New() pref.Message { return new(MapScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800685func (m *MapScalars) Interface() pref.ProtoMessage { return m }
686func (m *MapScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800687
688func TestMapScalars(t *testing.T) {
689 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000690 emptyFS := empty.KnownFields()
691
Joe Tsaif0c01e42018-11-06 13:05:20 -0800692 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000693 KeyBools: map[bool]string{true: "true", false: "false"},
694 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
695 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
696 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
697 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
698 KeyStrings: map[string]string{"": "", "foo": "bar"},
699
700 ValBools: map[string]bool{"true": true, "false": false},
701 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
702 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
703 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
704 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
705 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
706 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
707 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
708 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
709 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
710 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
711
712 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
713 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
714 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
715 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
716
717 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
718 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
719 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
720 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800721 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000722 wantFS := want.KnownFields()
723
Joe Tsaif0c01e42018-11-06 13:05:20 -0800724 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000725 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},
726 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)},
727 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)},
728 mapFields{
729 2: {
730 lenMap(0),
731 hasMap{int32(0): false, int32(-1): false, int32(2): false},
732 setMap{int32(0): V("zero")},
733 lenMap(1),
734 hasMap{int32(0): true, int32(-1): false, int32(2): false},
735 setMap{int32(-1): V("one")},
736 lenMap(2),
737 hasMap{int32(0): true, int32(-1): true, int32(2): false},
738 setMap{int32(2): V("two")},
739 lenMap(3),
740 hasMap{int32(0): true, int32(-1): true, int32(2): true},
741 },
742 4: {
743 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800744 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000745 },
746 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800747 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000748 setMap{"foo": V("bar")},
749 setMap{"": V("empty")},
750 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
751 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800752 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000753 },
754 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800755 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000756 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
757 },
758 10: {
759 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
760 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800761 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000762 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
763 },
764 12: {
765 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800766 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000767 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
768 },
769 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800770 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000771 setMap{"s1": V("s1"), "s2": V("s2")},
772 },
773 16: {
774 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800775 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000776 },
777 18: {
778 hasMap{"s1": false, "s2": false, "s3": false},
779 setMap{"s1": V("s1"), "s2": V("s2")},
780 hasMap{"s1": true, "s2": true, "s3": false},
781 },
782 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800783 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000784 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
785 },
786 22: {
787 rangeMap{},
788 setMap{"s1": V("s1"), "s2": V("s2")},
789 rangeMap{"s1": V("s1"), "s2": V("s2")},
790 lenMap(2),
791 },
792 24: {
793 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800794 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000795 },
796 },
797 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 -0800798 equalMessage{want},
799 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},
800 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700801 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800802
803 // Test read-only operations on nil message.
804 testMessage(t, nil, (*MapScalars)(nil), messageOps{
805 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},
806 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)}},
807 })
Joe Tsai91e14662018-09-13 13:24:35 -0700808}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700809
Joe Tsai87b955b2018-11-14 21:59:49 -0800810type OneofScalars struct {
811 Union isOneofScalars_Union `protobuf_oneof:"union"`
812}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700813
Damien Neil8012b442019-01-18 09:32:24 -0800814var oneofScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(OneofScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800815 mustMakeMessageDesc(ptype.StandaloneMessage{
816 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800817 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800818 Fields: []ptype.Field{
819 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
820 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
821 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
822 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
823 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
824 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
825 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
826 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
827 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
828 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
829 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
830 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
831 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
832 },
833 Oneofs: []ptype.Oneof{{Name: "union"}},
834 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800835 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800836 return new(OneofScalars)
837 },
838)}
839
Joe Tsai0fc49f82019-05-01 12:29:25 -0700840// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800841func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700842func (m *OneofScalars) Descriptor() pref.MessageDescriptor {
843 return oneofScalarsType.PBType.Descriptor()
844}
Damien Neil374cdb82019-01-29 16:45:30 -0800845func (m *OneofScalars) KnownFields() pref.KnownFields {
846 return oneofScalarsType.MessageOf(m).KnownFields()
847}
848func (m *OneofScalars) UnknownFields() pref.UnknownFields {
849 return oneofScalarsType.MessageOf(m).UnknownFields()
850}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700851func (m *OneofScalars) New() pref.Message { return new(OneofScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800852func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
853func (m *OneofScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800854
Joe Tsaif18ab532018-11-27 17:25:04 -0800855func (*OneofScalars) XXX_OneofWrappers() []interface{} {
856 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700857 (*OneofScalars_Bool)(nil),
858 (*OneofScalars_Int32)(nil),
859 (*OneofScalars_Int64)(nil),
860 (*OneofScalars_Uint32)(nil),
861 (*OneofScalars_Uint64)(nil),
862 (*OneofScalars_Float32)(nil),
863 (*OneofScalars_Float64)(nil),
864 (*OneofScalars_String)(nil),
865 (*OneofScalars_StringA)(nil),
866 (*OneofScalars_StringB)(nil),
867 (*OneofScalars_Bytes)(nil),
868 (*OneofScalars_BytesA)(nil),
869 (*OneofScalars_BytesB)(nil),
870 }
871}
872
Joe Tsai87b955b2018-11-14 21:59:49 -0800873type (
874 isOneofScalars_Union interface {
875 isOneofScalars_Union()
876 }
877 OneofScalars_Bool struct {
878 Bool bool `protobuf:"1"`
879 }
880 OneofScalars_Int32 struct {
881 Int32 MyInt32 `protobuf:"2"`
882 }
883 OneofScalars_Int64 struct {
884 Int64 int64 `protobuf:"3"`
885 }
886 OneofScalars_Uint32 struct {
887 Uint32 MyUint32 `protobuf:"4"`
888 }
889 OneofScalars_Uint64 struct {
890 Uint64 uint64 `protobuf:"5"`
891 }
892 OneofScalars_Float32 struct {
893 Float32 MyFloat32 `protobuf:"6"`
894 }
895 OneofScalars_Float64 struct {
896 Float64 float64 `protobuf:"7"`
897 }
898 OneofScalars_String struct {
899 String string `protobuf:"8"`
900 }
901 OneofScalars_StringA struct {
902 StringA []byte `protobuf:"9"`
903 }
904 OneofScalars_StringB struct {
905 StringB MyString `protobuf:"10"`
906 }
907 OneofScalars_Bytes struct {
908 Bytes []byte `protobuf:"11"`
909 }
910 OneofScalars_BytesA struct {
911 BytesA string `protobuf:"12"`
912 }
913 OneofScalars_BytesB struct {
914 BytesB MyBytes `protobuf:"13"`
915 }
916)
917
Joe Tsai2c870bb2018-10-17 11:46:52 -0700918func (*OneofScalars_Bool) isOneofScalars_Union() {}
919func (*OneofScalars_Int32) isOneofScalars_Union() {}
920func (*OneofScalars_Int64) isOneofScalars_Union() {}
921func (*OneofScalars_Uint32) isOneofScalars_Union() {}
922func (*OneofScalars_Uint64) isOneofScalars_Union() {}
923func (*OneofScalars_Float32) isOneofScalars_Union() {}
924func (*OneofScalars_Float64) isOneofScalars_Union() {}
925func (*OneofScalars_String) isOneofScalars_Union() {}
926func (*OneofScalars_StringA) isOneofScalars_Union() {}
927func (*OneofScalars_StringB) isOneofScalars_Union() {}
928func (*OneofScalars_Bytes) isOneofScalars_Union() {}
929func (*OneofScalars_BytesA) isOneofScalars_Union() {}
930func (*OneofScalars_BytesB) isOneofScalars_Union() {}
931
932func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800933 empty := &OneofScalars{}
934 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
935 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
936 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
937 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
938 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
939 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
940 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
941 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
942 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
943 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
944 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
945 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
946 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700947
Joe Tsaif0c01e42018-11-06 13:05:20 -0800948 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700949 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},
950 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 -0700951 whichOneofs{"union": 0, "Union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700952
Joe Tsai87b955b2018-11-14 21:59:49 -0800953 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
954 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
955 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
956 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
957 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
958 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
959 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700960
961 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},
962 whichOneofs{"union": 7, "Union": 0},
963
Joe Tsai87b955b2018-11-14 21:59:49 -0800964 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
965 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
966 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
967 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
968 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
969 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700970
971 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},
972 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 -0800973 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700974 whichOneofs{"union": 13, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800975 equalMessage{want13},
976 clearFields{13},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700977 whichOneofs{"union": 0, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800978 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700979 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800980
981 // Test read-only operations on nil message.
982 testMessage(t, nil, (*OneofScalars)(nil), messageOps{
983 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},
984 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"))},
985 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700986}
987
Joe Tsai87b955b2018-11-14 21:59:49 -0800988type EnumProto2 int32
989
990var enumProto2Type = ptype.GoEnum(
991 mustMakeEnumDesc(ptype.StandaloneEnum{
992 Syntax: pref.Proto2,
993 FullName: "EnumProto2",
994 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
995 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800996 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800997 return EnumProto2(n)
998 },
999)
1000
Joe Tsai0fc49f82019-05-01 12:29:25 -07001001// TODO: Remove this.
1002func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
1003func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
1004func (e EnumProto2) Enum() *EnumProto2 { return &e }
1005func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001006
1007type EnumProto3 int32
1008
1009var enumProto3Type = ptype.GoEnum(
1010 mustMakeEnumDesc(ptype.StandaloneEnum{
1011 Syntax: pref.Proto3,
1012 FullName: "EnumProto3",
1013 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
1014 }),
Damien Neila8593ba2019-01-08 16:18:07 -08001015 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -08001016 return EnumProto3(n)
1017 },
1018)
1019
Joe Tsai0fc49f82019-05-01 12:29:25 -07001020// TODO: Remove this.
1021func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
1022func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
1023func (e EnumProto3) Enum() *EnumProto3 { return &e }
1024func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001025
1026type EnumMessages struct {
1027 EnumP2 *EnumProto2 `protobuf:"1"`
1028 EnumP3 *EnumProto3 `protobuf:"2"`
1029 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
1030 MessageCycle *EnumMessages `protobuf:"4"`
1031 EnumList []EnumProto2 `protobuf:"5"`
1032 MessageList []*ScalarProto2 `protobuf:"6"`
1033 EnumMap map[string]EnumProto3 `protobuf:"7"`
1034 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
1035 Union isEnumMessages_Union `protobuf_oneof:"union"`
1036}
1037
Damien Neil8012b442019-01-18 09:32:24 -08001038var enumMessagesType = pimpl.MessageType{GoType: reflect.TypeOf(new(EnumMessages)), PBType: ptype.GoMessage(
Joe Tsai87b955b2018-11-14 21:59:49 -08001039 mustMakeMessageDesc(ptype.StandaloneMessage{
1040 Syntax: pref.Proto2,
1041 FullName: "EnumMessages",
1042 Fields: []ptype.Field{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001043 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type.Descriptor()},
1044 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type.Descriptor()},
1045 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: pimpl.Export{}.MessageDescriptorOf(new(proto2_20180125.Message))},
Joe Tsai87b955b2018-11-14 21:59:49 -08001046 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001047 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type.Descriptor()},
1048 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001049 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
1050 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001051 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type.Descriptor()},
1052 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type.Descriptor()},
1053 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType.Descriptor()},
1054 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001055 },
1056 Oneofs: []ptype.Oneof{{Name: "union"}},
1057 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001058 func(pref.MessageType) pref.Message {
Joe Tsai87b955b2018-11-14 21:59:49 -08001059 return new(EnumMessages)
1060 },
1061)}
1062
1063var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1064 Syntax: pref.Proto2,
1065 FullName: "EnumMessages.F7Entry",
1066 Fields: []ptype.Field{
1067 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001068 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001069 },
Damien Neil232ea152018-12-10 15:14:36 -08001070 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1071 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001072})
1073
1074var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1075 Syntax: pref.Proto2,
1076 FullName: "EnumMessages.F8Entry",
1077 Fields: []ptype.Field{
1078 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001079 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001080 },
Damien Neil232ea152018-12-10 15:14:36 -08001081 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1082 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001083})
1084
Joe Tsai0fc49f82019-05-01 12:29:25 -07001085// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -08001086func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -07001087func (m *EnumMessages) Descriptor() pref.MessageDescriptor {
1088 return enumMessagesType.PBType.Descriptor()
1089}
Damien Neil374cdb82019-01-29 16:45:30 -08001090func (m *EnumMessages) KnownFields() pref.KnownFields {
1091 return enumMessagesType.MessageOf(m).KnownFields()
1092}
1093func (m *EnumMessages) UnknownFields() pref.UnknownFields {
1094 return enumMessagesType.MessageOf(m).UnknownFields()
1095}
Joe Tsai0fc49f82019-05-01 12:29:25 -07001096func (m *EnumMessages) New() pref.Message { return new(EnumMessages) }
Damien Neil374cdb82019-01-29 16:45:30 -08001097func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
1098func (m *EnumMessages) ProtoReflect() pref.Message { return m }
Joe Tsai87b955b2018-11-14 21:59:49 -08001099
Joe Tsaif18ab532018-11-27 17:25:04 -08001100func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1101 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001102 (*EnumMessages_OneofE2)(nil),
1103 (*EnumMessages_OneofE3)(nil),
1104 (*EnumMessages_OneofM2)(nil),
1105 (*EnumMessages_OneofM3)(nil),
1106 }
1107}
1108
1109type (
1110 isEnumMessages_Union interface {
1111 isEnumMessages_Union()
1112 }
1113 EnumMessages_OneofE2 struct {
1114 OneofE2 EnumProto2 `protobuf:"9"`
1115 }
1116 EnumMessages_OneofE3 struct {
1117 OneofE3 EnumProto3 `protobuf:"10"`
1118 }
1119 EnumMessages_OneofM2 struct {
1120 OneofM2 *ScalarProto2 `protobuf:"11"`
1121 }
1122 EnumMessages_OneofM3 struct {
1123 OneofM3 *ScalarProto3 `protobuf:"12"`
1124 }
1125)
1126
1127func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1128func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1129func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1130func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1131
1132func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001133 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001134 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001135 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1136 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001137 wantM3a := &ScalarProto3{Float32: math.Pi}
1138 wantM3b := &ScalarProto3{Float32: math.Ln2}
1139
1140 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1141 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1142
1143 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1144 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1145
1146 testMessage(t, nil, &EnumMessages{}, messageOps{
1147 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 -08001148 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 -08001149
1150 // Test singular enums.
1151 setFields{1: VE(0xdead), 2: VE(0)},
1152 getFields{1: VE(0xdead), 2: VE(0)},
1153 hasFields{1: true, 2: true},
1154
1155 // Test singular messages.
1156 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1157 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1158 getFields{3: V(wantL), 4: V(wantM)},
1159 clearFields{3, 4},
1160 hasFields{3: false, 4: false},
1161 setFields{3: V(wantL), 4: V(wantM)},
1162 hasFields{3: true, 4: true},
1163
1164 // Test list of enums and messages.
1165 listFields{
1166 5: listOps{
1167 appendList{VE(111), VE(222)},
1168 setList{0: VE(333)},
1169 getList{0: VE(333), 1: VE(222)},
1170 lenList(2),
1171 },
1172 6: listOps{
1173 appendMessageList{setFields{4: V(uint32(1e6))}},
1174 appendMessageList{setFields{6: V(float32(math.Phi))}},
1175 setList{0: V(wantM2a)},
1176 getList{0: V(wantM2a), 1: V(wantM2b)},
1177 },
1178 },
1179 getFields{5: wantList5, 6: wantList6},
1180 hasFields{5: true, 6: true},
1181 listFields{5: listOps{truncList(0)}},
1182 hasFields{5: false, 6: true},
1183
1184 // Test maps of enums and messages.
1185 mapFields{
1186 7: mapOps{
1187 setMap{"one": VE(1), "two": VE(2)},
1188 hasMap{"one": true, "two": true, "three": false},
1189 lenMap(2),
1190 },
1191 8: mapOps{
1192 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1193 setMap{"ln2": V(wantM3b)},
1194 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1195 lenMap(2),
1196 },
1197 },
1198 getFields{7: wantMap7, 8: wantMap8},
1199 hasFields{7: true, 8: true},
1200 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1201 hasFields{7: true, 8: false},
1202
1203 // Test oneofs of enums and messages.
1204 setFields{9: VE(0xdead)},
1205 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1206 setFields{10: VE(0)},
1207 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1208 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1209 getFields{11: V(wantM2a)},
1210 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1211 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1212 getFields{12: V(wantM3a)},
1213 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1214
1215 // Check entire message.
1216 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1217 equalMessage{&EnumMessages{
1218 EnumP2: EnumProto2(0xdead).Enum(),
1219 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001220 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001221 MessageCycle: wantM,
1222 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1223 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1224 Union: &EnumMessages_OneofM3{wantM3a},
1225 }},
1226 clearFields{1, 2, 3, 4, 6, 7, 12},
1227 equalMessage{&EnumMessages{}},
1228 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001229
1230 // Test read-only operations on nil message.
1231 testMessage(t, nil, (*EnumMessages)(nil), messageOps{
1232 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},
1233 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)},
1234 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1235 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1236 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001237}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001238
Joe Tsai91e14662018-09-13 13:24:35 -07001239var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001240 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1241 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001242 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001243 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1244 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001245 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001246 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001247 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001248 }),
1249 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001250 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001251}
1252
1253func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1254 fs := m.KnownFields()
1255 for i, op := range tt {
1256 p.Push(i)
1257 switch op := op.(type) {
1258 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001259 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001260 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1261 }
1262 case hasFields:
1263 got := map[pref.FieldNumber]bool{}
1264 want := map[pref.FieldNumber]bool(op)
1265 for n := range want {
1266 got[n] = fs.Has(n)
1267 }
1268 if diff := cmp.Diff(want, got); diff != "" {
1269 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1270 }
1271 case getFields:
1272 got := map[pref.FieldNumber]pref.Value{}
1273 want := map[pref.FieldNumber]pref.Value(op)
1274 for n := range want {
1275 got[n] = fs.Get(n)
1276 }
1277 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1278 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1279 }
1280 case setFields:
1281 for n, v := range op {
1282 fs.Set(n, v)
1283 }
1284 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001285 for _, n := range op {
1286 fs.Clear(n)
1287 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001288 case whichOneofs:
1289 got := map[pref.Name]pref.FieldNumber{}
1290 want := map[pref.Name]pref.FieldNumber(op)
1291 for s := range want {
1292 got[s] = fs.WhichOneof(s)
1293 }
1294 if diff := cmp.Diff(want, got); diff != "" {
1295 t.Errorf("operation %v, KnownFields.WhichOneof mismatch (-want, +got):\n%s", p, diff)
1296 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001297 case messageFields:
1298 for n, tt := range op {
1299 p.Push(int(n))
Damien Neil97e7f572018-12-07 14:28:33 -08001300 if !fs.Has(n) {
1301 fs.Set(n, V(fs.NewMessage(n)))
1302 }
1303 testMessage(t, p, fs.Get(n).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001304 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001305 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001306 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001307 for n, tt := range op {
1308 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001309 testLists(t, p, fs.Get(n).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001310 p.Pop()
1311 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001312 case mapFields:
1313 for n, tt := range op {
1314 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001315 testMaps(t, p, fs.Get(n).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001316 p.Pop()
1317 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001318 case rangeFields:
1319 got := map[pref.FieldNumber]pref.Value{}
1320 want := map[pref.FieldNumber]pref.Value(op)
1321 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1322 got[n] = v
1323 return true
1324 })
1325 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1326 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1327 }
Joe Tsai91e14662018-09-13 13:24:35 -07001328 default:
1329 t.Fatalf("operation %v, invalid operation: %T", p, op)
1330 }
1331 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001332 }
1333}
Joe Tsai91e14662018-09-13 13:24:35 -07001334
Joe Tsai4b7aff62018-11-14 14:05:19 -08001335func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001336 for i, op := range tt {
1337 p.Push(i)
1338 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001339 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001340 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001341 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001342 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001343 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001344 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001345 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001346 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001347 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001348 got := map[int]pref.Value{}
1349 want := map[int]pref.Value(op)
1350 for n := range want {
1351 got[n] = v.Get(n)
1352 }
1353 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001354 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001355 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001356 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001357 for n, e := range op {
1358 v.Set(n, e)
1359 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001360 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001361 for _, e := range op {
1362 v.Append(e)
1363 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001364 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001365 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001366 v.Append(V(m))
1367 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001368 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001369 v.Truncate(int(op))
1370 default:
1371 t.Fatalf("operation %v, invalid operation: %T", p, op)
1372 }
1373 p.Pop()
1374 }
1375}
1376
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001377func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1378 for i, op := range tt {
1379 p.Push(i)
1380 switch op := op.(type) {
1381 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001382 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001383 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1384 }
1385 case lenMap:
1386 if got, want := m.Len(), int(op); got != want {
1387 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1388 }
1389 case hasMap:
1390 got := map[interface{}]bool{}
1391 want := map[interface{}]bool(op)
1392 for k := range want {
1393 got[k] = m.Has(V(k).MapKey())
1394 }
1395 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1396 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1397 }
1398 case getMap:
1399 got := map[interface{}]pref.Value{}
1400 want := map[interface{}]pref.Value(op)
1401 for k := range want {
1402 got[k] = m.Get(V(k).MapKey())
1403 }
1404 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1405 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1406 }
1407 case setMap:
1408 for k, v := range op {
1409 m.Set(V(k).MapKey(), v)
1410 }
1411 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001412 for _, k := range op {
1413 m.Clear(V(k).MapKey())
1414 }
1415 case messageMap:
1416 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001417 mk := V(k).MapKey()
1418 if !m.Has(mk) {
1419 m.Set(mk, V(m.NewMessage()))
1420 }
1421 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001422 }
1423 case rangeMap:
1424 got := map[interface{}]pref.Value{}
1425 want := map[interface{}]pref.Value(op)
1426 m.Range(func(k pref.MapKey, v pref.Value) bool {
1427 got[k.Interface()] = v
1428 return true
1429 })
1430 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1431 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1432 }
1433 default:
1434 t.Fatalf("operation %v, invalid operation: %T", p, op)
1435 }
1436 p.Pop()
1437 }
1438}
1439
Joe Tsai91e14662018-09-13 13:24:35 -07001440type path []int
1441
1442func (p *path) Push(i int) { *p = append(*p, i) }
1443func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1444func (p path) String() string {
1445 var ss []string
1446 for _, i := range p {
1447 ss = append(ss, fmt.Sprint(i))
1448 }
1449 return strings.Join(ss, ".")
1450}