blob: 69b5764956c8677da32d43925a5084e0e4c3bfb0 [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package impl
6
7import (
Joe Tsai91e14662018-09-13 13:24:35 -07008 "fmt"
9 "math"
10 "strings"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070011 "testing"
12
13 "github.com/google/go-cmp/cmp"
Joe Tsai91e14662018-09-13 13:24:35 -070014 "github.com/google/go-cmp/cmp/cmpopts"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070015
Damien Neil204f1c02018-10-23 15:03:38 -070016 protoV1 "github.com/golang/protobuf/proto"
17 descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
Joe Tsai01ab2962018-09-21 17:44:00 -070018 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
19 ptype "github.com/golang/protobuf/v2/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070020)
21
Joe Tsaic6b75612018-09-13 14:24:37 -070022func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
23 md, err := ptype.NewMessage(&t)
24 if err != nil {
25 panic(err)
26 }
27 return md
28}
29
Joe Tsai91e14662018-09-13 13:24:35 -070030var V = pref.ValueOf
31
Joe Tsaifa02f4e2018-09-12 16:20:37 -070032type (
33 MyBool bool
34 MyInt32 int32
35 MyInt64 int64
36 MyUint32 uint32
37 MyUint64 uint64
38 MyFloat32 float32
39 MyFloat64 float64
40 MyString string
41 MyBytes []byte
Joe Tsai91e14662018-09-13 13:24:35 -070042
Joe Tsai4b7aff62018-11-14 14:05:19 -080043 ListStrings []MyString
44 ListBytes []MyBytes
Joe Tsaibbfaeb72018-10-17 00:27:21 +000045
46 MapStrings map[MyString]MyString
47 MapBytes map[MyString]MyBytes
Joe Tsaifa02f4e2018-09-12 16:20:37 -070048)
49
Joe Tsai4b7aff62018-11-14 14:05:19 -080050// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070051type (
Joe Tsai4b7aff62018-11-14 14:05:19 -080052 messageOp interface{} // equalMessage | hasFields | getFields | setFields | clearFields | listFields | mapFields
Joe Tsai91e14662018-09-13 13:24:35 -070053 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070054
Joe Tsai4b7aff62018-11-14 14:05:19 -080055 listOp interface{} // equalList | lenList | getList | setList | appendList | truncList
56 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070057
Joe Tsaibbfaeb72018-10-17 00:27:21 +000058 mapOp interface{} // equalMap | lenMap | hasMap | getMap | setMap | clearMap | rangeMap
59 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070060)
61
62// Test operations performed on a message.
63type (
64 equalMessage pref.Message
65 hasFields map[pref.FieldNumber]bool
66 getFields map[pref.FieldNumber]pref.Value
67 setFields map[pref.FieldNumber]pref.Value
68 clearFields map[pref.FieldNumber]bool
Joe Tsai4b7aff62018-11-14 14:05:19 -080069 listFields map[pref.FieldNumber]listOps
Joe Tsai91e14662018-09-13 13:24:35 -070070 mapFields map[pref.FieldNumber]mapOps
71 messageFields map[pref.FieldNumber]messageOps
72 // TODO: Mutable, Range, ExtensionTypes
73)
74
Joe Tsai4b7aff62018-11-14 14:05:19 -080075// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070076type (
Joe Tsai4b7aff62018-11-14 14:05:19 -080077 equalList pref.List
78 lenList int
79 getList map[int]pref.Value
80 setList map[int]pref.Value
81 appendList []pref.Value
82 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070083 // TODO: Mutable, MutableAppend
84)
85
Joe Tsaibbfaeb72018-10-17 00:27:21 +000086// Test operations performed on a map.
87type (
88 equalMap pref.Map
89 lenMap int
90 hasMap map[interface{}]bool
91 getMap map[interface{}]pref.Value
92 setMap map[interface{}]pref.Value
93 clearMap map[interface{}]bool
94 rangeMap map[interface{}]pref.Value
Joe Tsai3903b212018-10-17 11:54:32 -070095 // TODO: Mutable
Joe Tsaibbfaeb72018-10-17 00:27:21 +000096)
97
Joe Tsaice6edd32018-10-19 16:27:46 -070098type ScalarProto2 struct {
99 Bool *bool `protobuf:"1"`
100 Int32 *int32 `protobuf:"2"`
101 Int64 *int64 `protobuf:"3"`
102 Uint32 *uint32 `protobuf:"4"`
103 Uint64 *uint64 `protobuf:"5"`
104 Float32 *float32 `protobuf:"6"`
105 Float64 *float64 `protobuf:"7"`
106 String *string `protobuf:"8"`
107 StringA []byte `protobuf:"9"`
108 Bytes []byte `protobuf:"10"`
109 BytesA *string `protobuf:"11"`
110
111 MyBool *MyBool `protobuf:"12"`
112 MyInt32 *MyInt32 `protobuf:"13"`
113 MyInt64 *MyInt64 `protobuf:"14"`
114 MyUint32 *MyUint32 `protobuf:"15"`
115 MyUint64 *MyUint64 `protobuf:"16"`
116 MyFloat32 *MyFloat32 `protobuf:"17"`
117 MyFloat64 *MyFloat64 `protobuf:"18"`
118 MyString *MyString `protobuf:"19"`
119 MyStringA MyBytes `protobuf:"20"`
120 MyBytes MyBytes `protobuf:"21"`
121 MyBytesA *MyString `protobuf:"22"`
122}
123
Joe Tsaif0c01e42018-11-06 13:05:20 -0800124var scalarProto2Type = MessageType{Type: ptype.GoMessage(
125 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700126 Syntax: pref.Proto2,
127 FullName: "ScalarProto2",
128 Fields: []ptype.Field{
129 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
130 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
131 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
132 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
133 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
134 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
135 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
136 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
137 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
138 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
139 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
140
141 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
142 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
143 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
144 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
145 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
146 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
147 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
148 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
149 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
150 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
151 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
152 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800153 }),
154 func(pref.MessageType) pref.ProtoMessage {
155 return new(ScalarProto2)
156 },
157)}
Joe Tsai91e14662018-09-13 13:24:35 -0700158
Joe Tsaif0c01e42018-11-06 13:05:20 -0800159func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.Type }
160func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) }
161func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) }
162func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
163func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
164func (m *ScalarProto2) ProtoMutable() {}
165
166func TestScalarProto2(t *testing.T) {
167 testMessage(t, nil, &ScalarProto2{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700168 hasFields{
169 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
170 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
171 },
172 getFields{
173 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")),
174 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")),
175 },
176 setFields{
177 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)),
178 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)),
179 },
180 hasFields{
181 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
182 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
183 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800184 equalMessage(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700185 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
186 new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
Joe Tsaif0c01e42018-11-06 13:05:20 -0800187 }),
Joe Tsai91e14662018-09-13 13:24:35 -0700188 clearFields{
189 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
190 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
191 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800192 equalMessage(&ScalarProto2{}),
Joe Tsai91e14662018-09-13 13:24:35 -0700193 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700194}
195
Joe Tsaice6edd32018-10-19 16:27:46 -0700196type ScalarProto3 struct {
197 Bool bool `protobuf:"1"`
198 Int32 int32 `protobuf:"2"`
199 Int64 int64 `protobuf:"3"`
200 Uint32 uint32 `protobuf:"4"`
201 Uint64 uint64 `protobuf:"5"`
202 Float32 float32 `protobuf:"6"`
203 Float64 float64 `protobuf:"7"`
204 String string `protobuf:"8"`
205 StringA []byte `protobuf:"9"`
206 Bytes []byte `protobuf:"10"`
207 BytesA string `protobuf:"11"`
208
209 MyBool MyBool `protobuf:"12"`
210 MyInt32 MyInt32 `protobuf:"13"`
211 MyInt64 MyInt64 `protobuf:"14"`
212 MyUint32 MyUint32 `protobuf:"15"`
213 MyUint64 MyUint64 `protobuf:"16"`
214 MyFloat32 MyFloat32 `protobuf:"17"`
215 MyFloat64 MyFloat64 `protobuf:"18"`
216 MyString MyString `protobuf:"19"`
217 MyStringA MyBytes `protobuf:"20"`
218 MyBytes MyBytes `protobuf:"21"`
219 MyBytesA MyString `protobuf:"22"`
220}
221
Joe Tsaif0c01e42018-11-06 13:05:20 -0800222var scalarProto3Type = MessageType{Type: ptype.GoMessage(
223 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700224 Syntax: pref.Proto3,
225 FullName: "ScalarProto3",
226 Fields: []ptype.Field{
227 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
228 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
229 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
230 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
231 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
232 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
233 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
234 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
235 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
236 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
237 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700238
Joe Tsai91e14662018-09-13 13:24:35 -0700239 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
240 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
241 {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
242 {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
243 {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
244 {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
245 {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
246 {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
247 {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
248 {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
249 {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
250 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800251 }),
252 func(pref.MessageType) pref.ProtoMessage {
253 return new(ScalarProto3)
254 },
255)}
Joe Tsai91e14662018-09-13 13:24:35 -0700256
Joe Tsaif0c01e42018-11-06 13:05:20 -0800257func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.Type }
258func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) }
259func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) }
260func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
261func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
262func (m *ScalarProto3) ProtoMutable() {}
263
264func TestScalarProto3(t *testing.T) {
265 testMessage(t, nil, &ScalarProto3{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700266 hasFields{
267 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
268 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
269 },
270 getFields{
271 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)),
272 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)),
273 },
274 setFields{
275 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)),
276 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)),
277 },
278 hasFields{
279 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
280 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
281 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800282 equalMessage(&ScalarProto3{}),
Joe Tsai91e14662018-09-13 13:24:35 -0700283 setFields{
284 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")),
285 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")),
286 },
287 hasFields{
288 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
289 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
290 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800291 equalMessage(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700292 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
293 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsaif0c01e42018-11-06 13:05:20 -0800294 }),
Joe Tsai91e14662018-09-13 13:24:35 -0700295 clearFields{
296 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
297 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
298 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800299 equalMessage(&ScalarProto3{}),
Joe Tsai91e14662018-09-13 13:24:35 -0700300 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700301}
302
Joe Tsaif0c01e42018-11-06 13:05:20 -0800303type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700304 Bools []bool `protobuf:"1"`
305 Int32s []int32 `protobuf:"2"`
306 Int64s []int64 `protobuf:"3"`
307 Uint32s []uint32 `protobuf:"4"`
308 Uint64s []uint64 `protobuf:"5"`
309 Float32s []float32 `protobuf:"6"`
310 Float64s []float64 `protobuf:"7"`
311 Strings []string `protobuf:"8"`
312 StringsA [][]byte `protobuf:"9"`
313 Bytes [][]byte `protobuf:"10"`
314 BytesA []string `protobuf:"11"`
315
316 MyStrings1 []MyString `protobuf:"12"`
317 MyStrings2 []MyBytes `protobuf:"13"`
318 MyBytes1 []MyBytes `protobuf:"14"`
319 MyBytes2 []MyString `protobuf:"15"`
320
Joe Tsai4b7aff62018-11-14 14:05:19 -0800321 MyStrings3 ListStrings `protobuf:"16"`
322 MyStrings4 ListBytes `protobuf:"17"`
323 MyBytes3 ListBytes `protobuf:"18"`
324 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700325}
326
Joe Tsaif0c01e42018-11-06 13:05:20 -0800327var listScalarsType = MessageType{Type: ptype.GoMessage(
328 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsai91e14662018-09-13 13:24:35 -0700329 Syntax: pref.Proto2,
Joe Tsaif0c01e42018-11-06 13:05:20 -0800330 FullName: "ListScalars",
Joe Tsai91e14662018-09-13 13:24:35 -0700331 Fields: []ptype.Field{
332 {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
333 {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
334 {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
335 {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
336 {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
337 {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
338 {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
339 {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
340 {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
341 {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
342 {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700343
Joe Tsai91e14662018-09-13 13:24:35 -0700344 {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
345 {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
346 {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
347 {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
348
349 {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
350 {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
351 {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
352 {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700353 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800354 }),
355 func(pref.MessageType) pref.ProtoMessage {
356 return new(ListScalars)
357 },
358)}
Joe Tsai91e14662018-09-13 13:24:35 -0700359
Joe Tsaif0c01e42018-11-06 13:05:20 -0800360func (m *ListScalars) Type() pref.MessageType { return listScalarsType.Type }
361func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) }
362func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) }
363func (m *ListScalars) Interface() pref.ProtoMessage { return m }
364func (m *ListScalars) ProtoReflect() pref.Message { return m }
365func (m *ListScalars) ProtoMutable() {}
366
367func TestListScalars(t *testing.T) {
368 empty := &ListScalars{}
Joe Tsai91e14662018-09-13 13:24:35 -0700369 emptyFS := empty.KnownFields()
370
Joe Tsaif0c01e42018-11-06 13:05:20 -0800371 want := &ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700372 Bools: []bool{true, false, true},
373 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
374 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
375 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
376 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
377 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
378 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
379 Strings: []string{"8", "", "eight"},
380 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
381 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
382 BytesA: []string{"11", "", "eleven"},
383
384 MyStrings1: []MyString{"12", "", "twelve"},
385 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
386 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
387 MyBytes2: []MyString{"15", "", "fifteen"},
388
Joe Tsai4b7aff62018-11-14 14:05:19 -0800389 MyStrings3: ListStrings{"16", "", "sixteen"},
390 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
391 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
392 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800393 }
Joe Tsai91e14662018-09-13 13:24:35 -0700394 wantFS := want.KnownFields()
395
Joe Tsaif0c01e42018-11-06 13:05:20 -0800396 testMessage(t, nil, &ListScalars{}, messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700397 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},
398 getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19)},
399 setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19)},
Joe Tsai4b7aff62018-11-14 14:05:19 -0800400 listFields{
Joe Tsai91e14662018-09-13 13:24:35 -0700401 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800402 lenList(0),
403 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
404 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
405 equalList(wantFS.Get(2).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700406 },
407 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800408 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
409 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
410 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700411 },
412 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800413 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
414 equalList(wantFS.Get(6).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700415 },
416 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800417 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
418 lenList(6),
419 setList{0: V("8"), 2: V("eight")},
420 truncList(3),
421 equalList(wantFS.Get(8).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700422 },
423 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800424 appendList{V([]byte(nil)), V([]byte(nil))},
425 setList{0: V([]byte("10"))},
426 appendList{V([]byte("wrong"))},
427 setList{2: V([]byte("ten"))},
428 equalList(wantFS.Get(10).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700429 },
430 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800431 appendList{V("12"), V("wrong"), V("twelve")},
432 setList{1: V("")},
433 equalList(wantFS.Get(12).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700434 },
435 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800436 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
437 equalList(wantFS.Get(14).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700438 },
439 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800440 appendList{V("16"), V(""), V("sixteen"), V("extra")},
441 truncList(3),
442 equalList(wantFS.Get(16).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700443 },
444 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800445 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
446 equalList(wantFS.Get(18).List()),
Joe Tsai91e14662018-09-13 13:24:35 -0700447 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700448 },
Joe Tsai91e14662018-09-13 13:24:35 -0700449 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},
450 equalMessage(want),
451 clearFields{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 Tsaibbfaeb72018-10-17 00:27:21 +0000452 equalMessage(empty),
453 })
454}
455
Joe Tsaice6edd32018-10-19 16:27:46 -0700456type MapScalars struct {
457 KeyBools map[bool]string `protobuf:"1"`
458 KeyInt32s map[int32]string `protobuf:"2"`
459 KeyInt64s map[int64]string `protobuf:"3"`
460 KeyUint32s map[uint32]string `protobuf:"4"`
461 KeyUint64s map[uint64]string `protobuf:"5"`
462 KeyStrings map[string]string `protobuf:"6"`
463
464 ValBools map[string]bool `protobuf:"7"`
465 ValInt32s map[string]int32 `protobuf:"8"`
466 ValInt64s map[string]int64 `protobuf:"9"`
467 ValUint32s map[string]uint32 `protobuf:"10"`
468 ValUint64s map[string]uint64 `protobuf:"11"`
469 ValFloat32s map[string]float32 `protobuf:"12"`
470 ValFloat64s map[string]float64 `protobuf:"13"`
471 ValStrings map[string]string `protobuf:"14"`
472 ValStringsA map[string][]byte `protobuf:"15"`
473 ValBytes map[string][]byte `protobuf:"16"`
474 ValBytesA map[string]string `protobuf:"17"`
475
476 MyStrings1 map[MyString]MyString `protobuf:"18"`
477 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
478 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
479 MyBytes2 map[MyString]MyString `protobuf:"21"`
480
481 MyStrings3 MapStrings `protobuf:"22"`
482 MyStrings4 MapBytes `protobuf:"23"`
483 MyBytes3 MapBytes `protobuf:"24"`
484 MyBytes4 MapStrings `protobuf:"25"`
485}
486
Joe Tsaif0c01e42018-11-06 13:05:20 -0800487func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
488 return ptype.Field{
489 Name: pref.Name(fmt.Sprintf("f%d", n)),
490 Number: n,
491 Cardinality: pref.Repeated,
492 Kind: pref.MessageKind,
493 MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
494 Syntax: pref.Proto2,
495 FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
496 Fields: []ptype.Field{
497 {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
498 {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
499 },
500 Options: &descriptorV1.MessageOptions{MapEntry: protoV1.Bool(true)},
501 }),
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000502 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800503}
504
505var mapScalarsType = MessageType{Type: ptype.GoMessage(
506 mustMakeMessageDesc(ptype.StandaloneMessage{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000507 Syntax: pref.Proto2,
508 FullName: "MapScalars",
509 Fields: []ptype.Field{
510 mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
511 mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
512 mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
513 mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
514 mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
515 mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
516
517 mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
518 mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
519 mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
520 mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
521 mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
522 mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
523 mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
524 mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
525 mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
526 mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
527 mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
528
529 mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
530 mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
531 mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
532 mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
533
534 mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
535 mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
536 mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
537 mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
538 },
Joe Tsaif0c01e42018-11-06 13:05:20 -0800539 }),
540 func(pref.MessageType) pref.ProtoMessage {
541 return new(MapScalars)
542 },
543)}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000544
Joe Tsaif0c01e42018-11-06 13:05:20 -0800545func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.Type }
546func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) }
547func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) }
548func (m *MapScalars) Interface() pref.ProtoMessage { return m }
549func (m *MapScalars) ProtoReflect() pref.Message { return m }
550func (m *MapScalars) ProtoMutable() {}
551
552func TestMapScalars(t *testing.T) {
553 empty := &MapScalars{}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000554 emptyFS := empty.KnownFields()
555
Joe Tsaif0c01e42018-11-06 13:05:20 -0800556 want := &MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000557 KeyBools: map[bool]string{true: "true", false: "false"},
558 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
559 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
560 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
561 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
562 KeyStrings: map[string]string{"": "", "foo": "bar"},
563
564 ValBools: map[string]bool{"true": true, "false": false},
565 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
566 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
567 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
568 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
569 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
570 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
571 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
572 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
573 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
574 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
575
576 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
577 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
578 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
579 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
580
581 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
582 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
583 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
584 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsaif0c01e42018-11-06 13:05:20 -0800585 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000586 wantFS := want.KnownFields()
587
Joe Tsaif0c01e42018-11-06 13:05:20 -0800588 testMessage(t, nil, &MapScalars{}, messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000589 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},
590 getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19), 21: emptyFS.Get(21), 23: emptyFS.Get(23), 25: emptyFS.Get(25)},
591 setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19), 21: wantFS.Get(21), 23: wantFS.Get(23), 25: wantFS.Get(25)},
592 mapFields{
593 2: {
594 lenMap(0),
595 hasMap{int32(0): false, int32(-1): false, int32(2): false},
596 setMap{int32(0): V("zero")},
597 lenMap(1),
598 hasMap{int32(0): true, int32(-1): false, int32(2): false},
599 setMap{int32(-1): V("one")},
600 lenMap(2),
601 hasMap{int32(0): true, int32(-1): true, int32(2): false},
602 setMap{int32(2): V("two")},
603 lenMap(3),
604 hasMap{int32(0): true, int32(-1): true, int32(2): true},
605 },
606 4: {
607 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
608 equalMap(wantFS.Get(4).Map()),
609 },
610 6: {
611 clearMap{"noexist": true},
612 setMap{"foo": V("bar")},
613 setMap{"": V("empty")},
614 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
615 setMap{"": V(""), "extra": V("extra")},
616 clearMap{"extra": true, "noexist": true},
617 },
618 8: {
619 equalMap(emptyFS.Get(8).Map()),
620 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
621 },
622 10: {
623 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
624 lenMap(3),
625 equalMap(wantFS.Get(10).Map()),
626 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
627 },
628 12: {
629 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
630 clearMap{"e": true, "phi": true},
631 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
632 },
633 14: {
634 equalMap(emptyFS.Get(14).Map()),
635 setMap{"s1": V("s1"), "s2": V("s2")},
636 },
637 16: {
638 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
639 equalMap(wantFS.Get(16).Map()),
640 },
641 18: {
642 hasMap{"s1": false, "s2": false, "s3": false},
643 setMap{"s1": V("s1"), "s2": V("s2")},
644 hasMap{"s1": true, "s2": true, "s3": false},
645 },
646 20: {
647 equalMap(emptyFS.Get(20).Map()),
648 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
649 },
650 22: {
651 rangeMap{},
652 setMap{"s1": V("s1"), "s2": V("s2")},
653 rangeMap{"s1": V("s1"), "s2": V("s2")},
654 lenMap(2),
655 },
656 24: {
657 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
658 equalMap(wantFS.Get(24).Map()),
659 },
660 },
661 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},
662 equalMessage(want),
663 clearFields{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},
664 equalMessage(empty),
Joe Tsai91e14662018-09-13 13:24:35 -0700665 })
666}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700667
Joe Tsai2c870bb2018-10-17 11:46:52 -0700668type (
669 OneofScalars struct {
670 Union isOneofScalars_Union `protobuf_oneof:"union"`
671 }
672 isOneofScalars_Union interface {
673 isOneofScalars_Union()
674 }
675
676 OneofScalars_Bool struct {
677 Bool bool `protobuf:"1"`
678 }
679 OneofScalars_Int32 struct {
680 Int32 MyInt32 `protobuf:"2"`
681 }
682 OneofScalars_Int64 struct {
683 Int64 int64 `protobuf:"3"`
684 }
685 OneofScalars_Uint32 struct {
686 Uint32 MyUint32 `protobuf:"4"`
687 }
688 OneofScalars_Uint64 struct {
689 Uint64 uint64 `protobuf:"5"`
690 }
691 OneofScalars_Float32 struct {
692 Float32 MyFloat32 `protobuf:"6"`
693 }
694 OneofScalars_Float64 struct {
695 Float64 float64 `protobuf:"7"`
696 }
697 OneofScalars_String struct {
698 String string `protobuf:"8"`
699 }
700 OneofScalars_StringA struct {
701 StringA []byte `protobuf:"9"`
702 }
703 OneofScalars_StringB struct {
704 StringB MyString `protobuf:"10"`
705 }
706 OneofScalars_Bytes struct {
707 Bytes []byte `protobuf:"11"`
708 }
709 OneofScalars_BytesA struct {
710 BytesA string `protobuf:"12"`
711 }
712 OneofScalars_BytesB struct {
713 BytesB MyBytes `protobuf:"13"`
714 }
715)
716
Joe Tsaif0c01e42018-11-06 13:05:20 -0800717var oneofScalarsType = MessageType{Type: ptype.GoMessage(
718 mustMakeMessageDesc(ptype.StandaloneMessage{
719 Syntax: pref.Proto2,
720 FullName: "ScalarProto2",
721 Fields: []ptype.Field{
722 {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
723 {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
724 {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
725 {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
726 {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
727 {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
728 {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
729 {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
730 {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
731 {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
732 {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
733 {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
734 {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
735 },
736 Oneofs: []ptype.Oneof{{Name: "union"}},
737 }),
738 func(pref.MessageType) pref.ProtoMessage {
739 return new(OneofScalars)
740 },
741)}
742
743func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.Type }
744func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) }
745func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) }
746func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
747func (m *OneofScalars) ProtoReflect() pref.Message { return m }
748func (m *OneofScalars) ProtoMutable() {}
749
750func (*OneofScalars) XXX_OneofFuncs() (func(protoV1.Message, *protoV1.Buffer) error, func(protoV1.Message, int, int, *protoV1.Buffer) (bool, error), func(protoV1.Message) int, []interface{}) {
Joe Tsai2c870bb2018-10-17 11:46:52 -0700751 return nil, nil, nil, []interface{}{
752 (*OneofScalars_Bool)(nil),
753 (*OneofScalars_Int32)(nil),
754 (*OneofScalars_Int64)(nil),
755 (*OneofScalars_Uint32)(nil),
756 (*OneofScalars_Uint64)(nil),
757 (*OneofScalars_Float32)(nil),
758 (*OneofScalars_Float64)(nil),
759 (*OneofScalars_String)(nil),
760 (*OneofScalars_StringA)(nil),
761 (*OneofScalars_StringB)(nil),
762 (*OneofScalars_Bytes)(nil),
763 (*OneofScalars_BytesA)(nil),
764 (*OneofScalars_BytesB)(nil),
765 }
766}
767
768func (*OneofScalars_Bool) isOneofScalars_Union() {}
769func (*OneofScalars_Int32) isOneofScalars_Union() {}
770func (*OneofScalars_Int64) isOneofScalars_Union() {}
771func (*OneofScalars_Uint32) isOneofScalars_Union() {}
772func (*OneofScalars_Uint64) isOneofScalars_Union() {}
773func (*OneofScalars_Float32) isOneofScalars_Union() {}
774func (*OneofScalars_Float64) isOneofScalars_Union() {}
775func (*OneofScalars_String) isOneofScalars_Union() {}
776func (*OneofScalars_StringA) isOneofScalars_Union() {}
777func (*OneofScalars_StringB) isOneofScalars_Union() {}
778func (*OneofScalars_Bytes) isOneofScalars_Union() {}
779func (*OneofScalars_BytesA) isOneofScalars_Union() {}
780func (*OneofScalars_BytesB) isOneofScalars_Union() {}
781
782func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800783 empty := &OneofScalars{}
784 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
785 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
786 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
787 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
788 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
789 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
790 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
791 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
792 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
793 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
794 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
795 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
796 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700797
Joe Tsaif0c01e42018-11-06 13:05:20 -0800798 testMessage(t, nil, &OneofScalars{}, messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700799 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},
800 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"))},
801
802 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage(want1),
803 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage(want2),
804 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage(want3),
805 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage(want4),
806 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage(want5),
807 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage(want6),
808 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage(want7),
809 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage(want8),
810 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage(want9),
811 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage(want10),
812 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage(want11),
813 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage(want12),
814 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage(want13),
815
816 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},
817 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"))},
818 clearFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true},
819 equalMessage(want13),
820 clearFields{13: true},
821 equalMessage(empty),
822 })
823}
824
Joe Tsai91e14662018-09-13 13:24:35 -0700825// TODO: Need to test singular and repeated messages
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700826
Joe Tsai91e14662018-09-13 13:24:35 -0700827var cmpOpts = cmp.Options{
828 cmp.Transformer("UnwrapValue", func(v pref.Value) interface{} {
829 return v.Interface()
830 }),
Joe Tsai4b7aff62018-11-14 14:05:19 -0800831 cmp.Transformer("UnwrapList", func(v pref.List) interface{} {
Joe Tsai91e14662018-09-13 13:24:35 -0700832 return v.(interface{ Unwrap() interface{} }).Unwrap()
833 }),
834 cmp.Transformer("UnwrapMap", func(m pref.Map) interface{} {
835 return m.(interface{ Unwrap() interface{} }).Unwrap()
836 }),
837 cmpopts.EquateNaNs(),
838}
839
840func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
841 fs := m.KnownFields()
842 for i, op := range tt {
843 p.Push(i)
844 switch op := op.(type) {
845 case equalMessage:
846 if diff := cmp.Diff(op, m, cmpOpts); diff != "" {
847 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
848 }
849 case hasFields:
850 got := map[pref.FieldNumber]bool{}
851 want := map[pref.FieldNumber]bool(op)
852 for n := range want {
853 got[n] = fs.Has(n)
854 }
855 if diff := cmp.Diff(want, got); diff != "" {
856 t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
857 }
858 case getFields:
859 got := map[pref.FieldNumber]pref.Value{}
860 want := map[pref.FieldNumber]pref.Value(op)
861 for n := range want {
862 got[n] = fs.Get(n)
863 }
864 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
865 t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
866 }
867 case setFields:
868 for n, v := range op {
869 fs.Set(n, v)
870 }
871 case clearFields:
872 for n, ok := range op {
873 if ok {
874 fs.Clear(n)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700875 }
876 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800877 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -0700878 for n, tt := range op {
879 p.Push(int(n))
Joe Tsai4b7aff62018-11-14 14:05:19 -0800880 testLists(t, p, fs.Mutable(n).(pref.List), tt)
Joe Tsai91e14662018-09-13 13:24:35 -0700881 p.Pop()
882 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000883 case mapFields:
884 for n, tt := range op {
885 p.Push(int(n))
886 testMaps(t, p, fs.Mutable(n).(pref.Map), tt)
887 p.Pop()
888 }
Joe Tsai91e14662018-09-13 13:24:35 -0700889 default:
890 t.Fatalf("operation %v, invalid operation: %T", p, op)
891 }
892 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700893 }
894}
Joe Tsai91e14662018-09-13 13:24:35 -0700895
Joe Tsai4b7aff62018-11-14 14:05:19 -0800896func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -0700897 for i, op := range tt {
898 p.Push(i)
899 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800900 case equalList:
Joe Tsai91e14662018-09-13 13:24:35 -0700901 if diff := cmp.Diff(op, v, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800902 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -0700903 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800904 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -0700905 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800906 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -0700907 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800908 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -0700909 got := map[int]pref.Value{}
910 want := map[int]pref.Value(op)
911 for n := range want {
912 got[n] = v.Get(n)
913 }
914 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800915 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -0700916 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800917 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -0700918 for n, e := range op {
919 v.Set(n, e)
920 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800921 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -0700922 for _, e := range op {
923 v.Append(e)
924 }
Joe Tsai4b7aff62018-11-14 14:05:19 -0800925 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -0700926 v.Truncate(int(op))
927 default:
928 t.Fatalf("operation %v, invalid operation: %T", p, op)
929 }
930 p.Pop()
931 }
932}
933
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000934func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
935 for i, op := range tt {
936 p.Push(i)
937 switch op := op.(type) {
938 case equalMap:
939 if diff := cmp.Diff(op, m, cmpOpts); diff != "" {
940 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
941 }
942 case lenMap:
943 if got, want := m.Len(), int(op); got != want {
944 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
945 }
946 case hasMap:
947 got := map[interface{}]bool{}
948 want := map[interface{}]bool(op)
949 for k := range want {
950 got[k] = m.Has(V(k).MapKey())
951 }
952 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
953 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
954 }
955 case getMap:
956 got := map[interface{}]pref.Value{}
957 want := map[interface{}]pref.Value(op)
958 for k := range want {
959 got[k] = m.Get(V(k).MapKey())
960 }
961 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
962 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
963 }
964 case setMap:
965 for k, v := range op {
966 m.Set(V(k).MapKey(), v)
967 }
968 case clearMap:
969 for v, ok := range op {
970 if ok {
971 m.Clear(V(v).MapKey())
972 }
973 }
974 case rangeMap:
975 got := map[interface{}]pref.Value{}
976 want := map[interface{}]pref.Value(op)
977 m.Range(func(k pref.MapKey, v pref.Value) bool {
978 got[k.Interface()] = v
979 return true
980 })
981 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
982 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
983 }
984 default:
985 t.Fatalf("operation %v, invalid operation: %T", p, op)
986 }
987 p.Pop()
988 }
989}
990
Joe Tsai91e14662018-09-13 13:24:35 -0700991type path []int
992
993func (p *path) Push(i int) { *p = append(*p, i) }
994func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
995func (p path) String() string {
996 var ss []string
997 for _, i := range p {
998 ss = append(ss, fmt.Sprint(i))
999 }
1000 return strings.Join(ss, ".")
1001}