blob: 6d82456921c2a87defbcdfc6fba5e0c6952e4461 [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
5package impl
6
7import (
Joe Tsai91e14662018-09-13 13:24:35 -07008 "fmt"
9 "math"
10 "strings"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070011 "testing"
12
Damien Neil204f1c02018-10-23 15:03:38 -070013 protoV1 "github.com/golang/protobuf/proto"
14 descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
Joe Tsai009e0672018-11-27 18:45:07 -080015 scalar "github.com/golang/protobuf/v2/internal/scalar"
Joe Tsai01ab2962018-09-21 17:44:00 -070016 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
17 ptype "github.com/golang/protobuf/v2/reflect/prototype"
Joe Tsai87b955b2018-11-14 21:59:49 -080018 cmp "github.com/google/go-cmp/cmp"
19 cmpopts "github.com/google/go-cmp/cmp/cmpopts"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070020
Joe Tsai87b955b2018-11-14 21:59:49 -080021 proto2_20180125 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
22 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070023)
24
Joe Tsai4b7aff62018-11-14 14:05:19 -080025// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070026type (
Joe Tsai87b955b2018-11-14 21:59:49 -080027 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070028 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070029
Joe Tsai87b955b2018-11-14 21:59:49 -080030 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080031 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070032
Joe Tsai87b955b2018-11-14 21:59:49 -080033 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000034 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070035)
36
37// Test operations performed on a message.
38type (
Joe Tsai87b955b2018-11-14 21:59:49 -080039 // check that the message contents match
40 equalMessage struct{ pref.Message }
41 // check presence for specific fields in the message
42 hasFields map[pref.FieldNumber]bool
43 // check that specific message fields match
44 getFields map[pref.FieldNumber]pref.Value
45 // set specific message fields
46 setFields map[pref.FieldNumber]pref.Value
47 // clear specific fields in the message
48 clearFields []pref.FieldNumber
49 // apply messageOps on each specified message field
Joe Tsai91e14662018-09-13 13:24:35 -070050 messageFields map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080051 // apply listOps on each specified list field
52 listFields map[pref.FieldNumber]listOps
53 // apply mapOps on each specified map fields
54 mapFields map[pref.FieldNumber]mapOps
55 // range through all fields and check that they match
56 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070057)
58
Joe Tsai87b955b2018-11-14 21:59:49 -080059func (equalMessage) isMessageOp() {}
60func (hasFields) isMessageOp() {}
61func (getFields) isMessageOp() {}
62func (setFields) isMessageOp() {}
63func (clearFields) isMessageOp() {}
64func (messageFields) isMessageOp() {}
65func (listFields) isMessageOp() {}
66func (mapFields) isMessageOp() {}
67func (rangeFields) isMessageOp() {}
68
Joe Tsai4b7aff62018-11-14 14:05:19 -080069// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070070type (
Joe Tsai87b955b2018-11-14 21:59:49 -080071 // check that the list contents match
72 equalList struct{ pref.List }
73 // check that list length matches
74 lenList int
75 // check that specific list entries match
76 getList map[int]pref.Value
77 // set specific list entries
78 setList map[int]pref.Value
79 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080080 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080081 // apply messageOps on a newly appended message
82 appendMessageList messageOps
83 // truncate the list to the specified length
84 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070085)
86
Joe Tsai87b955b2018-11-14 21:59:49 -080087func (equalList) isListOp() {}
88func (lenList) isListOp() {}
89func (getList) isListOp() {}
90func (setList) isListOp() {}
91func (appendList) isListOp() {}
92func (appendMessageList) isListOp() {}
93func (truncList) isListOp() {}
94
Joe Tsaibbfaeb72018-10-17 00:27:21 +000095// Test operations performed on a map.
96type (
Joe Tsai87b955b2018-11-14 21:59:49 -080097 // check that the map contents match
98 equalMap struct{ pref.Map }
99 // check that map length matches
100 lenMap int
101 // check presence for specific entries in the map
102 hasMap map[interface{}]bool
103 // check that specific map entries match
104 getMap map[interface{}]pref.Value
105 // set specific map entries
106 setMap map[interface{}]pref.Value
107 // clear specific entries in the map
108 clearMap []interface{}
109 // apply messageOps on each specified message entry
110 messageMap map[interface{}]messageOps
111 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000112 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000113)
114
Joe Tsai87b955b2018-11-14 21:59:49 -0800115func (equalMap) isMapOp() {}
116func (lenMap) isMapOp() {}
117func (hasMap) isMapOp() {}
118func (getMap) isMapOp() {}
119func (setMap) isMapOp() {}
120func (clearMap) isMapOp() {}
121func (messageMap) isMapOp() {}
122func (rangeMap) isMapOp() {}
123
Joe Tsaice6edd32018-10-19 16:27:46 -0700124type ScalarProto2 struct {
125 Bool *bool `protobuf:"1"`
126 Int32 *int32 `protobuf:"2"`
127 Int64 *int64 `protobuf:"3"`
128 Uint32 *uint32 `protobuf:"4"`
129 Uint64 *uint64 `protobuf:"5"`
130 Float32 *float32 `protobuf:"6"`
131 Float64 *float64 `protobuf:"7"`
132 String *string `protobuf:"8"`
133 StringA []byte `protobuf:"9"`
134 Bytes []byte `protobuf:"10"`
135 BytesA *string `protobuf:"11"`
136
137 MyBool *MyBool `protobuf:"12"`
138 MyInt32 *MyInt32 `protobuf:"13"`
139 MyInt64 *MyInt64 `protobuf:"14"`
140 MyUint32 *MyUint32 `protobuf:"15"`
141 MyUint64 *MyUint64 `protobuf:"16"`
142 MyFloat32 *MyFloat32 `protobuf:"17"`
143 MyFloat64 *MyFloat64 `protobuf:"18"`
144 MyString *MyString `protobuf:"19"`
145 MyStringA MyBytes `protobuf:"20"`
146 MyBytes MyBytes `protobuf:"21"`
147 MyBytesA *MyString `protobuf:"22"`
148}
149
Joe Tsai87b955b2018-11-14 21:59:49 -0800150func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
151 ed, err := ptype.NewEnum(&t)
152 if err != nil {
153 panic(err)
154 }
155 return ed
156}
157
158func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
159 md, err := ptype.NewMessage(&t)
160 if err != nil {
161 panic(err)
162 }
163 return md
164}
165
166var V = pref.ValueOf
167var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
168
169type (
170 MyBool bool
171 MyInt32 int32
172 MyInt64 int64
173 MyUint32 uint32
174 MyUint64 uint64
175 MyFloat32 float32
176 MyFloat64 float64
177 MyString string
178 MyBytes []byte
179
180 ListStrings []MyString
181 ListBytes []MyBytes
182
183 MapStrings map[MyString]MyString
184 MapBytes map[MyString]MyBytes
185)
186
Joe Tsaif0c01e42018-11-06 13:05:20 -0800187var scalarProto2Type = MessageType{Type: ptype.GoMessage(
188 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700189 Syntax: pref.Proto2,
190 FullName: "ScalarProto2",
191 Fields: []ptype.Field{
192 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
193 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
194 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
195 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
196 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
197 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
198 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
199 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
200 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
201 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
202 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
203
204 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
205 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
206 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
207 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
208 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
209 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
210 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
211 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
212 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
213 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
214 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
215 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800216 }),
217 func(pref.MessageType) pref.ProtoMessage {
218 return new(ScalarProto2)
219 },
220)}
Joe Tsai91e14662018-09-13 13:24:35 -0700221
Joe Tsaif0c01e42018-11-06 13:05:20 -0800222func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.Type }
223func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) }
224func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) }
225func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
226func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
227func (m *ScalarProto2) ProtoMutable() {}
228
229func TestScalarProto2(t *testing.T) {
230 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700231 hasFields{
232 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
233 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
234 },
235 getFields{
236 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
237 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
238 },
239 setFields{
240 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
241 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
242 },
243 hasFields{
244 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
245 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
246 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800247 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700248 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
249 new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
Joe Tsai87b955b2018-11-14 21:59:49 -0800250 }},
251 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
252 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700253 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700254}
255
Joe Tsaice6edd32018-10-19 16:27:46 -0700256type ScalarProto3 struct {
257 Bool bool `protobuf:"1"`
258 Int32 int32 `protobuf:"2"`
259 Int64 int64 `protobuf:"3"`
260 Uint32 uint32 `protobuf:"4"`
261 Uint64 uint64 `protobuf:"5"`
262 Float32 float32 `protobuf:"6"`
263 Float64 float64 `protobuf:"7"`
264 String string `protobuf:"8"`
265 StringA []byte `protobuf:"9"`
266 Bytes []byte `protobuf:"10"`
267 BytesA string `protobuf:"11"`
268
269 MyBool MyBool `protobuf:"12"`
270 MyInt32 MyInt32 `protobuf:"13"`
271 MyInt64 MyInt64 `protobuf:"14"`
272 MyUint32 MyUint32 `protobuf:"15"`
273 MyUint64 MyUint64 `protobuf:"16"`
274 MyFloat32 MyFloat32 `protobuf:"17"`
275 MyFloat64 MyFloat64 `protobuf:"18"`
276 MyString MyString `protobuf:"19"`
277 MyStringA MyBytes `protobuf:"20"`
278 MyBytes MyBytes `protobuf:"21"`
279 MyBytesA MyString `protobuf:"22"`
280}
281
Joe Tsaif0c01e42018-11-06 13:05:20 -0800282var scalarProto3Type = MessageType{Type: ptype.GoMessage(
283 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700284 Syntax: pref.Proto3,
285 FullName: "ScalarProto3",
286 Fields: []ptype.Field{
287 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
288 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
289 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
290 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
291 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
292 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
293 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
294 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
295 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
296 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
297 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700298
Joe Tsai91e14662018-09-13 13:24:35 -0700299 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
300 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
301 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
302 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
303 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
304 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
305 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
306 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
307 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
308 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
309 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
310 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800311 }),
312 func(pref.MessageType) pref.ProtoMessage {
313 return new(ScalarProto3)
314 },
315)}
Joe Tsai91e14662018-09-13 13:24:35 -0700316
Joe Tsaif0c01e42018-11-06 13:05:20 -0800317func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.Type }
318func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) }
319func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) }
320func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
321func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
322func (m *ScalarProto3) ProtoMutable() {}
323
324func TestScalarProto3(t *testing.T) {
325 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700326 hasFields{
327 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
328 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
329 },
330 getFields{
331 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
332 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
333 },
334 setFields{
335 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
336 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
337 },
338 hasFields{
339 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
340 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
341 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800342 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700343 setFields{
344 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
345 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
346 },
347 hasFields{
348 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
349 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
350 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800351 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700352 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
353 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800354 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800355 setFields{
356 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
357 },
358 hasFields{
359 2: true, 3: true, 6: true, 7: true,
360 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800361 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
362 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700363 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700364}
365
Joe Tsaif0c01e42018-11-06 13:05:20 -0800366type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700367 Bools []bool `protobuf:"1"`
368 Int32s []int32 `protobuf:"2"`
369 Int64s []int64 `protobuf:"3"`
370 Uint32s []uint32 `protobuf:"4"`
371 Uint64s []uint64 `protobuf:"5"`
372 Float32s []float32 `protobuf:"6"`
373 Float64s []float64 `protobuf:"7"`
374 Strings []string `protobuf:"8"`
375 StringsA [][]byte `protobuf:"9"`
376 Bytes [][]byte `protobuf:"10"`
377 BytesA []string `protobuf:"11"`
378
379 MyStrings1 []MyString `protobuf:"12"`
380 MyStrings2 []MyBytes `protobuf:"13"`
381 MyBytes1 []MyBytes `protobuf:"14"`
382 MyBytes2 []MyString `protobuf:"15"`
383
Joe Tsai4b7aff62018-11-14 14:05:19 -0800384 MyStrings3 ListStrings `protobuf:"16"`
385 MyStrings4 ListBytes `protobuf:"17"`
386 MyBytes3 ListBytes `protobuf:"18"`
387 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700388}
389
Joe Tsaif0c01e42018-11-06 13:05:20 -0800390var listScalarsType = MessageType{Type: ptype.GoMessage(
391 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700392 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800393 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700394 Fields: []ptype.Field{
395 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
396 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
397 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
398 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
399 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
400 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
401 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
402 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
403 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
404 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
405 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700406
Joe Tsai91e14662018-09-13 13:24:35 -0700407 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
408 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
409 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
410 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
411
412 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
413 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
414 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
415 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700416 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800417 }),
418 func(pref.MessageType) pref.ProtoMessage {
419 return new(ListScalars)
420 },
421)}
Joe Tsai91e14662018-09-13 13:24:35 -0700422
Joe Tsaif0c01e42018-11-06 13:05:20 -0800423func (m *ListScalars) Type() pref.MessageType { return listScalarsType.Type }
424func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) }
425func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) }
426func (m *ListScalars) Interface() pref.ProtoMessage { return m }
427func (m *ListScalars) ProtoReflect() pref.Message { return m }
428func (m *ListScalars) ProtoMutable() {}
429
430func TestListScalars(t *testing.T) {
431 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700432 emptyFS := empty.KnownFields()
433
Joe Tsaif0c01e42018-11-06 13:05:20 -0800434 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700435 Bools: []bool{true, false, true},
436 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
437 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
438 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
439 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
440 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
441 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
442 Strings: []string{"8", "", "eight"},
443 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
444 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
445 BytesA: []string{"11", "", "eleven"},
446
447 MyStrings1: []MyString{"12", "", "twelve"},
448 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
449 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
450 MyBytes2: []MyString{"15", "", "fifteen"},
451
Joe Tsai4b7aff62018-11-14 14:05:19 -0800452 MyStrings3: ListStrings{"16", "", "sixteen"},
453 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
454 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
455 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800456 }
Joe Tsai91e14662018-09-13 13:24:35 -0700457 wantFS := want.KnownFields()
458
Joe Tsaif0c01e42018-11-06 13:05:20 -0800459 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700460 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false},
461 getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19)},
462 setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19)},
Joe Tsai4b7aff62018-11-14 14:05:19 -0800463 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700464 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800465 lenList(0),
466 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
467 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800468 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700469 },
470 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800471 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
472 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
473 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700474 },
475 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800476 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800477 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700478 },
479 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800480 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
481 lenList(6),
482 setList{0: V("8"), 2: V("eight")},
483 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800484 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700485 },
486 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800487 appendList{V([]byte(nil)), V([]byte(nil))},
488 setList{0: V([]byte("10"))},
489 appendList{V([]byte("wrong"))},
490 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800491 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700492 },
493 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800494 appendList{V("12"), V("wrong"), V("twelve")},
495 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800496 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700497 },
498 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800499 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800500 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700501 },
502 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800503 appendList{V("16"), V(""), V("sixteen"), V("extra")},
504 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800505 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700506 },
507 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800508 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800509 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700510 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700511 },
Joe Tsai91e14662018-09-13 13:24:35 -0700512 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 -0800513 equalMessage{want},
514 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
515 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000516 })
517}
518
Joe Tsaice6edd32018-10-19 16:27:46 -0700519type MapScalars struct {
520 KeyBools map[bool]string `protobuf:"1"`
521 KeyInt32s map[int32]string `protobuf:"2"`
522 KeyInt64s map[int64]string `protobuf:"3"`
523 KeyUint32s map[uint32]string `protobuf:"4"`
524 KeyUint64s map[uint64]string `protobuf:"5"`
525 KeyStrings map[string]string `protobuf:"6"`
526
527 ValBools map[string]bool `protobuf:"7"`
528 ValInt32s map[string]int32 `protobuf:"8"`
529 ValInt64s map[string]int64 `protobuf:"9"`
530 ValUint32s map[string]uint32 `protobuf:"10"`
531 ValUint64s map[string]uint64 `protobuf:"11"`
532 ValFloat32s map[string]float32 `protobuf:"12"`
533 ValFloat64s map[string]float64 `protobuf:"13"`
534 ValStrings map[string]string `protobuf:"14"`
535 ValStringsA map[string][]byte `protobuf:"15"`
536 ValBytes map[string][]byte `protobuf:"16"`
537 ValBytesA map[string]string `protobuf:"17"`
538
539 MyStrings1 map[MyString]MyString `protobuf:"18"`
540 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
541 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
542 MyBytes2 map[MyString]MyString `protobuf:"21"`
543
544 MyStrings3 MapStrings `protobuf:"22"`
545 MyStrings4 MapBytes `protobuf:"23"`
546 MyBytes3 MapBytes `protobuf:"24"`
547 MyBytes4 MapStrings `protobuf:"25"`
548}
549
Joe Tsaif0c01e42018-11-06 13:05:20 -0800550func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
551 return ptype.Field{
552 Name: pref.Name(fmt.Sprintf("f%d", n)),
553 Number: n,
554 Cardinality: pref.Repeated,
555 Kind: pref.MessageKind,
556 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
557 Syntax: pref.Proto2,
558 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
559 Fields: []ptype.Field{
560 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
561 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
562 },
Joe Tsai009e0672018-11-27 18:45:07 -0800563 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800564 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000565 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800566}
567
568var mapScalarsType = MessageType{Type: ptype.GoMessage(
569 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000570 Syntax: pref.Proto2,
571 FullName: "MapScalars",
572 Fields: []ptype.Field{
573 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
574 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
575 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
576 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
577 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
578 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
579
580 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
581 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
582 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
583 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
584 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
585 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
586 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
587 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
588 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
589 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
590 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
591
592 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
593 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
594 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
595 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
596
597 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
598 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
599 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
600 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
601 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800602 }),
603 func(pref.MessageType) pref.ProtoMessage {
604 return new(MapScalars)
605 },
606)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000607
Joe Tsaif0c01e42018-11-06 13:05:20 -0800608func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.Type }
609func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) }
610func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) }
611func (m *MapScalars) Interface() pref.ProtoMessage { return m }
612func (m *MapScalars) ProtoReflect() pref.Message { return m }
613func (m *MapScalars) ProtoMutable() {}
614
615func TestMapScalars(t *testing.T) {
616 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000617 emptyFS := empty.KnownFields()
618
Joe Tsaif0c01e42018-11-06 13:05:20 -0800619 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000620 KeyBools: map[bool]string{true: "true", false: "false"},
621 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
622 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
623 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
624 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
625 KeyStrings: map[string]string{"": "", "foo": "bar"},
626
627 ValBools: map[string]bool{"true": true, "false": false},
628 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
629 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
630 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
631 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
632 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
633 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
634 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
635 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
636 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
637 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
638
639 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
640 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
641 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
642 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
643
644 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
645 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
646 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
647 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800648 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000649 wantFS := want.KnownFields()
650
Joe Tsaif0c01e42018-11-06 13:05:20 -0800651 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000652 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false},
653 getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19), 21: emptyFS.Get(21), 23: emptyFS.Get(23), 25: emptyFS.Get(25)},
654 setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19), 21: wantFS.Get(21), 23: wantFS.Get(23), 25: wantFS.Get(25)},
655 mapFields{
656 2: {
657 lenMap(0),
658 hasMap{int32(0): false, int32(-1): false, int32(2): false},
659 setMap{int32(0): V("zero")},
660 lenMap(1),
661 hasMap{int32(0): true, int32(-1): false, int32(2): false},
662 setMap{int32(-1): V("one")},
663 lenMap(2),
664 hasMap{int32(0): true, int32(-1): true, int32(2): false},
665 setMap{int32(2): V("two")},
666 lenMap(3),
667 hasMap{int32(0): true, int32(-1): true, int32(2): true},
668 },
669 4: {
670 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800671 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000672 },
673 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800674 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000675 setMap{"foo": V("bar")},
676 setMap{"": V("empty")},
677 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
678 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800679 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000680 },
681 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800682 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000683 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
684 },
685 10: {
686 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
687 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800688 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000689 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
690 },
691 12: {
692 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800693 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000694 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
695 },
696 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800697 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000698 setMap{"s1": V("s1"), "s2": V("s2")},
699 },
700 16: {
701 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800702 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000703 },
704 18: {
705 hasMap{"s1": false, "s2": false, "s3": false},
706 setMap{"s1": V("s1"), "s2": V("s2")},
707 hasMap{"s1": true, "s2": true, "s3": false},
708 },
709 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800710 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000711 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
712 },
713 22: {
714 rangeMap{},
715 setMap{"s1": V("s1"), "s2": V("s2")},
716 rangeMap{"s1": V("s1"), "s2": V("s2")},
717 lenMap(2),
718 },
719 24: {
720 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800721 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000722 },
723 },
724 hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, 23: true, 24: true, 25: true},
Joe Tsai87b955b2018-11-14 21:59:49 -0800725 equalMessage{want},
726 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
727 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700728 })
729}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700730
Joe Tsai87b955b2018-11-14 21:59:49 -0800731type OneofScalars struct {
732 Union isOneofScalars_Union `protobuf_oneof:"union"`
733}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700734
Joe Tsaif0c01e42018-11-06 13:05:20 -0800735var oneofScalarsType = MessageType{Type: ptype.GoMessage(
736 mustMakeMessageDesc(ptype.StandaloneMessage{
737 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800738 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800739 Fields: []ptype.Field{
740 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
741 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
742 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
743 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
744 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
745 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
746 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
747 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
748 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
749 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
750 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
751 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
752 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
753 },
754 Oneofs: []ptype.Oneof{{Name: "union"}},
755 }),
756 func(pref.MessageType) pref.ProtoMessage {
757 return new(OneofScalars)
758 },
759)}
760
761func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.Type }
762func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) }
763func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) }
764func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
765func (m *OneofScalars) ProtoReflect() pref.Message { return m }
766func (m *OneofScalars) ProtoMutable() {}
767
Joe Tsaif18ab532018-11-27 17:25:04 -0800768func (*OneofScalars) XXX_OneofWrappers() []interface{} {
769 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700770 (*OneofScalars_Bool)(nil),
771 (*OneofScalars_Int32)(nil),
772 (*OneofScalars_Int64)(nil),
773 (*OneofScalars_Uint32)(nil),
774 (*OneofScalars_Uint64)(nil),
775 (*OneofScalars_Float32)(nil),
776 (*OneofScalars_Float64)(nil),
777 (*OneofScalars_String)(nil),
778 (*OneofScalars_StringA)(nil),
779 (*OneofScalars_StringB)(nil),
780 (*OneofScalars_Bytes)(nil),
781 (*OneofScalars_BytesA)(nil),
782 (*OneofScalars_BytesB)(nil),
783 }
784}
785
Joe Tsai87b955b2018-11-14 21:59:49 -0800786type (
787 isOneofScalars_Union interface {
788 isOneofScalars_Union()
789 }
790 OneofScalars_Bool struct {
791 Bool bool `protobuf:"1"`
792 }
793 OneofScalars_Int32 struct {
794 Int32 MyInt32 `protobuf:"2"`
795 }
796 OneofScalars_Int64 struct {
797 Int64 int64 `protobuf:"3"`
798 }
799 OneofScalars_Uint32 struct {
800 Uint32 MyUint32 `protobuf:"4"`
801 }
802 OneofScalars_Uint64 struct {
803 Uint64 uint64 `protobuf:"5"`
804 }
805 OneofScalars_Float32 struct {
806 Float32 MyFloat32 `protobuf:"6"`
807 }
808 OneofScalars_Float64 struct {
809 Float64 float64 `protobuf:"7"`
810 }
811 OneofScalars_String struct {
812 String string `protobuf:"8"`
813 }
814 OneofScalars_StringA struct {
815 StringA []byte `protobuf:"9"`
816 }
817 OneofScalars_StringB struct {
818 StringB MyString `protobuf:"10"`
819 }
820 OneofScalars_Bytes struct {
821 Bytes []byte `protobuf:"11"`
822 }
823 OneofScalars_BytesA struct {
824 BytesA string `protobuf:"12"`
825 }
826 OneofScalars_BytesB struct {
827 BytesB MyBytes `protobuf:"13"`
828 }
829)
830
Joe Tsai2c870bb2018-10-17 11:46:52 -0700831func (*OneofScalars_Bool) isOneofScalars_Union() {}
832func (*OneofScalars_Int32) isOneofScalars_Union() {}
833func (*OneofScalars_Int64) isOneofScalars_Union() {}
834func (*OneofScalars_Uint32) isOneofScalars_Union() {}
835func (*OneofScalars_Uint64) isOneofScalars_Union() {}
836func (*OneofScalars_Float32) isOneofScalars_Union() {}
837func (*OneofScalars_Float64) isOneofScalars_Union() {}
838func (*OneofScalars_String) isOneofScalars_Union() {}
839func (*OneofScalars_StringA) isOneofScalars_Union() {}
840func (*OneofScalars_StringB) isOneofScalars_Union() {}
841func (*OneofScalars_Bytes) isOneofScalars_Union() {}
842func (*OneofScalars_BytesA) isOneofScalars_Union() {}
843func (*OneofScalars_BytesB) isOneofScalars_Union() {}
844
845func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800846 empty := &OneofScalars{}
847 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
848 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
849 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
850 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
851 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
852 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
853 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
854 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
855 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
856 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
857 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
858 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
859 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700860
Joe Tsaif0c01e42018-11-06 13:05:20 -0800861 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700862 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
863 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))},
864
Joe Tsai87b955b2018-11-14 21:59:49 -0800865 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
866 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
867 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
868 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
869 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
870 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
871 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
872 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
873 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
874 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
875 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
876 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
877 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700878
879 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: true},
880 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("130"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800881 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
882 equalMessage{want13},
883 clearFields{13},
884 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700885 })
886}
887
Joe Tsai87b955b2018-11-14 21:59:49 -0800888type EnumProto2 int32
889
890var enumProto2Type = ptype.GoEnum(
891 mustMakeEnumDesc(ptype.StandaloneEnum{
892 Syntax: pref.Proto2,
893 FullName: "EnumProto2",
894 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
895 }),
896 func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
897 return EnumProto2(n)
898 },
899)
900
901func (e EnumProto2) Enum() *EnumProto2 { return &e }
902func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
903func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
904func (e EnumProto2) ProtoReflect() pref.Enum { return e }
905
906type EnumProto3 int32
907
908var enumProto3Type = ptype.GoEnum(
909 mustMakeEnumDesc(ptype.StandaloneEnum{
910 Syntax: pref.Proto3,
911 FullName: "EnumProto3",
912 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
913 }),
914 func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
915 return EnumProto3(n)
916 },
917)
918
919func (e EnumProto3) Enum() *EnumProto3 { return &e }
920func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
921func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
922func (e EnumProto3) ProtoReflect() pref.Enum { return e }
923
924type EnumMessages struct {
925 EnumP2 *EnumProto2 `protobuf:"1"`
926 EnumP3 *EnumProto3 `protobuf:"2"`
927 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
928 MessageCycle *EnumMessages `protobuf:"4"`
929 EnumList []EnumProto2 `protobuf:"5"`
930 MessageList []*ScalarProto2 `protobuf:"6"`
931 EnumMap map[string]EnumProto3 `protobuf:"7"`
932 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
933 Union isEnumMessages_Union `protobuf_oneof:"union"`
934}
935
936var enumMessagesType = MessageType{Type: ptype.GoMessage(
937 mustMakeMessageDesc(ptype.StandaloneMessage{
938 Syntax: pref.Proto2,
939 FullName: "EnumMessages",
940 Fields: []ptype.Field{
941 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
942 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
943 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: MessageOf(new(proto2_20180125.Message)).Type()},
944 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
945 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
946 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.Type},
947 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
948 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
949 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
950 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
951 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.Type},
952 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.Type},
953 },
954 Oneofs: []ptype.Oneof{{Name: "union"}},
955 }),
956 func(pref.MessageType) pref.ProtoMessage {
957 return new(EnumMessages)
958 },
959)}
960
961var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
962 Syntax: pref.Proto2,
963 FullName: "EnumMessages.F7Entry",
964 Fields: []ptype.Field{
965 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
966 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
967 },
Joe Tsai009e0672018-11-27 18:45:07 -0800968 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsai87b955b2018-11-14 21:59:49 -0800969})
970
971var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
972 Syntax: pref.Proto2,
973 FullName: "EnumMessages.F8Entry",
974 Fields: []ptype.Field{
975 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
976 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.Type},
977 },
Joe Tsai009e0672018-11-27 18:45:07 -0800978 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsai87b955b2018-11-14 21:59:49 -0800979})
980
981func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.Type }
982func (m *EnumMessages) KnownFields() pref.KnownFields { return enumMessagesType.KnownFieldsOf(m) }
983func (m *EnumMessages) UnknownFields() pref.UnknownFields { return enumMessagesType.UnknownFieldsOf(m) }
984func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
985func (m *EnumMessages) ProtoReflect() pref.Message { return m }
986func (m *EnumMessages) ProtoMutable() {}
987
Joe Tsaif18ab532018-11-27 17:25:04 -0800988func (*EnumMessages) XXX_OneofWrappers() []interface{} {
989 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -0800990 (*EnumMessages_OneofE2)(nil),
991 (*EnumMessages_OneofE3)(nil),
992 (*EnumMessages_OneofM2)(nil),
993 (*EnumMessages_OneofM3)(nil),
994 }
995}
996
997type (
998 isEnumMessages_Union interface {
999 isEnumMessages_Union()
1000 }
1001 EnumMessages_OneofE2 struct {
1002 OneofE2 EnumProto2 `protobuf:"9"`
1003 }
1004 EnumMessages_OneofE3 struct {
1005 OneofE3 EnumProto3 `protobuf:"10"`
1006 }
1007 EnumMessages_OneofM2 struct {
1008 OneofM2 *ScalarProto2 `protobuf:"11"`
1009 }
1010 EnumMessages_OneofM3 struct {
1011 OneofM3 *ScalarProto3 `protobuf:"12"`
1012 }
1013)
1014
1015func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1016func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1017func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1018func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1019
1020func TestEnumMessages(t *testing.T) {
Joe Tsai009e0672018-11-27 18:45:07 -08001021 wantL := MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001022 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001023 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1024 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001025 wantM3a := &ScalarProto3{Float32: math.Pi}
1026 wantM3b := &ScalarProto3{Float32: math.Ln2}
1027
1028 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1029 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1030
1031 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1032 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1033
1034 testMessage(t, nil, &EnumMessages{}, messageOps{
1035 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false},
Joe Tsaif6d4a422018-11-19 14:26:06 -08001036 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 -08001037
1038 // Test singular enums.
1039 setFields{1: VE(0xdead), 2: VE(0)},
1040 getFields{1: VE(0xdead), 2: VE(0)},
1041 hasFields{1: true, 2: true},
1042
1043 // Test singular messages.
1044 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1045 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1046 getFields{3: V(wantL), 4: V(wantM)},
1047 clearFields{3, 4},
1048 hasFields{3: false, 4: false},
1049 setFields{3: V(wantL), 4: V(wantM)},
1050 hasFields{3: true, 4: true},
1051
1052 // Test list of enums and messages.
1053 listFields{
1054 5: listOps{
1055 appendList{VE(111), VE(222)},
1056 setList{0: VE(333)},
1057 getList{0: VE(333), 1: VE(222)},
1058 lenList(2),
1059 },
1060 6: listOps{
1061 appendMessageList{setFields{4: V(uint32(1e6))}},
1062 appendMessageList{setFields{6: V(float32(math.Phi))}},
1063 setList{0: V(wantM2a)},
1064 getList{0: V(wantM2a), 1: V(wantM2b)},
1065 },
1066 },
1067 getFields{5: wantList5, 6: wantList6},
1068 hasFields{5: true, 6: true},
1069 listFields{5: listOps{truncList(0)}},
1070 hasFields{5: false, 6: true},
1071
1072 // Test maps of enums and messages.
1073 mapFields{
1074 7: mapOps{
1075 setMap{"one": VE(1), "two": VE(2)},
1076 hasMap{"one": true, "two": true, "three": false},
1077 lenMap(2),
1078 },
1079 8: mapOps{
1080 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1081 setMap{"ln2": V(wantM3b)},
1082 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1083 lenMap(2),
1084 },
1085 },
1086 getFields{7: wantMap7, 8: wantMap8},
1087 hasFields{7: true, 8: true},
1088 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1089 hasFields{7: true, 8: false},
1090
1091 // Test oneofs of enums and messages.
1092 setFields{9: VE(0xdead)},
1093 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1094 setFields{10: VE(0)},
1095 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1096 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1097 getFields{11: V(wantM2a)},
1098 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1099 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1100 getFields{12: V(wantM3a)},
1101 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1102
1103 // Check entire message.
1104 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1105 equalMessage{&EnumMessages{
1106 EnumP2: EnumProto2(0xdead).Enum(),
1107 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001108 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001109 MessageCycle: wantM,
1110 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1111 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1112 Union: &EnumMessages_OneofM3{wantM3a},
1113 }},
1114 clearFields{1, 2, 3, 4, 6, 7, 12},
1115 equalMessage{&EnumMessages{}},
1116 })
1117}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001118
Joe Tsai91e14662018-09-13 13:24:35 -07001119var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001120 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1121 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001122 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001123 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1124 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001125 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001126 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
1127 return x.Unwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001128 }),
1129 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001130 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001131}
1132
1133func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1134 fs := m.KnownFields()
1135 for i, op := range tt {
1136 p.Push(i)
1137 switch op := op.(type) {
1138 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001139 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001140 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1141 }
1142 case hasFields:
1143 got := map[pref.FieldNumber]bool{}
1144 want := map[pref.FieldNumber]bool(op)
1145 for n := range want {
1146 got[n] = fs.Has(n)
1147 }
1148 if diff := cmp.Diff(want, got); diff != "" {
1149 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1150 }
1151 case getFields:
1152 got := map[pref.FieldNumber]pref.Value{}
1153 want := map[pref.FieldNumber]pref.Value(op)
1154 for n := range want {
1155 got[n] = fs.Get(n)
1156 }
1157 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1158 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1159 }
1160 case setFields:
1161 for n, v := range op {
1162 fs.Set(n, v)
1163 }
1164 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001165 for _, n := range op {
1166 fs.Clear(n)
1167 }
1168 case messageFields:
1169 for n, tt := range op {
1170 p.Push(int(n))
1171 testMessage(t, p, fs.Mutable(n).(pref.Message), tt)
1172 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001173 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001174 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001175 for n, tt := range op {
1176 p.Push(int(n))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001177 testLists(t, p, fs.Mutable(n).(pref.List), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001178 p.Pop()
1179 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001180 case mapFields:
1181 for n, tt := range op {
1182 p.Push(int(n))
1183 testMaps(t, p, fs.Mutable(n).(pref.Map), tt)
1184 p.Pop()
1185 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001186 case rangeFields:
1187 got := map[pref.FieldNumber]pref.Value{}
1188 want := map[pref.FieldNumber]pref.Value(op)
1189 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1190 got[n] = v
1191 return true
1192 })
1193 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1194 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1195 }
Joe Tsai91e14662018-09-13 13:24:35 -07001196 default:
1197 t.Fatalf("operation %v, invalid operation: %T", p, op)
1198 }
1199 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001200 }
1201}
Joe Tsai91e14662018-09-13 13:24:35 -07001202
Joe Tsai4b7aff62018-11-14 14:05:19 -08001203func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001204 for i, op := range tt {
1205 p.Push(i)
1206 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001207 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001208 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001209 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001210 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001211 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001212 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001213 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001214 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001215 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001216 got := map[int]pref.Value{}
1217 want := map[int]pref.Value(op)
1218 for n := range want {
1219 got[n] = v.Get(n)
1220 }
1221 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001222 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001223 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001224 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001225 for n, e := range op {
1226 v.Set(n, e)
1227 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001228 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001229 for _, e := range op {
1230 v.Append(e)
1231 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001232 case appendMessageList:
1233 testMessage(t, p, v.MutableAppend().(pref.Message), messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001234 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001235 v.Truncate(int(op))
1236 default:
1237 t.Fatalf("operation %v, invalid operation: %T", p, op)
1238 }
1239 p.Pop()
1240 }
1241}
1242
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001243func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1244 for i, op := range tt {
1245 p.Push(i)
1246 switch op := op.(type) {
1247 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001248 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001249 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1250 }
1251 case lenMap:
1252 if got, want := m.Len(), int(op); got != want {
1253 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1254 }
1255 case hasMap:
1256 got := map[interface{}]bool{}
1257 want := map[interface{}]bool(op)
1258 for k := range want {
1259 got[k] = m.Has(V(k).MapKey())
1260 }
1261 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1262 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1263 }
1264 case getMap:
1265 got := map[interface{}]pref.Value{}
1266 want := map[interface{}]pref.Value(op)
1267 for k := range want {
1268 got[k] = m.Get(V(k).MapKey())
1269 }
1270 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1271 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1272 }
1273 case setMap:
1274 for k, v := range op {
1275 m.Set(V(k).MapKey(), v)
1276 }
1277 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001278 for _, k := range op {
1279 m.Clear(V(k).MapKey())
1280 }
1281 case messageMap:
1282 for k, tt := range op {
1283 testMessage(t, p, m.Mutable(V(k).MapKey()).(pref.Message), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001284 }
1285 case rangeMap:
1286 got := map[interface{}]pref.Value{}
1287 want := map[interface{}]pref.Value(op)
1288 m.Range(func(k pref.MapKey, v pref.Value) bool {
1289 got[k.Interface()] = v
1290 return true
1291 })
1292 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1293 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1294 }
1295 default:
1296 t.Fatalf("operation %v, invalid operation: %T", p, op)
1297 }
1298 p.Pop()
1299 }
1300}
1301
Joe Tsai91e14662018-09-13 13:24:35 -07001302type path []int
1303
1304func (p *path) Push(i int) { *p = append(*p, i) }
1305func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1306func (p path) String() string {
1307 var ss []string
1308 for _, i := range p {
1309 ss = append(ss, fmt.Sprint(i))
1310 }
1311 return strings.Join(ss, ".")
1312}