blob: e1fc1ce479cb52ab31dec4bea60e89f8ad0c7971 [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Joe Tsai08e00302018-11-26 22:32:06 -08005package impl_test
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7import (
Joe Tsai91e14662018-09-13 13:24:35 -07008 "fmt"
9 "math"
Damien Neil8012b442019-01-18 09:32:24 -080010 "reflect"
Joe Tsai91e14662018-09-13 13:24:35 -070011 "strings"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070012 "testing"
13
Damien Neil204f1c02018-10-23 15:03:38 -070014 protoV1 "github.com/golang/protobuf/proto"
Joe Tsai87b955b2018-11-14 21:59:49 -080015 cmp "github.com/google/go-cmp/cmp"
16 cmpopts "github.com/google/go-cmp/cmp/cmpopts"
Damien Neile89e6242019-05-13 23:55:40 -070017 pimpl "google.golang.org/protobuf/internal/impl"
18 ptype "google.golang.org/protobuf/internal/prototype"
19 scalar "google.golang.org/protobuf/internal/scalar"
20 pvalue "google.golang.org/protobuf/internal/value"
21 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaib2f66be2019-05-22 00:42:45 -040022 "google.golang.org/protobuf/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070023
Damien Neile89e6242019-05-13 23:55:40 -070024 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsaia95b29f2019-05-16 12:47:20 -070025 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070026)
27
Joe Tsai4b7aff62018-11-14 14:05:19 -080028// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070029type (
Joe Tsai87b955b2018-11-14 21:59:49 -080030 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070031 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070032
Joe Tsai87b955b2018-11-14 21:59:49 -080033 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080034 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070035
Joe Tsai87b955b2018-11-14 21:59:49 -080036 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000037 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070038)
39
40// Test operations performed on a message.
41type (
Joe Tsai87b955b2018-11-14 21:59:49 -080042 // check that the message contents match
43 equalMessage struct{ pref.Message }
44 // check presence for specific fields in the message
45 hasFields map[pref.FieldNumber]bool
46 // check that specific message fields match
47 getFields map[pref.FieldNumber]pref.Value
48 // set specific message fields
49 setFields map[pref.FieldNumber]pref.Value
50 // clear specific fields in the message
51 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070052 // check for the presence of specific oneof member fields.
53 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080054 // 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() {}
Joe Tsai4ec39c72019-04-03 13:40:53 -070069func (whichOneofs) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080070func (messageFields) isMessageOp() {}
71func (listFields) isMessageOp() {}
72func (mapFields) isMessageOp() {}
73func (rangeFields) isMessageOp() {}
74
Joe Tsai4b7aff62018-11-14 14:05:19 -080075// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070076type (
Joe Tsai87b955b2018-11-14 21:59:49 -080077 // check that the list contents match
78 equalList struct{ pref.List }
79 // check that list length matches
80 lenList int
81 // check that specific list entries match
82 getList map[int]pref.Value
83 // set specific list entries
84 setList map[int]pref.Value
85 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080086 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080087 // apply messageOps on a newly appended message
88 appendMessageList messageOps
89 // truncate the list to the specified length
90 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070091)
92
Joe Tsai87b955b2018-11-14 21:59:49 -080093func (equalList) isListOp() {}
94func (lenList) isListOp() {}
95func (getList) isListOp() {}
96func (setList) isListOp() {}
97func (appendList) isListOp() {}
98func (appendMessageList) isListOp() {}
99func (truncList) isListOp() {}
100
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000101// Test operations performed on a map.
102type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800103 // check that the map contents match
104 equalMap struct{ pref.Map }
105 // check that map length matches
106 lenMap int
107 // check presence for specific entries in the map
108 hasMap map[interface{}]bool
109 // check that specific map entries match
110 getMap map[interface{}]pref.Value
111 // set specific map entries
112 setMap map[interface{}]pref.Value
113 // clear specific entries in the map
114 clearMap []interface{}
115 // apply messageOps on each specified message entry
116 messageMap map[interface{}]messageOps
117 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000118 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000119)
120
Joe Tsai87b955b2018-11-14 21:59:49 -0800121func (equalMap) isMapOp() {}
122func (lenMap) isMapOp() {}
123func (hasMap) isMapOp() {}
124func (getMap) isMapOp() {}
125func (setMap) isMapOp() {}
126func (clearMap) isMapOp() {}
127func (messageMap) isMapOp() {}
128func (rangeMap) isMapOp() {}
129
Joe Tsaice6edd32018-10-19 16:27:46 -0700130type ScalarProto2 struct {
131 Bool *bool `protobuf:"1"`
132 Int32 *int32 `protobuf:"2"`
133 Int64 *int64 `protobuf:"3"`
134 Uint32 *uint32 `protobuf:"4"`
135 Uint64 *uint64 `protobuf:"5"`
136 Float32 *float32 `protobuf:"6"`
137 Float64 *float64 `protobuf:"7"`
138 String *string `protobuf:"8"`
139 StringA []byte `protobuf:"9"`
140 Bytes []byte `protobuf:"10"`
141 BytesA *string `protobuf:"11"`
142
143 MyBool *MyBool `protobuf:"12"`
144 MyInt32 *MyInt32 `protobuf:"13"`
145 MyInt64 *MyInt64 `protobuf:"14"`
146 MyUint32 *MyUint32 `protobuf:"15"`
147 MyUint64 *MyUint64 `protobuf:"16"`
148 MyFloat32 *MyFloat32 `protobuf:"17"`
149 MyFloat64 *MyFloat64 `protobuf:"18"`
150 MyString *MyString `protobuf:"19"`
151 MyStringA MyBytes `protobuf:"20"`
152 MyBytes MyBytes `protobuf:"21"`
153 MyBytesA *MyString `protobuf:"22"`
154}
155
Joe Tsai87b955b2018-11-14 21:59:49 -0800156func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
157 ed, err := ptype.NewEnum(&t)
158 if err != nil {
159 panic(err)
160 }
161 return ed
162}
163
164func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
165 md, err := ptype.NewMessage(&t)
166 if err != nil {
167 panic(err)
168 }
169 return md
170}
171
172var V = pref.ValueOf
173var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
174
175type (
176 MyBool bool
177 MyInt32 int32
178 MyInt64 int64
179 MyUint32 uint32
180 MyUint64 uint64
181 MyFloat32 float32
182 MyFloat64 float64
183 MyString string
184 MyBytes []byte
185
186 ListStrings []MyString
187 ListBytes []MyBytes
188
189 MapStrings map[MyString]MyString
190 MapBytes map[MyString]MyBytes
191)
192
Joe Tsaib2f66be2019-05-22 00:42:45 -0400193var scalarProto2Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: &prototype.Message{
194 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700195 Syntax: pref.Proto2,
196 FullName: "ScalarProto2",
197 Fields: []ptype.Field{
198 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
199 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
200 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
201 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
202 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
203 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
204 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
205 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
206 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
207 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
208 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
209
210 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
211 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
212 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
213 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
214 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
215 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
216 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
217 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
218 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
219 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
220 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
221 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800222 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400223 NewMessage: func() pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800224 return new(ScalarProto2)
225 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400226}}
Joe Tsai91e14662018-09-13 13:24:35 -0700227
Joe Tsai0fc49f82019-05-01 12:29:25 -0700228// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800229func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700230func (m *ScalarProto2) Descriptor() pref.MessageDescriptor {
231 return scalarProto2Type.PBType.Descriptor()
232}
Damien Neil374cdb82019-01-29 16:45:30 -0800233func (m *ScalarProto2) KnownFields() pref.KnownFields {
234 return scalarProto2Type.MessageOf(m).KnownFields()
235}
236func (m *ScalarProto2) UnknownFields() pref.UnknownFields {
237 return scalarProto2Type.MessageOf(m).UnknownFields()
238}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700239func (m *ScalarProto2) New() pref.Message { return new(ScalarProto2) }
Damien Neil374cdb82019-01-29 16:45:30 -0800240func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
241func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800242
243func TestScalarProto2(t *testing.T) {
244 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700245 hasFields{
246 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
247 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
248 },
249 getFields{
250 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")),
251 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")),
252 },
253 setFields{
254 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)),
255 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)),
256 },
257 hasFields{
258 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
259 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
260 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800261 equalMessage{&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700262 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
263 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 -0800264 }},
265 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
266 equalMessage{&ScalarProto2{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700267 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800268
269 // Test read-only operations on nil message.
270 testMessage(t, nil, (*ScalarProto2)(nil), messageOps{
271 hasFields{
272 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
273 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
274 },
275 getFields{
276 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")),
277 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")),
278 },
279 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700280}
281
Joe Tsaice6edd32018-10-19 16:27:46 -0700282type ScalarProto3 struct {
283 Bool bool `protobuf:"1"`
284 Int32 int32 `protobuf:"2"`
285 Int64 int64 `protobuf:"3"`
286 Uint32 uint32 `protobuf:"4"`
287 Uint64 uint64 `protobuf:"5"`
288 Float32 float32 `protobuf:"6"`
289 Float64 float64 `protobuf:"7"`
290 String string `protobuf:"8"`
291 StringA []byte `protobuf:"9"`
292 Bytes []byte `protobuf:"10"`
293 BytesA string `protobuf:"11"`
294
295 MyBool MyBool `protobuf:"12"`
296 MyInt32 MyInt32 `protobuf:"13"`
297 MyInt64 MyInt64 `protobuf:"14"`
298 MyUint32 MyUint32 `protobuf:"15"`
299 MyUint64 MyUint64 `protobuf:"16"`
300 MyFloat32 MyFloat32 `protobuf:"17"`
301 MyFloat64 MyFloat64 `protobuf:"18"`
302 MyString MyString `protobuf:"19"`
303 MyStringA MyBytes `protobuf:"20"`
304 MyBytes MyBytes `protobuf:"21"`
305 MyBytesA MyString `protobuf:"22"`
306}
307
Joe Tsaib2f66be2019-05-22 00:42:45 -0400308var scalarProto3Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: &prototype.Message{
309 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700310 Syntax: pref.Proto3,
311 FullName: "ScalarProto3",
312 Fields: []ptype.Field{
313 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
314 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
315 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
316 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
317 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
318 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
319 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
320 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
321 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
322 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
323 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700324
Joe Tsai91e14662018-09-13 13:24:35 -0700325 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
326 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
327 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
328 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
329 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
330 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
331 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
332 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
333 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
334 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
335 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
336 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800337 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400338 NewMessage: func() pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800339 return new(ScalarProto3)
340 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400341}}
Joe Tsai91e14662018-09-13 13:24:35 -0700342
Joe Tsai0fc49f82019-05-01 12:29:25 -0700343// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800344func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700345func (m *ScalarProto3) Descriptor() pref.MessageDescriptor {
346 return scalarProto3Type.PBType.Descriptor()
347}
Damien Neil374cdb82019-01-29 16:45:30 -0800348func (m *ScalarProto3) KnownFields() pref.KnownFields {
349 return scalarProto3Type.MessageOf(m).KnownFields()
350}
351func (m *ScalarProto3) UnknownFields() pref.UnknownFields {
352 return scalarProto3Type.MessageOf(m).UnknownFields()
353}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700354func (m *ScalarProto3) New() pref.Message { return new(ScalarProto3) }
Damien Neil374cdb82019-01-29 16:45:30 -0800355func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
356func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800357
358func TestScalarProto3(t *testing.T) {
359 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700360 hasFields{
361 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
362 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
363 },
364 getFields{
365 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)),
366 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)),
367 },
368 setFields{
369 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)),
370 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)),
371 },
372 hasFields{
373 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
374 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
375 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800376 equalMessage{&ScalarProto3{}},
Joe Tsai91e14662018-09-13 13:24:35 -0700377 setFields{
378 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")),
379 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")),
380 },
381 hasFields{
382 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
383 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
384 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800385 equalMessage{&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700386 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
387 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai87b955b2018-11-14 21:59:49 -0800388 }},
Joe Tsai44e389c2018-11-19 15:27:09 -0800389 setFields{
390 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
391 },
392 hasFields{
393 2: true, 3: true, 6: true, 7: true,
394 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800395 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
396 equalMessage{&ScalarProto3{}},
Joe Tsai060cdac2019-04-22 11:44:49 -0700397
398 // Verify that -0 triggers proper Has behavior.
399 hasFields{6: false, 7: false},
400 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
401 hasFields{6: true, 7: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700402 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800403
404 // Test read-only operations on nil message.
405 testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
406 hasFields{
407 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
408 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
409 },
410 getFields{
411 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)),
412 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)),
413 },
414 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700415}
416
Joe Tsaif0c01e42018-11-06 13:05:20 -0800417type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700418 Bools []bool `protobuf:"1"`
419 Int32s []int32 `protobuf:"2"`
420 Int64s []int64 `protobuf:"3"`
421 Uint32s []uint32 `protobuf:"4"`
422 Uint64s []uint64 `protobuf:"5"`
423 Float32s []float32 `protobuf:"6"`
424 Float64s []float64 `protobuf:"7"`
425 Strings []string `protobuf:"8"`
426 StringsA [][]byte `protobuf:"9"`
427 Bytes [][]byte `protobuf:"10"`
428 BytesA []string `protobuf:"11"`
429
430 MyStrings1 []MyString `protobuf:"12"`
431 MyStrings2 []MyBytes `protobuf:"13"`
432 MyBytes1 []MyBytes `protobuf:"14"`
433 MyBytes2 []MyString `protobuf:"15"`
434
Joe Tsai4b7aff62018-11-14 14:05:19 -0800435 MyStrings3 ListStrings `protobuf:"16"`
436 MyStrings4 ListBytes `protobuf:"17"`
437 MyBytes3 ListBytes `protobuf:"18"`
438 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700439}
440
Joe Tsaib2f66be2019-05-22 00:42:45 -0400441var listScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ListScalars)), PBType: &prototype.Message{
442 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700443 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800444 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700445 Fields: []ptype.Field{
446 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
447 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
448 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
449 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
450 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
451 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
452 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
453 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
454 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
455 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
456 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700457
Joe Tsai91e14662018-09-13 13:24:35 -0700458 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
459 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
460 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
461 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
462
463 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
464 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
465 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
466 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700467 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800468 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400469 NewMessage: func() pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800470 return new(ListScalars)
471 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400472}}
Joe Tsai91e14662018-09-13 13:24:35 -0700473
Joe Tsai0fc49f82019-05-01 12:29:25 -0700474// TODO: Remove this.
475func (m *ListScalars) Type() pref.MessageType { return listScalarsType.PBType }
476func (m *ListScalars) Descriptor() pref.MessageDescriptor { return listScalarsType.PBType.Descriptor() }
Damien Neil374cdb82019-01-29 16:45:30 -0800477func (m *ListScalars) KnownFields() pref.KnownFields {
478 return listScalarsType.MessageOf(m).KnownFields()
479}
480func (m *ListScalars) UnknownFields() pref.UnknownFields {
481 return listScalarsType.MessageOf(m).UnknownFields()
482}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700483func (m *ListScalars) New() pref.Message { return new(ListScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800484func (m *ListScalars) Interface() pref.ProtoMessage { return m }
485func (m *ListScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800486
487func TestListScalars(t *testing.T) {
488 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700489 emptyFS := empty.KnownFields()
490
Joe Tsaif0c01e42018-11-06 13:05:20 -0800491 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700492 Bools: []bool{true, false, true},
493 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
494 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
495 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
496 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
497 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
498 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
499 Strings: []string{"8", "", "eight"},
500 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
501 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
502 BytesA: []string{"11", "", "eleven"},
503
504 MyStrings1: []MyString{"12", "", "twelve"},
505 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
506 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
507 MyBytes2: []MyString{"15", "", "fifteen"},
508
Joe Tsai4b7aff62018-11-14 14:05:19 -0800509 MyStrings3: ListStrings{"16", "", "sixteen"},
510 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
511 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
512 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800513 }
Joe Tsai91e14662018-09-13 13:24:35 -0700514 wantFS := want.KnownFields()
515
Joe Tsaif0c01e42018-11-06 13:05:20 -0800516 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700517 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},
518 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)},
519 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 -0800520 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700521 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800522 lenList(0),
523 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
524 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800525 equalList{wantFS.Get(2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700526 },
527 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800528 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
529 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
530 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700531 },
532 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800533 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800534 equalList{wantFS.Get(6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700535 },
536 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800537 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
538 lenList(6),
539 setList{0: V("8"), 2: V("eight")},
540 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800541 equalList{wantFS.Get(8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700542 },
543 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800544 appendList{V([]byte(nil)), V([]byte(nil))},
545 setList{0: V([]byte("10"))},
546 appendList{V([]byte("wrong"))},
547 setList{2: V([]byte("ten"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800548 equalList{wantFS.Get(10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700549 },
550 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800551 appendList{V("12"), V("wrong"), V("twelve")},
552 setList{1: V("")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800553 equalList{wantFS.Get(12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700554 },
555 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800556 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800557 equalList{wantFS.Get(14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700558 },
559 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800560 appendList{V("16"), V(""), V("sixteen"), V("extra")},
561 truncList(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800562 equalList{wantFS.Get(16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700563 },
564 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800565 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800566 equalList{wantFS.Get(18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700567 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700568 },
Joe Tsai91e14662018-09-13 13:24:35 -0700569 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 -0800570 equalMessage{want},
571 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
572 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000573 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800574
575 // Test read-only operations on nil message.
576 testMessage(t, nil, (*ListScalars)(nil), messageOps{
577 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},
578 listFields{2: {lenList(0)}, 4: {lenList(0)}, 6: {lenList(0)}, 8: {lenList(0)}, 10: {lenList(0)}, 12: {lenList(0)}, 14: {lenList(0)}, 16: {lenList(0)}, 18: {lenList(0)}},
579 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000580}
581
Joe Tsaice6edd32018-10-19 16:27:46 -0700582type MapScalars struct {
583 KeyBools map[bool]string `protobuf:"1"`
584 KeyInt32s map[int32]string `protobuf:"2"`
585 KeyInt64s map[int64]string `protobuf:"3"`
586 KeyUint32s map[uint32]string `protobuf:"4"`
587 KeyUint64s map[uint64]string `protobuf:"5"`
588 KeyStrings map[string]string `protobuf:"6"`
589
590 ValBools map[string]bool `protobuf:"7"`
591 ValInt32s map[string]int32 `protobuf:"8"`
592 ValInt64s map[string]int64 `protobuf:"9"`
593 ValUint32s map[string]uint32 `protobuf:"10"`
594 ValUint64s map[string]uint64 `protobuf:"11"`
595 ValFloat32s map[string]float32 `protobuf:"12"`
596 ValFloat64s map[string]float64 `protobuf:"13"`
597 ValStrings map[string]string `protobuf:"14"`
598 ValStringsA map[string][]byte `protobuf:"15"`
599 ValBytes map[string][]byte `protobuf:"16"`
600 ValBytesA map[string]string `protobuf:"17"`
601
602 MyStrings1 map[MyString]MyString `protobuf:"18"`
603 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
604 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
605 MyBytes2 map[MyString]MyString `protobuf:"21"`
606
607 MyStrings3 MapStrings `protobuf:"22"`
608 MyStrings4 MapBytes `protobuf:"23"`
609 MyBytes3 MapBytes `protobuf:"24"`
610 MyBytes4 MapStrings `protobuf:"25"`
611}
612
Joe Tsaif0c01e42018-11-06 13:05:20 -0800613func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
614 return ptype.Field{
615 Name: pref.Name(fmt.Sprintf("f%d", n)),
616 Number: n,
617 Cardinality: pref.Repeated,
618 Kind: pref.MessageKind,
619 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
620 Syntax: pref.Proto2,
621 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
622 Fields: []ptype.Field{
623 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
624 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
625 },
Damien Neil232ea152018-12-10 15:14:36 -0800626 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
627 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800628 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000629 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800630}
631
Joe Tsaib2f66be2019-05-22 00:42:45 -0400632var mapScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(MapScalars)), PBType: &prototype.Message{
633 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000634 Syntax: pref.Proto2,
635 FullName: "MapScalars",
636 Fields: []ptype.Field{
637 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
638 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
639 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
640 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
641 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
642 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
643
644 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
645 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
646 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
647 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
648 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
649 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
650 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
651 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
652 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
653 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
654 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
655
656 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
657 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
658 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
659 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
660
661 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
662 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
663 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
664 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
665 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800666 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400667 NewMessage: func() pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800668 return new(MapScalars)
669 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400670}}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000671
Joe Tsai0fc49f82019-05-01 12:29:25 -0700672// TODO: Remove this.
673func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.PBType }
674func (m *MapScalars) Descriptor() pref.MessageDescriptor { return mapScalarsType.PBType.Descriptor() }
Damien Neil374cdb82019-01-29 16:45:30 -0800675func (m *MapScalars) KnownFields() pref.KnownFields {
676 return mapScalarsType.MessageOf(m).KnownFields()
677}
678func (m *MapScalars) UnknownFields() pref.UnknownFields {
679 return mapScalarsType.MessageOf(m).UnknownFields()
680}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700681func (m *MapScalars) New() pref.Message { return new(MapScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800682func (m *MapScalars) Interface() pref.ProtoMessage { return m }
683func (m *MapScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800684
685func TestMapScalars(t *testing.T) {
686 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000687 emptyFS := empty.KnownFields()
688
Joe Tsaif0c01e42018-11-06 13:05:20 -0800689 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000690 KeyBools: map[bool]string{true: "true", false: "false"},
691 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
692 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
693 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
694 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
695 KeyStrings: map[string]string{"": "", "foo": "bar"},
696
697 ValBools: map[string]bool{"true": true, "false": false},
698 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
699 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
700 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
701 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
702 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
703 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
704 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
705 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
706 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
707 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
708
709 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
710 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
711 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
712 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
713
714 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
715 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
716 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
717 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800718 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000719 wantFS := want.KnownFields()
720
Joe Tsaif0c01e42018-11-06 13:05:20 -0800721 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000722 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},
723 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)},
724 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)},
725 mapFields{
726 2: {
727 lenMap(0),
728 hasMap{int32(0): false, int32(-1): false, int32(2): false},
729 setMap{int32(0): V("zero")},
730 lenMap(1),
731 hasMap{int32(0): true, int32(-1): false, int32(2): false},
732 setMap{int32(-1): V("one")},
733 lenMap(2),
734 hasMap{int32(0): true, int32(-1): true, int32(2): false},
735 setMap{int32(2): V("two")},
736 lenMap(3),
737 hasMap{int32(0): true, int32(-1): true, int32(2): true},
738 },
739 4: {
740 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800741 equalMap{wantFS.Get(4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000742 },
743 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800744 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000745 setMap{"foo": V("bar")},
746 setMap{"": V("empty")},
747 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
748 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800749 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000750 },
751 8: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800752 equalMap{emptyFS.Get(8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000753 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
754 },
755 10: {
756 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
757 lenMap(3),
Joe Tsai87b955b2018-11-14 21:59:49 -0800758 equalMap{wantFS.Get(10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000759 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
760 },
761 12: {
762 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800763 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000764 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
765 },
766 14: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800767 equalMap{emptyFS.Get(14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000768 setMap{"s1": V("s1"), "s2": V("s2")},
769 },
770 16: {
771 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800772 equalMap{wantFS.Get(16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000773 },
774 18: {
775 hasMap{"s1": false, "s2": false, "s3": false},
776 setMap{"s1": V("s1"), "s2": V("s2")},
777 hasMap{"s1": true, "s2": true, "s3": false},
778 },
779 20: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800780 equalMap{emptyFS.Get(20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000781 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
782 },
783 22: {
784 rangeMap{},
785 setMap{"s1": V("s1"), "s2": V("s2")},
786 rangeMap{"s1": V("s1"), "s2": V("s2")},
787 lenMap(2),
788 },
789 24: {
790 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800791 equalMap{wantFS.Get(24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000792 },
793 },
794 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 -0800795 equalMessage{want},
796 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},
797 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700798 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800799
800 // Test read-only operations on nil message.
801 testMessage(t, nil, (*MapScalars)(nil), messageOps{
802 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},
803 mapFields{2: {lenMap(0)}, 4: {lenMap(0)}, 6: {lenMap(0)}, 8: {lenMap(0)}, 10: {lenMap(0)}, 12: {lenMap(0)}, 14: {lenMap(0)}, 16: {lenMap(0)}, 18: {lenMap(0)}, 20: {lenMap(0)}, 22: {lenMap(0)}, 24: {lenMap(0)}},
804 })
Joe Tsai91e14662018-09-13 13:24:35 -0700805}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700806
Joe Tsai87b955b2018-11-14 21:59:49 -0800807type OneofScalars struct {
808 Union isOneofScalars_Union `protobuf_oneof:"union"`
809}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700810
Joe Tsaib2f66be2019-05-22 00:42:45 -0400811var oneofScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(OneofScalars)), PBType: &prototype.Message{
812 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaif0c01e42018-11-06 13:05:20 -0800813 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800814 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800815 Fields: []ptype.Field{
816 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
817 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
818 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
819 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
820 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
821 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
822 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
823 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
824 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
825 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
826 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
827 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
828 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
829 },
830 Oneofs: []ptype.Oneof{{Name: "union"}},
831 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400832 NewMessage: func() pref.Message {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800833 return new(OneofScalars)
834 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400835}}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800836
Joe Tsai0fc49f82019-05-01 12:29:25 -0700837// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -0800838func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700839func (m *OneofScalars) Descriptor() pref.MessageDescriptor {
840 return oneofScalarsType.PBType.Descriptor()
841}
Damien Neil374cdb82019-01-29 16:45:30 -0800842func (m *OneofScalars) KnownFields() pref.KnownFields {
843 return oneofScalarsType.MessageOf(m).KnownFields()
844}
845func (m *OneofScalars) UnknownFields() pref.UnknownFields {
846 return oneofScalarsType.MessageOf(m).UnknownFields()
847}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700848func (m *OneofScalars) New() pref.Message { return new(OneofScalars) }
Damien Neil374cdb82019-01-29 16:45:30 -0800849func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
850func (m *OneofScalars) ProtoReflect() pref.Message { return m }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800851
Joe Tsaif18ab532018-11-27 17:25:04 -0800852func (*OneofScalars) XXX_OneofWrappers() []interface{} {
853 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700854 (*OneofScalars_Bool)(nil),
855 (*OneofScalars_Int32)(nil),
856 (*OneofScalars_Int64)(nil),
857 (*OneofScalars_Uint32)(nil),
858 (*OneofScalars_Uint64)(nil),
859 (*OneofScalars_Float32)(nil),
860 (*OneofScalars_Float64)(nil),
861 (*OneofScalars_String)(nil),
862 (*OneofScalars_StringA)(nil),
863 (*OneofScalars_StringB)(nil),
864 (*OneofScalars_Bytes)(nil),
865 (*OneofScalars_BytesA)(nil),
866 (*OneofScalars_BytesB)(nil),
867 }
868}
869
Joe Tsai87b955b2018-11-14 21:59:49 -0800870type (
871 isOneofScalars_Union interface {
872 isOneofScalars_Union()
873 }
874 OneofScalars_Bool struct {
875 Bool bool `protobuf:"1"`
876 }
877 OneofScalars_Int32 struct {
878 Int32 MyInt32 `protobuf:"2"`
879 }
880 OneofScalars_Int64 struct {
881 Int64 int64 `protobuf:"3"`
882 }
883 OneofScalars_Uint32 struct {
884 Uint32 MyUint32 `protobuf:"4"`
885 }
886 OneofScalars_Uint64 struct {
887 Uint64 uint64 `protobuf:"5"`
888 }
889 OneofScalars_Float32 struct {
890 Float32 MyFloat32 `protobuf:"6"`
891 }
892 OneofScalars_Float64 struct {
893 Float64 float64 `protobuf:"7"`
894 }
895 OneofScalars_String struct {
896 String string `protobuf:"8"`
897 }
898 OneofScalars_StringA struct {
899 StringA []byte `protobuf:"9"`
900 }
901 OneofScalars_StringB struct {
902 StringB MyString `protobuf:"10"`
903 }
904 OneofScalars_Bytes struct {
905 Bytes []byte `protobuf:"11"`
906 }
907 OneofScalars_BytesA struct {
908 BytesA string `protobuf:"12"`
909 }
910 OneofScalars_BytesB struct {
911 BytesB MyBytes `protobuf:"13"`
912 }
913)
914
Joe Tsai2c870bb2018-10-17 11:46:52 -0700915func (*OneofScalars_Bool) isOneofScalars_Union() {}
916func (*OneofScalars_Int32) isOneofScalars_Union() {}
917func (*OneofScalars_Int64) isOneofScalars_Union() {}
918func (*OneofScalars_Uint32) isOneofScalars_Union() {}
919func (*OneofScalars_Uint64) isOneofScalars_Union() {}
920func (*OneofScalars_Float32) isOneofScalars_Union() {}
921func (*OneofScalars_Float64) isOneofScalars_Union() {}
922func (*OneofScalars_String) isOneofScalars_Union() {}
923func (*OneofScalars_StringA) isOneofScalars_Union() {}
924func (*OneofScalars_StringB) isOneofScalars_Union() {}
925func (*OneofScalars_Bytes) isOneofScalars_Union() {}
926func (*OneofScalars_BytesA) isOneofScalars_Union() {}
927func (*OneofScalars_BytesB) isOneofScalars_Union() {}
928
929func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800930 empty := &OneofScalars{}
931 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
932 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
933 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
934 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
935 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
936 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
937 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
938 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
939 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
940 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
941 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
942 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
943 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700944
Joe Tsaif0c01e42018-11-06 13:05:20 -0800945 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700946 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},
947 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700948 whichOneofs{"union": 0, "Union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700949
Joe Tsai87b955b2018-11-14 21:59:49 -0800950 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
951 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
952 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
953 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
954 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
955 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
956 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700957
958 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: true, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
959 whichOneofs{"union": 7, "Union": 0},
960
Joe Tsai87b955b2018-11-14 21:59:49 -0800961 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
962 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
963 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
964 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
965 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
966 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700967
968 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},
969 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 -0800970 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700971 whichOneofs{"union": 13, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800972 equalMessage{want13},
973 clearFields{13},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700974 whichOneofs{"union": 0, "Union": 0},
Joe Tsai87b955b2018-11-14 21:59:49 -0800975 equalMessage{empty},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700976 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800977
978 // Test read-only operations on nil message.
979 testMessage(t, nil, (*OneofScalars)(nil), messageOps{
980 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},
981 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"))},
982 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700983}
984
Joe Tsai87b955b2018-11-14 21:59:49 -0800985type EnumProto2 int32
986
Joe Tsaib2f66be2019-05-22 00:42:45 -0400987var enumProto2Type = &prototype.Enum{
988 EnumDescriptor: mustMakeEnumDesc(ptype.StandaloneEnum{
Joe Tsai87b955b2018-11-14 21:59:49 -0800989 Syntax: pref.Proto2,
990 FullName: "EnumProto2",
991 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
992 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400993 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800994 return EnumProto2(n)
995 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400996}
Joe Tsai87b955b2018-11-14 21:59:49 -0800997
Joe Tsai0fc49f82019-05-01 12:29:25 -0700998// TODO: Remove this.
999func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
1000func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
1001func (e EnumProto2) Enum() *EnumProto2 { return &e }
1002func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001003
1004type EnumProto3 int32
1005
Joe Tsaib2f66be2019-05-22 00:42:45 -04001006var enumProto3Type = &prototype.Enum{
1007 EnumDescriptor: mustMakeEnumDesc(ptype.StandaloneEnum{
Joe Tsai87b955b2018-11-14 21:59:49 -08001008 Syntax: pref.Proto3,
1009 FullName: "EnumProto3",
1010 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
1011 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001012 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -08001013 return EnumProto3(n)
1014 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001015}
Joe Tsai87b955b2018-11-14 21:59:49 -08001016
Joe Tsai0fc49f82019-05-01 12:29:25 -07001017// TODO: Remove this.
1018func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
1019func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
1020func (e EnumProto3) Enum() *EnumProto3 { return &e }
1021func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001022
1023type EnumMessages struct {
1024 EnumP2 *EnumProto2 `protobuf:"1"`
1025 EnumP3 *EnumProto3 `protobuf:"2"`
1026 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
1027 MessageCycle *EnumMessages `protobuf:"4"`
1028 EnumList []EnumProto2 `protobuf:"5"`
1029 MessageList []*ScalarProto2 `protobuf:"6"`
1030 EnumMap map[string]EnumProto3 `protobuf:"7"`
1031 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
1032 Union isEnumMessages_Union `protobuf_oneof:"union"`
1033}
1034
Joe Tsaib2f66be2019-05-22 00:42:45 -04001035var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
1036 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai87b955b2018-11-14 21:59:49 -08001037 Syntax: pref.Proto2,
1038 FullName: "EnumMessages",
1039 Fields: []ptype.Field{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001040 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type.Descriptor()},
1041 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type.Descriptor()},
1042 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: pimpl.Export{}.MessageDescriptorOf(new(proto2_20180125.Message))},
Joe Tsai87b955b2018-11-14 21:59:49 -08001043 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001044 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type.Descriptor()},
1045 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001046 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
1047 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001048 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type.Descriptor()},
1049 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type.Descriptor()},
1050 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType.Descriptor()},
1051 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001052 },
1053 Oneofs: []ptype.Oneof{{Name: "union"}},
1054 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001055 NewMessage: func() pref.Message {
Joe Tsai87b955b2018-11-14 21:59:49 -08001056 return new(EnumMessages)
1057 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001058}}
Joe Tsai87b955b2018-11-14 21:59:49 -08001059
1060var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1061 Syntax: pref.Proto2,
1062 FullName: "EnumMessages.F7Entry",
1063 Fields: []ptype.Field{
1064 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001065 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001066 },
Damien Neil232ea152018-12-10 15:14:36 -08001067 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1068 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001069})
1070
1071var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1072 Syntax: pref.Proto2,
1073 FullName: "EnumMessages.F8Entry",
1074 Fields: []ptype.Field{
1075 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001076 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001077 },
Damien Neil232ea152018-12-10 15:14:36 -08001078 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1079 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001080})
1081
Joe Tsai0fc49f82019-05-01 12:29:25 -07001082// TODO: Remove this.
Damien Neil374cdb82019-01-29 16:45:30 -08001083func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.PBType }
Joe Tsai0fc49f82019-05-01 12:29:25 -07001084func (m *EnumMessages) Descriptor() pref.MessageDescriptor {
1085 return enumMessagesType.PBType.Descriptor()
1086}
Damien Neil374cdb82019-01-29 16:45:30 -08001087func (m *EnumMessages) KnownFields() pref.KnownFields {
1088 return enumMessagesType.MessageOf(m).KnownFields()
1089}
1090func (m *EnumMessages) UnknownFields() pref.UnknownFields {
1091 return enumMessagesType.MessageOf(m).UnknownFields()
1092}
Joe Tsai0fc49f82019-05-01 12:29:25 -07001093func (m *EnumMessages) New() pref.Message { return new(EnumMessages) }
Damien Neil374cdb82019-01-29 16:45:30 -08001094func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
1095func (m *EnumMessages) ProtoReflect() pref.Message { return m }
Joe Tsai87b955b2018-11-14 21:59:49 -08001096
Joe Tsaif18ab532018-11-27 17:25:04 -08001097func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1098 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001099 (*EnumMessages_OneofE2)(nil),
1100 (*EnumMessages_OneofE3)(nil),
1101 (*EnumMessages_OneofM2)(nil),
1102 (*EnumMessages_OneofM3)(nil),
1103 }
1104}
1105
1106type (
1107 isEnumMessages_Union interface {
1108 isEnumMessages_Union()
1109 }
1110 EnumMessages_OneofE2 struct {
1111 OneofE2 EnumProto2 `protobuf:"9"`
1112 }
1113 EnumMessages_OneofE3 struct {
1114 OneofE3 EnumProto3 `protobuf:"10"`
1115 }
1116 EnumMessages_OneofM2 struct {
1117 OneofM2 *ScalarProto2 `protobuf:"11"`
1118 }
1119 EnumMessages_OneofM3 struct {
1120 OneofM3 *ScalarProto3 `protobuf:"12"`
1121 }
1122)
1123
1124func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1125func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1126func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1127func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1128
1129func TestEnumMessages(t *testing.T) {
Joe Tsai08e00302018-11-26 22:32:06 -08001130 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai87b955b2018-11-14 21:59:49 -08001131 wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
Joe Tsai009e0672018-11-27 18:45:07 -08001132 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1133 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001134 wantM3a := &ScalarProto3{Float32: math.Pi}
1135 wantM3b := &ScalarProto3{Float32: math.Ln2}
1136
1137 wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
1138 wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
1139
1140 wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
1141 wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
1142
1143 testMessage(t, nil, &EnumMessages{}, messageOps{
1144 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 -08001145 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 -08001146
1147 // Test singular enums.
1148 setFields{1: VE(0xdead), 2: VE(0)},
1149 getFields{1: VE(0xdead), 2: VE(0)},
1150 hasFields{1: true, 2: true},
1151
1152 // Test singular messages.
1153 messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
1154 messageFields{4: messageOps{setFields{1: VE(1234)}}},
1155 getFields{3: V(wantL), 4: V(wantM)},
1156 clearFields{3, 4},
1157 hasFields{3: false, 4: false},
1158 setFields{3: V(wantL), 4: V(wantM)},
1159 hasFields{3: true, 4: true},
1160
1161 // Test list of enums and messages.
1162 listFields{
1163 5: listOps{
1164 appendList{VE(111), VE(222)},
1165 setList{0: VE(333)},
1166 getList{0: VE(333), 1: VE(222)},
1167 lenList(2),
1168 },
1169 6: listOps{
1170 appendMessageList{setFields{4: V(uint32(1e6))}},
1171 appendMessageList{setFields{6: V(float32(math.Phi))}},
1172 setList{0: V(wantM2a)},
1173 getList{0: V(wantM2a), 1: V(wantM2b)},
1174 },
1175 },
1176 getFields{5: wantList5, 6: wantList6},
1177 hasFields{5: true, 6: true},
1178 listFields{5: listOps{truncList(0)}},
1179 hasFields{5: false, 6: true},
1180
1181 // Test maps of enums and messages.
1182 mapFields{
1183 7: mapOps{
1184 setMap{"one": VE(1), "two": VE(2)},
1185 hasMap{"one": true, "two": true, "three": false},
1186 lenMap(2),
1187 },
1188 8: mapOps{
1189 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
1190 setMap{"ln2": V(wantM3b)},
1191 getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
1192 lenMap(2),
1193 },
1194 },
1195 getFields{7: wantMap7, 8: wantMap8},
1196 hasFields{7: true, 8: true},
1197 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1198 hasFields{7: true, 8: false},
1199
1200 // Test oneofs of enums and messages.
1201 setFields{9: VE(0xdead)},
1202 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1203 setFields{10: VE(0)},
1204 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
1205 messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1206 getFields{11: V(wantM2a)},
1207 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
1208 messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1209 getFields{12: V(wantM3a)},
1210 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1211
1212 // Check entire message.
1213 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
1214 equalMessage{&EnumMessages{
1215 EnumP2: EnumProto2(0xdead).Enum(),
1216 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001217 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001218 MessageCycle: wantM,
1219 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1220 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1221 Union: &EnumMessages_OneofM3{wantM3a},
1222 }},
1223 clearFields{1, 2, 3, 4, 6, 7, 12},
1224 equalMessage{&EnumMessages{}},
1225 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001226
1227 // Test read-only operations on nil message.
1228 testMessage(t, nil, (*EnumMessages)(nil), messageOps{
1229 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},
1230 getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1), 11: V(nil), 12: V(nil)},
1231 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1232 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1233 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001234}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001235
Joe Tsai91e14662018-09-13 13:24:35 -07001236var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001237 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1238 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001239 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001240 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
1241 return pv.Interface()
Joe Tsai91e14662018-09-13 13:24:35 -07001242 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001243 cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
Joe Tsaiba0ef9a2018-11-29 14:54:05 -08001244 return x.ProtoUnwrap()
Joe Tsai91e14662018-09-13 13:24:35 -07001245 }),
1246 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001247 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001248}
1249
1250func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
1251 fs := m.KnownFields()
1252 for i, op := range tt {
1253 p.Push(i)
1254 switch op := op.(type) {
1255 case equalMessage:
Joe Tsai87b955b2018-11-14 21:59:49 -08001256 if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001257 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1258 }
1259 case hasFields:
1260 got := map[pref.FieldNumber]bool{}
1261 want := map[pref.FieldNumber]bool(op)
1262 for n := range want {
1263 got[n] = fs.Has(n)
1264 }
1265 if diff := cmp.Diff(want, got); diff != "" {
1266 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
1267 }
1268 case getFields:
1269 got := map[pref.FieldNumber]pref.Value{}
1270 want := map[pref.FieldNumber]pref.Value(op)
1271 for n := range want {
1272 got[n] = fs.Get(n)
1273 }
1274 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1275 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
1276 }
1277 case setFields:
1278 for n, v := range op {
1279 fs.Set(n, v)
1280 }
1281 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001282 for _, n := range op {
1283 fs.Clear(n)
1284 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001285 case whichOneofs:
1286 got := map[pref.Name]pref.FieldNumber{}
1287 want := map[pref.Name]pref.FieldNumber(op)
1288 for s := range want {
1289 got[s] = fs.WhichOneof(s)
1290 }
1291 if diff := cmp.Diff(want, got); diff != "" {
1292 t.Errorf("operation %v, KnownFields.WhichOneof mismatch (-want, +got):\n%s", p, diff)
1293 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001294 case messageFields:
1295 for n, tt := range op {
1296 p.Push(int(n))
Damien Neil97e7f572018-12-07 14:28:33 -08001297 if !fs.Has(n) {
1298 fs.Set(n, V(fs.NewMessage(n)))
1299 }
1300 testMessage(t, p, fs.Get(n).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001301 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001302 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001303 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001304 for n, tt := range op {
1305 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001306 testLists(t, p, fs.Get(n).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001307 p.Pop()
1308 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001309 case mapFields:
1310 for n, tt := range op {
1311 p.Push(int(n))
Joe Tsai6cf80c42018-12-01 04:57:09 -08001312 testMaps(t, p, fs.Get(n).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001313 p.Pop()
1314 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001315 case rangeFields:
1316 got := map[pref.FieldNumber]pref.Value{}
1317 want := map[pref.FieldNumber]pref.Value(op)
1318 fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
1319 got[n] = v
1320 return true
1321 })
1322 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1323 t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
1324 }
Joe Tsai91e14662018-09-13 13:24:35 -07001325 default:
1326 t.Fatalf("operation %v, invalid operation: %T", p, op)
1327 }
1328 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001329 }
1330}
Joe Tsai91e14662018-09-13 13:24:35 -07001331
Joe Tsai4b7aff62018-11-14 14:05:19 -08001332func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001333 for i, op := range tt {
1334 p.Push(i)
1335 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001336 case equalList:
Joe Tsai87b955b2018-11-14 21:59:49 -08001337 if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001338 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001339 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001340 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001341 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001342 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001343 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001344 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001345 got := map[int]pref.Value{}
1346 want := map[int]pref.Value(op)
1347 for n := range want {
1348 got[n] = v.Get(n)
1349 }
1350 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001351 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001352 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001353 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001354 for n, e := range op {
1355 v.Set(n, e)
1356 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001357 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001358 for _, e := range op {
1359 v.Append(e)
1360 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001361 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001362 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001363 v.Append(V(m))
1364 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001365 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001366 v.Truncate(int(op))
1367 default:
1368 t.Fatalf("operation %v, invalid operation: %T", p, op)
1369 }
1370 p.Pop()
1371 }
1372}
1373
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001374func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1375 for i, op := range tt {
1376 p.Push(i)
1377 switch op := op.(type) {
1378 case equalMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001379 if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001380 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1381 }
1382 case lenMap:
1383 if got, want := m.Len(), int(op); got != want {
1384 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1385 }
1386 case hasMap:
1387 got := map[interface{}]bool{}
1388 want := map[interface{}]bool(op)
1389 for k := range want {
1390 got[k] = m.Has(V(k).MapKey())
1391 }
1392 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1393 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1394 }
1395 case getMap:
1396 got := map[interface{}]pref.Value{}
1397 want := map[interface{}]pref.Value(op)
1398 for k := range want {
1399 got[k] = m.Get(V(k).MapKey())
1400 }
1401 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1402 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1403 }
1404 case setMap:
1405 for k, v := range op {
1406 m.Set(V(k).MapKey(), v)
1407 }
1408 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001409 for _, k := range op {
1410 m.Clear(V(k).MapKey())
1411 }
1412 case messageMap:
1413 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001414 mk := V(k).MapKey()
1415 if !m.Has(mk) {
1416 m.Set(mk, V(m.NewMessage()))
1417 }
1418 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001419 }
1420 case rangeMap:
1421 got := map[interface{}]pref.Value{}
1422 want := map[interface{}]pref.Value(op)
1423 m.Range(func(k pref.MapKey, v pref.Value) bool {
1424 got[k.Interface()] = v
1425 return true
1426 })
1427 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1428 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1429 }
1430 default:
1431 t.Fatalf("operation %v, invalid operation: %T", p, op)
1432 }
1433 p.Pop()
1434 }
1435}
1436
Joe Tsai91e14662018-09-13 13:24:35 -07001437type path []int
1438
1439func (p *path) Push(i int) { *p = append(*p, i) }
1440func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1441func (p path) String() string {
1442 var ss []string
1443 for _, i := range p {
1444 ss = append(ss, fmt.Sprint(i))
1445 }
1446 return strings.Join(ss, ".")
1447}