blob: b7f3770591df14ff436d2224ebc51396c14fdc09 [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 Tsai009e0672018-11-27 18:45:07 -080016 scalar "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsai08e00302018-11-26 22:32:06 -080017 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai01ab2962018-09-21 17:44:00 -070018 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
19 ptype "github.com/golang/protobuf/v2/reflect/prototype"
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
55 // apply messageOps on each specified message field
Joe Tsai91e14662018-09-13 13:24:35 -070056 messageFields map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080057 // apply listOps on each specified list field
58 listFields map[pref.FieldNumber]listOps
59 // apply mapOps on each specified map fields
60 mapFields map[pref.FieldNumber]mapOps
61 // range through all fields and check that they match
62 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070063)
64
Joe Tsai87b955b2018-11-14 21:59:49 -080065func (equalMessage) isMessageOp() {}
66func (hasFields) isMessageOp() {}
67func (getFields) isMessageOp() {}
68func (setFields) isMessageOp() {}
69func (clearFields) isMessageOp() {}
70func (messageFields) isMessageOp() {}
71func (listFields) isMessageOp() {}
72func (mapFields) isMessageOp() {}
73func (rangeFields) isMessageOp() {}
74
Joe Tsai4b7aff62018-11-14 14:05:19 -080075// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070076type (
Joe Tsai87b955b2018-11-14 21:59:49 -080077 // check that the list contents match
78 equalList struct{ pref.List }
79 // check that list length matches
80 lenList int
81 // check that specific list entries match
82 getList map[int]pref.Value
83 // set specific list entries
84 setList map[int]pref.Value
85 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080086 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080087 // apply messageOps on a newly appended message
88 appendMessageList messageOps
89 // truncate the list to the specified length
90 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070091)
92
Joe Tsai87b955b2018-11-14 21:59:49 -080093func (equalList) isListOp() {}
94func (lenList) isListOp() {}
95func (getList) isListOp() {}
96func (setList) isListOp() {}
97func (appendList) isListOp() {}
98func (appendMessageList) isListOp() {}
99func (truncList) isListOp() {}
100
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000101// Test operations performed on a map.
102type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800103 // check that the map contents match
104 equalMap struct{ pref.Map }
105 // check that map length matches
106 lenMap int
107 // check presence for specific entries in the map
108 hasMap map[interface{}]bool
109 // check that specific map entries match
110 getMap map[interface{}]pref.Value
111 // set specific map entries
112 setMap map[interface{}]pref.Value
113 // clear specific entries in the map
114 clearMap []interface{}
115 // apply messageOps on each specified message entry
116 messageMap map[interface{}]messageOps
117 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000118 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000119)
120
Joe Tsai87b955b2018-11-14 21:59:49 -0800121func (equalMap) isMapOp() {}
122func (lenMap) isMapOp() {}
123func (hasMap) isMapOp() {}
124func (getMap) isMapOp() {}
125func (setMap) isMapOp() {}
126func (clearMap) isMapOp() {}
127func (messageMap) isMapOp() {}
128func (rangeMap) isMapOp() {}
129
Joe Tsaice6edd32018-10-19 16:27:46 -0700130type ScalarProto2 struct {
131 Bool *bool `protobuf:"1"`
132 Int32 *int32 `protobuf:"2"`
133 Int64 *int64 `protobuf:"3"`
134 Uint32 *uint32 `protobuf:"4"`
135 Uint64 *uint64 `protobuf:"5"`
136 Float32 *float32 `protobuf:"6"`
137 Float64 *float64 `protobuf:"7"`
138 String *string `protobuf:"8"`
139 StringA []byte `protobuf:"9"`
140 Bytes []byte `protobuf:"10"`
141 BytesA *string `protobuf:"11"`
142
143 MyBool *MyBool `protobuf:"12"`
144 MyInt32 *MyInt32 `protobuf:"13"`
145 MyInt64 *MyInt64 `protobuf:"14"`
146 MyUint32 *MyUint32 `protobuf:"15"`
147 MyUint64 *MyUint64 `protobuf:"16"`
148 MyFloat32 *MyFloat32 `protobuf:"17"`
149 MyFloat64 *MyFloat64 `protobuf:"18"`
150 MyString *MyString `protobuf:"19"`
151 MyStringA MyBytes `protobuf:"20"`
152 MyBytes MyBytes `protobuf:"21"`
153 MyBytesA *MyString `protobuf:"22"`
154}
155
Joe Tsai87b955b2018-11-14 21:59:49 -0800156func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
157 ed, err := ptype.NewEnum(&t)
158 if err != nil {
159 panic(err)
160 }
161 return ed
162}
163
164func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
165 md, err := ptype.NewMessage(&t)
166 if err != nil {
167 panic(err)
168 }
169 return md
170}
171
172var V = pref.ValueOf
173var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
174
175type (
176 MyBool bool
177 MyInt32 int32
178 MyInt64 int64
179 MyUint32 uint32
180 MyUint64 uint64
181 MyFloat32 float32
182 MyFloat64 float64
183 MyString string
184 MyBytes []byte
185
186 ListStrings []MyString
187 ListBytes []MyBytes
188
189 MapStrings map[MyString]MyString
190 MapBytes map[MyString]MyBytes
191)
192
Damien Neil8012b442019-01-18 09:32:24 -0800193var scalarProto2Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800194 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700195 Syntax: pref.Proto2,
196 FullName: "ScalarProto2",
197 Fields: []ptype.Field{
198 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
199 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
200 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
201 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
202 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
203 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
204 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
205 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
206 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
207 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
208 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
209
210 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
211 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
212 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
213 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
214 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
215 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
216 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
217 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
218 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
219 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
220 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
221 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800222 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800223 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800224 return new(ScalarProto2)
225 },
226)}
Joe Tsai91e14662018-09-13 13:24:35 -0700227
Damien Neil374cdb82019-01-29 16:45:30 -0800228func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.PBType }
229func (m *ScalarProto2) KnownFields() pref.KnownFields {
230 return scalarProto2Type.MessageOf(m).KnownFields()
231}
232func (m *ScalarProto2) UnknownFields() pref.UnknownFields {
233 return scalarProto2Type.MessageOf(m).UnknownFields()
234}
235func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
236func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800237
238func TestScalarProto2(t *testing.T) {
239 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700240 hasFields{
241 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
242 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
243 },
244 getFields{
245 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")),
246 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")),
247 },
248 setFields{
249 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)),
250 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)),
251 },
252 hasFields{
253 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
254 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
255 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800256 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700257 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
258 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 -0800259 }},
260 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
261 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700262 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800263
264 // Test read-only operations on nil message.
265 testMessage(t, nil, (*ScalarProto2)(nil), messageOps{
266 hasFields{
267 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
268 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
269 },
270 getFields{
271 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")),
272 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")),
273 },
274 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700275}
276
Joe Tsaice6edd32018-10-19 16:27:46 -0700277type ScalarProto3 struct {
278 Bool bool `protobuf:"1"`
279 Int32 int32 `protobuf:"2"`
280 Int64 int64 `protobuf:"3"`
281 Uint32 uint32 `protobuf:"4"`
282 Uint64 uint64 `protobuf:"5"`
283 Float32 float32 `protobuf:"6"`
284 Float64 float64 `protobuf:"7"`
285 String string `protobuf:"8"`
286 StringA []byte `protobuf:"9"`
287 Bytes []byte `protobuf:"10"`
288 BytesA string `protobuf:"11"`
289
290 MyBool MyBool `protobuf:"12"`
291 MyInt32 MyInt32 `protobuf:"13"`
292 MyInt64 MyInt64 `protobuf:"14"`
293 MyUint32 MyUint32 `protobuf:"15"`
294 MyUint64 MyUint64 `protobuf:"16"`
295 MyFloat32 MyFloat32 `protobuf:"17"`
296 MyFloat64 MyFloat64 `protobuf:"18"`
297 MyString MyString `protobuf:"19"`
298 MyStringA MyBytes `protobuf:"20"`
299 MyBytes MyBytes `protobuf:"21"`
300 MyBytesA MyString `protobuf:"22"`
301}
302
Damien Neil8012b442019-01-18 09:32:24 -0800303var scalarProto3Type = pimpl.MessageType{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800304 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700305 Syntax: pref.Proto3,
306 FullName: "ScalarProto3",
307 Fields: []ptype.Field{
308 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
309 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
310 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
311 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
312 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
313 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
314 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
315 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
316 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
317 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
318 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700319
Joe Tsai91e14662018-09-13 13:24:35 -0700320 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
321 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
322 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
323 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
324 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
325 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
326 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
327 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
328 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
329 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
330 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
331 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800332 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800333 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800334 return new(ScalarProto3)
335 },
336)}
Joe Tsai91e14662018-09-13 13:24:35 -0700337
Damien Neil374cdb82019-01-29 16:45:30 -0800338func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.PBType }
339func (m *ScalarProto3) KnownFields() pref.KnownFields {
340 return scalarProto3Type.MessageOf(m).KnownFields()
341}
342func (m *ScalarProto3) UnknownFields() pref.UnknownFields {
343 return scalarProto3Type.MessageOf(m).UnknownFields()
344}
345func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
346func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800347
348func TestScalarProto3(t *testing.T) {
349 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700350 hasFields{
351 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
352 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
353 },
354 getFields{
355 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)),
356 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)),
357 },
358 setFields{
359 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)),
360 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)),
361 },
362 hasFields{
363 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
364 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
365 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800366 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700367 setFields{
368 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")),
369 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")),
370 },
371 hasFields{
372 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
373 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
374 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800375 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700376 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
377 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800378 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800379 setFields{
380 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
381 },
382 hasFields{
383 2: true, 3: true, 6: true, 7: true,
384 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800385 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
386 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700387 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800388
389 // Test read-only operations on nil message.
390 testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
391 hasFields{
392 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
393 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
394 },
395 getFields{
396 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)),
397 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)),
398 },
399 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700400}
401
Joe Tsaif0c01e42018-11-06 13:05:20 -0800402type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700403 Bools []bool `protobuf:"1"`
404 Int32s []int32 `protobuf:"2"`
405 Int64s []int64 `protobuf:"3"`
406 Uint32s []uint32 `protobuf:"4"`
407 Uint64s []uint64 `protobuf:"5"`
408 Float32s []float32 `protobuf:"6"`
409 Float64s []float64 `protobuf:"7"`
410 Strings []string `protobuf:"8"`
411 StringsA [][]byte `protobuf:"9"`
412 Bytes [][]byte `protobuf:"10"`
413 BytesA []string `protobuf:"11"`
414
415 MyStrings1 []MyString `protobuf:"12"`
416 MyStrings2 []MyBytes `protobuf:"13"`
417 MyBytes1 []MyBytes `protobuf:"14"`
418 MyBytes2 []MyString `protobuf:"15"`
419
Joe Tsai4b7aff62018-11-14 14:05:19 -0800420 MyStrings3 ListStrings `protobuf:"16"`
421 MyStrings4 ListBytes `protobuf:"17"`
422 MyBytes3 ListBytes `protobuf:"18"`
423 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700424}
425
Damien Neil8012b442019-01-18 09:32:24 -0800426var listScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(ListScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800427 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700428 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800429 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700430 Fields: []ptype.Field{
431 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
432 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
433 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
434 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
435 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
436 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
437 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
438 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
439 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
440 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
441 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700442
Joe Tsai91e14662018-09-13 13:24:35 -0700443 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
444 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
445 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
446 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
447
448 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
449 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
450 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
451 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700452 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800453 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800454 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800455 return new(ListScalars)
456 },
457)}
Joe Tsai91e14662018-09-13 13:24:35 -0700458
Damien Neil374cdb82019-01-29 16:45:30 -0800459func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType }
460func (m *ListScalars) KnownFields() pref.KnownFields {
461 return listScalarsType.MessageOf(m).KnownFields()
462}
463func (m *ListScalars) UnknownFields() pref.UnknownFields {
464 return listScalarsType.MessageOf(m).UnknownFields()
465}
466func (m *ListScalars) Interface() pref.ProtoMessage { return m }
467func (m *ListScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800468
469func TestListScalars(t *testing.T) {
470 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700471 emptyFS := empty.KnownFields()
472
Joe Tsaif0c01e42018-11-06 13:05:20 -0800473 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700474 Bools: []bool{true, false, true},
475 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
476 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
477 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
478 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
479 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
480 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
481 Strings: []string{"8", "", "eight"},
482 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
483 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
484 BytesA: []string{"11", "", "eleven"},
485
486 MyStrings1: []MyString{"12", "", "twelve"},
487 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
488 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
489 MyBytes2: []MyString{"15", "", "fifteen"},
490
Joe Tsai4b7aff62018-11-14 14:05:19 -0800491 MyStrings3: ListStrings{"16", "", "sixteen"},
492 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
493 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
494 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800495 }
Joe Tsai91e14662018-09-13 13:24:35 -0700496 wantFS := want.KnownFields()
497
Joe Tsaif0c01e42018-11-06 13:05:20 -0800498 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700499 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},
500 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)},
501 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 -0800502 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700503 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800504 lenList(0),
505 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
506 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800507 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700508 },
509 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800510 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
511 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
512 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700513 },
514 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800515 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800516 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700517 },
518 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800519 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
520 lenList(6),
521 setList{0: V("8"), 2: V("eight")},
522 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800523 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700524 },
525 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800526 appendList{V([]byte(nil)), V([]byte(nil))},
527 setList{0: V([]byte("10"))},
528 appendList{V([]byte("wrong"))},
529 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800530 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700531 },
532 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800533 appendList{V("12"), V("wrong"), V("twelve")},
534 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800535 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700536 },
537 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800538 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800539 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700540 },
541 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800542 appendList{V("16"), V(""), V("sixteen"), V("extra")},
543 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800544 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700545 },
546 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800547 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800548 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700549 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700550 },
Joe Tsai91e14662018-09-13 13:24:35 -0700551 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 -0800552 equalMessage{want},
553 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
554 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000555 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800556
557 // Test read-only operations on nil message.
558 testMessage(t, nil, (*ListScalars)(nil), messageOps{
559 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},
560 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)}},
561 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000562}
563
Joe Tsaice6edd32018-10-19 16:27:46 -0700564type MapScalars struct {
565 KeyBools map[bool]string `protobuf:"1"`
566 KeyInt32s map[int32]string `protobuf:"2"`
567 KeyInt64s map[int64]string `protobuf:"3"`
568 KeyUint32s map[uint32]string `protobuf:"4"`
569 KeyUint64s map[uint64]string `protobuf:"5"`
570 KeyStrings map[string]string `protobuf:"6"`
571
572 ValBools map[string]bool `protobuf:"7"`
573 ValInt32s map[string]int32 `protobuf:"8"`
574 ValInt64s map[string]int64 `protobuf:"9"`
575 ValUint32s map[string]uint32 `protobuf:"10"`
576 ValUint64s map[string]uint64 `protobuf:"11"`
577 ValFloat32s map[string]float32 `protobuf:"12"`
578 ValFloat64s map[string]float64 `protobuf:"13"`
579 ValStrings map[string]string `protobuf:"14"`
580 ValStringsA map[string][]byte `protobuf:"15"`
581 ValBytes map[string][]byte `protobuf:"16"`
582 ValBytesA map[string]string `protobuf:"17"`
583
584 MyStrings1 map[MyString]MyString `protobuf:"18"`
585 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
586 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
587 MyBytes2 map[MyString]MyString `protobuf:"21"`
588
589 MyStrings3 MapStrings `protobuf:"22"`
590 MyStrings4 MapBytes `protobuf:"23"`
591 MyBytes3 MapBytes `protobuf:"24"`
592 MyBytes4 MapStrings `protobuf:"25"`
593}
594
Joe Tsaif0c01e42018-11-06 13:05:20 -0800595func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
596 return ptype.Field{
597 Name: pref.Name(fmt.Sprintf("f%d", n)),
598 Number: n,
599 Cardinality: pref.Repeated,
600 Kind: pref.MessageKind,
601 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
602 Syntax: pref.Proto2,
603 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
604 Fields: []ptype.Field{
605 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
606 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
607 },
Damien Neil232ea152018-12-10 15:14:36 -0800608 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
609 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800610 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000611 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800612}
613
Damien Neil8012b442019-01-18 09:32:24 -0800614var mapScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(MapScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800615 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000616 Syntax: pref.Proto2,
617 FullName: "MapScalars",
618 Fields: []ptype.Field{
619 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
620 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
621 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
622 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
623 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
624 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
625
626 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
627 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
628 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
629 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
630 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
631 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
632 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
633 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
634 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
635 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
636 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
637
638 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
639 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
640 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
641 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
642
643 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
644 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
645 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
646 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
647 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800648 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800649 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800650 return new(MapScalars)
651 },
652)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000653
Damien Neil374cdb82019-01-29 16:45:30 -0800654func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType }
655func (m *MapScalars) KnownFields() pref.KnownFields {
656 return mapScalarsType.MessageOf(m).KnownFields()
657}
658func (m *MapScalars) UnknownFields() pref.UnknownFields {
659 return mapScalarsType.MessageOf(m).UnknownFields()
660}
661func (m *MapScalars) Interface() pref.ProtoMessage { return m }
662func (m *MapScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800663
664func TestMapScalars(t *testing.T) {
665 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000666 emptyFS := empty.KnownFields()
667
Joe Tsaif0c01e42018-11-06 13:05:20 -0800668 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000669 KeyBools: map[bool]string{true: "true", false: "false"},
670 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
671 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
672 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
673 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
674 KeyStrings: map[string]string{"": "", "foo": "bar"},
675
676 ValBools: map[string]bool{"true": true, "false": false},
677 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
678 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
679 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
680 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
681 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
682 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
683 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
684 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
685 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
686 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
687
688 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
689 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
690 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
691 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
692
693 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
694 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
695 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
696 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800697 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000698 wantFS := want.KnownFields()
699
Joe Tsaif0c01e42018-11-06 13:05:20 -0800700 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000701 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},
702 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)},
703 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)},
704 mapFields{
705 2: {
706 lenMap(0),
707 hasMap{int32(0): false, int32(-1): false, int32(2): false},
708 setMap{int32(0): V("zero")},
709 lenMap(1),
710 hasMap{int32(0): true, int32(-1): false, int32(2): false},
711 setMap{int32(-1): V("one")},
712 lenMap(2),
713 hasMap{int32(0): true, int32(-1): true, int32(2): false},
714 setMap{int32(2): V("two")},
715 lenMap(3),
716 hasMap{int32(0): true, int32(-1): true, int32(2): true},
717 },
718 4: {
719 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800720 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000721 },
722 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800723 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000724 setMap{"foo": V("bar")},
725 setMap{"": V("empty")},
726 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
727 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800728 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000729 },
730 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800731 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
733 },
734 10: {
735 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
736 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800737 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000738 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
739 },
740 12: {
741 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800742 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000743 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
744 },
745 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800746 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000747 setMap{"s1": V("s1"), "s2": V("s2")},
748 },
749 16: {
750 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800751 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000752 },
753 18: {
754 hasMap{"s1": false, "s2": false, "s3": false},
755 setMap{"s1": V("s1"), "s2": V("s2")},
756 hasMap{"s1": true, "s2": true, "s3": false},
757 },
758 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800759 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000760 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
761 },
762 22: {
763 rangeMap{},
764 setMap{"s1": V("s1"), "s2": V("s2")},
765 rangeMap{"s1": V("s1"), "s2": V("s2")},
766 lenMap(2),
767 },
768 24: {
769 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800770 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000771 },
772 },
773 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 -0800774 equalMessage{want},
775 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},
776 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700777 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800778
779 // Test read-only operations on nil message.
780 testMessage(t, nil, (*MapScalars)(nil), messageOps{
781 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},
782 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)}},
783 })
Joe Tsai91e14662018-09-13 13:24:35 -0700784}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700785
Joe Tsai87b955b2018-11-14 21:59:49 -0800786type OneofScalars struct {
787 Union isOneofScalars_Union `protobuf_oneof:"union"`
788}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700789
Damien Neil8012b442019-01-18 09:32:24 -0800790var oneofScalarsType = pimpl.MessageType{GoType: reflect.TypeOf(new(OneofScalars)), PBType: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800791 mustMakeMessageDesc(ptype.StandaloneMessage{
792 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800793 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800794 Fields: []ptype.Field{
795 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
796 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
797 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
798 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
799 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
800 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
801 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
802 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
803 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
804 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
805 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
806 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
807 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
808 },
809 Oneofs: []ptype.Oneof{{Name: "union"}},
810 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800811 func(pref.MessageType) pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800812 return new(OneofScalars)
813 },
814)}
815
Damien Neil374cdb82019-01-29 16:45:30 -0800816func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType }
817func (m *OneofScalars) KnownFields() pref.KnownFields {
818 return oneofScalarsType.MessageOf(m).KnownFields()
819}
820func (m *OneofScalars) UnknownFields() pref.UnknownFields {
821 return oneofScalarsType.MessageOf(m).UnknownFields()
822}
823func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
824func (m *OneofScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800825
Joe Tsaif18ab532018-11-27 17:25:04 -0800826func (*OneofScalars) XXX_OneofWrappers() []interface{} {
827 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700828 (*OneofScalars_Bool)(nil),
829 (*OneofScalars_Int32)(nil),
830 (*OneofScalars_Int64)(nil),
831 (*OneofScalars_Uint32)(nil),
832 (*OneofScalars_Uint64)(nil),
833 (*OneofScalars_Float32)(nil),
834 (*OneofScalars_Float64)(nil),
835 (*OneofScalars_String)(nil),
836 (*OneofScalars_StringA)(nil),
837 (*OneofScalars_StringB)(nil),
838 (*OneofScalars_Bytes)(nil),
839 (*OneofScalars_BytesA)(nil),
840 (*OneofScalars_BytesB)(nil),
841 }
842}
843
Joe Tsai87b955b2018-11-14 21:59:49 -0800844type (
845 isOneofScalars_Union interface {
846 isOneofScalars_Union()
847 }
848 OneofScalars_Bool struct {
849 Bool bool `protobuf:"1"`
850 }
851 OneofScalars_Int32 struct {
852 Int32 MyInt32 `protobuf:"2"`
853 }
854 OneofScalars_Int64 struct {
855 Int64 int64 `protobuf:"3"`
856 }
857 OneofScalars_Uint32 struct {
858 Uint32 MyUint32 `protobuf:"4"`
859 }
860 OneofScalars_Uint64 struct {
861 Uint64 uint64 `protobuf:"5"`
862 }
863 OneofScalars_Float32 struct {
864 Float32 MyFloat32 `protobuf:"6"`
865 }
866 OneofScalars_Float64 struct {
867 Float64 float64 `protobuf:"7"`
868 }
869 OneofScalars_String struct {
870 String string `protobuf:"8"`
871 }
872 OneofScalars_StringA struct {
873 StringA []byte `protobuf:"9"`
874 }
875 OneofScalars_StringB struct {
876 StringB MyString `protobuf:"10"`
877 }
878 OneofScalars_Bytes struct {
879 Bytes []byte `protobuf:"11"`
880 }
881 OneofScalars_BytesA struct {
882 BytesA string `protobuf:"12"`
883 }
884 OneofScalars_BytesB struct {
885 BytesB MyBytes `protobuf:"13"`
886 }
887)
888
Joe Tsai2c870bb2018-10-17 11:46:52 -0700889func (*OneofScalars_Bool) isOneofScalars_Union() {}
890func (*OneofScalars_Int32) isOneofScalars_Union() {}
891func (*OneofScalars_Int64) isOneofScalars_Union() {}
892func (*OneofScalars_Uint32) isOneofScalars_Union() {}
893func (*OneofScalars_Uint64) isOneofScalars_Union() {}
894func (*OneofScalars_Float32) isOneofScalars_Union() {}
895func (*OneofScalars_Float64) isOneofScalars_Union() {}
896func (*OneofScalars_String) isOneofScalars_Union() {}
897func (*OneofScalars_StringA) isOneofScalars_Union() {}
898func (*OneofScalars_StringB) isOneofScalars_Union() {}
899func (*OneofScalars_Bytes) isOneofScalars_Union() {}
900func (*OneofScalars_BytesA) isOneofScalars_Union() {}
901func (*OneofScalars_BytesB) isOneofScalars_Union() {}
902
903func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800904 empty := &OneofScalars{}
905 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
906 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
907 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
908 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
909 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
910 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
911 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
912 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
913 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
914 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
915 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
916 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
917 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700918
Joe Tsaif0c01e42018-11-06 13:05:20 -0800919 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700920 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},
921 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"))},
922
Joe Tsai87b955b2018-11-14 21:59:49 -0800923 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
924 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
925 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
926 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
927 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
928 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
929 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
930 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
931 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
932 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
933 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
934 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
935 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700936
937 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},
938 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 -0800939 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
940 equalMessage{want13},
941 clearFields{13},
942 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700943 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800944
945 // Test read-only operations on nil message.
946 testMessage(t, nil, (*OneofScalars)(nil), messageOps{
947 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},
948 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"))},
949 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700950}
951
Joe Tsai87b955b2018-11-14 21:59:49 -0800952type EnumProto2 int32
953
954var enumProto2Type = ptype.GoEnum(
955 mustMakeEnumDesc(ptype.StandaloneEnum{
956 Syntax: pref.Proto2,
957 FullName: "EnumProto2",
958 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
959 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800960 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800961 return EnumProto2(n)
962 },
963)
964
965func (e EnumProto2) Enum() *EnumProto2 { return &e }
966func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
967func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800968
969type EnumProto3 int32
970
971var enumProto3Type = ptype.GoEnum(
972 mustMakeEnumDesc(ptype.StandaloneEnum{
973 Syntax: pref.Proto3,
974 FullName: "EnumProto3",
975 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
976 }),
Damien Neila8593ba2019-01-08 16:18:07 -0800977 func(_ pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800978 return EnumProto3(n)
979 },
980)
981
982func (e EnumProto3) Enum() *EnumProto3 { return &e }
983func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
984func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800985
986type EnumMessages struct {
987 EnumP2 *EnumProto2 `protobuf:"1"`
988 EnumP3 *EnumProto3 `protobuf:"2"`
989 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
990 MessageCycle *EnumMessages `protobuf:"4"`
991 EnumList []EnumProto2 `protobuf:"5"`
992 MessageList []*ScalarProto2 `protobuf:"6"`
993 EnumMap map[string]EnumProto3 `protobuf:"7"`
994 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
995 Union isEnumMessages_Union `protobuf_oneof:"union"`
996}
997
Damien Neil8012b442019-01-18 09:32:24 -0800998var enumMessagesType = pimpl.MessageType{GoType: reflect.TypeOf(new(EnumMessages)), PBType: ptype.GoMessage(
Joe Tsai87b955b2018-11-14 21:59:49 -0800999 mustMakeMessageDesc(ptype.StandaloneMessage{
1000 Syntax: pref.Proto2,
1001 FullName: "EnumMessages",
1002 Fields: []ptype.Field{
1003 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
1004 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
Joe Tsai08e00302018-11-26 22:32:06 -08001005 {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 -08001006 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
1007 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
Damien Neil8012b442019-01-18 09:32:24 -08001008 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001009 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
1010 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
1011 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
1012 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
Damien Neil8012b442019-01-18 09:32:24 -08001013 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType},
1014 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001015 },
1016 Oneofs: []ptype.Oneof{{Name: "union"}},
1017 }),
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001018 func(pref.MessageType) pref.Message {
Joe Tsai87b955b2018-11-14 21:59:49 -08001019 return new(EnumMessages)
1020 },
1021)}
1022
1023var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1024 Syntax: pref.Proto2,
1025 FullName: "EnumMessages.F7Entry",
1026 Fields: []ptype.Field{
1027 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
1028 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
1029 },
Damien Neil232ea152018-12-10 15:14:36 -08001030 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1031 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001032})
1033
1034var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1035 Syntax: pref.Proto2,
1036 FullName: "EnumMessages.F8Entry",
1037 Fields: []ptype.Field{
1038 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Damien Neil8012b442019-01-18 09:32:24 -08001039 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType},
Joe Tsai87b955b2018-11-14 21:59:49 -08001040 },
Damien Neil232ea152018-12-10 15:14:36 -08001041 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1042 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001043})
1044
Damien Neil374cdb82019-01-29 16:45:30 -08001045func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType }
1046func (m *EnumMessages) KnownFields() pref.KnownFields {
1047 return enumMessagesType.MessageOf(m).KnownFields()
1048}
1049func (m *EnumMessages) UnknownFields() pref.UnknownFields {
1050 return enumMessagesType.MessageOf(m).UnknownFields()
1051}
1052func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
1053func (m *EnumMessages) ProtoReflect() pref.Message { return m }
Joe Tsai87b955b2018-11-14 21:59:49 -08001054
Joe Tsaif18ab532018-11-27 17:25:04 -08001055func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1056 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001057 (*EnumMessages_OneofE2)(nil),
1058 (*EnumMessages_OneofE3)(nil),
1059 (*EnumMessages_OneofM2)(nil),
1060 (*EnumMessages_OneofM3)(nil),
1061 }
1062}
1063
1064type (
1065 isEnumMessages_Union interface {
1066 isEnumMessages_Union()
1067 }
1068 EnumMessages_OneofE2 struct {
1069 OneofE2 EnumProto2 `protobuf:"9"`
1070 }
1071 EnumMessages_OneofE3 struct {
1072 OneofE3 EnumProto3 `protobuf:"10"`
1073 }
1074 EnumMessages_OneofM2 struct {
1075 OneofM2 *ScalarProto2 `protobuf:"11"`
1076 }
1077 EnumMessages_OneofM3 struct {
1078 OneofM3 *ScalarProto3 `protobuf:"12"`
1079 }
1080)
1081
1082func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1083func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1084func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1085func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1086
1087func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001088 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001089 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001090 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1091 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001092 wantM3a := &ScalarProto3{Float32: math.Pi}
1093 wantM3b := &ScalarProto3{Float32: math.Ln2}
1094
1095 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1096 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1097
1098 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1099 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1100
1101 testMessage(t, nil, &EnumMessages{}, messageOps{
1102 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 -08001103 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 -08001104
1105 // Test singular enums.
1106 setFields{1: VE(0xdead), 2: VE(0)},
1107 getFields{1: VE(0xdead), 2: VE(0)},
1108 hasFields{1: true, 2: true},
1109
1110 // Test singular messages.
1111 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1112 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1113 getFields{3: V(wantL), 4: V(wantM)},
1114 clearFields{3, 4},
1115 hasFields{3: false, 4: false},
1116 setFields{3: V(wantL), 4: V(wantM)},
1117 hasFields{3: true, 4: true},
1118
1119 // Test list of enums and messages.
1120 listFields{
1121 5: listOps{
1122 appendList{VE(111), VE(222)},
1123 setList{0: VE(333)},
1124 getList{0: VE(333), 1: VE(222)},
1125 lenList(2),
1126 },
1127 6: listOps{
1128 appendMessageList{setFields{4: V(uint32(1e6))}},
1129 appendMessageList{setFields{6: V(float32(math.Phi))}},
1130 setList{0: V(wantM2a)},
1131 getList{0: V(wantM2a), 1: V(wantM2b)},
1132 },
1133 },
1134 getFields{5: wantList5, 6: wantList6},
1135 hasFields{5: true, 6: true},
1136 listFields{5: listOps{truncList(0)}},
1137 hasFields{5: false, 6: true},
1138
1139 // Test maps of enums and messages.
1140 mapFields{
1141 7: mapOps{
1142 setMap{"one": VE(1), "two": VE(2)},
1143 hasMap{"one": true, "two": true, "three": false},
1144 lenMap(2),
1145 },
1146 8: mapOps{
1147 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1148 setMap{"ln2": V(wantM3b)},
1149 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1150 lenMap(2),
1151 },
1152 },
1153 getFields{7: wantMap7, 8: wantMap8},
1154 hasFields{7: true, 8: true},
1155 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1156 hasFields{7: true, 8: false},
1157
1158 // Test oneofs of enums and messages.
1159 setFields{9: VE(0xdead)},
1160 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1161 setFields{10: VE(0)},
1162 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1163 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1164 getFields{11: V(wantM2a)},
1165 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1166 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1167 getFields{12: V(wantM3a)},
1168 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1169
1170 // Check entire message.
1171 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1172 equalMessage{&EnumMessages{
1173 EnumP2: EnumProto2(0xdead).Enum(),
1174 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001175 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001176 MessageCycle: wantM,
1177 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1178 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1179 Union: &EnumMessages_OneofM3{wantM3a},
1180 }},
1181 clearFields{1, 2, 3, 4, 6, 7, 12},
1182 equalMessage{&EnumMessages{}},
1183 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001184
1185 // Test read-only operations on nil message.
1186 testMessage(t, nil, (*EnumMessages)(nil), messageOps{
1187 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},
1188 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)},
1189 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1190 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1191 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001192}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001193
Joe Tsai91e14662018-09-13 13:24:35 -07001194var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001195 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1196 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001197 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001198 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1199 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001200 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001201 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001202 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001203 }),
1204 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001205 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001206}
1207
1208func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1209 fs := m.KnownFields()
1210 for i, op := range tt {
1211 p.Push(i)
1212 switch op := op.(type) {
1213 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001214 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001215 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1216 }
1217 case hasFields:
1218 got := map[pref.FieldNumber]bool{}
1219 want := map[pref.FieldNumber]bool(op)
1220 for n := range want {
1221 got[n] = fs.Has(n)
1222 }
1223 if diff := cmp.Diff(want, got); diff != "" {
1224 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1225 }
1226 case getFields:
1227 got := map[pref.FieldNumber]pref.Value{}
1228 want := map[pref.FieldNumber]pref.Value(op)
1229 for n := range want {
1230 got[n] = fs.Get(n)
1231 }
1232 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1233 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1234 }
1235 case setFields:
1236 for n, v := range op {
1237 fs.Set(n, v)
1238 }
1239 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001240 for _, n := range op {
1241 fs.Clear(n)
1242 }
1243 case messageFields:
1244 for n, tt := range op {
1245 p.Push(int(n))
Damien Neil97e7f572018-12-07 14:28:33 -08001246 if !fs.Has(n) {
1247 fs.Set(n, V(fs.NewMessage(n)))
1248 }
1249 testMessage(t, p, fs.Get(n).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001250 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001251 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001252 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001253 for n, tt := range op {
1254 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001255 testLists(t, p, fs.Get(n).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001256 p.Pop()
1257 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001258 case mapFields:
1259 for n, tt := range op {
1260 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001261 testMaps(t, p, fs.Get(n).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001262 p.Pop()
1263 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001264 case rangeFields:
1265 got := map[pref.FieldNumber]pref.Value{}
1266 want := map[pref.FieldNumber]pref.Value(op)
1267 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1268 got[n] = v
1269 return true
1270 })
1271 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1272 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1273 }
Joe Tsai91e14662018-09-13 13:24:35 -07001274 default:
1275 t.Fatalf("operation %v, invalid operation: %T", p, op)
1276 }
1277 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001278 }
1279}
Joe Tsai91e14662018-09-13 13:24:35 -07001280
Joe Tsai4b7aff62018-11-14 14:05:19 -08001281func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001282 for i, op := range tt {
1283 p.Push(i)
1284 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001285 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001286 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001287 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001288 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001289 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001290 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001291 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001292 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001293 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001294 got := map[int]pref.Value{}
1295 want := map[int]pref.Value(op)
1296 for n := range want {
1297 got[n] = v.Get(n)
1298 }
1299 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001300 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001301 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001302 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001303 for n, e := range op {
1304 v.Set(n, e)
1305 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001306 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001307 for _, e := range op {
1308 v.Append(e)
1309 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001310 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001311 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001312 v.Append(V(m))
1313 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001314 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001315 v.Truncate(int(op))
1316 default:
1317 t.Fatalf("operation %v, invalid operation: %T", p, op)
1318 }
1319 p.Pop()
1320 }
1321}
1322
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001323func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1324 for i, op := range tt {
1325 p.Push(i)
1326 switch op := op.(type) {
1327 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001328 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001329 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1330 }
1331 case lenMap:
1332 if got, want := m.Len(), int(op); got != want {
1333 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1334 }
1335 case hasMap:
1336 got := map[interface{}]bool{}
1337 want := map[interface{}]bool(op)
1338 for k := range want {
1339 got[k] = m.Has(V(k).MapKey())
1340 }
1341 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1342 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1343 }
1344 case getMap:
1345 got := map[interface{}]pref.Value{}
1346 want := map[interface{}]pref.Value(op)
1347 for k := range want {
1348 got[k] = m.Get(V(k).MapKey())
1349 }
1350 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1351 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1352 }
1353 case setMap:
1354 for k, v := range op {
1355 m.Set(V(k).MapKey(), v)
1356 }
1357 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001358 for _, k := range op {
1359 m.Clear(V(k).MapKey())
1360 }
1361 case messageMap:
1362 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001363 mk := V(k).MapKey()
1364 if !m.Has(mk) {
1365 m.Set(mk, V(m.NewMessage()))
1366 }
1367 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001368 }
1369 case rangeMap:
1370 got := map[interface{}]pref.Value{}
1371 want := map[interface{}]pref.Value(op)
1372 m.Range(func(k pref.MapKey, v pref.Value) bool {
1373 got[k.Interface()] = v
1374 return true
1375 })
1376 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1377 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1378 }
1379 default:
1380 t.Fatalf("operation %v, invalid operation: %T", p, op)
1381 }
1382 p.Pop()
1383 }
1384}
1385
Joe Tsai91e14662018-09-13 13:24:35 -07001386type path []int
1387
1388func (p *path) Push(i int) { *p = append(*p, i) }
1389func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1390func (p path) String() string {
1391 var ss []string
1392 for _, i := range p {
1393 ss = append(ss, fmt.Sprint(i))
1394 }
1395 return strings.Join(ss, ".")
1396}