blob: feb4ac647692240e34c8ec84ca25166d69ff97d6 [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// 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
Damien Neil4be2fb42018-12-17 11:16:16 -08005package proto_test
Damien Neilba23aa52018-12-07 14:38:17 -08006
7import (
8 "fmt"
9 "reflect"
10 "testing"
11
12 protoV1 "github.com/golang/protobuf/proto"
Damien Neil61e93c72019-03-27 09:23:20 -070013 "github.com/golang/protobuf/v2/encoding/textpb"
Damien Neilba23aa52018-12-07 14:38:17 -080014 "github.com/golang/protobuf/v2/internal/encoding/pack"
Damien Neilba23aa52018-12-07 14:38:17 -080015 "github.com/golang/protobuf/v2/internal/scalar"
Damien Neil4be2fb42018-12-17 11:16:16 -080016 "github.com/golang/protobuf/v2/proto"
Damien Neilba23aa52018-12-07 14:38:17 -080017 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai19058432019-02-27 21:46:29 -080018
19 testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
Damien Neil3b46ade2019-03-26 13:55:02 -070020 test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
Damien Neilba23aa52018-12-07 14:38:17 -080021)
22
23type testProto struct {
24 desc string
Damien Neil4be2fb42018-12-17 11:16:16 -080025 decodeTo []proto.Message
Damien Neilba23aa52018-12-07 14:38:17 -080026 wire []byte
27}
28
29func TestDecode(t *testing.T) {
30 for _, test := range testProtos {
31 for _, want := range test.decodeTo {
32 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
33 wire := append(([]byte)(nil), test.wire...)
Damien Neil4be2fb42018-12-17 11:16:16 -080034 got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
35 if err := proto.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070036 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080037 return
38 }
39
40 // Aliasing check: Modifying the original wire bytes shouldn't
41 // affect the unmarshaled message.
42 for i := range wire {
43 wire[i] = 0
44 }
45
46 if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
Damien Neil61e93c72019-03-27 09:23:20 -070047 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neilba23aa52018-12-07 14:38:17 -080048 }
49 })
50 }
51 }
52}
53
54var testProtos = []testProto{
55 {
56 desc: "basic scalar types",
Damien Neil4be2fb42018-12-17 11:16:16 -080057 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -080058 OptionalInt32: scalar.Int32(1001),
59 OptionalInt64: scalar.Int64(1002),
60 OptionalUint32: scalar.Uint32(1003),
61 OptionalUint64: scalar.Uint64(1004),
62 OptionalSint32: scalar.Int32(1005),
63 OptionalSint64: scalar.Int64(1006),
64 OptionalFixed32: scalar.Uint32(1007),
65 OptionalFixed64: scalar.Uint64(1008),
66 OptionalSfixed32: scalar.Int32(1009),
67 OptionalSfixed64: scalar.Int64(1010),
68 OptionalFloat: scalar.Float32(1011.5),
69 OptionalDouble: scalar.Float64(1012.5),
70 OptionalBool: scalar.Bool(true),
71 OptionalString: scalar.String("string"),
72 OptionalBytes: []byte("bytes"),
73 OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
Damien Neil3b46ade2019-03-26 13:55:02 -070074 }, &test3pb.TestAllTypes{
75 OptionalInt32: 1001,
76 OptionalInt64: 1002,
77 OptionalUint32: 1003,
78 OptionalUint64: 1004,
79 OptionalSint32: 1005,
80 OptionalSint64: 1006,
81 OptionalFixed32: 1007,
82 OptionalFixed64: 1008,
83 OptionalSfixed32: 1009,
84 OptionalSfixed64: 1010,
85 OptionalFloat: 1011.5,
86 OptionalDouble: 1012.5,
87 OptionalBool: true,
88 OptionalString: "string",
89 OptionalBytes: []byte("bytes"),
90 OptionalNestedEnum: test3pb.TestAllTypes_BAR,
Damien Neilba23aa52018-12-07 14:38:17 -080091 }, build(
92 &testpb.TestAllExtensions{},
93 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
94 extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
95 extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
96 extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
97 extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
98 extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
99 extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
100 extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
101 extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
102 extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
103 extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
104 extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
105 extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
106 extend(testpb.E_OptionalStringExtension, scalar.String("string")),
107 extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
108 extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
109 )},
110 wire: pack.Message{
111 pack.Tag{1, pack.VarintType}, pack.Varint(1001),
112 pack.Tag{2, pack.VarintType}, pack.Varint(1002),
113 pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
114 pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
115 pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
116 pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
117 pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
118 pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
119 pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
120 pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
121 pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
122 pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
123 pack.Tag{13, pack.VarintType}, pack.Bool(true),
124 pack.Tag{14, pack.BytesType}, pack.String("string"),
125 pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
126 pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
127 }.Marshal(),
128 },
129 {
130 desc: "groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800131 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800132 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
133 A: scalar.Int32(1017),
134 },
135 }, build(
136 &testpb.TestAllExtensions{},
137 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
138 A: scalar.Int32(1017),
139 }),
140 )},
141 wire: pack.Message{
142 pack.Tag{16, pack.StartGroupType},
143 pack.Tag{17, pack.VarintType}, pack.Varint(1017),
144 pack.Tag{16, pack.EndGroupType},
145 }.Marshal(),
146 },
147 {
148 desc: "groups (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800149 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800150 Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
151 A: scalar.Int32(2),
152 },
153 }, build(
154 &testpb.TestAllExtensions{},
155 extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
156 A: scalar.Int32(2),
157 }),
158 )},
159 wire: pack.Message{
160 pack.Tag{16, pack.StartGroupType},
161 pack.Tag{17, pack.VarintType}, pack.Varint(1),
162 pack.Tag{16, pack.EndGroupType},
163 pack.Tag{16, pack.StartGroupType},
164 pack.Tag{17, pack.VarintType}, pack.Varint(2),
165 pack.Tag{16, pack.EndGroupType},
166 }.Marshal(),
167 },
168 {
169 desc: "messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800170 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800171 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
172 A: scalar.Int32(42),
173 Corecursive: &testpb.TestAllTypes{
174 OptionalInt32: scalar.Int32(43),
175 },
176 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700177 }, &test3pb.TestAllTypes{
178 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
179 A: 42,
180 Corecursive: &test3pb.TestAllTypes{
181 OptionalInt32: 43,
182 },
183 },
Damien Neilba23aa52018-12-07 14:38:17 -0800184 }, build(
185 &testpb.TestAllExtensions{},
186 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
187 A: scalar.Int32(42),
188 Corecursive: &testpb.TestAllTypes{
189 OptionalInt32: scalar.Int32(43),
190 },
191 }),
192 )},
193 wire: pack.Message{
194 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
195 pack.Tag{1, pack.VarintType}, pack.Varint(42),
196 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
197 pack.Tag{1, pack.VarintType}, pack.Varint(43),
198 }),
199 }),
200 }.Marshal(),
201 },
202 {
203 desc: "messages (split across multiple tags)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800204 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800205 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
206 A: scalar.Int32(42),
207 Corecursive: &testpb.TestAllTypes{
208 OptionalInt32: scalar.Int32(43),
209 },
210 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700211 }, &test3pb.TestAllTypes{
212 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
213 A: 42,
214 Corecursive: &test3pb.TestAllTypes{
215 OptionalInt32: 43,
216 },
217 },
Damien Neilba23aa52018-12-07 14:38:17 -0800218 }, build(
219 &testpb.TestAllExtensions{},
220 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
221 A: scalar.Int32(42),
222 Corecursive: &testpb.TestAllTypes{
223 OptionalInt32: scalar.Int32(43),
224 },
225 }),
226 )},
227 wire: pack.Message{
228 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
229 pack.Tag{1, pack.VarintType}, pack.Varint(42),
230 }),
231 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
232 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
233 pack.Tag{1, pack.VarintType}, pack.Varint(43),
234 }),
235 }),
236 }.Marshal(),
237 },
238 {
239 desc: "messages (field overridden)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800240 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800241 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
242 A: scalar.Int32(2),
243 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700244 }, &test3pb.TestAllTypes{
245 OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
246 A: 2,
247 },
Damien Neilba23aa52018-12-07 14:38:17 -0800248 }, build(
249 &testpb.TestAllExtensions{},
250 extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
251 A: scalar.Int32(2),
252 }),
253 )},
254 wire: pack.Message{
255 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
256 pack.Tag{1, pack.VarintType}, pack.Varint(1),
257 }),
258 pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
259 pack.Tag{1, pack.VarintType}, pack.Varint(2),
260 }),
261 }.Marshal(),
262 },
263 {
264 desc: "basic repeated types",
Damien Neil4be2fb42018-12-17 11:16:16 -0800265 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800266 RepeatedInt32: []int32{1001, 2001},
267 RepeatedInt64: []int64{1002, 2002},
268 RepeatedUint32: []uint32{1003, 2003},
269 RepeatedUint64: []uint64{1004, 2004},
270 RepeatedSint32: []int32{1005, 2005},
271 RepeatedSint64: []int64{1006, 2006},
272 RepeatedFixed32: []uint32{1007, 2007},
273 RepeatedFixed64: []uint64{1008, 2008},
274 RepeatedSfixed32: []int32{1009, 2009},
275 RepeatedSfixed64: []int64{1010, 2010},
276 RepeatedFloat: []float32{1011.5, 2011.5},
277 RepeatedDouble: []float64{1012.5, 2012.5},
278 RepeatedBool: []bool{true, false},
279 RepeatedString: []string{"foo", "bar"},
280 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
281 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
282 testpb.TestAllTypes_FOO,
283 testpb.TestAllTypes_BAR,
284 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700285 }, &test3pb.TestAllTypes{
286 RepeatedInt32: []int32{1001, 2001},
287 RepeatedInt64: []int64{1002, 2002},
288 RepeatedUint32: []uint32{1003, 2003},
289 RepeatedUint64: []uint64{1004, 2004},
290 RepeatedSint32: []int32{1005, 2005},
291 RepeatedSint64: []int64{1006, 2006},
292 RepeatedFixed32: []uint32{1007, 2007},
293 RepeatedFixed64: []uint64{1008, 2008},
294 RepeatedSfixed32: []int32{1009, 2009},
295 RepeatedSfixed64: []int64{1010, 2010},
296 RepeatedFloat: []float32{1011.5, 2011.5},
297 RepeatedDouble: []float64{1012.5, 2012.5},
298 RepeatedBool: []bool{true, false},
299 RepeatedString: []string{"foo", "bar"},
300 RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
301 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
302 test3pb.TestAllTypes_FOO,
303 test3pb.TestAllTypes_BAR,
304 },
Damien Neilba23aa52018-12-07 14:38:17 -0800305 }, build(
306 &testpb.TestAllExtensions{},
307 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
308 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
309 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
310 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
311 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
312 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
313 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
314 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
315 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
316 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
317 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
318 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
319 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
320 extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
321 extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
322 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
323 testpb.TestAllTypes_FOO,
324 testpb.TestAllTypes_BAR,
325 }),
326 )},
327 wire: pack.Message{
328 pack.Tag{31, pack.VarintType}, pack.Varint(1001),
329 pack.Tag{31, pack.VarintType}, pack.Varint(2001),
330 pack.Tag{32, pack.VarintType}, pack.Varint(1002),
331 pack.Tag{32, pack.VarintType}, pack.Varint(2002),
332 pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
333 pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
334 pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
335 pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
336 pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
337 pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
338 pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
339 pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
340 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
341 pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
342 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
343 pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
344 pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
345 pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
346 pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
347 pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
348 pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
349 pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
350 pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
351 pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
352 pack.Tag{43, pack.VarintType}, pack.Bool(true),
353 pack.Tag{43, pack.VarintType}, pack.Bool(false),
354 pack.Tag{44, pack.BytesType}, pack.String("foo"),
355 pack.Tag{44, pack.BytesType}, pack.String("bar"),
356 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
357 pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
358 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
359 pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
360 }.Marshal(),
361 },
362 {
363 desc: "basic repeated types (packed encoding)",
Damien Neil4be2fb42018-12-17 11:16:16 -0800364 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800365 RepeatedInt32: []int32{1001, 2001},
366 RepeatedInt64: []int64{1002, 2002},
367 RepeatedUint32: []uint32{1003, 2003},
368 RepeatedUint64: []uint64{1004, 2004},
369 RepeatedSint32: []int32{1005, 2005},
370 RepeatedSint64: []int64{1006, 2006},
371 RepeatedFixed32: []uint32{1007, 2007},
372 RepeatedFixed64: []uint64{1008, 2008},
373 RepeatedSfixed32: []int32{1009, 2009},
374 RepeatedSfixed64: []int64{1010, 2010},
375 RepeatedFloat: []float32{1011.5, 2011.5},
376 RepeatedDouble: []float64{1012.5, 2012.5},
377 RepeatedBool: []bool{true, false},
378 RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
379 testpb.TestAllTypes_FOO,
380 testpb.TestAllTypes_BAR,
381 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700382 }, &test3pb.TestAllTypes{
383 RepeatedInt32: []int32{1001, 2001},
384 RepeatedInt64: []int64{1002, 2002},
385 RepeatedUint32: []uint32{1003, 2003},
386 RepeatedUint64: []uint64{1004, 2004},
387 RepeatedSint32: []int32{1005, 2005},
388 RepeatedSint64: []int64{1006, 2006},
389 RepeatedFixed32: []uint32{1007, 2007},
390 RepeatedFixed64: []uint64{1008, 2008},
391 RepeatedSfixed32: []int32{1009, 2009},
392 RepeatedSfixed64: []int64{1010, 2010},
393 RepeatedFloat: []float32{1011.5, 2011.5},
394 RepeatedDouble: []float64{1012.5, 2012.5},
395 RepeatedBool: []bool{true, false},
396 RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
397 test3pb.TestAllTypes_FOO,
398 test3pb.TestAllTypes_BAR,
399 },
Damien Neilba23aa52018-12-07 14:38:17 -0800400 }, build(
401 &testpb.TestAllExtensions{},
402 extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
403 extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
404 extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
405 extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
406 extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
407 extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
408 extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
409 extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
410 extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
411 extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
412 extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
413 extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
414 extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
415 extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
416 testpb.TestAllTypes_FOO,
417 testpb.TestAllTypes_BAR,
418 }),
419 )},
420 wire: pack.Message{
421 pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
422 pack.Varint(1001), pack.Varint(2001),
423 },
424 pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
425 pack.Varint(1002), pack.Varint(2002),
426 },
427 pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
428 pack.Uvarint(1003), pack.Uvarint(2003),
429 },
430 pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
431 pack.Uvarint(1004), pack.Uvarint(2004),
432 },
433 pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
434 pack.Svarint(1005), pack.Svarint(2005),
435 },
436 pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
437 pack.Svarint(1006), pack.Svarint(2006),
438 },
439 pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
440 pack.Uint32(1007), pack.Uint32(2007),
441 },
442 pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
443 pack.Uint64(1008), pack.Uint64(2008),
444 },
445 pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
446 pack.Int32(1009), pack.Int32(2009),
447 },
448 pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
449 pack.Int64(1010), pack.Int64(2010),
450 },
451 pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
452 pack.Float32(1011.5), pack.Float32(2011.5),
453 },
454 pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
455 pack.Float64(1012.5), pack.Float64(2012.5),
456 },
457 pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
458 pack.Bool(true), pack.Bool(false),
459 },
460 pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
461 pack.Varint(int(testpb.TestAllTypes_FOO)),
462 pack.Varint(int(testpb.TestAllTypes_BAR)),
463 },
464 }.Marshal(),
465 },
466 {
467 desc: "repeated messages",
Damien Neil4be2fb42018-12-17 11:16:16 -0800468 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800469 RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
470 {A: scalar.Int32(1)},
471 {A: scalar.Int32(2)},
472 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700473 }, &test3pb.TestAllTypes{
474 RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
475 {A: 1},
476 {A: 2},
477 },
Damien Neilba23aa52018-12-07 14:38:17 -0800478 }, build(
479 &testpb.TestAllExtensions{},
480 extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
481 {A: scalar.Int32(1)},
482 {A: scalar.Int32(2)},
483 }),
484 )},
485 wire: pack.Message{
486 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
487 pack.Tag{1, pack.VarintType}, pack.Varint(1),
488 }),
489 pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
490 pack.Tag{1, pack.VarintType}, pack.Varint(2),
491 }),
492 }.Marshal(),
493 },
494 {
495 desc: "repeated groups",
Damien Neil4be2fb42018-12-17 11:16:16 -0800496 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800497 Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
498 {A: scalar.Int32(1017)},
499 {A: scalar.Int32(2017)},
500 },
501 }, build(
502 &testpb.TestAllExtensions{},
503 extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
504 {A: scalar.Int32(1017)},
505 {A: scalar.Int32(2017)},
506 }),
507 )},
508 wire: pack.Message{
509 pack.Tag{46, pack.StartGroupType},
510 pack.Tag{47, pack.VarintType}, pack.Varint(1017),
511 pack.Tag{46, pack.EndGroupType},
512 pack.Tag{46, pack.StartGroupType},
513 pack.Tag{47, pack.VarintType}, pack.Varint(2017),
514 pack.Tag{46, pack.EndGroupType},
515 }.Marshal(),
516 },
517 {
518 desc: "maps",
Damien Neil4be2fb42018-12-17 11:16:16 -0800519 decodeTo: []proto.Message{&testpb.TestAllTypes{
Damien Neilba23aa52018-12-07 14:38:17 -0800520 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
521 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
522 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
523 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
524 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
525 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
526 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
527 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
528 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
529 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
530 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
531 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
532 MapBoolBool: map[bool]bool{true: false, false: true},
533 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
534 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
535 MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
536 "71.1.key": {A: scalar.Int32(1171)},
537 "71.2.key": {A: scalar.Int32(2171)},
538 },
539 MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
540 "73.1.key": testpb.TestAllTypes_FOO,
541 "73.2.key": testpb.TestAllTypes_BAR,
542 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700543 }, &test3pb.TestAllTypes{
544 MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
545 MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
546 MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
547 MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
548 MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
549 MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
550 MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
551 MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
552 MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
553 MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
554 MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
555 MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
556 MapBoolBool: map[bool]bool{true: false, false: true},
557 MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
558 MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
559 MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
560 "71.1.key": {A: 1171},
561 "71.2.key": {A: 2171},
562 },
563 MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
564 "73.1.key": test3pb.TestAllTypes_FOO,
565 "73.2.key": test3pb.TestAllTypes_BAR,
566 },
Damien Neilba23aa52018-12-07 14:38:17 -0800567 }},
568 wire: pack.Message{
569 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
570 pack.Tag{1, pack.VarintType}, pack.Varint(1056),
571 pack.Tag{2, pack.VarintType}, pack.Varint(1156),
572 }),
573 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
574 pack.Tag{1, pack.VarintType}, pack.Varint(2056),
575 pack.Tag{2, pack.VarintType}, pack.Varint(2156),
576 }),
577 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
578 pack.Tag{1, pack.VarintType}, pack.Varint(1057),
579 pack.Tag{2, pack.VarintType}, pack.Varint(1157),
580 }),
581 pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
582 pack.Tag{1, pack.VarintType}, pack.Varint(2057),
583 pack.Tag{2, pack.VarintType}, pack.Varint(2157),
584 }),
585 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
586 pack.Tag{1, pack.VarintType}, pack.Varint(1058),
587 pack.Tag{2, pack.VarintType}, pack.Varint(1158),
588 }),
589 pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
590 pack.Tag{1, pack.VarintType}, pack.Varint(2058),
591 pack.Tag{2, pack.VarintType}, pack.Varint(2158),
592 }),
593 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
594 pack.Tag{1, pack.VarintType}, pack.Varint(1059),
595 pack.Tag{2, pack.VarintType}, pack.Varint(1159),
596 }),
597 pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
598 pack.Tag{1, pack.VarintType}, pack.Varint(2059),
599 pack.Tag{2, pack.VarintType}, pack.Varint(2159),
600 }),
601 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
602 pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
603 pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
604 }),
605 pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
606 pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
607 pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
608 }),
609 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
610 pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
611 pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
612 }),
613 pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
614 pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
615 pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
616 }),
617 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
618 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
619 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
620 }),
621 pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
622 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
623 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
624 }),
625 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
626 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
627 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
628 }),
629 pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
630 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
631 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
632 }),
633 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
634 pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
635 pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
636 }),
637 pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
638 pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
639 pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
640 }),
641 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
642 pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
643 pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
644 }),
645 pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
646 pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
647 pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
648 }),
649 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
650 pack.Tag{1, pack.VarintType}, pack.Varint(1066),
651 pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
652 }),
653 pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
654 pack.Tag{1, pack.VarintType}, pack.Varint(2066),
655 pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
656 }),
657 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
658 pack.Tag{1, pack.VarintType}, pack.Varint(1067),
659 pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
660 }),
661 pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
662 pack.Tag{1, pack.VarintType}, pack.Varint(2067),
663 pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
664 }),
665 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
666 pack.Tag{1, pack.VarintType}, pack.Bool(true),
667 pack.Tag{2, pack.VarintType}, pack.Bool(false),
668 }),
669 pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
670 pack.Tag{1, pack.VarintType}, pack.Bool(false),
671 pack.Tag{2, pack.VarintType}, pack.Bool(true),
672 }),
673 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
674 pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
675 pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
676 }),
677 pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
678 pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
679 pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
680 }),
681 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
682 pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
683 pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
684 }),
685 pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
686 pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
687 pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
688 }),
689 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
690 pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
691 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
692 pack.Tag{1, pack.VarintType}, pack.Varint(1171),
693 }),
694 }),
695 pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
696 pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
697 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
698 pack.Tag{1, pack.VarintType}, pack.Varint(2171),
699 }),
700 }),
701 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
702 pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
703 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
704 }),
705 pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
706 pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
707 pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
708 }),
709 }.Marshal(),
710 },
711 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700712 desc: "oneof (uint32)",
713 decodeTo: []proto.Message{
714 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
715 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
716 },
717 wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800718 },
719 {
720 desc: "oneof (message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700721 decodeTo: []proto.Message{
722 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
723 &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
724 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
725 &test3pb.TestAllTypes_NestedMessage{A: 1112},
726 }},
727 },
Damien Neilba23aa52018-12-07 14:38:17 -0800728 wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
729 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
730 })}.Marshal(),
731 },
732 {
733 desc: "oneof (overridden message)",
Damien Neil3b46ade2019-03-26 13:55:02 -0700734 decodeTo: []proto.Message{
735 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
736 &testpb.TestAllTypes_NestedMessage{
737 Corecursive: &testpb.TestAllTypes{
738 OptionalInt32: scalar.Int32(43),
739 },
Damien Neilba23aa52018-12-07 14:38:17 -0800740 },
Damien Neil3b46ade2019-03-26 13:55:02 -0700741 }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
742 &test3pb.TestAllTypes_NestedMessage{
743 Corecursive: &test3pb.TestAllTypes{
744 OptionalInt32: 43,
745 },
746 },
747 }}},
Damien Neilba23aa52018-12-07 14:38:17 -0800748 wire: pack.Message{
749 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
750 pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
751 }),
752 pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
753 pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
754 pack.Tag{1, pack.VarintType}, pack.Varint(43),
755 }),
756 }),
757 }.Marshal(),
758 },
759 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700760 desc: "oneof (string)",
761 decodeTo: []proto.Message{
762 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
763 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
764 },
765 wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800766 },
767 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700768 desc: "oneof (bytes)",
769 decodeTo: []proto.Message{
770 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
771 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
772 },
773 wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800774 },
775 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700776 desc: "oneof (bool)",
777 decodeTo: []proto.Message{
778 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
779 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
780 },
781 wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800782 },
783 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700784 desc: "oneof (uint64)",
785 decodeTo: []proto.Message{
786 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
787 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
788 },
789 wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800790 },
791 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700792 desc: "oneof (float)",
793 decodeTo: []proto.Message{
794 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
795 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
796 },
797 wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800798 },
799 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700800 desc: "oneof (double)",
801 decodeTo: []proto.Message{
802 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
803 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
804 },
805 wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800806 },
807 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700808 desc: "oneof (enum)",
809 decodeTo: []proto.Message{
810 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
811 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
812 },
813 wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
Damien Neilba23aa52018-12-07 14:38:17 -0800814 },
815 {
Damien Neil3b46ade2019-03-26 13:55:02 -0700816 desc: "oneof (overridden value)",
817 decodeTo: []proto.Message{
818 &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
819 &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
820 },
Damien Neilba23aa52018-12-07 14:38:17 -0800821 wire: pack.Message{
822 pack.Tag{111, pack.VarintType}, pack.Varint(1),
823 pack.Tag{116, pack.VarintType}, pack.Varint(2),
824 }.Marshal(),
825 },
826 // TODO: More unknown field tests for ordering, repeated fields, etc.
827 //
828 // It is currently impossible to produce results that the v1 Equal
829 // considers equivalent to those of the v1 decoder. Figure out if
830 // that's a problem or not.
831 {
832 desc: "unknown fields",
Damien Neil4be2fb42018-12-17 11:16:16 -0800833 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800834 &testpb.TestAllTypes{},
835 unknown(100000, pack.Message{
836 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
837 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700838 ), build(
839 &test3pb.TestAllTypes{},
840 unknown(100000, pack.Message{
841 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
842 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800843 )},
844 wire: pack.Message{
845 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
846 }.Marshal(),
847 },
848 {
849 desc: "field type mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800850 decodeTo: []proto.Message{build(
Damien Neilba23aa52018-12-07 14:38:17 -0800851 &testpb.TestAllTypes{},
852 unknown(1, pack.Message{
853 pack.Tag{1, pack.BytesType}, pack.String("string"),
854 }.Marshal()),
Damien Neil3b46ade2019-03-26 13:55:02 -0700855 ), build(
856 &test3pb.TestAllTypes{},
857 unknown(1, pack.Message{
858 pack.Tag{1, pack.BytesType}, pack.String("string"),
859 }.Marshal()),
Damien Neilba23aa52018-12-07 14:38:17 -0800860 )},
861 wire: pack.Message{
862 pack.Tag{1, pack.BytesType}, pack.String("string"),
863 }.Marshal(),
864 },
865 {
866 desc: "map field element mismatch",
Damien Neil4be2fb42018-12-17 11:16:16 -0800867 decodeTo: []proto.Message{
Damien Neilba23aa52018-12-07 14:38:17 -0800868 &testpb.TestAllTypes{
869 MapInt32Int32: map[int32]int32{1: 0},
Damien Neil3b46ade2019-03-26 13:55:02 -0700870 }, &test3pb.TestAllTypes{
871 MapInt32Int32: map[int32]int32{1: 0},
Damien Neilba23aa52018-12-07 14:38:17 -0800872 },
873 },
874 wire: pack.Message{
875 pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
876 pack.Tag{1, pack.VarintType}, pack.Varint(1),
877 pack.Tag{2, pack.BytesType}, pack.String("string"),
878 }),
879 }.Marshal(),
880 },
881}
882
Damien Neil4be2fb42018-12-17 11:16:16 -0800883func build(m proto.Message, opts ...buildOpt) proto.Message {
Damien Neilba23aa52018-12-07 14:38:17 -0800884 for _, opt := range opts {
885 opt(m)
886 }
887 return m
888}
889
Damien Neil4be2fb42018-12-17 11:16:16 -0800890type buildOpt func(proto.Message)
Damien Neilba23aa52018-12-07 14:38:17 -0800891
892func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -0800893 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -0800894 m.ProtoReflect().UnknownFields().Set(num, raw)
895 }
896}
897
898func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
Damien Neil4be2fb42018-12-17 11:16:16 -0800899 return func(m proto.Message) {
Damien Neilba23aa52018-12-07 14:38:17 -0800900 if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
901 panic(err)
902 }
903 }
904}
Damien Neil61e93c72019-03-27 09:23:20 -0700905
906func marshalText(m proto.Message) string {
907 b, _ := textpb.Marshal(m)
908 return string(b)
909}