blob: d388494e547f86d3baa2096df265ddc2ecb042e2 [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"
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 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 Tsaifa02f4e2018-09-12 16:20:37 -070028)
29
Joe Tsai4b7aff62018-11-14 14:05:19 -080030// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070031type (
Joe Tsai87b955b2018-11-14 21:59:49 -080032 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070033 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070034
Joe Tsai87b955b2018-11-14 21:59:49 -080035 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080036 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070037
Joe Tsai87b955b2018-11-14 21:59:49 -080038 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000039 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070040)
41
42// Test operations performed on a message.
43type (
Joe Tsai87b955b2018-11-14 21:59:49 -080044 // check that the message contents match
45 equalMessage struct{ pref.Message }
46 // check presence for specific fields in the message
47 hasFields map[pref.FieldNumber]bool
48 // check that specific message fields match
49 getFields map[pref.FieldNumber]pref.Value
50 // set specific message fields
51 setFields map[pref.FieldNumber]pref.Value
52 // clear specific fields in the message
53 clearFields []pref.FieldNumber
54 // apply messageOps on each specified message field
Joe Tsai91e14662018-09-13 13:24:35 -070055 messageFields map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080056 // apply listOps on each specified list field
57 listFields map[pref.FieldNumber]listOps
58 // apply mapOps on each specified map fields
59 mapFields map[pref.FieldNumber]mapOps
60 // range through all fields and check that they match
61 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070062)
63
Joe Tsai87b955b2018-11-14 21:59:49 -080064func (equalMessage) isMessageOp() {}
65func (hasFields) isMessageOp() {}
66func (getFields) isMessageOp() {}
67func (setFields) isMessageOp() {}
68func (clearFields) isMessageOp() {}
69func (messageFields) isMessageOp() {}
70func (listFields) isMessageOp() {}
71func (mapFields) isMessageOp() {}
72func (rangeFields) isMessageOp() {}
73
Joe Tsai4b7aff62018-11-14 14:05:19 -080074// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070075type (
Joe Tsai87b955b2018-11-14 21:59:49 -080076 // check that the list contents match
77 equalList struct{ pref.List }
78 // check that list length matches
79 lenList int
80 // check that specific list entries match
81 getList map[int]pref.Value
82 // set specific list entries
83 setList map[int]pref.Value
84 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080085 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080086 // apply messageOps on a newly appended message
87 appendMessageList messageOps
88 // truncate the list to the specified length
89 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070090)
91
Joe Tsai87b955b2018-11-14 21:59:49 -080092func (equalList) isListOp() {}
93func (lenList) isListOp() {}
94func (getList) isListOp() {}
95func (setList) isListOp() {}
96func (appendList) isListOp() {}
97func (appendMessageList) isListOp() {}
98func (truncList) isListOp() {}
99
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000100// Test operations performed on a map.
101type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800102 // check that the map contents match
103 equalMap struct{ pref.Map }
104 // check that map length matches
105 lenMap int
106 // check presence for specific entries in the map
107 hasMap map[interface{}]bool
108 // check that specific map entries match
109 getMap map[interface{}]pref.Value
110 // set specific map entries
111 setMap map[interface{}]pref.Value
112 // clear specific entries in the map
113 clearMap []interface{}
114 // apply messageOps on each specified message entry
115 messageMap map[interface{}]messageOps
116 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000117 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000118)
119
Joe Tsai87b955b2018-11-14 21:59:49 -0800120func (equalMap) isMapOp() {}
121func (lenMap) isMapOp() {}
122func (hasMap) isMapOp() {}
123func (getMap) isMapOp() {}
124func (setMap) isMapOp() {}
125func (clearMap) isMapOp() {}
126func (messageMap) isMapOp() {}
127func (rangeMap) isMapOp() {}
128
Joe Tsaice6edd32018-10-19 16:27:46 -0700129type ScalarProto2 struct {
130 Bool *bool `protobuf:"1"`
131 Int32 *int32 `protobuf:"2"`
132 Int64 *int64 `protobuf:"3"`
133 Uint32 *uint32 `protobuf:"4"`
134 Uint64 *uint64 `protobuf:"5"`
135 Float32 *float32 `protobuf:"6"`
136 Float64 *float64 `protobuf:"7"`
137 String *string `protobuf:"8"`
138 StringA []byte `protobuf:"9"`
139 Bytes []byte `protobuf:"10"`
140 BytesA *string `protobuf:"11"`
141
142 MyBool *MyBool `protobuf:"12"`
143 MyInt32 *MyInt32 `protobuf:"13"`
144 MyInt64 *MyInt64 `protobuf:"14"`
145 MyUint32 *MyUint32 `protobuf:"15"`
146 MyUint64 *MyUint64 `protobuf:"16"`
147 MyFloat32 *MyFloat32 `protobuf:"17"`
148 MyFloat64 *MyFloat64 `protobuf:"18"`
149 MyString *MyString `protobuf:"19"`
150 MyStringA MyBytes `protobuf:"20"`
151 MyBytes MyBytes `protobuf:"21"`
152 MyBytesA *MyString `protobuf:"22"`
153}
154
Joe Tsai87b955b2018-11-14 21:59:49 -0800155func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
156 ed, err := ptype.NewEnum(&t)
157 if err != nil {
158 panic(err)
159 }
160 return ed
161}
162
163func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
164 md, err := ptype.NewMessage(&t)
165 if err != nil {
166 panic(err)
167 }
168 return md
169}
170
171var V = pref.ValueOf
172var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
173
174type (
175 MyBool bool
176 MyInt32 int32
177 MyInt64 int64
178 MyUint32 uint32
179 MyUint64 uint64
180 MyFloat32 float32
181 MyFloat64 float64
182 MyString string
183 MyBytes []byte
184
185 ListStrings []MyString
186 ListBytes []MyBytes
187
188 MapStrings map[MyString]MyString
189 MapBytes map[MyString]MyBytes
190)
191
Joe Tsai08e00302018-11-26 22:32:06 -0800192var scalarProto2Type = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800193 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700194 Syntax: pref.Proto2,
195 FullName: "ScalarProto2",
196 Fields: []ptype.Field{
197 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
198 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
199 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
200 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
201 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
202 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
203 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
204 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
205 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
206 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
207 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
208
209 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
210 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
211 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
212 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
213 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
214 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
215 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
216 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
217 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
218 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
219 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
220 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800221 }),
222 func(pref.MessageType) pref.ProtoMessage {
223 return new(ScalarProto2)
224 },
225)}
Joe Tsai91e14662018-09-13 13:24:35 -0700226
Joe Tsaif0c01e42018-11-06 13:05:20 -0800227func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.Type }
228func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) }
229func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) }
230func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
231func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
232func (m *ScalarProto2) ProtoMutable() {}
233
234func TestScalarProto2(t *testing.T) {
235 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700236 hasFields{
237 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
238 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
239 },
240 getFields{
241 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")),
242 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")),
243 },
244 setFields{
245 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)),
246 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)),
247 },
248 hasFields{
249 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
250 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
251 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800252 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700253 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
254 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 -0800255 }},
256 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
257 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700258 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700259}
260
Joe Tsaice6edd32018-10-19 16:27:46 -0700261type ScalarProto3 struct {
262 Bool bool `protobuf:"1"`
263 Int32 int32 `protobuf:"2"`
264 Int64 int64 `protobuf:"3"`
265 Uint32 uint32 `protobuf:"4"`
266 Uint64 uint64 `protobuf:"5"`
267 Float32 float32 `protobuf:"6"`
268 Float64 float64 `protobuf:"7"`
269 String string `protobuf:"8"`
270 StringA []byte `protobuf:"9"`
271 Bytes []byte `protobuf:"10"`
272 BytesA string `protobuf:"11"`
273
274 MyBool MyBool `protobuf:"12"`
275 MyInt32 MyInt32 `protobuf:"13"`
276 MyInt64 MyInt64 `protobuf:"14"`
277 MyUint32 MyUint32 `protobuf:"15"`
278 MyUint64 MyUint64 `protobuf:"16"`
279 MyFloat32 MyFloat32 `protobuf:"17"`
280 MyFloat64 MyFloat64 `protobuf:"18"`
281 MyString MyString `protobuf:"19"`
282 MyStringA MyBytes `protobuf:"20"`
283 MyBytes MyBytes `protobuf:"21"`
284 MyBytesA MyString `protobuf:"22"`
285}
286
Joe Tsai08e00302018-11-26 22:32:06 -0800287var scalarProto3Type = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800288 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700289 Syntax: pref.Proto3,
290 FullName: "ScalarProto3",
291 Fields: []ptype.Field{
292 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
293 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
294 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
295 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
296 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
297 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
298 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
299 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
300 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
301 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
302 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700303
Joe Tsai91e14662018-09-13 13:24:35 -0700304 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
305 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
306 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
307 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
308 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
309 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
310 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
311 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
312 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
313 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
314 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
315 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800316 }),
317 func(pref.MessageType) pref.ProtoMessage {
318 return new(ScalarProto3)
319 },
320)}
Joe Tsai91e14662018-09-13 13:24:35 -0700321
Joe Tsaif0c01e42018-11-06 13:05:20 -0800322func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.Type }
323func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) }
324func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) }
325func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
326func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
327func (m *ScalarProto3) ProtoMutable() {}
328
329func TestScalarProto3(t *testing.T) {
330 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700331 hasFields{
332 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
333 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
334 },
335 getFields{
336 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)),
337 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)),
338 },
339 setFields{
340 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)),
341 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)),
342 },
343 hasFields{
344 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
345 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
346 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800347 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700348 setFields{
349 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")),
350 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")),
351 },
352 hasFields{
353 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
354 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
355 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800356 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700357 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
358 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800359 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800360 setFields{
361 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
362 },
363 hasFields{
364 2: true, 3: true, 6: true, 7: true,
365 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800366 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
367 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700368 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700369}
370
Joe Tsaif0c01e42018-11-06 13:05:20 -0800371type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700372 Bools []bool `protobuf:"1"`
373 Int32s []int32 `protobuf:"2"`
374 Int64s []int64 `protobuf:"3"`
375 Uint32s []uint32 `protobuf:"4"`
376 Uint64s []uint64 `protobuf:"5"`
377 Float32s []float32 `protobuf:"6"`
378 Float64s []float64 `protobuf:"7"`
379 Strings []string `protobuf:"8"`
380 StringsA [][]byte `protobuf:"9"`
381 Bytes [][]byte `protobuf:"10"`
382 BytesA []string `protobuf:"11"`
383
384 MyStrings1 []MyString `protobuf:"12"`
385 MyStrings2 []MyBytes `protobuf:"13"`
386 MyBytes1 []MyBytes `protobuf:"14"`
387 MyBytes2 []MyString `protobuf:"15"`
388
Joe Tsai4b7aff62018-11-14 14:05:19 -0800389 MyStrings3 ListStrings `protobuf:"16"`
390 MyStrings4 ListBytes `protobuf:"17"`
391 MyBytes3 ListBytes `protobuf:"18"`
392 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700393}
394
Joe Tsai08e00302018-11-26 22:32:06 -0800395var listScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800396 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700397 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800398 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700399 Fields: []ptype.Field{
400 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
401 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
402 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
403 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
404 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
405 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
406 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
407 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
408 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
409 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
410 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700411
Joe Tsai91e14662018-09-13 13:24:35 -0700412 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
413 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
414 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
415 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
416
417 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
418 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
419 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
420 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700421 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800422 }),
423 func(pref.MessageType) pref.ProtoMessage {
424 return new(ListScalars)
425 },
426)}
Joe Tsai91e14662018-09-13 13:24:35 -0700427
Joe Tsaif0c01e42018-11-06 13:05:20 -0800428func (m *ListScalars) Type() pref.MessageType { return listScalarsType.Type }
429func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) }
430func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) }
431func (m *ListScalars) Interface() pref.ProtoMessage { return m }
432func (m *ListScalars) ProtoReflect() pref.Message { return m }
433func (m *ListScalars) ProtoMutable() {}
434
435func TestListScalars(t *testing.T) {
436 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700437 emptyFS := empty.KnownFields()
438
Joe Tsaif0c01e42018-11-06 13:05:20 -0800439 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700440 Bools: []bool{true, false, true},
441 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
442 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
443 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
444 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
445 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
446 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
447 Strings: []string{"8", "", "eight"},
448 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
449 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
450 BytesA: []string{"11", "", "eleven"},
451
452 MyStrings1: []MyString{"12", "", "twelve"},
453 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
454 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
455 MyBytes2: []MyString{"15", "", "fifteen"},
456
Joe Tsai4b7aff62018-11-14 14:05:19 -0800457 MyStrings3: ListStrings{"16", "", "sixteen"},
458 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
459 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
460 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800461 }
Joe Tsai91e14662018-09-13 13:24:35 -0700462 wantFS := want.KnownFields()
463
Joe Tsaif0c01e42018-11-06 13:05:20 -0800464 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700465 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},
466 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)},
467 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 -0800468 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700469 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800470 lenList(0),
471 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
472 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800473 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700474 },
475 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800476 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
477 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
478 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700479 },
480 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800481 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800482 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700483 },
484 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800485 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
486 lenList(6),
487 setList{0: V("8"), 2: V("eight")},
488 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800489 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700490 },
491 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800492 appendList{V([]byte(nil)), V([]byte(nil))},
493 setList{0: V([]byte("10"))},
494 appendList{V([]byte("wrong"))},
495 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800496 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700497 },
498 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800499 appendList{V("12"), V("wrong"), V("twelve")},
500 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800501 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700502 },
503 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800504 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800505 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700506 },
507 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800508 appendList{V("16"), V(""), V("sixteen"), V("extra")},
509 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800510 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700511 },
512 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800513 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800514 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700515 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700516 },
Joe Tsai91e14662018-09-13 13:24:35 -0700517 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 -0800518 equalMessage{want},
519 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
520 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000521 })
522}
523
Joe Tsaice6edd32018-10-19 16:27:46 -0700524type MapScalars struct {
525 KeyBools map[bool]string `protobuf:"1"`
526 KeyInt32s map[int32]string `protobuf:"2"`
527 KeyInt64s map[int64]string `protobuf:"3"`
528 KeyUint32s map[uint32]string `protobuf:"4"`
529 KeyUint64s map[uint64]string `protobuf:"5"`
530 KeyStrings map[string]string `protobuf:"6"`
531
532 ValBools map[string]bool `protobuf:"7"`
533 ValInt32s map[string]int32 `protobuf:"8"`
534 ValInt64s map[string]int64 `protobuf:"9"`
535 ValUint32s map[string]uint32 `protobuf:"10"`
536 ValUint64s map[string]uint64 `protobuf:"11"`
537 ValFloat32s map[string]float32 `protobuf:"12"`
538 ValFloat64s map[string]float64 `protobuf:"13"`
539 ValStrings map[string]string `protobuf:"14"`
540 ValStringsA map[string][]byte `protobuf:"15"`
541 ValBytes map[string][]byte `protobuf:"16"`
542 ValBytesA map[string]string `protobuf:"17"`
543
544 MyStrings1 map[MyString]MyString `protobuf:"18"`
545 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
546 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
547 MyBytes2 map[MyString]MyString `protobuf:"21"`
548
549 MyStrings3 MapStrings `protobuf:"22"`
550 MyStrings4 MapBytes `protobuf:"23"`
551 MyBytes3 MapBytes `protobuf:"24"`
552 MyBytes4 MapStrings `protobuf:"25"`
553}
554
Joe Tsaif0c01e42018-11-06 13:05:20 -0800555func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
556 return ptype.Field{
557 Name: pref.Name(fmt.Sprintf("f%d", n)),
558 Number: n,
559 Cardinality: pref.Repeated,
560 Kind: pref.MessageKind,
561 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
562 Syntax: pref.Proto2,
563 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
564 Fields: []ptype.Field{
565 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
566 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
567 },
Joe Tsai009e0672018-11-27 18:45:07 -0800568 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800569 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000570 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800571}
572
Joe Tsai08e00302018-11-26 22:32:06 -0800573var mapScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800574 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000575 Syntax: pref.Proto2,
576 FullName: "MapScalars",
577 Fields: []ptype.Field{
578 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
579 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
580 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
581 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
582 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
583 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
584
585 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
586 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
587 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
588 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
589 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
590 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
591 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
592 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
593 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
594 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
595 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
596
597 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
598 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
599 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
600 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
601
602 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
603 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
604 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
605 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
606 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800607 }),
608 func(pref.MessageType) pref.ProtoMessage {
609 return new(MapScalars)
610 },
611)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000612
Joe Tsaif0c01e42018-11-06 13:05:20 -0800613func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.Type }
614func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) }
615func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) }
616func (m *MapScalars) Interface() pref.ProtoMessage { return m }
617func (m *MapScalars) ProtoReflect() pref.Message { return m }
618func (m *MapScalars) ProtoMutable() {}
619
620func TestMapScalars(t *testing.T) {
621 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000622 emptyFS := empty.KnownFields()
623
Joe Tsaif0c01e42018-11-06 13:05:20 -0800624 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000625 KeyBools: map[bool]string{true: "true", false: "false"},
626 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
627 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
628 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
629 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
630 KeyStrings: map[string]string{"": "", "foo": "bar"},
631
632 ValBools: map[string]bool{"true": true, "false": false},
633 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
634 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
635 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
636 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
637 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
638 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
639 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
640 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
641 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
642 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
643
644 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
645 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
646 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
647 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
648
649 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
650 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
651 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
652 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800653 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000654 wantFS := want.KnownFields()
655
Joe Tsaif0c01e42018-11-06 13:05:20 -0800656 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000657 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},
658 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)},
659 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)},
660 mapFields{
661 2: {
662 lenMap(0),
663 hasMap{int32(0): false, int32(-1): false, int32(2): false},
664 setMap{int32(0): V("zero")},
665 lenMap(1),
666 hasMap{int32(0): true, int32(-1): false, int32(2): false},
667 setMap{int32(-1): V("one")},
668 lenMap(2),
669 hasMap{int32(0): true, int32(-1): true, int32(2): false},
670 setMap{int32(2): V("two")},
671 lenMap(3),
672 hasMap{int32(0): true, int32(-1): true, int32(2): true},
673 },
674 4: {
675 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800676 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000677 },
678 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800679 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000680 setMap{"foo": V("bar")},
681 setMap{"": V("empty")},
682 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
683 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800684 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000685 },
686 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800687 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000688 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
689 },
690 10: {
691 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
692 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800693 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000694 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
695 },
696 12: {
697 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800698 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000699 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
700 },
701 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800702 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000703 setMap{"s1": V("s1"), "s2": V("s2")},
704 },
705 16: {
706 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800707 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000708 },
709 18: {
710 hasMap{"s1": false, "s2": false, "s3": false},
711 setMap{"s1": V("s1"), "s2": V("s2")},
712 hasMap{"s1": true, "s2": true, "s3": false},
713 },
714 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800715 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000716 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
717 },
718 22: {
719 rangeMap{},
720 setMap{"s1": V("s1"), "s2": V("s2")},
721 rangeMap{"s1": V("s1"), "s2": V("s2")},
722 lenMap(2),
723 },
724 24: {
725 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800726 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000727 },
728 },
729 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 -0800730 equalMessage{want},
731 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},
732 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700733 })
734}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700735
Joe Tsai87b955b2018-11-14 21:59:49 -0800736type OneofScalars struct {
737 Union isOneofScalars_Union `protobuf_oneof:"union"`
738}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700739
Joe Tsai08e00302018-11-26 22:32:06 -0800740var oneofScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsaif0c01e42018-11-06 13:05:20 -0800741 mustMakeMessageDesc(ptype.StandaloneMessage{
742 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800743 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800744 Fields: []ptype.Field{
745 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
746 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
747 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
748 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
749 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
750 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
751 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
752 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
753 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
754 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
755 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
756 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
757 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
758 },
759 Oneofs: []ptype.Oneof{{Name: "union"}},
760 }),
761 func(pref.MessageType) pref.ProtoMessage {
762 return new(OneofScalars)
763 },
764)}
765
766func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.Type }
767func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) }
768func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) }
769func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
770func (m *OneofScalars) ProtoReflect() pref.Message { return m }
771func (m *OneofScalars) ProtoMutable() {}
772
Joe Tsaif18ab532018-11-27 17:25:04 -0800773func (*OneofScalars) XXX_OneofWrappers() []interface{} {
774 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700775 (*OneofScalars_Bool)(nil),
776 (*OneofScalars_Int32)(nil),
777 (*OneofScalars_Int64)(nil),
778 (*OneofScalars_Uint32)(nil),
779 (*OneofScalars_Uint64)(nil),
780 (*OneofScalars_Float32)(nil),
781 (*OneofScalars_Float64)(nil),
782 (*OneofScalars_String)(nil),
783 (*OneofScalars_StringA)(nil),
784 (*OneofScalars_StringB)(nil),
785 (*OneofScalars_Bytes)(nil),
786 (*OneofScalars_BytesA)(nil),
787 (*OneofScalars_BytesB)(nil),
788 }
789}
790
Joe Tsai87b955b2018-11-14 21:59:49 -0800791type (
792 isOneofScalars_Union interface {
793 isOneofScalars_Union()
794 }
795 OneofScalars_Bool struct {
796 Bool bool `protobuf:"1"`
797 }
798 OneofScalars_Int32 struct {
799 Int32 MyInt32 `protobuf:"2"`
800 }
801 OneofScalars_Int64 struct {
802 Int64 int64 `protobuf:"3"`
803 }
804 OneofScalars_Uint32 struct {
805 Uint32 MyUint32 `protobuf:"4"`
806 }
807 OneofScalars_Uint64 struct {
808 Uint64 uint64 `protobuf:"5"`
809 }
810 OneofScalars_Float32 struct {
811 Float32 MyFloat32 `protobuf:"6"`
812 }
813 OneofScalars_Float64 struct {
814 Float64 float64 `protobuf:"7"`
815 }
816 OneofScalars_String struct {
817 String string `protobuf:"8"`
818 }
819 OneofScalars_StringA struct {
820 StringA []byte `protobuf:"9"`
821 }
822 OneofScalars_StringB struct {
823 StringB MyString `protobuf:"10"`
824 }
825 OneofScalars_Bytes struct {
826 Bytes []byte `protobuf:"11"`
827 }
828 OneofScalars_BytesA struct {
829 BytesA string `protobuf:"12"`
830 }
831 OneofScalars_BytesB struct {
832 BytesB MyBytes `protobuf:"13"`
833 }
834)
835
Joe Tsai2c870bb2018-10-17 11:46:52 -0700836func (*OneofScalars_Bool) isOneofScalars_Union() {}
837func (*OneofScalars_Int32) isOneofScalars_Union() {}
838func (*OneofScalars_Int64) isOneofScalars_Union() {}
839func (*OneofScalars_Uint32) isOneofScalars_Union() {}
840func (*OneofScalars_Uint64) isOneofScalars_Union() {}
841func (*OneofScalars_Float32) isOneofScalars_Union() {}
842func (*OneofScalars_Float64) isOneofScalars_Union() {}
843func (*OneofScalars_String) isOneofScalars_Union() {}
844func (*OneofScalars_StringA) isOneofScalars_Union() {}
845func (*OneofScalars_StringB) isOneofScalars_Union() {}
846func (*OneofScalars_Bytes) isOneofScalars_Union() {}
847func (*OneofScalars_BytesA) isOneofScalars_Union() {}
848func (*OneofScalars_BytesB) isOneofScalars_Union() {}
849
850func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800851 empty := &OneofScalars{}
852 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
853 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
854 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
855 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
856 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
857 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
858 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
859 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
860 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
861 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
862 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
863 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
864 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700865
Joe Tsaif0c01e42018-11-06 13:05:20 -0800866 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700867 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},
868 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"))},
869
Joe Tsai87b955b2018-11-14 21:59:49 -0800870 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
871 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
872 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
873 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
874 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
875 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
876 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
877 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
878 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
879 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
880 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
881 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
882 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700883
884 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},
885 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 -0800886 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
887 equalMessage{want13},
888 clearFields{13},
889 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700890 })
891}
892
Joe Tsai87b955b2018-11-14 21:59:49 -0800893type EnumProto2 int32
894
895var enumProto2Type = ptype.GoEnum(
896 mustMakeEnumDesc(ptype.StandaloneEnum{
897 Syntax: pref.Proto2,
898 FullName: "EnumProto2",
899 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
900 }),
901 func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
902 return EnumProto2(n)
903 },
904)
905
906func (e EnumProto2) Enum() *EnumProto2 { return &e }
907func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
908func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
909func (e EnumProto2) ProtoReflect() pref.Enum { return e }
910
911type EnumProto3 int32
912
913var enumProto3Type = ptype.GoEnum(
914 mustMakeEnumDesc(ptype.StandaloneEnum{
915 Syntax: pref.Proto3,
916 FullName: "EnumProto3",
917 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
918 }),
919 func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
920 return EnumProto3(n)
921 },
922)
923
924func (e EnumProto3) Enum() *EnumProto3 { return &e }
925func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
926func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
927func (e EnumProto3) ProtoReflect() pref.Enum { return e }
928
929type EnumMessages struct {
930 EnumP2 *EnumProto2 `protobuf:"1"`
931 EnumP3 *EnumProto3 `protobuf:"2"`
932 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
933 MessageCycle *EnumMessages `protobuf:"4"`
934 EnumList []EnumProto2 `protobuf:"5"`
935 MessageList []*ScalarProto2 `protobuf:"6"`
936 EnumMap map[string]EnumProto3 `protobuf:"7"`
937 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
938 Union isEnumMessages_Union `protobuf_oneof:"union"`
939}
940
Joe Tsai08e00302018-11-26 22:32:06 -0800941var enumMessagesType = pimpl.MessageType{Type: ptype.GoMessage(
Joe Tsai87b955b2018-11-14 21:59:49 -0800942 mustMakeMessageDesc(ptype.StandaloneMessage{
943 Syntax: pref.Proto2,
944 FullName: "EnumMessages",
945 Fields: []ptype.Field{
946 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
947 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
Joe Tsai08e00302018-11-26 22:32:06 -0800948 {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 -0800949 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
950 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
951 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.Type},
952 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
953 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
954 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
955 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
956 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.Type},
957 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.Type},
958 },
959 Oneofs: []ptype.Oneof{{Name: "union"}},
960 }),
961 func(pref.MessageType) pref.ProtoMessage {
962 return new(EnumMessages)
963 },
964)}
965
966var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
967 Syntax: pref.Proto2,
968 FullName: "EnumMessages.F7Entry",
969 Fields: []ptype.Field{
970 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
971 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
972 },
Joe Tsai009e0672018-11-27 18:45:07 -0800973 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsai87b955b2018-11-14 21:59:49 -0800974})
975
976var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
977 Syntax: pref.Proto2,
978 FullName: "EnumMessages.F8Entry",
979 Fields: []ptype.Field{
980 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
981 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.Type},
982 },
Joe Tsai009e0672018-11-27 18:45:07 -0800983 Options: &descriptorV1.MessageOptions{MapEntry: scalar.Bool(true)},
Joe Tsai87b955b2018-11-14 21:59:49 -0800984})
985
986func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.Type }
987func (m *EnumMessages) KnownFields() pref.KnownFields { return enumMessagesType.KnownFieldsOf(m) }
988func (m *EnumMessages) UnknownFields() pref.UnknownFields { return enumMessagesType.UnknownFieldsOf(m) }
989func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
990func (m *EnumMessages) ProtoReflect() pref.Message { return m }
991func (m *EnumMessages) ProtoMutable() {}
992
Joe Tsaif18ab532018-11-27 17:25:04 -0800993func (*EnumMessages) XXX_OneofWrappers() []interface{} {
994 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -0800995 (*EnumMessages_OneofE2)(nil),
996 (*EnumMessages_OneofE3)(nil),
997 (*EnumMessages_OneofM2)(nil),
998 (*EnumMessages_OneofM3)(nil),
999 }
1000}
1001
1002type (
1003 isEnumMessages_Union interface {
1004 isEnumMessages_Union()
1005 }
1006 EnumMessages_OneofE2 struct {
1007 OneofE2 EnumProto2 `protobuf:"9"`
1008 }
1009 EnumMessages_OneofE3 struct {
1010 OneofE3 EnumProto3 `protobuf:"10"`
1011 }
1012 EnumMessages_OneofM2 struct {
1013 OneofM2 *ScalarProto2 `protobuf:"11"`
1014 }
1015 EnumMessages_OneofM3 struct {
1016 OneofM3 *ScalarProto3 `protobuf:"12"`
1017 }
1018)
1019
1020func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1021func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1022func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1023func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1024
1025func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001026 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001027 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001028 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1029 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001030 wantM3a := &ScalarProto3{Float32: math.Pi}
1031 wantM3b := &ScalarProto3{Float32: math.Ln2}
1032
1033 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1034 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1035
1036 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1037 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1038
1039 testMessage(t, nil, &EnumMessages{}, messageOps{
1040 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 -08001041 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 -08001042
1043 // Test singular enums.
1044 setFields{1: VE(0xdead), 2: VE(0)},
1045 getFields{1: VE(0xdead), 2: VE(0)},
1046 hasFields{1: true, 2: true},
1047
1048 // Test singular messages.
1049 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1050 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1051 getFields{3: V(wantL), 4: V(wantM)},
1052 clearFields{3, 4},
1053 hasFields{3: false, 4: false},
1054 setFields{3: V(wantL), 4: V(wantM)},
1055 hasFields{3: true, 4: true},
1056
1057 // Test list of enums and messages.
1058 listFields{
1059 5: listOps{
1060 appendList{VE(111), VE(222)},
1061 setList{0: VE(333)},
1062 getList{0: VE(333), 1: VE(222)},
1063 lenList(2),
1064 },
1065 6: listOps{
1066 appendMessageList{setFields{4: V(uint32(1e6))}},
1067 appendMessageList{setFields{6: V(float32(math.Phi))}},
1068 setList{0: V(wantM2a)},
1069 getList{0: V(wantM2a), 1: V(wantM2b)},
1070 },
1071 },
1072 getFields{5: wantList5, 6: wantList6},
1073 hasFields{5: true, 6: true},
1074 listFields{5: listOps{truncList(0)}},
1075 hasFields{5: false, 6: true},
1076
1077 // Test maps of enums and messages.
1078 mapFields{
1079 7: mapOps{
1080 setMap{"one": VE(1), "two": VE(2)},
1081 hasMap{"one": true, "two": true, "three": false},
1082 lenMap(2),
1083 },
1084 8: mapOps{
1085 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1086 setMap{"ln2": V(wantM3b)},
1087 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1088 lenMap(2),
1089 },
1090 },
1091 getFields{7: wantMap7, 8: wantMap8},
1092 hasFields{7: true, 8: true},
1093 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1094 hasFields{7: true, 8: false},
1095
1096 // Test oneofs of enums and messages.
1097 setFields{9: VE(0xdead)},
1098 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1099 setFields{10: VE(0)},
1100 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1101 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1102 getFields{11: V(wantM2a)},
1103 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1104 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1105 getFields{12: V(wantM3a)},
1106 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1107
1108 // Check entire message.
1109 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1110 equalMessage{&EnumMessages{
1111 EnumP2: EnumProto2(0xdead).Enum(),
1112 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001113 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001114 MessageCycle: wantM,
1115 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1116 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1117 Union: &EnumMessages_OneofM3{wantM3a},
1118 }},
1119 clearFields{1, 2, 3, 4, 6, 7, 12},
1120 equalMessage{&EnumMessages{}},
1121 })
1122}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001123
Joe Tsai91e14662018-09-13 13:24:35 -07001124var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001125 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1126 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001127 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001128 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1129 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001130 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001131 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001132 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001133 }),
1134 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001135 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001136}
1137
1138func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1139 fs := m.KnownFields()
1140 for i, op := range tt {
1141 p.Push(i)
1142 switch op := op.(type) {
1143 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001144 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001145 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1146 }
1147 case hasFields:
1148 got := map[pref.FieldNumber]bool{}
1149 want := map[pref.FieldNumber]bool(op)
1150 for n := range want {
1151 got[n] = fs.Has(n)
1152 }
1153 if diff := cmp.Diff(want, got); diff != "" {
1154 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1155 }
1156 case getFields:
1157 got := map[pref.FieldNumber]pref.Value{}
1158 want := map[pref.FieldNumber]pref.Value(op)
1159 for n := range want {
1160 got[n] = fs.Get(n)
1161 }
1162 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1163 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1164 }
1165 case setFields:
1166 for n, v := range op {
1167 fs.Set(n, v)
1168 }
1169 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001170 for _, n := range op {
1171 fs.Clear(n)
1172 }
1173 case messageFields:
1174 for n, tt := range op {
1175 p.Push(int(n))
1176 testMessage(t, p, fs.Mutable(n).(pref.Message), tt)
1177 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001178 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001179 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001180 for n, tt := range op {
1181 p.Push(int(n))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001182 testLists(t, p, fs.Mutable(n).(pref.List), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001183 p.Pop()
1184 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001185 case mapFields:
1186 for n, tt := range op {
1187 p.Push(int(n))
1188 testMaps(t, p, fs.Mutable(n).(pref.Map), tt)
1189 p.Pop()
1190 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001191 case rangeFields:
1192 got := map[pref.FieldNumber]pref.Value{}
1193 want := map[pref.FieldNumber]pref.Value(op)
1194 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1195 got[n] = v
1196 return true
1197 })
1198 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1199 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1200 }
Joe Tsai91e14662018-09-13 13:24:35 -07001201 default:
1202 t.Fatalf("operation %v, invalid operation: %T", p, op)
1203 }
1204 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001205 }
1206}
Joe Tsai91e14662018-09-13 13:24:35 -07001207
Joe Tsai4b7aff62018-11-14 14:05:19 -08001208func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001209 for i, op := range tt {
1210 p.Push(i)
1211 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001212 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001213 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001214 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001215 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001216 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001217 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001218 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001219 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001220 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001221 got := map[int]pref.Value{}
1222 want := map[int]pref.Value(op)
1223 for n := range want {
1224 got[n] = v.Get(n)
1225 }
1226 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001227 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001228 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001229 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001230 for n, e := range op {
1231 v.Set(n, e)
1232 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001233 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001234 for _, e := range op {
1235 v.Append(e)
1236 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001237 case appendMessageList:
1238 testMessage(t, p, v.MutableAppend().(pref.Message), messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001239 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001240 v.Truncate(int(op))
1241 default:
1242 t.Fatalf("operation %v, invalid operation: %T", p, op)
1243 }
1244 p.Pop()
1245 }
1246}
1247
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001248func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1249 for i, op := range tt {
1250 p.Push(i)
1251 switch op := op.(type) {
1252 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001253 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001254 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1255 }
1256 case lenMap:
1257 if got, want := m.Len(), int(op); got != want {
1258 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1259 }
1260 case hasMap:
1261 got := map[interface{}]bool{}
1262 want := map[interface{}]bool(op)
1263 for k := range want {
1264 got[k] = m.Has(V(k).MapKey())
1265 }
1266 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1267 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1268 }
1269 case getMap:
1270 got := map[interface{}]pref.Value{}
1271 want := map[interface{}]pref.Value(op)
1272 for k := range want {
1273 got[k] = m.Get(V(k).MapKey())
1274 }
1275 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1276 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1277 }
1278 case setMap:
1279 for k, v := range op {
1280 m.Set(V(k).MapKey(), v)
1281 }
1282 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001283 for _, k := range op {
1284 m.Clear(V(k).MapKey())
1285 }
1286 case messageMap:
1287 for k, tt := range op {
1288 testMessage(t, p, m.Mutable(V(k).MapKey()).(pref.Message), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001289 }
1290 case rangeMap:
1291 got := map[interface{}]pref.Value{}
1292 want := map[interface{}]pref.Value(op)
1293 m.Range(func(k pref.MapKey, v pref.Value) bool {
1294 got[k.Interface()] = v
1295 return true
1296 })
1297 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1298 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1299 }
1300 default:
1301 t.Fatalf("operation %v, invalid operation: %T", p, op)
1302 }
1303 p.Pop()
1304 }
1305}
1306
Joe Tsai91e14662018-09-13 13:24:35 -07001307type path []int
1308
1309func (p *path) Push(i int) { *p = append(*p, i) }
1310func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1311func (p path) String() string {
1312 var ss []string
1313 for _, i := range p {
1314 ss = append(ss, fmt.Sprint(i))
1315 }
1316 return strings.Join(ss, ".")
1317}