blob: 722d1c0e2b5a97ffe0df4739b03cf68eb749b180 [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"
Damien Neile89e6242019-05-13 23:55:40 -070020 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaib2f66be2019-05-22 00:42:45 -040021 "google.golang.org/protobuf/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070022
Damien Neile89e6242019-05-13 23:55:40 -070023 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsaia95b29f2019-05-16 12:47:20 -070024 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070025)
26
Joe Tsai4b7aff62018-11-14 14:05:19 -080027// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070028type (
Joe Tsai87b955b2018-11-14 21:59:49 -080029 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070030 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070031
Joe Tsai87b955b2018-11-14 21:59:49 -080032 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080033 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070034
Joe Tsai87b955b2018-11-14 21:59:49 -080035 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000036 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070037)
38
39// Test operations performed on a message.
40type (
Joe Tsai87b955b2018-11-14 21:59:49 -080041 // check that the message contents match
42 equalMessage struct{ pref.Message }
43 // check presence for specific fields in the message
44 hasFields map[pref.FieldNumber]bool
45 // check that specific message fields match
46 getFields map[pref.FieldNumber]pref.Value
47 // set specific message fields
48 setFields map[pref.FieldNumber]pref.Value
49 // clear specific fields in the message
50 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070051 // check for the presence of specific oneof member fields.
52 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080053 // apply messageOps on each specified message field
Joe Tsai378c1322019-04-25 23:48:08 -070054 messageFields map[pref.FieldNumber]messageOps
55 messageFieldsMutable map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080056 // apply listOps on each specified list field
Joe Tsai378c1322019-04-25 23:48:08 -070057 listFields map[pref.FieldNumber]listOps
58 listFieldsMutable map[pref.FieldNumber]listOps
Joe Tsai87b955b2018-11-14 21:59:49 -080059 // apply mapOps on each specified map fields
Joe Tsai378c1322019-04-25 23:48:08 -070060 mapFields map[pref.FieldNumber]mapOps
61 mapFieldsMutable map[pref.FieldNumber]mapOps
Joe Tsai87b955b2018-11-14 21:59:49 -080062 // range through all fields and check that they match
63 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070064)
65
Joe Tsai378c1322019-04-25 23:48:08 -070066func (equalMessage) isMessageOp() {}
67func (hasFields) isMessageOp() {}
68func (getFields) isMessageOp() {}
69func (setFields) isMessageOp() {}
70func (clearFields) isMessageOp() {}
71func (whichOneofs) isMessageOp() {}
72func (messageFields) isMessageOp() {}
73func (messageFieldsMutable) isMessageOp() {}
74func (listFields) isMessageOp() {}
75func (listFieldsMutable) isMessageOp() {}
76func (mapFields) isMessageOp() {}
77func (mapFieldsMutable) isMessageOp() {}
78func (rangeFields) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080079
Joe Tsai4b7aff62018-11-14 14:05:19 -080080// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070081type (
Joe Tsai87b955b2018-11-14 21:59:49 -080082 // check that the list contents match
83 equalList struct{ pref.List }
84 // check that list length matches
85 lenList int
86 // check that specific list entries match
87 getList map[int]pref.Value
88 // set specific list entries
89 setList map[int]pref.Value
90 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080091 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080092 // apply messageOps on a newly appended message
93 appendMessageList messageOps
94 // truncate the list to the specified length
95 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070096)
97
Joe Tsai87b955b2018-11-14 21:59:49 -080098func (equalList) isListOp() {}
99func (lenList) isListOp() {}
100func (getList) isListOp() {}
101func (setList) isListOp() {}
102func (appendList) isListOp() {}
103func (appendMessageList) isListOp() {}
104func (truncList) isListOp() {}
105
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000106// Test operations performed on a map.
107type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800108 // check that the map contents match
109 equalMap struct{ pref.Map }
110 // check that map length matches
111 lenMap int
112 // check presence for specific entries in the map
113 hasMap map[interface{}]bool
114 // check that specific map entries match
115 getMap map[interface{}]pref.Value
116 // set specific map entries
117 setMap map[interface{}]pref.Value
118 // clear specific entries in the map
119 clearMap []interface{}
120 // apply messageOps on each specified message entry
121 messageMap map[interface{}]messageOps
122 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000123 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000124)
125
Joe Tsai87b955b2018-11-14 21:59:49 -0800126func (equalMap) isMapOp() {}
127func (lenMap) isMapOp() {}
128func (hasMap) isMapOp() {}
129func (getMap) isMapOp() {}
130func (setMap) isMapOp() {}
131func (clearMap) isMapOp() {}
132func (messageMap) isMapOp() {}
133func (rangeMap) isMapOp() {}
134
Joe Tsaice6edd32018-10-19 16:27:46 -0700135type ScalarProto2 struct {
136 Bool *bool `protobuf:"1"`
137 Int32 *int32 `protobuf:"2"`
138 Int64 *int64 `protobuf:"3"`
139 Uint32 *uint32 `protobuf:"4"`
140 Uint64 *uint64 `protobuf:"5"`
141 Float32 *float32 `protobuf:"6"`
142 Float64 *float64 `protobuf:"7"`
143 String *string `protobuf:"8"`
144 StringA []byte `protobuf:"9"`
145 Bytes []byte `protobuf:"10"`
146 BytesA *string `protobuf:"11"`
147
148 MyBool *MyBool `protobuf:"12"`
149 MyInt32 *MyInt32 `protobuf:"13"`
150 MyInt64 *MyInt64 `protobuf:"14"`
151 MyUint32 *MyUint32 `protobuf:"15"`
152 MyUint64 *MyUint64 `protobuf:"16"`
153 MyFloat32 *MyFloat32 `protobuf:"17"`
154 MyFloat64 *MyFloat64 `protobuf:"18"`
155 MyString *MyString `protobuf:"19"`
156 MyStringA MyBytes `protobuf:"20"`
157 MyBytes MyBytes `protobuf:"21"`
158 MyBytesA *MyString `protobuf:"22"`
159}
160
Joe Tsai87b955b2018-11-14 21:59:49 -0800161func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
162 ed, err := ptype.NewEnum(&t)
163 if err != nil {
164 panic(err)
165 }
166 return ed
167}
168
169func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
170 md, err := ptype.NewMessage(&t)
171 if err != nil {
172 panic(err)
173 }
174 return md
175}
176
177var V = pref.ValueOf
178var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
179
180type (
181 MyBool bool
182 MyInt32 int32
183 MyInt64 int64
184 MyUint32 uint32
185 MyUint64 uint64
186 MyFloat32 float32
187 MyFloat64 float64
188 MyString string
189 MyBytes []byte
190
191 ListStrings []MyString
192 ListBytes []MyBytes
193
194 MapStrings map[MyString]MyString
195 MapBytes map[MyString]MyBytes
196)
197
Joe Tsaib2f66be2019-05-22 00:42:45 -0400198var scalarProto2Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: &prototype.Message{
199 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700200 Syntax: pref.Proto2,
201 FullName: "ScalarProto2",
202 Fields: []ptype.Field{
203 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
204 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
205 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
206 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
207 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
208 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
209 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
210 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
211 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
212 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
213 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
214
215 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
216 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
217 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
218 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
219 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
220 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
221 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
222 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
223 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
224 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
225 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
226 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800227 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400228 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700229 return pref.ProtoMessage(new(ScalarProto2)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800230 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400231}}
Joe Tsai91e14662018-09-13 13:24:35 -0700232
Joe Tsai378c1322019-04-25 23:48:08 -0700233func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800234
235func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700236 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700237 hasFields{
238 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
239 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
240 },
241 getFields{
242 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")),
243 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")),
244 },
245 setFields{
246 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)),
247 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)),
248 },
249 hasFields{
250 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
251 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
252 },
Joe Tsai378c1322019-04-25 23:48:08 -0700253 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700254 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
255 new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
Joe Tsai378c1322019-04-25 23:48:08 -0700256 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800257 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
Joe Tsai378c1322019-04-25 23:48:08 -0700258 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700259 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800260
261 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700262 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800263 hasFields{
264 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
265 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
266 },
267 getFields{
268 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")),
269 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")),
270 },
271 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700272}
273
Joe Tsaice6edd32018-10-19 16:27:46 -0700274type ScalarProto3 struct {
275 Bool bool `protobuf:"1"`
276 Int32 int32 `protobuf:"2"`
277 Int64 int64 `protobuf:"3"`
278 Uint32 uint32 `protobuf:"4"`
279 Uint64 uint64 `protobuf:"5"`
280 Float32 float32 `protobuf:"6"`
281 Float64 float64 `protobuf:"7"`
282 String string `protobuf:"8"`
283 StringA []byte `protobuf:"9"`
284 Bytes []byte `protobuf:"10"`
285 BytesA string `protobuf:"11"`
286
287 MyBool MyBool `protobuf:"12"`
288 MyInt32 MyInt32 `protobuf:"13"`
289 MyInt64 MyInt64 `protobuf:"14"`
290 MyUint32 MyUint32 `protobuf:"15"`
291 MyUint64 MyUint64 `protobuf:"16"`
292 MyFloat32 MyFloat32 `protobuf:"17"`
293 MyFloat64 MyFloat64 `protobuf:"18"`
294 MyString MyString `protobuf:"19"`
295 MyStringA MyBytes `protobuf:"20"`
296 MyBytes MyBytes `protobuf:"21"`
297 MyBytesA MyString `protobuf:"22"`
298}
299
Joe Tsaib2f66be2019-05-22 00:42:45 -0400300var scalarProto3Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: &prototype.Message{
301 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700302 Syntax: pref.Proto3,
303 FullName: "ScalarProto3",
304 Fields: []ptype.Field{
305 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
306 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
307 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
308 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
309 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
310 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
311 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
312 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
313 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
314 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
315 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700316
Joe Tsai91e14662018-09-13 13:24:35 -0700317 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
318 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
319 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
320 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
321 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
322 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
323 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
324 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
325 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
326 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
327 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
328 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800329 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400330 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700331 return pref.ProtoMessage(new(ScalarProto3)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800332 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400333}}
Joe Tsai91e14662018-09-13 13:24:35 -0700334
Joe Tsai378c1322019-04-25 23:48:08 -0700335func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800336
337func TestScalarProto3(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700338 testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700339 hasFields{
340 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
341 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
342 },
343 getFields{
344 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)),
345 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)),
346 },
347 setFields{
348 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)),
349 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)),
350 },
351 hasFields{
352 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
353 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
354 },
Joe Tsai378c1322019-04-25 23:48:08 -0700355 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700356 setFields{
357 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")),
358 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")),
359 },
360 hasFields{
361 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
362 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
363 },
Joe Tsai378c1322019-04-25 23:48:08 -0700364 equalMessage{(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700365 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
366 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai378c1322019-04-25 23:48:08 -0700367 }).ProtoReflect()},
Joe Tsai44e389c2018-11-19 15:27:09 -0800368 setFields{
369 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
370 },
371 hasFields{
372 2: true, 3: true, 6: true, 7: true,
373 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800374 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
Joe Tsai378c1322019-04-25 23:48:08 -0700375 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai060cdac2019-04-22 11:44:49 -0700376
377 // Verify that -0 triggers proper Has behavior.
378 hasFields{6: false, 7: false},
379 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
380 hasFields{6: true, 7: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700381 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800382
383 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700384 testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800385 hasFields{
386 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
387 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
388 },
389 getFields{
390 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)),
391 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)),
392 },
393 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700394}
395
Joe Tsaif0c01e42018-11-06 13:05:20 -0800396type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700397 Bools []bool `protobuf:"1"`
398 Int32s []int32 `protobuf:"2"`
399 Int64s []int64 `protobuf:"3"`
400 Uint32s []uint32 `protobuf:"4"`
401 Uint64s []uint64 `protobuf:"5"`
402 Float32s []float32 `protobuf:"6"`
403 Float64s []float64 `protobuf:"7"`
404 Strings []string `protobuf:"8"`
405 StringsA [][]byte `protobuf:"9"`
406 Bytes [][]byte `protobuf:"10"`
407 BytesA []string `protobuf:"11"`
408
409 MyStrings1 []MyString `protobuf:"12"`
410 MyStrings2 []MyBytes `protobuf:"13"`
411 MyBytes1 []MyBytes `protobuf:"14"`
412 MyBytes2 []MyString `protobuf:"15"`
413
Joe Tsai4b7aff62018-11-14 14:05:19 -0800414 MyStrings3 ListStrings `protobuf:"16"`
415 MyStrings4 ListBytes `protobuf:"17"`
416 MyBytes3 ListBytes `protobuf:"18"`
417 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700418}
419
Joe Tsaib2f66be2019-05-22 00:42:45 -0400420var listScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ListScalars)), PBType: &prototype.Message{
421 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700422 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800423 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700424 Fields: []ptype.Field{
425 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
426 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
427 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
428 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
429 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
430 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
431 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
432 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
433 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
434 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
435 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700436
Joe Tsai91e14662018-09-13 13:24:35 -0700437 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
438 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
439 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
440 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
441
442 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
443 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
444 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
445 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700446 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800447 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400448 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700449 return pref.ProtoMessage(new(ListScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800450 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400451}}
Joe Tsai91e14662018-09-13 13:24:35 -0700452
Joe Tsai378c1322019-04-25 23:48:08 -0700453func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800454
455func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700456 empty := new(ListScalars).ProtoReflect()
457 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700458 Bools: []bool{true, false, true},
459 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
460 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
461 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
462 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
463 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
464 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
465 Strings: []string{"8", "", "eight"},
466 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
467 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
468 BytesA: []string{"11", "", "eleven"},
469
470 MyStrings1: []MyString{"12", "", "twelve"},
471 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
472 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
473 MyBytes2: []MyString{"15", "", "fifteen"},
474
Joe Tsai4b7aff62018-11-14 14:05:19 -0800475 MyStrings3: ListStrings{"16", "", "sixteen"},
476 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
477 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
478 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700479 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700480
Joe Tsai378c1322019-04-25 23:48:08 -0700481 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700482 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},
Joe Tsai378c1322019-04-25 23:48:08 -0700483 getFields{1: getField(empty, 1), 3: getField(empty, 3), 5: getField(empty, 5), 7: getField(empty, 7), 9: getField(empty, 9), 11: getField(empty, 11), 13: getField(empty, 13), 15: getField(empty, 15), 17: getField(empty, 17), 19: getField(empty, 19)},
484 setFields{1: getField(want, 1), 3: getField(want, 3), 5: getField(want, 5), 7: getField(want, 7), 9: getField(want, 9), 11: getField(want, 11), 13: getField(want, 13), 15: getField(want, 15), 17: getField(want, 17), 19: getField(want, 19)},
485 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700486 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800487 lenList(0),
488 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
489 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700490 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700491 },
492 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800493 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
494 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
495 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700496 },
497 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800498 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700499 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700500 },
501 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800502 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
503 lenList(6),
504 setList{0: V("8"), 2: V("eight")},
505 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700506 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700507 },
508 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800509 appendList{V([]byte(nil)), V([]byte(nil))},
510 setList{0: V([]byte("10"))},
511 appendList{V([]byte("wrong"))},
512 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700513 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700514 },
515 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800516 appendList{V("12"), V("wrong"), V("twelve")},
517 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700518 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700519 },
520 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800521 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700522 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700523 },
524 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800525 appendList{V("16"), V(""), V("sixteen"), V("extra")},
526 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700527 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700528 },
529 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800530 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700531 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700532 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700533 },
Joe Tsai91e14662018-09-13 13:24:35 -0700534 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 -0800535 equalMessage{want},
536 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
537 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000538 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800539
540 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700541 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800542 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},
543 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)}},
544 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000545}
546
Joe Tsaice6edd32018-10-19 16:27:46 -0700547type MapScalars struct {
548 KeyBools map[bool]string `protobuf:"1"`
549 KeyInt32s map[int32]string `protobuf:"2"`
550 KeyInt64s map[int64]string `protobuf:"3"`
551 KeyUint32s map[uint32]string `protobuf:"4"`
552 KeyUint64s map[uint64]string `protobuf:"5"`
553 KeyStrings map[string]string `protobuf:"6"`
554
555 ValBools map[string]bool `protobuf:"7"`
556 ValInt32s map[string]int32 `protobuf:"8"`
557 ValInt64s map[string]int64 `protobuf:"9"`
558 ValUint32s map[string]uint32 `protobuf:"10"`
559 ValUint64s map[string]uint64 `protobuf:"11"`
560 ValFloat32s map[string]float32 `protobuf:"12"`
561 ValFloat64s map[string]float64 `protobuf:"13"`
562 ValStrings map[string]string `protobuf:"14"`
563 ValStringsA map[string][]byte `protobuf:"15"`
564 ValBytes map[string][]byte `protobuf:"16"`
565 ValBytesA map[string]string `protobuf:"17"`
566
567 MyStrings1 map[MyString]MyString `protobuf:"18"`
568 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
569 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
570 MyBytes2 map[MyString]MyString `protobuf:"21"`
571
572 MyStrings3 MapStrings `protobuf:"22"`
573 MyStrings4 MapBytes `protobuf:"23"`
574 MyBytes3 MapBytes `protobuf:"24"`
575 MyBytes4 MapStrings `protobuf:"25"`
576}
577
Joe Tsaif0c01e42018-11-06 13:05:20 -0800578func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
579 return ptype.Field{
580 Name: pref.Name(fmt.Sprintf("f%d", n)),
581 Number: n,
582 Cardinality: pref.Repeated,
583 Kind: pref.MessageKind,
584 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
585 Syntax: pref.Proto2,
586 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
587 Fields: []ptype.Field{
588 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
589 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
590 },
Damien Neil232ea152018-12-10 15:14:36 -0800591 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
592 IsMapEntry: true,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800593 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000594 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800595}
596
Joe Tsaib2f66be2019-05-22 00:42:45 -0400597var mapScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(MapScalars)), PBType: &prototype.Message{
598 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000599 Syntax: pref.Proto2,
600 FullName: "MapScalars",
601 Fields: []ptype.Field{
602 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
603 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
604 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
605 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
606 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
607 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
608
609 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
610 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
611 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
612 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
613 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
614 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
615 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
616 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
617 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
618 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
619 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
620
621 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
622 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
623 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
624 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
625
626 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
627 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
628 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
629 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
630 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800631 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400632 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700633 return pref.ProtoMessage(new(MapScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800634 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400635}}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000636
Joe Tsai378c1322019-04-25 23:48:08 -0700637func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800638
639func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700640 empty := new(MapScalars).ProtoReflect()
641 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000642 KeyBools: map[bool]string{true: "true", false: "false"},
643 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
644 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
645 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
646 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
647 KeyStrings: map[string]string{"": "", "foo": "bar"},
648
649 ValBools: map[string]bool{"true": true, "false": false},
650 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
651 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
652 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
653 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
654 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
655 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
656 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
657 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
658 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
659 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
660
661 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
662 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
663 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
664 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
665
666 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
667 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
668 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
669 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700670 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000671
Joe Tsai378c1322019-04-25 23:48:08 -0700672 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000673 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},
Joe Tsai378c1322019-04-25 23:48:08 -0700674 getFields{1: getField(empty, 1), 3: getField(empty, 3), 5: getField(empty, 5), 7: getField(empty, 7), 9: getField(empty, 9), 11: getField(empty, 11), 13: getField(empty, 13), 15: getField(empty, 15), 17: getField(empty, 17), 19: getField(empty, 19), 21: getField(empty, 21), 23: getField(empty, 23), 25: getField(empty, 25)},
675 setFields{1: getField(want, 1), 3: getField(want, 3), 5: getField(want, 5), 7: getField(want, 7), 9: getField(want, 9), 11: getField(want, 11), 13: getField(want, 13), 15: getField(want, 15), 17: getField(want, 17), 19: getField(want, 19), 21: getField(want, 21), 23: getField(want, 23), 25: getField(want, 25)},
676 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000677 2: {
678 lenMap(0),
679 hasMap{int32(0): false, int32(-1): false, int32(2): false},
680 setMap{int32(0): V("zero")},
681 lenMap(1),
682 hasMap{int32(0): true, int32(-1): false, int32(2): false},
683 setMap{int32(-1): V("one")},
684 lenMap(2),
685 hasMap{int32(0): true, int32(-1): true, int32(2): false},
686 setMap{int32(2): V("two")},
687 lenMap(3),
688 hasMap{int32(0): true, int32(-1): true, int32(2): true},
689 },
690 4: {
691 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700692 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000693 },
694 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800695 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000696 setMap{"foo": V("bar")},
697 setMap{"": V("empty")},
698 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
699 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800700 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000701 },
702 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700703 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000704 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
705 },
706 10: {
707 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
708 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700709 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000710 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
711 },
712 12: {
713 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800714 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000715 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
716 },
717 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700718 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000719 setMap{"s1": V("s1"), "s2": V("s2")},
720 },
721 16: {
722 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700723 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000724 },
725 18: {
726 hasMap{"s1": false, "s2": false, "s3": false},
727 setMap{"s1": V("s1"), "s2": V("s2")},
728 hasMap{"s1": true, "s2": true, "s3": false},
729 },
730 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700731 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
733 },
734 22: {
735 rangeMap{},
736 setMap{"s1": V("s1"), "s2": V("s2")},
737 rangeMap{"s1": V("s1"), "s2": V("s2")},
738 lenMap(2),
739 },
740 24: {
741 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700742 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000743 },
744 },
745 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 -0800746 equalMessage{want},
747 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},
748 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700749 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800750
751 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700752 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800753 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},
754 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)}},
755 })
Joe Tsai91e14662018-09-13 13:24:35 -0700756}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700757
Joe Tsai87b955b2018-11-14 21:59:49 -0800758type OneofScalars struct {
759 Union isOneofScalars_Union `protobuf_oneof:"union"`
760}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700761
Joe Tsaib2f66be2019-05-22 00:42:45 -0400762var oneofScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(OneofScalars)), PBType: &prototype.Message{
763 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaif0c01e42018-11-06 13:05:20 -0800764 Syntax: pref.Proto2,
Joe Tsai87b955b2018-11-14 21:59:49 -0800765 FullName: "OneofScalars",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800766 Fields: []ptype.Field{
767 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
768 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
769 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
770 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
771 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
772 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
773 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
774 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
775 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
776 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
777 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
778 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
779 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
780 },
781 Oneofs: []ptype.Oneof{{Name: "union"}},
782 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400783 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700784 return pref.ProtoMessage(new(OneofScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800785 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400786}}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800787
Joe Tsai378c1322019-04-25 23:48:08 -0700788func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800789
Joe Tsaif18ab532018-11-27 17:25:04 -0800790func (*OneofScalars) XXX_OneofWrappers() []interface{} {
791 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700792 (*OneofScalars_Bool)(nil),
793 (*OneofScalars_Int32)(nil),
794 (*OneofScalars_Int64)(nil),
795 (*OneofScalars_Uint32)(nil),
796 (*OneofScalars_Uint64)(nil),
797 (*OneofScalars_Float32)(nil),
798 (*OneofScalars_Float64)(nil),
799 (*OneofScalars_String)(nil),
800 (*OneofScalars_StringA)(nil),
801 (*OneofScalars_StringB)(nil),
802 (*OneofScalars_Bytes)(nil),
803 (*OneofScalars_BytesA)(nil),
804 (*OneofScalars_BytesB)(nil),
805 }
806}
807
Joe Tsai87b955b2018-11-14 21:59:49 -0800808type (
809 isOneofScalars_Union interface {
810 isOneofScalars_Union()
811 }
812 OneofScalars_Bool struct {
813 Bool bool `protobuf:"1"`
814 }
815 OneofScalars_Int32 struct {
816 Int32 MyInt32 `protobuf:"2"`
817 }
818 OneofScalars_Int64 struct {
819 Int64 int64 `protobuf:"3"`
820 }
821 OneofScalars_Uint32 struct {
822 Uint32 MyUint32 `protobuf:"4"`
823 }
824 OneofScalars_Uint64 struct {
825 Uint64 uint64 `protobuf:"5"`
826 }
827 OneofScalars_Float32 struct {
828 Float32 MyFloat32 `protobuf:"6"`
829 }
830 OneofScalars_Float64 struct {
831 Float64 float64 `protobuf:"7"`
832 }
833 OneofScalars_String struct {
834 String string `protobuf:"8"`
835 }
836 OneofScalars_StringA struct {
837 StringA []byte `protobuf:"9"`
838 }
839 OneofScalars_StringB struct {
840 StringB MyString `protobuf:"10"`
841 }
842 OneofScalars_Bytes struct {
843 Bytes []byte `protobuf:"11"`
844 }
845 OneofScalars_BytesA struct {
846 BytesA string `protobuf:"12"`
847 }
848 OneofScalars_BytesB struct {
849 BytesB MyBytes `protobuf:"13"`
850 }
851)
852
Joe Tsai2c870bb2018-10-17 11:46:52 -0700853func (*OneofScalars_Bool) isOneofScalars_Union() {}
854func (*OneofScalars_Int32) isOneofScalars_Union() {}
855func (*OneofScalars_Int64) isOneofScalars_Union() {}
856func (*OneofScalars_Uint32) isOneofScalars_Union() {}
857func (*OneofScalars_Uint64) isOneofScalars_Union() {}
858func (*OneofScalars_Float32) isOneofScalars_Union() {}
859func (*OneofScalars_Float64) isOneofScalars_Union() {}
860func (*OneofScalars_String) isOneofScalars_Union() {}
861func (*OneofScalars_StringA) isOneofScalars_Union() {}
862func (*OneofScalars_StringB) isOneofScalars_Union() {}
863func (*OneofScalars_Bytes) isOneofScalars_Union() {}
864func (*OneofScalars_BytesA) isOneofScalars_Union() {}
865func (*OneofScalars_BytesB) isOneofScalars_Union() {}
866
867func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800868 empty := &OneofScalars{}
869 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
870 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
871 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
872 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
873 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
874 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
875 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
876 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
877 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
878 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
879 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
880 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
881 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700882
Joe Tsai378c1322019-04-25 23:48:08 -0700883 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700884 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},
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("13"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700886 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700887
Joe Tsai378c1322019-04-25 23:48:08 -0700888 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
889 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
890 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
891 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
892 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
893 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
894 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700895
896 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},
Joe Tsai378c1322019-04-25 23:48:08 -0700897 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700898
Joe Tsai378c1322019-04-25 23:48:08 -0700899 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
900 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
901 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
902 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
903 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
904 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700905
906 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},
907 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 -0800908 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700909 whichOneofs{"union": 13},
910 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800911 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700912 whichOneofs{"union": 0},
913 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700914 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800915
916 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700917 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800918 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},
919 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"))},
920 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700921}
922
Joe Tsai87b955b2018-11-14 21:59:49 -0800923type EnumProto2 int32
924
Joe Tsaib2f66be2019-05-22 00:42:45 -0400925var enumProto2Type = &prototype.Enum{
926 EnumDescriptor: mustMakeEnumDesc(ptype.StandaloneEnum{
Joe Tsai87b955b2018-11-14 21:59:49 -0800927 Syntax: pref.Proto2,
928 FullName: "EnumProto2",
929 Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
930 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400931 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800932 return EnumProto2(n)
933 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400934}
Joe Tsai87b955b2018-11-14 21:59:49 -0800935
Joe Tsai0fc49f82019-05-01 12:29:25 -0700936func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
937func (e EnumProto2) Enum() *EnumProto2 { return &e }
938func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800939
940type EnumProto3 int32
941
Joe Tsaib2f66be2019-05-22 00:42:45 -0400942var enumProto3Type = &prototype.Enum{
943 EnumDescriptor: mustMakeEnumDesc(ptype.StandaloneEnum{
Joe Tsai87b955b2018-11-14 21:59:49 -0800944 Syntax: pref.Proto3,
945 FullName: "EnumProto3",
946 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
947 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400948 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800949 return EnumProto3(n)
950 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400951}
Joe Tsai87b955b2018-11-14 21:59:49 -0800952
Joe Tsai0fc49f82019-05-01 12:29:25 -0700953func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
954func (e EnumProto3) Enum() *EnumProto3 { return &e }
955func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800956
957type EnumMessages struct {
958 EnumP2 *EnumProto2 `protobuf:"1"`
959 EnumP3 *EnumProto3 `protobuf:"2"`
960 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
961 MessageCycle *EnumMessages `protobuf:"4"`
962 EnumList []EnumProto2 `protobuf:"5"`
963 MessageList []*ScalarProto2 `protobuf:"6"`
964 EnumMap map[string]EnumProto3 `protobuf:"7"`
965 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
966 Union isEnumMessages_Union `protobuf_oneof:"union"`
967}
968
Joe Tsaib2f66be2019-05-22 00:42:45 -0400969var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
970 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai87b955b2018-11-14 21:59:49 -0800971 Syntax: pref.Proto2,
972 FullName: "EnumMessages",
973 Fields: []ptype.Field{
Joe Tsai0fc49f82019-05-01 12:29:25 -0700974 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type.Descriptor()},
975 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type.Descriptor()},
976 {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 -0800977 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
Joe Tsai0fc49f82019-05-01 12:29:25 -0700978 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type.Descriptor()},
979 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800980 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
981 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
Joe Tsai0fc49f82019-05-01 12:29:25 -0700982 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type.Descriptor()},
983 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type.Descriptor()},
984 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType.Descriptor()},
985 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800986 },
987 Oneofs: []ptype.Oneof{{Name: "union"}},
988 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400989 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700990 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -0800991 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400992}}
Joe Tsai87b955b2018-11-14 21:59:49 -0800993
994var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
995 Syntax: pref.Proto2,
996 FullName: "EnumMessages.F7Entry",
997 Fields: []ptype.Field{
998 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -0700999 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001000 },
Damien Neil232ea152018-12-10 15:14:36 -08001001 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1002 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001003})
1004
1005var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1006 Syntax: pref.Proto2,
1007 FullName: "EnumMessages.F8Entry",
1008 Fields: []ptype.Field{
1009 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001010 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001011 },
Damien Neil232ea152018-12-10 15:14:36 -08001012 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1013 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001014})
1015
Joe Tsai378c1322019-04-25 23:48:08 -07001016func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001017
Joe Tsaif18ab532018-11-27 17:25:04 -08001018func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1019 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001020 (*EnumMessages_OneofE2)(nil),
1021 (*EnumMessages_OneofE3)(nil),
1022 (*EnumMessages_OneofM2)(nil),
1023 (*EnumMessages_OneofM3)(nil),
1024 }
1025}
1026
1027type (
1028 isEnumMessages_Union interface {
1029 isEnumMessages_Union()
1030 }
1031 EnumMessages_OneofE2 struct {
1032 OneofE2 EnumProto2 `protobuf:"9"`
1033 }
1034 EnumMessages_OneofE3 struct {
1035 OneofE3 EnumProto3 `protobuf:"10"`
1036 }
1037 EnumMessages_OneofM2 struct {
1038 OneofM2 *ScalarProto2 `protobuf:"11"`
1039 }
1040 EnumMessages_OneofM3 struct {
1041 OneofM3 *ScalarProto3 `protobuf:"12"`
1042 }
1043)
1044
1045func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1046func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1047func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1048func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1049
1050func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001051 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1052 emptyM := new(EnumMessages).ProtoReflect()
1053 emptyM2 := new(ScalarProto2).ProtoReflect()
1054 emptyM3 := new(ScalarProto3).ProtoReflect()
1055
Joe Tsai08e00302018-11-26 22:32:06 -08001056 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001057 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Joe Tsai009e0672018-11-27 18:45:07 -08001058 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1059 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001060 wantM3a := &ScalarProto3{Float32: math.Pi}
1061 wantM3b := &ScalarProto3{Float32: math.Ln2}
1062
Joe Tsai378c1322019-04-25 23:48:08 -07001063 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1064 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001065
Joe Tsai378c1322019-04-25 23:48:08 -07001066 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1067 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001068
Joe Tsai378c1322019-04-25 23:48:08 -07001069 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001070 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 Tsai378c1322019-04-25 23:48:08 -07001071 getFields{1: VE(0xbeef), 2: VE(1), 3: V(emptyL), 4: V(emptyM), 9: VE(0xbeef), 10: VE(1)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001072
1073 // Test singular enums.
1074 setFields{1: VE(0xdead), 2: VE(0)},
1075 getFields{1: VE(0xdead), 2: VE(0)},
1076 hasFields{1: true, 2: true},
1077
1078 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001079 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1080 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001081 getFields{3: V(wantL), 4: V(wantM)},
1082 clearFields{3, 4},
1083 hasFields{3: false, 4: false},
1084 setFields{3: V(wantL), 4: V(wantM)},
1085 hasFields{3: true, 4: true},
1086
1087 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001088 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001089 5: listOps{
1090 appendList{VE(111), VE(222)},
1091 setList{0: VE(333)},
1092 getList{0: VE(333), 1: VE(222)},
1093 lenList(2),
1094 },
1095 6: listOps{
1096 appendMessageList{setFields{4: V(uint32(1e6))}},
1097 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001098 setList{0: V(wantM2a.ProtoReflect())},
1099 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001100 },
1101 },
1102 getFields{5: wantList5, 6: wantList6},
1103 hasFields{5: true, 6: true},
1104 listFields{5: listOps{truncList(0)}},
1105 hasFields{5: false, 6: true},
1106
1107 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001108 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001109 7: mapOps{
1110 setMap{"one": VE(1), "two": VE(2)},
1111 hasMap{"one": true, "two": true, "three": false},
1112 lenMap(2),
1113 },
1114 8: mapOps{
1115 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001116 setMap{"ln2": V(wantM3b.ProtoReflect())},
1117 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001118 lenMap(2),
1119 },
1120 },
1121 getFields{7: wantMap7, 8: wantMap8},
1122 hasFields{7: true, 8: true},
1123 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1124 hasFields{7: true, 8: false},
1125
1126 // Test oneofs of enums and messages.
1127 setFields{9: VE(0xdead)},
1128 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1129 setFields{10: VE(0)},
1130 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001131 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1132 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001133 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001134 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1135 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001136 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1137
1138 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001139 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1140 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001141 EnumP2: EnumProto2(0xdead).Enum(),
1142 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001143 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001144 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001145 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1146 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1147 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001148 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001149 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001150 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001151 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001152
1153 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001154 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001155 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 Tsai378c1322019-04-25 23:48:08 -07001156 getFields{1: VE(0xbeef), 2: VE(1), 3: V(emptyL), 4: V(emptyM), 9: VE(0xbeef), 10: VE(1), 11: V(emptyM2), 12: V(emptyM3)},
Joe Tsai6cf80c42018-12-01 04:57:09 -08001157 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1158 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1159 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001160}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001161
Joe Tsai91e14662018-09-13 13:24:35 -07001162var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001163 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1164 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001165 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001166 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001167 switch v := pv.Interface().(type) {
1168 case pref.Message:
1169 out := make(map[pref.FieldNumber]pref.Value)
1170 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1171 out[fd.Number()] = v
1172 return true
1173 })
1174 return out
1175 case pref.List:
1176 var out []pref.Value
1177 for i := 0; i < v.Len(); i++ {
1178 out = append(out, v.Get(i))
1179 }
1180 return out
1181 case pref.Map:
1182 out := make(map[interface{}]pref.Value)
1183 v.Range(func(k pref.MapKey, v pref.Value) bool {
1184 out[k.Interface()] = v
1185 return true
1186 })
1187 return out
1188 default:
1189 return v
1190 }
Joe Tsai91e14662018-09-13 13:24:35 -07001191 }),
1192 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001193 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001194}
1195
1196func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001197 fieldDescs := m.Descriptor().Fields()
1198 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001199 for i, op := range tt {
1200 p.Push(i)
1201 switch op := op.(type) {
1202 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001203 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001204 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1205 }
1206 case hasFields:
1207 got := map[pref.FieldNumber]bool{}
1208 want := map[pref.FieldNumber]bool(op)
1209 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001210 fd := fieldDescs.ByNumber(n)
1211 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001212 }
1213 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001214 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001215 }
1216 case getFields:
1217 got := map[pref.FieldNumber]pref.Value{}
1218 want := map[pref.FieldNumber]pref.Value(op)
1219 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001220 fd := fieldDescs.ByNumber(n)
1221 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001222 }
1223 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001224 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001225 }
1226 case setFields:
1227 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001228 fd := fieldDescs.ByNumber(n)
1229 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001230 }
1231 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001232 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001233 fd := fieldDescs.ByNumber(n)
1234 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001235 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001236 case whichOneofs:
1237 got := map[pref.Name]pref.FieldNumber{}
1238 want := map[pref.Name]pref.FieldNumber(op)
1239 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001240 od := oneofDescs.ByName(s)
1241 fd := m.WhichOneof(od)
1242 if fd == nil {
1243 got[s] = 0
1244 } else {
1245 got[s] = fd.Number()
1246 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001247 }
1248 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001249 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001250 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001251 case messageFields:
1252 for n, tt := range op {
1253 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001254 fd := fieldDescs.ByNumber(n)
1255 testMessage(t, p, m.Get(fd).Message(), tt)
1256 p.Pop()
1257 }
1258 case messageFieldsMutable:
1259 for n, tt := range op {
1260 p.Push(int(n))
1261 fd := fieldDescs.ByNumber(n)
1262 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001263 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001264 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001265 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001266 for n, tt := range op {
1267 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001268 fd := fieldDescs.ByNumber(n)
1269 testLists(t, p, m.Get(fd).List(), tt)
1270 p.Pop()
1271 }
1272 case listFieldsMutable:
1273 for n, tt := range op {
1274 p.Push(int(n))
1275 fd := fieldDescs.ByNumber(n)
1276 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001277 p.Pop()
1278 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001279 case mapFields:
1280 for n, tt := range op {
1281 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001282 fd := fieldDescs.ByNumber(n)
1283 testMaps(t, p, m.Get(fd).Map(), tt)
1284 p.Pop()
1285 }
1286 case mapFieldsMutable:
1287 for n, tt := range op {
1288 p.Push(int(n))
1289 fd := fieldDescs.ByNumber(n)
1290 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001291 p.Pop()
1292 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001293 case rangeFields:
1294 got := map[pref.FieldNumber]pref.Value{}
1295 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001296 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1297 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001298 return true
1299 })
1300 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001301 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001302 }
Joe Tsai91e14662018-09-13 13:24:35 -07001303 default:
1304 t.Fatalf("operation %v, invalid operation: %T", p, op)
1305 }
1306 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001307 }
1308}
Joe Tsai91e14662018-09-13 13:24:35 -07001309
Joe Tsai4b7aff62018-11-14 14:05:19 -08001310func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001311 for i, op := range tt {
1312 p.Push(i)
1313 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001314 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001315 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001316 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001317 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001318 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001319 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001320 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001321 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001322 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001323 got := map[int]pref.Value{}
1324 want := map[int]pref.Value(op)
1325 for n := range want {
1326 got[n] = v.Get(n)
1327 }
1328 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001329 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001330 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001331 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001332 for n, e := range op {
1333 v.Set(n, e)
1334 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001335 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001336 for _, e := range op {
1337 v.Append(e)
1338 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001339 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001340 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001341 v.Append(V(m))
1342 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001343 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001344 v.Truncate(int(op))
1345 default:
1346 t.Fatalf("operation %v, invalid operation: %T", p, op)
1347 }
1348 p.Pop()
1349 }
1350}
1351
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001352func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1353 for i, op := range tt {
1354 p.Push(i)
1355 switch op := op.(type) {
1356 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001357 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001358 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1359 }
1360 case lenMap:
1361 if got, want := m.Len(), int(op); got != want {
1362 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1363 }
1364 case hasMap:
1365 got := map[interface{}]bool{}
1366 want := map[interface{}]bool(op)
1367 for k := range want {
1368 got[k] = m.Has(V(k).MapKey())
1369 }
1370 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1371 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1372 }
1373 case getMap:
1374 got := map[interface{}]pref.Value{}
1375 want := map[interface{}]pref.Value(op)
1376 for k := range want {
1377 got[k] = m.Get(V(k).MapKey())
1378 }
1379 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1380 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1381 }
1382 case setMap:
1383 for k, v := range op {
1384 m.Set(V(k).MapKey(), v)
1385 }
1386 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001387 for _, k := range op {
1388 m.Clear(V(k).MapKey())
1389 }
1390 case messageMap:
1391 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001392 mk := V(k).MapKey()
1393 if !m.Has(mk) {
1394 m.Set(mk, V(m.NewMessage()))
1395 }
1396 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001397 }
1398 case rangeMap:
1399 got := map[interface{}]pref.Value{}
1400 want := map[interface{}]pref.Value(op)
1401 m.Range(func(k pref.MapKey, v pref.Value) bool {
1402 got[k.Interface()] = v
1403 return true
1404 })
1405 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1406 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1407 }
1408 default:
1409 t.Fatalf("operation %v, invalid operation: %T", p, op)
1410 }
1411 p.Pop()
1412 }
1413}
1414
Joe Tsai378c1322019-04-25 23:48:08 -07001415func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1416 fd := m.Descriptor().Fields().ByNumber(n)
1417 return m.Get(fd)
1418}
1419
Joe Tsai91e14662018-09-13 13:24:35 -07001420type path []int
1421
1422func (p *path) Push(i int) { *p = append(*p, i) }
1423func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1424func (p path) String() string {
1425 var ss []string
1426 for _, i := range p {
1427 ss = append(ss, fmt.Sprint(i))
1428 }
1429 return strings.Join(ss, ".")
1430}