blob: bb10ead5460c69f8b928393509aad0e3b4b62ae8 [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 -0700936// TODO: Remove this.
937func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
938func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
939func (e EnumProto2) Enum() *EnumProto2 { return &e }
940func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800941
942type EnumProto3 int32
943
Joe Tsaib2f66be2019-05-22 00:42:45 -0400944var enumProto3Type = &prototype.Enum{
945 EnumDescriptor: mustMakeEnumDesc(ptype.StandaloneEnum{
Joe Tsai87b955b2018-11-14 21:59:49 -0800946 Syntax: pref.Proto3,
947 FullName: "EnumProto3",
948 Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
949 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400950 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800951 return EnumProto3(n)
952 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400953}
Joe Tsai87b955b2018-11-14 21:59:49 -0800954
Joe Tsai0fc49f82019-05-01 12:29:25 -0700955// TODO: Remove this.
956func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
957func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
958func (e EnumProto3) Enum() *EnumProto3 { return &e }
959func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800960
961type EnumMessages struct {
962 EnumP2 *EnumProto2 `protobuf:"1"`
963 EnumP3 *EnumProto3 `protobuf:"2"`
964 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
965 MessageCycle *EnumMessages `protobuf:"4"`
966 EnumList []EnumProto2 `protobuf:"5"`
967 MessageList []*ScalarProto2 `protobuf:"6"`
968 EnumMap map[string]EnumProto3 `protobuf:"7"`
969 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
970 Union isEnumMessages_Union `protobuf_oneof:"union"`
971}
972
Joe Tsaib2f66be2019-05-22 00:42:45 -0400973var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
974 MessageDescriptor: mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai87b955b2018-11-14 21:59:49 -0800975 Syntax: pref.Proto2,
976 FullName: "EnumMessages",
977 Fields: []ptype.Field{
Joe Tsai0fc49f82019-05-01 12:29:25 -0700978 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type.Descriptor()},
979 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type.Descriptor()},
980 {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 -0800981 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
Joe Tsai0fc49f82019-05-01 12:29:25 -0700982 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type.Descriptor()},
983 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800984 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
985 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
Joe Tsai0fc49f82019-05-01 12:29:25 -0700986 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type.Descriptor()},
987 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type.Descriptor()},
988 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.PBType.Descriptor()},
989 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800990 },
991 Oneofs: []ptype.Oneof{{Name: "union"}},
992 }),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400993 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700994 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -0800995 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400996}}
Joe Tsai87b955b2018-11-14 21:59:49 -0800997
998var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
999 Syntax: pref.Proto2,
1000 FullName: "EnumMessages.F7Entry",
1001 Fields: []ptype.Field{
1002 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001003 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001004 },
Damien Neil232ea152018-12-10 15:14:36 -08001005 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1006 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001007})
1008
1009var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
1010 Syntax: pref.Proto2,
1011 FullName: "EnumMessages.F8Entry",
1012 Fields: []ptype.Field{
1013 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
Joe Tsai0fc49f82019-05-01 12:29:25 -07001014 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.PBType.Descriptor()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001015 },
Damien Neil232ea152018-12-10 15:14:36 -08001016 Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
1017 IsMapEntry: true,
Joe Tsai87b955b2018-11-14 21:59:49 -08001018})
1019
Joe Tsai378c1322019-04-25 23:48:08 -07001020func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001021
Joe Tsaif18ab532018-11-27 17:25:04 -08001022func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1023 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001024 (*EnumMessages_OneofE2)(nil),
1025 (*EnumMessages_OneofE3)(nil),
1026 (*EnumMessages_OneofM2)(nil),
1027 (*EnumMessages_OneofM3)(nil),
1028 }
1029}
1030
1031type (
1032 isEnumMessages_Union interface {
1033 isEnumMessages_Union()
1034 }
1035 EnumMessages_OneofE2 struct {
1036 OneofE2 EnumProto2 `protobuf:"9"`
1037 }
1038 EnumMessages_OneofE3 struct {
1039 OneofE3 EnumProto3 `protobuf:"10"`
1040 }
1041 EnumMessages_OneofM2 struct {
1042 OneofM2 *ScalarProto2 `protobuf:"11"`
1043 }
1044 EnumMessages_OneofM3 struct {
1045 OneofM3 *ScalarProto3 `protobuf:"12"`
1046 }
1047)
1048
1049func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1050func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1051func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1052func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1053
1054func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001055 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1056 emptyM := new(EnumMessages).ProtoReflect()
1057 emptyM2 := new(ScalarProto2).ProtoReflect()
1058 emptyM3 := new(ScalarProto3).ProtoReflect()
1059
Joe Tsai08e00302018-11-26 22:32:06 -08001060 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001061 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Joe Tsai009e0672018-11-27 18:45:07 -08001062 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1063 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001064 wantM3a := &ScalarProto3{Float32: math.Pi}
1065 wantM3b := &ScalarProto3{Float32: math.Ln2}
1066
Joe Tsai378c1322019-04-25 23:48:08 -07001067 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1068 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001069
Joe Tsai378c1322019-04-25 23:48:08 -07001070 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1071 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001072
Joe Tsai378c1322019-04-25 23:48:08 -07001073 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001074 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 -07001075 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 -08001076
1077 // Test singular enums.
1078 setFields{1: VE(0xdead), 2: VE(0)},
1079 getFields{1: VE(0xdead), 2: VE(0)},
1080 hasFields{1: true, 2: true},
1081
1082 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001083 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1084 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001085 getFields{3: V(wantL), 4: V(wantM)},
1086 clearFields{3, 4},
1087 hasFields{3: false, 4: false},
1088 setFields{3: V(wantL), 4: V(wantM)},
1089 hasFields{3: true, 4: true},
1090
1091 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001092 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001093 5: listOps{
1094 appendList{VE(111), VE(222)},
1095 setList{0: VE(333)},
1096 getList{0: VE(333), 1: VE(222)},
1097 lenList(2),
1098 },
1099 6: listOps{
1100 appendMessageList{setFields{4: V(uint32(1e6))}},
1101 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001102 setList{0: V(wantM2a.ProtoReflect())},
1103 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001104 },
1105 },
1106 getFields{5: wantList5, 6: wantList6},
1107 hasFields{5: true, 6: true},
1108 listFields{5: listOps{truncList(0)}},
1109 hasFields{5: false, 6: true},
1110
1111 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001112 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001113 7: mapOps{
1114 setMap{"one": VE(1), "two": VE(2)},
1115 hasMap{"one": true, "two": true, "three": false},
1116 lenMap(2),
1117 },
1118 8: mapOps{
1119 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001120 setMap{"ln2": V(wantM3b.ProtoReflect())},
1121 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001122 lenMap(2),
1123 },
1124 },
1125 getFields{7: wantMap7, 8: wantMap8},
1126 hasFields{7: true, 8: true},
1127 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1128 hasFields{7: true, 8: false},
1129
1130 // Test oneofs of enums and messages.
1131 setFields{9: VE(0xdead)},
1132 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1133 setFields{10: VE(0)},
1134 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001135 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1136 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001137 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001138 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1139 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001140 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1141
1142 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001143 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1144 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001145 EnumP2: EnumProto2(0xdead).Enum(),
1146 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001147 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001148 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001149 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1150 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1151 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001152 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001153 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001154 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001155 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001156
1157 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001158 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001159 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 -07001160 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 -08001161 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1162 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1163 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001164}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001165
Joe Tsai91e14662018-09-13 13:24:35 -07001166var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001167 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
1168 return protoV1.Equal(x, y)
Joe Tsai91e14662018-09-13 13:24:35 -07001169 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001170 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001171 switch v := pv.Interface().(type) {
1172 case pref.Message:
1173 out := make(map[pref.FieldNumber]pref.Value)
1174 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1175 out[fd.Number()] = v
1176 return true
1177 })
1178 return out
1179 case pref.List:
1180 var out []pref.Value
1181 for i := 0; i < v.Len(); i++ {
1182 out = append(out, v.Get(i))
1183 }
1184 return out
1185 case pref.Map:
1186 out := make(map[interface{}]pref.Value)
1187 v.Range(func(k pref.MapKey, v pref.Value) bool {
1188 out[k.Interface()] = v
1189 return true
1190 })
1191 return out
1192 default:
1193 return v
1194 }
Joe Tsai91e14662018-09-13 13:24:35 -07001195 }),
1196 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001197 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001198}
1199
1200func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001201 fieldDescs := m.Descriptor().Fields()
1202 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001203 for i, op := range tt {
1204 p.Push(i)
1205 switch op := op.(type) {
1206 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001207 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001208 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1209 }
1210 case hasFields:
1211 got := map[pref.FieldNumber]bool{}
1212 want := map[pref.FieldNumber]bool(op)
1213 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001214 fd := fieldDescs.ByNumber(n)
1215 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001216 }
1217 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001218 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001219 }
1220 case getFields:
1221 got := map[pref.FieldNumber]pref.Value{}
1222 want := map[pref.FieldNumber]pref.Value(op)
1223 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001224 fd := fieldDescs.ByNumber(n)
1225 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001226 }
1227 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001228 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001229 }
1230 case setFields:
1231 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001232 fd := fieldDescs.ByNumber(n)
1233 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001234 }
1235 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001236 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001237 fd := fieldDescs.ByNumber(n)
1238 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001239 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001240 case whichOneofs:
1241 got := map[pref.Name]pref.FieldNumber{}
1242 want := map[pref.Name]pref.FieldNumber(op)
1243 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001244 od := oneofDescs.ByName(s)
1245 fd := m.WhichOneof(od)
1246 if fd == nil {
1247 got[s] = 0
1248 } else {
1249 got[s] = fd.Number()
1250 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001251 }
1252 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001253 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001254 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001255 case messageFields:
1256 for n, tt := range op {
1257 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001258 fd := fieldDescs.ByNumber(n)
1259 testMessage(t, p, m.Get(fd).Message(), tt)
1260 p.Pop()
1261 }
1262 case messageFieldsMutable:
1263 for n, tt := range op {
1264 p.Push(int(n))
1265 fd := fieldDescs.ByNumber(n)
1266 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001267 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001268 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001269 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001270 for n, tt := range op {
1271 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001272 fd := fieldDescs.ByNumber(n)
1273 testLists(t, p, m.Get(fd).List(), tt)
1274 p.Pop()
1275 }
1276 case listFieldsMutable:
1277 for n, tt := range op {
1278 p.Push(int(n))
1279 fd := fieldDescs.ByNumber(n)
1280 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001281 p.Pop()
1282 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001283 case mapFields:
1284 for n, tt := range op {
1285 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001286 fd := fieldDescs.ByNumber(n)
1287 testMaps(t, p, m.Get(fd).Map(), tt)
1288 p.Pop()
1289 }
1290 case mapFieldsMutable:
1291 for n, tt := range op {
1292 p.Push(int(n))
1293 fd := fieldDescs.ByNumber(n)
1294 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001295 p.Pop()
1296 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001297 case rangeFields:
1298 got := map[pref.FieldNumber]pref.Value{}
1299 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001300 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1301 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001302 return true
1303 })
1304 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001305 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001306 }
Joe Tsai91e14662018-09-13 13:24:35 -07001307 default:
1308 t.Fatalf("operation %v, invalid operation: %T", p, op)
1309 }
1310 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001311 }
1312}
Joe Tsai91e14662018-09-13 13:24:35 -07001313
Joe Tsai4b7aff62018-11-14 14:05:19 -08001314func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001315 for i, op := range tt {
1316 p.Push(i)
1317 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001318 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001319 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001320 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001321 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001322 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001323 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001324 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001325 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001326 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001327 got := map[int]pref.Value{}
1328 want := map[int]pref.Value(op)
1329 for n := range want {
1330 got[n] = v.Get(n)
1331 }
1332 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001333 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001334 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001335 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001336 for n, e := range op {
1337 v.Set(n, e)
1338 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001339 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001340 for _, e := range op {
1341 v.Append(e)
1342 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001343 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001344 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001345 v.Append(V(m))
1346 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001347 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001348 v.Truncate(int(op))
1349 default:
1350 t.Fatalf("operation %v, invalid operation: %T", p, op)
1351 }
1352 p.Pop()
1353 }
1354}
1355
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001356func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1357 for i, op := range tt {
1358 p.Push(i)
1359 switch op := op.(type) {
1360 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001361 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001362 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1363 }
1364 case lenMap:
1365 if got, want := m.Len(), int(op); got != want {
1366 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1367 }
1368 case hasMap:
1369 got := map[interface{}]bool{}
1370 want := map[interface{}]bool(op)
1371 for k := range want {
1372 got[k] = m.Has(V(k).MapKey())
1373 }
1374 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1375 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1376 }
1377 case getMap:
1378 got := map[interface{}]pref.Value{}
1379 want := map[interface{}]pref.Value(op)
1380 for k := range want {
1381 got[k] = m.Get(V(k).MapKey())
1382 }
1383 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1384 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1385 }
1386 case setMap:
1387 for k, v := range op {
1388 m.Set(V(k).MapKey(), v)
1389 }
1390 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001391 for _, k := range op {
1392 m.Clear(V(k).MapKey())
1393 }
1394 case messageMap:
1395 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001396 mk := V(k).MapKey()
1397 if !m.Has(mk) {
1398 m.Set(mk, V(m.NewMessage()))
1399 }
1400 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001401 }
1402 case rangeMap:
1403 got := map[interface{}]pref.Value{}
1404 want := map[interface{}]pref.Value(op)
1405 m.Range(func(k pref.MapKey, v pref.Value) bool {
1406 got[k.Interface()] = v
1407 return true
1408 })
1409 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1410 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1411 }
1412 default:
1413 t.Fatalf("operation %v, invalid operation: %T", p, op)
1414 }
1415 p.Pop()
1416 }
1417}
1418
Joe Tsai378c1322019-04-25 23:48:08 -07001419func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1420 fd := m.Descriptor().Fields().ByNumber(n)
1421 return m.Get(fd)
1422}
1423
Joe Tsai91e14662018-09-13 13:24:35 -07001424type path []int
1425
1426func (p *path) Push(i int) { *p = append(*p, i) }
1427func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1428func (p path) String() string {
1429 var ss []string
1430 for _, i := range p {
1431 ss = append(ss, fmt.Sprint(i))
1432 }
1433 return strings.Join(ss, ".")
1434}