blob: b5a9af257b29351159c8fbe760e51e6b42e9c7ea [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 Tsai060cdac2019-04-22 11:44:49 -0700390
391 // Verify that -0 triggers proper Has behavior.
392 hasFields{6: false, 7: false},
393 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
394 hasFields{6: true, 7: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700395 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800396
397 // Test read-only operations on nil message.
398 testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
399 hasFields{
400 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
401 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
402 },
403 getFields{
404 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)),
405 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)),
406 },
407 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700408}
409
Joe Tsaif0c01e42018-11-06 13:05:20 -0800410type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700411 Bools []bool `protobuf:"1"`
412 Int32s []int32 `protobuf:"2"`
413 Int64s []int64 `protobuf:"3"`
414 Uint32s []uint32 `protobuf:"4"`
415 Uint64s []uint64 `protobuf:"5"`
416 Float32s []float32 `protobuf:"6"`
417 Float64s []float64 `protobuf:"7"`
418 Strings []string `protobuf:"8"`
419 StringsA [][]byte `protobuf:"9"`
420 Bytes [][]byte `protobuf:"10"`
421 BytesA []string `protobuf:"11"`
422
423 MyStrings1 []MyString `protobuf:"12"`
424 MyStrings2 []MyBytes `protobuf:"13"`
425 MyBytes1 []MyBytes `protobuf:"14"`
426 MyBytes2 []MyString `protobuf:"15"`
427
Joe Tsai4b7aff62018-11-14 14:05:19 -0800428 MyStrings3 ListStrings `protobuf:"16"`
429 MyStrings4 ListBytes `protobuf:"17"`
430 MyBytes3 ListBytes `protobuf:"18"`
431 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700432}
433
Damien Neil8012b442019-01-18 09:32:24 -0800434var listScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(ListScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800435 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700436 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800437 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700438 Fields: []ptype.Field{
439 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
440 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
441 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
442 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
443 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
444 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
445 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
446 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
447 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
448 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
449 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700450
Joe Tsai91e14662018-09-13 13:24:35 -0700451 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
452 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
453 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
454 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
455
456 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
457 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
458 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
459 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700460 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800461 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800462 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800463 return new(ListScalars)
464 },
465)}
Joe Tsai91e14662018-09-13 13:24:35 -0700466
Damien Neil374cdb82019-01-29 16:45:30 -0800467func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType }
468func (m *ListScalars) KnownFields() pref.KnownFields {
469 return listScalarsType.MessageOf(m).KnownFields()
470}
471func (m *ListScalars) UnknownFields() pref.UnknownFields {
472 return listScalarsType.MessageOf(m).UnknownFields()
473}
474func (m *ListScalars) Interface() pref.ProtoMessage { return m }
475func (m *ListScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800476
477func TestListScalars(t *testing.T) {
478 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700479 emptyFS := empty.KnownFields()
480
Joe Tsaif0c01e42018-11-06 13:05:20 -0800481 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700482 Bools: []bool{true, false, true},
483 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
484 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
485 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
486 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
487 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
488 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
489 Strings: []string{"8", "", "eight"},
490 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
491 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
492 BytesA: []string{"11", "", "eleven"},
493
494 MyStrings1: []MyString{"12", "", "twelve"},
495 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
496 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
497 MyBytes2: []MyString{"15", "", "fifteen"},
498
Joe Tsai4b7aff62018-11-14 14:05:19 -0800499 MyStrings3: ListStrings{"16", "", "sixteen"},
500 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
501 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
502 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800503 }
Joe Tsai91e14662018-09-13 13:24:35 -0700504 wantFS := want.KnownFields()
505
Joe Tsaif0c01e42018-11-06 13:05:20 -0800506 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700507 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},
508 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)},
509 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 -0800510 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700511 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800512 lenList(0),
513 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
514 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800515 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700516 },
517 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800518 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
519 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
520 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700521 },
522 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800523 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800524 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700525 },
526 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800527 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
528 lenList(6),
529 setList{0: V("8"), 2: V("eight")},
530 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800531 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700532 },
533 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800534 appendList{V([]byte(nil)), V([]byte(nil))},
535 setList{0: V([]byte("10"))},
536 appendList{V([]byte("wrong"))},
537 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800538 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700539 },
540 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800541 appendList{V("12"), V("wrong"), V("twelve")},
542 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800543 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700544 },
545 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800546 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800547 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700548 },
549 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800550 appendList{V("16"), V(""), V("sixteen"), V("extra")},
551 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800552 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700553 },
554 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800555 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800556 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700557 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700558 },
Joe Tsai91e14662018-09-13 13:24:35 -0700559 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 -0800560 equalMessage{want},
561 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
562 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000563 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800564
565 // Test read-only operations on nil message.
566 testMessage(t, nil, (*ListScalars)(nil), messageOps{
567 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},
568 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)}},
569 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000570}
571
Joe Tsaice6edd32018-10-19 16:27:46 -0700572type MapScalars struct {
573 KeyBools map[bool]string `protobuf:"1"`
574 KeyInt32s map[int32]string `protobuf:"2"`
575 KeyInt64s map[int64]string `protobuf:"3"`
576 KeyUint32s map[uint32]string `protobuf:"4"`
577 KeyUint64s map[uint64]string `protobuf:"5"`
578 KeyStrings map[string]string `protobuf:"6"`
579
580 ValBools map[string]bool `protobuf:"7"`
581 ValInt32s map[string]int32 `protobuf:"8"`
582 ValInt64s map[string]int64 `protobuf:"9"`
583 ValUint32s map[string]uint32 `protobuf:"10"`
584 ValUint64s map[string]uint64 `protobuf:"11"`
585 ValFloat32s map[string]float32 `protobuf:"12"`
586 ValFloat64s map[string]float64 `protobuf:"13"`
587 ValStrings map[string]string `protobuf:"14"`
588 ValStringsA map[string][]byte `protobuf:"15"`
589 ValBytes map[string][]byte `protobuf:"16"`
590 ValBytesA map[string]string `protobuf:"17"`
591
592 MyStrings1 map[MyString]MyString `protobuf:"18"`
593 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
594 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
595 MyBytes2 map[MyString]MyString `protobuf:"21"`
596
597 MyStrings3 MapStrings `protobuf:"22"`
598 MyStrings4 MapBytes `protobuf:"23"`
599 MyBytes3 MapBytes `protobuf:"24"`
600 MyBytes4 MapStrings `protobuf:"25"`
601}
602
Joe Tsaif0c01e42018-11-06 13:05:20 -0800603func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
604 return ptype.Field{
605 Name: pref.Name(fmt.Sprintf("f%d", n)),
606 Number: n,
607 Cardinality: pref.Repeated,
608 Kind: pref.MessageKind,
609 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
610 Syntax: pref.Proto2,
611 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
612 Fields: []ptype.Field{
613 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
614 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
615 },
Damien Neil232ea152018-12-10 15:14:36 -0800616 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
617 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800618 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000619 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800620}
621
Damien Neil8012b442019-01-18 09:32:24 -0800622var mapScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(MapScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800623 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000624 Syntax: pref.Proto2,
625 FullName: "MapScalars",
626 Fields: []ptype.Field{
627 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
628 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
629 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
630 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
631 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
632 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
633
634 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
635 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
636 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
637 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
638 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
639 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
640 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
641 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
642 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
643 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
644 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
645
646 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
647 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
648 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
649 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
650
651 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
652 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
653 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
654 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
655 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800656 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800657 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800658 return new(MapScalars)
659 },
660)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000661
Damien Neil374cdb82019-01-29 16:45:30 -0800662func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType }
663func (m *MapScalars) KnownFields() pref.KnownFields {
664 return mapScalarsType.MessageOf(m).KnownFields()
665}
666func (m *MapScalars) UnknownFields() pref.UnknownFields {
667 return mapScalarsType.MessageOf(m).UnknownFields()
668}
669func (m *MapScalars) Interface() pref.ProtoMessage { return m }
670func (m *MapScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800671
672func TestMapScalars(t *testing.T) {
673 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000674 emptyFS := empty.KnownFields()
675
Joe Tsaif0c01e42018-11-06 13:05:20 -0800676 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000677 KeyBools: map[bool]string{true: "true", false: "false"},
678 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
679 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
680 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
681 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
682 KeyStrings: map[string]string{"": "", "foo": "bar"},
683
684 ValBools: map[string]bool{"true": true, "false": false},
685 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
686 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
687 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
688 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
689 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
690 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
691 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
692 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
693 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
694 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
695
696 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
697 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
698 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
699 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
700
701 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
702 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
703 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
704 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800705 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000706 wantFS := want.KnownFields()
707
Joe Tsaif0c01e42018-11-06 13:05:20 -0800708 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000709 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},
710 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)},
711 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)},
712 mapFields{
713 2: {
714 lenMap(0),
715 hasMap{int32(0): false, int32(-1): false, int32(2): false},
716 setMap{int32(0): V("zero")},
717 lenMap(1),
718 hasMap{int32(0): true, int32(-1): false, int32(2): false},
719 setMap{int32(-1): V("one")},
720 lenMap(2),
721 hasMap{int32(0): true, int32(-1): true, int32(2): false},
722 setMap{int32(2): V("two")},
723 lenMap(3),
724 hasMap{int32(0): true, int32(-1): true, int32(2): true},
725 },
726 4: {
727 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800728 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000729 },
730 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800731 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 setMap{"foo": V("bar")},
733 setMap{"": V("empty")},
734 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
735 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800736 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000737 },
738 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800739 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000740 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
741 },
742 10: {
743 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
744 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800745 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000746 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
747 },
748 12: {
749 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800750 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000751 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
752 },
753 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800754 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000755 setMap{"s1": V("s1"), "s2": V("s2")},
756 },
757 16: {
758 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800759 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000760 },
761 18: {
762 hasMap{"s1": false, "s2": false, "s3": false},
763 setMap{"s1": V("s1"), "s2": V("s2")},
764 hasMap{"s1": true, "s2": true, "s3": false},
765 },
766 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800767 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000768 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
769 },
770 22: {
771 rangeMap{},
772 setMap{"s1": V("s1"), "s2": V("s2")},
773 rangeMap{"s1": V("s1"), "s2": V("s2")},
774 lenMap(2),
775 },
776 24: {
777 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800778 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000779 },
780 },
781 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 -0800782 equalMessage{want},
783 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},
784 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700785 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800786
787 // Test read-only operations on nil message.
788 testMessage(t, nil, (*MapScalars)(nil), messageOps{
789 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},
790 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)}},
791 })
Joe Tsai91e14662018-09-13 13:24:35 -0700792}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700793
Joe Tsai87b955b2018-11-14 21:59:49 -0800794type OneofScalars struct {
795 Union isOneofScalars_Union `protobuf_oneof:"union"`
796}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700797
Damien Neil8012b442019-01-18 09:32:24 -0800798var oneofScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(OneofScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800799 mustMakeMessageDesc(ptype.StandaloneMessage{
800 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800801 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800802 Fields: []ptype.Field{
803 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
804 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
805 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
806 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
807 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
808 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
809 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
810 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
811 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
812 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
813 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
814 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
815 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
816 },
817 Oneofs: []ptype.Oneof{{Name: "union"}},
818 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800819 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800820 return new(OneofScalars)
821 },
822)}
823
Damien Neil374cdb82019-01-29 16:45:30 -0800824func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType }
825func (m *OneofScalars) KnownFields() pref.KnownFields {
826 return oneofScalarsType.MessageOf(m).KnownFields()
827}
828func (m *OneofScalars) UnknownFields() pref.UnknownFields {
829 return oneofScalarsType.MessageOf(m).UnknownFields()
830}
831func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
832func (m *OneofScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800833
Joe Tsaif18ab532018-11-27 17:25:04 -0800834func (*OneofScalars) XXX_OneofWrappers() []interface{} {
835 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700836 (*OneofScalars_Bool)(nil),
837 (*OneofScalars_Int32)(nil),
838 (*OneofScalars_Int64)(nil),
839 (*OneofScalars_Uint32)(nil),
840 (*OneofScalars_Uint64)(nil),
841 (*OneofScalars_Float32)(nil),
842 (*OneofScalars_Float64)(nil),
843 (*OneofScalars_String)(nil),
844 (*OneofScalars_StringA)(nil),
845 (*OneofScalars_StringB)(nil),
846 (*OneofScalars_Bytes)(nil),
847 (*OneofScalars_BytesA)(nil),
848 (*OneofScalars_BytesB)(nil),
849 }
850}
851
Joe Tsai87b955b2018-11-14 21:59:49 -0800852type (
853 isOneofScalars_Union interface {
854 isOneofScalars_Union()
855 }
856 OneofScalars_Bool struct {
857 Bool bool `protobuf:"1"`
858 }
859 OneofScalars_Int32 struct {
860 Int32 MyInt32 `protobuf:"2"`
861 }
862 OneofScalars_Int64 struct {
863 Int64 int64 `protobuf:"3"`
864 }
865 OneofScalars_Uint32 struct {
866 Uint32 MyUint32 `protobuf:"4"`
867 }
868 OneofScalars_Uint64 struct {
869 Uint64 uint64 `protobuf:"5"`
870 }
871 OneofScalars_Float32 struct {
872 Float32 MyFloat32 `protobuf:"6"`
873 }
874 OneofScalars_Float64 struct {
875 Float64 float64 `protobuf:"7"`
876 }
877 OneofScalars_String struct {
878 String string `protobuf:"8"`
879 }
880 OneofScalars_StringA struct {
881 StringA []byte `protobuf:"9"`
882 }
883 OneofScalars_StringB struct {
884 StringB MyString `protobuf:"10"`
885 }
886 OneofScalars_Bytes struct {
887 Bytes []byte `protobuf:"11"`
888 }
889 OneofScalars_BytesA struct {
890 BytesA string `protobuf:"12"`
891 }
892 OneofScalars_BytesB struct {
893 BytesB MyBytes `protobuf:"13"`
894 }
895)
896
Joe Tsai2c870bb2018-10-17 11:46:52 -0700897func (*OneofScalars_Bool) isOneofScalars_Union() {}
898func (*OneofScalars_Int32) isOneofScalars_Union() {}
899func (*OneofScalars_Int64) isOneofScalars_Union() {}
900func (*OneofScalars_Uint32) isOneofScalars_Union() {}
901func (*OneofScalars_Uint64) isOneofScalars_Union() {}
902func (*OneofScalars_Float32) isOneofScalars_Union() {}
903func (*OneofScalars_Float64) isOneofScalars_Union() {}
904func (*OneofScalars_String) isOneofScalars_Union() {}
905func (*OneofScalars_StringA) isOneofScalars_Union() {}
906func (*OneofScalars_StringB) isOneofScalars_Union() {}
907func (*OneofScalars_Bytes) isOneofScalars_Union() {}
908func (*OneofScalars_BytesA) isOneofScalars_Union() {}
909func (*OneofScalars_BytesB) isOneofScalars_Union() {}
910
911func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800912 empty := &OneofScalars{}
913 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
914 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
915 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
916 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
917 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
918 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
919 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
920 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
921 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
922 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
923 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
924 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
925 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700926
Joe Tsaif0c01e42018-11-06 13:05:20 -0800927 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700928 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},
929 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 -0700930 whichOneofs{"union": 0, "Union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700931
Joe Tsai87b955b2018-11-14 21:59:49 -0800932 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
933 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
934 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
935 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
936 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
937 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
938 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700939
940 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},
941 whichOneofs{"union": 7, "Union": 0},
942
Joe Tsai87b955b2018-11-14 21:59:49 -0800943 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
944 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
945 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
946 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
947 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
948 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700949
950 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},
951 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 -0800952 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700953 whichOneofs{"union": 13, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800954 equalMessage{want13},
955 clearFields{13},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700956 whichOneofs{"union": 0, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800957 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700958 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800959
960 // Test read-only operations on nil message.
961 testMessage(t, nil, (*OneofScalars)(nil), messageOps{
962 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},
963 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"))},
964 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700965}
966
Joe Tsai87b955b2018-11-14 21:59:49 -0800967type EnumProto2 int32
968
969var enumProto2Type = ptype.GoEnum(
970 mustMakeEnumDesc(ptype.StandaloneEnum{
971 Syntax: pref.Proto2,
972 FullName: "EnumProto2",
973 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
974 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800975 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800976 return EnumProto2(n)
977 },
978)
979
980func (e EnumProto2) Enum() *EnumProto2 { return &e }
981func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
982func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800983
984type EnumProto3 int32
985
986var enumProto3Type = ptype.GoEnum(
987 mustMakeEnumDesc(ptype.StandaloneEnum{
988 Syntax: pref.Proto3,
989 FullName: "EnumProto3",
990 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
991 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800992 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800993 return EnumProto3(n)
994 },
995)
996
997func (e EnumProto3) Enum() *EnumProto3 { return &e }
998func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
999func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001000
1001type EnumMessages struct {
1002 EnumP2 *EnumProto2 `protobuf:"1"`
1003 EnumP3 *EnumProto3 `protobuf:"2"`
1004 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
1005 MessageCycle *EnumMessages `protobuf:"4"`
1006 EnumList []EnumProto2 `protobuf:"5"`
1007 MessageList []*ScalarProto2 `protobuf:"6"`
1008 EnumMap map[string]EnumProto3 `protobuf:"7"`
1009 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
1010 Union isEnumMessages_Union `protobuf_oneof:"union"`
1011}
1012
Damien Neil8012b442019-01-18 09:32:24 -08001013var enumMessagesType = pimpl.MessageType{GoType: reflect.TypeOf(new(EnumMessages)), PBType: ptype.GoMessage(
Joe Tsai87b955b2018-11-14 21:59:49 -08001014 mustMakeMessageDesc(ptype.StandaloneMessage{
1015 Syntax: pref.Proto2,
1016 FullName: "EnumMessages",
1017 Fields: []ptype.Field{
1018 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
1019 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
Joe Tsai08e00302018-11-26 22:32:06 -08001020 {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 -08001021 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
1022 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
Damien Neil8012b442019-01-18 09:32:24 -08001023 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001024 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
1025 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
1026 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
1027 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
Damien Neil8012b442019-01-18 09:32:24 -08001028 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType},
1029 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001030 },
1031 Oneofs: []ptype.Oneof{{Name: "union"}},
1032 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001033 func(pref.MessageType) pref.Message {
Joe Tsai87b955b2018-11-14 21:59:49 -08001034 return new(EnumMessages)
1035 },
1036)}
1037
1038var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1039 Syntax: pref.Proto2,
1040 FullName: "EnumMessages.F7Entry",
1041 Fields: []ptype.Field{
1042 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
1043 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
1044 },
Damien Neil232ea152018-12-10 15:14:36 -08001045 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1046 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001047})
1048
1049var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1050 Syntax: pref.Proto2,
1051 FullName: "EnumMessages.F8Entry",
1052 Fields: []ptype.Field{
1053 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Damien Neil8012b442019-01-18 09:32:24 -08001054 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001055 },
Damien Neil232ea152018-12-10 15:14:36 -08001056 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1057 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001058})
1059
Damien Neil374cdb82019-01-29 16:45:30 -08001060func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType }
1061func (m *EnumMessages) KnownFields() pref.KnownFields {
1062 return enumMessagesType.MessageOf(m).KnownFields()
1063}
1064func (m *EnumMessages) UnknownFields() pref.UnknownFields {
1065 return enumMessagesType.MessageOf(m).UnknownFields()
1066}
1067func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
1068func (m *EnumMessages) ProtoReflect() pref.Message { return m }
Joe Tsai87b955b2018-11-14 21:59:49 -08001069
Joe Tsaif18ab532018-11-27 17:25:04 -08001070func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1071 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001072 (*EnumMessages_OneofE2)(nil),
1073 (*EnumMessages_OneofE3)(nil),
1074 (*EnumMessages_OneofM2)(nil),
1075 (*EnumMessages_OneofM3)(nil),
1076 }
1077}
1078
1079type (
1080 isEnumMessages_Union interface {
1081 isEnumMessages_Union()
1082 }
1083 EnumMessages_OneofE2 struct {
1084 OneofE2 EnumProto2 `protobuf:"9"`
1085 }
1086 EnumMessages_OneofE3 struct {
1087 OneofE3 EnumProto3 `protobuf:"10"`
1088 }
1089 EnumMessages_OneofM2 struct {
1090 OneofM2 *ScalarProto2 `protobuf:"11"`
1091 }
1092 EnumMessages_OneofM3 struct {
1093 OneofM3 *ScalarProto3 `protobuf:"12"`
1094 }
1095)
1096
1097func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1098func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1099func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1100func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1101
1102func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001103 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001104 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001105 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1106 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001107 wantM3a := &ScalarProto3{Float32: math.Pi}
1108 wantM3b := &ScalarProto3{Float32: math.Ln2}
1109
1110 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1111 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1112
1113 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1114 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1115
1116 testMessage(t, nil, &EnumMessages{}, messageOps{
1117 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 -08001118 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 -08001119
1120 // Test singular enums.
1121 setFields{1: VE(0xdead), 2: VE(0)},
1122 getFields{1: VE(0xdead), 2: VE(0)},
1123 hasFields{1: true, 2: true},
1124
1125 // Test singular messages.
1126 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1127 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1128 getFields{3: V(wantL), 4: V(wantM)},
1129 clearFields{3, 4},
1130 hasFields{3: false, 4: false},
1131 setFields{3: V(wantL), 4: V(wantM)},
1132 hasFields{3: true, 4: true},
1133
1134 // Test list of enums and messages.
1135 listFields{
1136 5: listOps{
1137 appendList{VE(111), VE(222)},
1138 setList{0: VE(333)},
1139 getList{0: VE(333), 1: VE(222)},
1140 lenList(2),
1141 },
1142 6: listOps{
1143 appendMessageList{setFields{4: V(uint32(1e6))}},
1144 appendMessageList{setFields{6: V(float32(math.Phi))}},
1145 setList{0: V(wantM2a)},
1146 getList{0: V(wantM2a), 1: V(wantM2b)},
1147 },
1148 },
1149 getFields{5: wantList5, 6: wantList6},
1150 hasFields{5: true, 6: true},
1151 listFields{5: listOps{truncList(0)}},
1152 hasFields{5: false, 6: true},
1153
1154 // Test maps of enums and messages.
1155 mapFields{
1156 7: mapOps{
1157 setMap{"one": VE(1), "two": VE(2)},
1158 hasMap{"one": true, "two": true, "three": false},
1159 lenMap(2),
1160 },
1161 8: mapOps{
1162 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1163 setMap{"ln2": V(wantM3b)},
1164 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1165 lenMap(2),
1166 },
1167 },
1168 getFields{7: wantMap7, 8: wantMap8},
1169 hasFields{7: true, 8: true},
1170 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1171 hasFields{7: true, 8: false},
1172
1173 // Test oneofs of enums and messages.
1174 setFields{9: VE(0xdead)},
1175 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1176 setFields{10: VE(0)},
1177 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1178 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1179 getFields{11: V(wantM2a)},
1180 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1181 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1182 getFields{12: V(wantM3a)},
1183 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1184
1185 // Check entire message.
1186 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1187 equalMessage{&EnumMessages{
1188 EnumP2: EnumProto2(0xdead).Enum(),
1189 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001190 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001191 MessageCycle: wantM,
1192 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1193 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1194 Union: &EnumMessages_OneofM3{wantM3a},
1195 }},
1196 clearFields{1, 2, 3, 4, 6, 7, 12},
1197 equalMessage{&EnumMessages{}},
1198 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001199
1200 // Test read-only operations on nil message.
1201 testMessage(t, nil, (*EnumMessages)(nil), messageOps{
1202 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},
1203 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)},
1204 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1205 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1206 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001207}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001208
Joe Tsai91e14662018-09-13 13:24:35 -07001209var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001210 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1211 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001212 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001213 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1214 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001215 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001216 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001217 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001218 }),
1219 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001220 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001221}
1222
1223func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1224 fs := m.KnownFields()
1225 for i, op := range tt {
1226 p.Push(i)
1227 switch op := op.(type) {
1228 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001229 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001230 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1231 }
1232 case hasFields:
1233 got := map[pref.FieldNumber]bool{}
1234 want := map[pref.FieldNumber]bool(op)
1235 for n := range want {
1236 got[n] = fs.Has(n)
1237 }
1238 if diff := cmp.Diff(want, got); diff != "" {
1239 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1240 }
1241 case getFields:
1242 got := map[pref.FieldNumber]pref.Value{}
1243 want := map[pref.FieldNumber]pref.Value(op)
1244 for n := range want {
1245 got[n] = fs.Get(n)
1246 }
1247 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1248 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1249 }
1250 case setFields:
1251 for n, v := range op {
1252 fs.Set(n, v)
1253 }
1254 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001255 for _, n := range op {
1256 fs.Clear(n)
1257 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001258 case whichOneofs:
1259 got := map[pref.Name]pref.FieldNumber{}
1260 want := map[pref.Name]pref.FieldNumber(op)
1261 for s := range want {
1262 got[s] = fs.WhichOneof(s)
1263 }
1264 if diff := cmp.Diff(want, got); diff != "" {
1265 t.Errorf("operation %v, KnownFields.WhichOneof mismatch (-want, +got):\n%s", p, diff)
1266 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001267 case messageFields:
1268 for n, tt := range op {
1269 p.Push(int(n))
Damien Neil97e7f572018-12-07 14:28:33 -08001270 if !fs.Has(n) {
1271 fs.Set(n, V(fs.NewMessage(n)))
1272 }
1273 testMessage(t, p, fs.Get(n).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001274 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001275 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001276 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001277 for n, tt := range op {
1278 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001279 testLists(t, p, fs.Get(n).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001280 p.Pop()
1281 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001282 case mapFields:
1283 for n, tt := range op {
1284 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001285 testMaps(t, p, fs.Get(n).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001286 p.Pop()
1287 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001288 case rangeFields:
1289 got := map[pref.FieldNumber]pref.Value{}
1290 want := map[pref.FieldNumber]pref.Value(op)
1291 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1292 got[n] = v
1293 return true
1294 })
1295 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1296 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1297 }
Joe Tsai91e14662018-09-13 13:24:35 -07001298 default:
1299 t.Fatalf("operation %v, invalid operation: %T", p, op)
1300 }
1301 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001302 }
1303}
Joe Tsai91e14662018-09-13 13:24:35 -07001304
Joe Tsai4b7aff62018-11-14 14:05:19 -08001305func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001306 for i, op := range tt {
1307 p.Push(i)
1308 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001309 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001310 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001311 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001312 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001313 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001314 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001315 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001316 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001317 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001318 got := map[int]pref.Value{}
1319 want := map[int]pref.Value(op)
1320 for n := range want {
1321 got[n] = v.Get(n)
1322 }
1323 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001324 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001325 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001326 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001327 for n, e := range op {
1328 v.Set(n, e)
1329 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001330 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001331 for _, e := range op {
1332 v.Append(e)
1333 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001334 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001335 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001336 v.Append(V(m))
1337 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001338 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001339 v.Truncate(int(op))
1340 default:
1341 t.Fatalf("operation %v, invalid operation: %T", p, op)
1342 }
1343 p.Pop()
1344 }
1345}
1346
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001347func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1348 for i, op := range tt {
1349 p.Push(i)
1350 switch op := op.(type) {
1351 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001352 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001353 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1354 }
1355 case lenMap:
1356 if got, want := m.Len(), int(op); got != want {
1357 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1358 }
1359 case hasMap:
1360 got := map[interface{}]bool{}
1361 want := map[interface{}]bool(op)
1362 for k := range want {
1363 got[k] = m.Has(V(k).MapKey())
1364 }
1365 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1366 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1367 }
1368 case getMap:
1369 got := map[interface{}]pref.Value{}
1370 want := map[interface{}]pref.Value(op)
1371 for k := range want {
1372 got[k] = m.Get(V(k).MapKey())
1373 }
1374 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1375 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1376 }
1377 case setMap:
1378 for k, v := range op {
1379 m.Set(V(k).MapKey(), v)
1380 }
1381 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001382 for _, k := range op {
1383 m.Clear(V(k).MapKey())
1384 }
1385 case messageMap:
1386 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001387 mk := V(k).MapKey()
1388 if !m.Has(mk) {
1389 m.Set(mk, V(m.NewMessage()))
1390 }
1391 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001392 }
1393 case rangeMap:
1394 got := map[interface{}]pref.Value{}
1395 want := map[interface{}]pref.Value(op)
1396 m.Range(func(k pref.MapKey, v pref.Value) bool {
1397 got[k.Interface()] = v
1398 return true
1399 })
1400 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1401 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1402 }
1403 default:
1404 t.Fatalf("operation %v, invalid operation: %T", p, op)
1405 }
1406 p.Pop()
1407 }
1408}
1409
Joe Tsai91e14662018-09-13 13:24:35 -07001410type path []int
1411
1412func (p *path) Push(i int) { *p = append(*p, i) }
1413func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1414func (p path) String() string {
1415 var ss []string
1416 for _, i := range p {
1417 ss = append(ss, fmt.Sprint(i))
1418 }
1419 return strings.Join(ss, ".")
1420}