blob: b32d2db31f1f5363790eb69ab7e153da2b1f6f65 [file] [log] [blame]
Herbie Ongcddf8192018-11-28 18:25:20 -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
5package textpb_test
6
7import (
Herbie Onga94f78c2019-01-03 15:39:58 -08008 "encoding/hex"
Herbie Ongcddf8192018-11-28 18:25:20 -08009 "math"
10 "strings"
11 "testing"
12
Herbie Ongf42b55f2019-01-02 15:46:07 -080013 protoV1 "github.com/golang/protobuf/proto"
Herbie Ongcf253082018-12-17 17:13:07 -080014 "github.com/golang/protobuf/protoapi"
Herbie Ongcddf8192018-11-28 18:25:20 -080015 "github.com/golang/protobuf/v2/encoding/textpb"
Herbie Ongcddf8192018-11-28 18:25:20 -080016 "github.com/golang/protobuf/v2/internal/detrand"
Herbie Ong20a1d312018-12-11 21:08:58 -080017 "github.com/golang/protobuf/v2/internal/encoding/pack"
Herbie Ongcf253082018-12-17 17:13:07 -080018 "github.com/golang/protobuf/v2/internal/encoding/wire"
Herbie Ongf42b55f2019-01-02 15:46:07 -080019 "github.com/golang/protobuf/v2/internal/impl"
Herbie Ongcf253082018-12-17 17:13:07 -080020 "github.com/golang/protobuf/v2/internal/legacy"
Herbie Ongcddf8192018-11-28 18:25:20 -080021 "github.com/golang/protobuf/v2/internal/scalar"
22 "github.com/golang/protobuf/v2/proto"
Herbie Ongf42b55f2019-01-02 15:46:07 -080023 preg "github.com/golang/protobuf/v2/reflect/protoregistry"
Herbie Ongcddf8192018-11-28 18:25:20 -080024 "github.com/google/go-cmp/cmp"
25 "github.com/google/go-cmp/cmp/cmpopts"
26
Joe Tsai08e00302018-11-26 22:32:06 -080027 // The legacy package must be imported prior to use of any legacy messages.
28 // TODO: Remove this when protoV1 registers these hooks for you.
29 _ "github.com/golang/protobuf/v2/internal/legacy"
30
Herbie Ongf42b55f2019-01-02 15:46:07 -080031 anypb "github.com/golang/protobuf/ptypes/any"
Joe Tsai08e00302018-11-26 22:32:06 -080032 "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb2"
33 "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb3"
Herbie Ongcddf8192018-11-28 18:25:20 -080034)
35
36func init() {
37 // Disable detrand to enable direct comparisons on outputs.
38 detrand.Disable()
39}
40
Herbie Ongcddf8192018-11-28 18:25:20 -080041// splitLines is a cmpopts.Option for comparing strings with line breaks.
42var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
43 return strings.Split(s, "\n")
44})
45
Herbie Ong800c9902018-12-06 15:28:53 -080046func pb2Enum(i int32) *pb2.Enum {
47 p := new(pb2.Enum)
48 *p = pb2.Enum(i)
49 return p
50}
51
52func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
53 p := new(pb2.Enums_NestedEnum)
54 *p = pb2.Enums_NestedEnum(i)
55 return p
56}
57
Herbie Ongcf253082018-12-17 17:13:07 -080058func setExtension(m proto.Message, xd *protoapi.ExtensionDesc, val interface{}) {
59 xt := legacy.Export{}.ExtensionTypeFromDesc(xd)
60 knownFields := m.ProtoReflect().KnownFields()
61 extTypes := knownFields.ExtensionTypes()
62 extTypes.Register(xt)
63 if val == nil {
64 return
65 }
66 pval := xt.ValueOf(val)
67 knownFields.Set(wire.Number(xd.Field), pval)
68}
69
Herbie Ong66c365c2019-01-04 14:08:41 -080070func wrapAnyPB(any *anypb.Any) proto.Message {
71 return impl.Export{}.MessageOf(any).Interface()
72}
73
Herbie Onga94f78c2019-01-03 15:39:58 -080074// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
75func dhex(s string) []byte {
76 b, err := hex.DecodeString(s)
77 if err != nil {
78 panic(err)
79 }
80 return b
81}
82
Herbie Ongcddf8192018-11-28 18:25:20 -080083func TestMarshal(t *testing.T) {
84 tests := []struct {
85 desc string
Herbie Ongf42b55f2019-01-02 15:46:07 -080086 mo textpb.MarshalOptions
Herbie Ong70651952018-12-13 14:19:50 -080087 input proto.Message
Herbie Ongcddf8192018-11-28 18:25:20 -080088 want string
89 wantErr bool
90 }{{
Herbie Ongcddf8192018-11-28 18:25:20 -080091 desc: "proto2 optional scalar fields not set",
Herbie Ong800c9902018-12-06 15:28:53 -080092 input: &pb2.Scalars{},
Herbie Ongcddf8192018-11-28 18:25:20 -080093 want: "\n",
94 }, {
95 desc: "proto3 scalar fields not set",
Herbie Ong800c9902018-12-06 15:28:53 -080096 input: &pb3.Scalars{},
Herbie Ongcddf8192018-11-28 18:25:20 -080097 want: "\n",
98 }, {
99 desc: "proto2 optional scalar fields set to zero values",
Herbie Ong800c9902018-12-06 15:28:53 -0800100 input: &pb2.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800101 OptBool: scalar.Bool(false),
102 OptInt32: scalar.Int32(0),
103 OptInt64: scalar.Int64(0),
104 OptUint32: scalar.Uint32(0),
105 OptUint64: scalar.Uint64(0),
106 OptSint32: scalar.Int32(0),
107 OptSint64: scalar.Int64(0),
108 OptFixed32: scalar.Uint32(0),
109 OptFixed64: scalar.Uint64(0),
110 OptSfixed32: scalar.Int32(0),
111 OptSfixed64: scalar.Int64(0),
112 OptFloat: scalar.Float32(0),
113 OptDouble: scalar.Float64(0),
114 OptBytes: []byte{},
115 OptString: scalar.String(""),
Herbie Ong800c9902018-12-06 15:28:53 -0800116 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800117 want: `opt_bool: false
118opt_int32: 0
119opt_int64: 0
120opt_uint32: 0
121opt_uint64: 0
122opt_sint32: 0
123opt_sint64: 0
124opt_fixed32: 0
125opt_fixed64: 0
126opt_sfixed32: 0
127opt_sfixed64: 0
128opt_float: 0
129opt_double: 0
130opt_bytes: ""
131opt_string: ""
132`,
133 }, {
134 desc: "proto3 scalar fields set to zero values",
Herbie Ong800c9902018-12-06 15:28:53 -0800135 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800136 SBool: false,
137 SInt32: 0,
138 SInt64: 0,
139 SUint32: 0,
140 SUint64: 0,
141 SSint32: 0,
142 SSint64: 0,
143 SFixed32: 0,
144 SFixed64: 0,
145 SSfixed32: 0,
146 SSfixed64: 0,
147 SFloat: 0,
148 SDouble: 0,
149 SBytes: []byte{},
150 SString: "",
Herbie Ong800c9902018-12-06 15:28:53 -0800151 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800152 want: "\n",
153 }, {
154 desc: "proto2 optional scalar fields set to some values",
Herbie Ong800c9902018-12-06 15:28:53 -0800155 input: &pb2.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800156 OptBool: scalar.Bool(true),
157 OptInt32: scalar.Int32(0xff),
158 OptInt64: scalar.Int64(0xdeadbeef),
159 OptUint32: scalar.Uint32(47),
160 OptUint64: scalar.Uint64(0xdeadbeef),
161 OptSint32: scalar.Int32(-1001),
162 OptSint64: scalar.Int64(-0xffff),
163 OptFixed64: scalar.Uint64(64),
164 OptSfixed32: scalar.Int32(-32),
Herbie Ong84f09602019-01-17 19:31:47 -0800165 OptFloat: scalar.Float32(1.02),
166 OptDouble: scalar.Float64(1.0199999809265137),
Herbie Ongcddf8192018-11-28 18:25:20 -0800167 // TODO: Update encoder to not output UTF8 for bytes.
168 OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
169 OptString: scalar.String("谷歌"),
Herbie Ong800c9902018-12-06 15:28:53 -0800170 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800171 want: `opt_bool: true
172opt_int32: 255
173opt_int64: 3735928559
174opt_uint32: 47
175opt_uint64: 3735928559
176opt_sint32: -1001
177opt_sint64: -65535
178opt_fixed64: 64
179opt_sfixed32: -32
Herbie Ong84f09602019-01-17 19:31:47 -0800180opt_float: 1.02
181opt_double: 1.0199999809265137
Herbie Ongcddf8192018-11-28 18:25:20 -0800182opt_bytes: "谷歌"
183opt_string: "谷歌"
184`,
185 }, {
Herbie Ongcddf8192018-11-28 18:25:20 -0800186 desc: "float32 nan",
Herbie Ong800c9902018-12-06 15:28:53 -0800187 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800188 SFloat: float32(math.NaN()),
Herbie Ong800c9902018-12-06 15:28:53 -0800189 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800190 want: "s_float: nan\n",
191 }, {
192 desc: "float32 positive infinity",
Herbie Ong800c9902018-12-06 15:28:53 -0800193 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800194 SFloat: float32(math.Inf(1)),
Herbie Ong800c9902018-12-06 15:28:53 -0800195 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800196 want: "s_float: inf\n",
197 }, {
198 desc: "float32 negative infinity",
Herbie Ong800c9902018-12-06 15:28:53 -0800199 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800200 SFloat: float32(math.Inf(-1)),
Herbie Ong800c9902018-12-06 15:28:53 -0800201 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800202 want: "s_float: -inf\n",
203 }, {
204 desc: "float64 nan",
Herbie Ong800c9902018-12-06 15:28:53 -0800205 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800206 SDouble: math.NaN(),
Herbie Ong800c9902018-12-06 15:28:53 -0800207 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800208 want: "s_double: nan\n",
209 }, {
210 desc: "float64 positive infinity",
Herbie Ong800c9902018-12-06 15:28:53 -0800211 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800212 SDouble: math.Inf(1),
Herbie Ong800c9902018-12-06 15:28:53 -0800213 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800214 want: "s_double: inf\n",
215 }, {
216 desc: "float64 negative infinity",
Herbie Ong800c9902018-12-06 15:28:53 -0800217 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800218 SDouble: math.Inf(-1),
Herbie Ong800c9902018-12-06 15:28:53 -0800219 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800220 want: "s_double: -inf\n",
221 }, {
222 desc: "proto2 bytes set to empty string",
Herbie Ong800c9902018-12-06 15:28:53 -0800223 input: &pb2.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800224 OptBytes: []byte(""),
Herbie Ong800c9902018-12-06 15:28:53 -0800225 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800226 want: "opt_bytes: \"\"\n",
227 }, {
228 desc: "proto3 bytes set to empty string",
Herbie Ong800c9902018-12-06 15:28:53 -0800229 input: &pb3.Scalars{
Herbie Ongcddf8192018-11-28 18:25:20 -0800230 SBytes: []byte(""),
Herbie Ong800c9902018-12-06 15:28:53 -0800231 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800232 want: "\n",
233 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800234 desc: "proto2 enum not set",
235 input: &pb2.Enums{},
Herbie Ongcddf8192018-11-28 18:25:20 -0800236 want: "\n",
237 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800238 desc: "proto2 enum set to zero value",
239 input: &pb2.Enums{
240 OptEnum: pb2.Enum_UNKNOWN.Enum(),
241 OptNestedEnum: pb2Enums_NestedEnum(0),
242 },
243 want: `opt_enum: UNKNOWN
244opt_nested_enum: 0
245`,
246 }, {
247 desc: "proto2 enum",
248 input: &pb2.Enums{
249 OptEnum: pb2.Enum_FIRST.Enum(),
250 OptNestedEnum: pb2.Enums_UNO.Enum(),
251 },
252 want: `opt_enum: FIRST
253opt_nested_enum: UNO
254`,
255 }, {
256 desc: "proto2 enum set to numeric values",
257 input: &pb2.Enums{
258 OptEnum: pb2Enum(1),
259 OptNestedEnum: pb2Enums_NestedEnum(2),
260 },
261 want: `opt_enum: FIRST
262opt_nested_enum: DOS
263`,
264 }, {
265 desc: "proto2 enum set to unnamed numeric values",
266 input: &pb2.Enums{
267 OptEnum: pb2Enum(101),
268 OptNestedEnum: pb2Enums_NestedEnum(-101),
269 },
270 want: `opt_enum: 101
271opt_nested_enum: -101
272`,
273 }, {
274 desc: "proto3 enum not set",
275 input: &pb3.Enums{},
276 want: "\n",
277 }, {
278 desc: "proto3 enum set to zero value",
279 input: &pb3.Enums{
280 SEnum: pb3.Enum_ZERO,
281 SNestedEnum: pb3.Enums_CERO,
282 },
283 want: "\n",
284 }, {
285 desc: "proto3 enum",
286 input: &pb3.Enums{
287 SEnum: pb3.Enum_ONE,
288 SNestedEnum: pb3.Enums_DIEZ,
289 },
290 want: `s_enum: ONE
291s_nested_enum: DIEZ
292`,
293 }, {
294 desc: "proto3 enum set to numeric values",
295 input: &pb3.Enums{
296 SEnum: 2,
297 SNestedEnum: 1,
298 },
299 want: `s_enum: TWO
300s_nested_enum: UNO
301`,
302 }, {
303 desc: "proto3 enum set to unnamed numeric values",
304 input: &pb3.Enums{
305 SEnum: -47,
306 SNestedEnum: 47,
307 },
308 want: `s_enum: -47
309s_nested_enum: 47
310`,
311 }, {
312 desc: "proto2 nested message not set",
313 input: &pb2.Nests{},
314 want: "\n",
315 }, {
316 desc: "proto2 nested message set to empty",
317 input: &pb2.Nests{
318 OptNested: &pb2.Nested{},
319 Optgroup: &pb2.Nests_OptGroup{},
320 },
321 want: `opt_nested: {}
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800322OptGroup: {}
Herbie Ong800c9902018-12-06 15:28:53 -0800323`,
324 }, {
325 desc: "proto2 nested messages",
326 input: &pb2.Nests{
327 OptNested: &pb2.Nested{
328 OptString: scalar.String("nested message"),
329 OptNested: &pb2.Nested{
330 OptString: scalar.String("another nested message"),
331 },
332 },
333 },
334 want: `opt_nested: {
335 opt_string: "nested message"
336 opt_nested: {
337 opt_string: "another nested message"
338 }
339}
340`,
341 }, {
342 desc: "proto2 group fields",
343 input: &pb2.Nests{
344 Optgroup: &pb2.Nests_OptGroup{
345 OptBool: scalar.Bool(true),
346 OptString: scalar.String("inside a group"),
347 OptNested: &pb2.Nested{
348 OptString: scalar.String("nested message inside a group"),
349 },
350 Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
351 OptEnum: pb2.Enum_TENTH.Enum(),
352 },
353 },
354 },
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800355 want: `OptGroup: {
Herbie Ong800c9902018-12-06 15:28:53 -0800356 opt_bool: true
357 opt_string: "inside a group"
358 opt_nested: {
359 opt_string: "nested message inside a group"
360 }
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800361 OptNestedGroup: {
Herbie Ong800c9902018-12-06 15:28:53 -0800362 opt_enum: TENTH
363 }
364}
365`,
366 }, {
367 desc: "proto3 nested message not set",
368 input: &pb3.Nests{},
369 want: "\n",
370 }, {
371 desc: "proto3 nested message",
372 input: &pb3.Nests{
373 SNested: &pb3.Nested{
374 SString: "nested message",
375 SNested: &pb3.Nested{
376 SString: "another nested message",
377 },
378 },
379 },
380 want: `s_nested: {
381 s_string: "nested message"
382 s_nested: {
383 s_string: "another nested message"
384 }
385}
386`,
387 }, {
388 desc: "oneof fields",
389 input: &pb2.Oneofs{},
390 want: "\n",
391 }, {
392 desc: "oneof field set to empty string",
393 input: &pb2.Oneofs{
394 Union: &pb2.Oneofs_Str{},
395 },
396 want: "str: \"\"\n",
397 }, {
398 desc: "oneof field set to string",
399 input: &pb2.Oneofs{
400 Union: &pb2.Oneofs_Str{
401 Str: "hello",
402 },
403 },
404 want: "str: \"hello\"\n",
405 }, {
406 desc: "oneof field set to empty message",
407 input: &pb2.Oneofs{
408 Union: &pb2.Oneofs_Msg{
409 Msg: &pb2.Nested{},
410 },
411 },
412 want: "msg: {}\n",
413 }, {
414 desc: "oneof field set to message",
415 input: &pb2.Oneofs{
416 Union: &pb2.Oneofs_Msg{
417 Msg: &pb2.Nested{
418 OptString: scalar.String("nested message"),
419 },
420 },
421 },
422 want: `msg: {
423 opt_string: "nested message"
424}
425`,
426 }, {
427 desc: "repeated not set",
428 input: &pb2.Repeats{},
429 want: "\n",
430 }, {
431 desc: "repeated set to empty slices",
432 input: &pb2.Repeats{
Herbie Ongcddf8192018-11-28 18:25:20 -0800433 RptBool: []bool{},
434 RptInt32: []int32{},
435 RptInt64: []int64{},
436 RptUint32: []uint32{},
437 RptUint64: []uint64{},
438 RptFloat: []float32{},
439 RptDouble: []float64{},
440 RptBytes: [][]byte{},
Herbie Ong800c9902018-12-06 15:28:53 -0800441 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800442 want: "\n",
443 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800444 desc: "repeated set to some values",
445 input: &pb2.Repeats{
Herbie Ongcddf8192018-11-28 18:25:20 -0800446 RptBool: []bool{true, false, true, true},
447 RptInt32: []int32{1, 6, 0, 0},
448 RptInt64: []int64{-64, 47},
449 RptUint32: []uint32{0xff, 0xffff},
450 RptUint64: []uint64{0xdeadbeef},
Herbie Ong84f09602019-01-17 19:31:47 -0800451 RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
Herbie Ongcddf8192018-11-28 18:25:20 -0800452 RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
453 RptString: []string{"hello", "世界"},
454 RptBytes: [][]byte{
455 []byte("hello"),
456 []byte("\xe4\xb8\x96\xe7\x95\x8c"),
457 },
Herbie Ong800c9902018-12-06 15:28:53 -0800458 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800459 want: `rpt_bool: true
460rpt_bool: false
461rpt_bool: true
462rpt_bool: true
463rpt_int32: 1
464rpt_int32: 6
465rpt_int32: 0
466rpt_int32: 0
467rpt_int64: -64
468rpt_int64: 47
469rpt_uint32: 255
470rpt_uint32: 65535
471rpt_uint64: 3735928559
Herbie Ong84f09602019-01-17 19:31:47 -0800472rpt_float: nan
473rpt_float: inf
474rpt_float: -inf
475rpt_float: 1.034
Herbie Ongcddf8192018-11-28 18:25:20 -0800476rpt_double: nan
477rpt_double: inf
478rpt_double: -inf
479rpt_double: 1.23e-308
480rpt_string: "hello"
481rpt_string: "世界"
482rpt_bytes: "hello"
483rpt_bytes: "世界"
484`,
485 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800486 desc: "repeated enum",
487 input: &pb2.Enums{
Herbie Ongcddf8192018-11-28 18:25:20 -0800488 RptEnum: []pb2.Enum{pb2.Enum_FIRST, 2, pb2.Enum_TENTH, 42},
Herbie Ongcddf8192018-11-28 18:25:20 -0800489 RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
Herbie Ong800c9902018-12-06 15:28:53 -0800490 },
491 want: `rpt_enum: FIRST
Herbie Ongcddf8192018-11-28 18:25:20 -0800492rpt_enum: SECOND
493rpt_enum: TENTH
494rpt_enum: 42
Herbie Ongcddf8192018-11-28 18:25:20 -0800495rpt_nested_enum: DOS
496rpt_nested_enum: 47
497rpt_nested_enum: DIEZ
498`,
499 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800500 desc: "repeated nested message set to empty",
501 input: &pb2.Nests{
Herbie Ongcddf8192018-11-28 18:25:20 -0800502 RptNested: []*pb2.Nested{},
503 Rptgroup: []*pb2.Nests_RptGroup{},
Herbie Ong800c9902018-12-06 15:28:53 -0800504 },
505 want: "\n",
Herbie Ongcddf8192018-11-28 18:25:20 -0800506 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800507 desc: "repeated nested messages",
508 input: &pb2.Nests{
Herbie Ongcddf8192018-11-28 18:25:20 -0800509 RptNested: []*pb2.Nested{
510 {
511 OptString: scalar.String("repeat nested one"),
512 },
513 {
514 OptString: scalar.String("repeat nested two"),
515 OptNested: &pb2.Nested{
516 OptString: scalar.String("inside repeat nested two"),
517 },
518 },
519 {},
520 },
Herbie Ong800c9902018-12-06 15:28:53 -0800521 },
522 want: `rpt_nested: {
Herbie Ongcddf8192018-11-28 18:25:20 -0800523 opt_string: "repeat nested one"
524}
525rpt_nested: {
526 opt_string: "repeat nested two"
527 opt_nested: {
528 opt_string: "inside repeat nested two"
529 }
530}
531rpt_nested: {}
532`,
533 }, {
Herbie Ongf5db2df2019-02-07 20:17:45 -0800534 desc: "repeated nested messages contains nil value",
535 input: &pb2.Nests{
536 RptNested: []*pb2.Nested{nil, {}},
537 },
538 want: `rpt_nested: {}
539rpt_nested: {}
540`,
541 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800542 desc: "repeated group fields",
543 input: &pb2.Nests{
Herbie Ongcddf8192018-11-28 18:25:20 -0800544 Rptgroup: []*pb2.Nests_RptGroup{
545 {
546 RptBool: []bool{true, false},
547 },
548 {},
549 },
Herbie Ong800c9902018-12-06 15:28:53 -0800550 },
Herbie Ongde7313b2019-01-14 19:26:50 -0800551 want: `RptGroup: {
Herbie Ongcddf8192018-11-28 18:25:20 -0800552 rpt_bool: true
553 rpt_bool: false
554}
Herbie Ongde7313b2019-01-14 19:26:50 -0800555RptGroup: {}
Herbie Ongcddf8192018-11-28 18:25:20 -0800556`,
557 }, {
Herbie Ongcddf8192018-11-28 18:25:20 -0800558 desc: "map fields empty",
Herbie Ong800c9902018-12-06 15:28:53 -0800559 input: &pb2.Maps{},
Herbie Ongcddf8192018-11-28 18:25:20 -0800560 want: "\n",
561 }, {
562 desc: "map fields set to empty maps",
Herbie Ong800c9902018-12-06 15:28:53 -0800563 input: &pb2.Maps{
Herbie Ongcddf8192018-11-28 18:25:20 -0800564 Int32ToStr: map[int32]string{},
565 Sfixed64ToBool: map[int64]bool{},
566 BoolToUint32: map[bool]uint32{},
567 Uint64ToEnum: map[uint64]pb2.Enum{},
568 StrToNested: map[string]*pb2.Nested{},
569 StrToOneofs: map[string]*pb2.Oneofs{},
Herbie Ong800c9902018-12-06 15:28:53 -0800570 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800571 want: "\n",
572 }, {
573 desc: "map fields 1",
Herbie Ong800c9902018-12-06 15:28:53 -0800574 input: &pb2.Maps{
Herbie Ongcddf8192018-11-28 18:25:20 -0800575 Int32ToStr: map[int32]string{
576 -101: "-101",
577 0xff: "0xff",
578 0: "zero",
579 },
580 Sfixed64ToBool: map[int64]bool{
581 0xcafe: true,
582 0: false,
583 },
584 BoolToUint32: map[bool]uint32{
585 true: 42,
586 false: 101,
587 },
Herbie Ong800c9902018-12-06 15:28:53 -0800588 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800589 want: `int32_to_str: {
590 key: -101
591 value: "-101"
592}
593int32_to_str: {
594 key: 0
595 value: "zero"
596}
597int32_to_str: {
598 key: 255
599 value: "0xff"
600}
601sfixed64_to_bool: {
602 key: 0
603 value: false
604}
605sfixed64_to_bool: {
606 key: 51966
607 value: true
608}
609bool_to_uint32: {
610 key: false
611 value: 101
612}
613bool_to_uint32: {
614 key: true
615 value: 42
616}
617`,
618 }, {
619 desc: "map fields 2",
Herbie Ong800c9902018-12-06 15:28:53 -0800620 input: &pb2.Maps{
Herbie Ongcddf8192018-11-28 18:25:20 -0800621 Uint64ToEnum: map[uint64]pb2.Enum{
622 1: pb2.Enum_FIRST,
623 2: pb2.Enum_SECOND,
624 10: pb2.Enum_TENTH,
625 },
Herbie Ong800c9902018-12-06 15:28:53 -0800626 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800627 want: `uint64_to_enum: {
628 key: 1
629 value: FIRST
630}
631uint64_to_enum: {
632 key: 2
633 value: SECOND
634}
635uint64_to_enum: {
636 key: 10
637 value: TENTH
638}
639`,
640 }, {
641 desc: "map fields 3",
Herbie Ong800c9902018-12-06 15:28:53 -0800642 input: &pb2.Maps{
Herbie Ongcddf8192018-11-28 18:25:20 -0800643 StrToNested: map[string]*pb2.Nested{
644 "nested_one": &pb2.Nested{
645 OptString: scalar.String("nested in a map"),
646 },
647 },
Herbie Ong800c9902018-12-06 15:28:53 -0800648 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800649 want: `str_to_nested: {
650 key: "nested_one"
651 value: {
652 opt_string: "nested in a map"
653 }
654}
655`,
656 }, {
657 desc: "map fields 4",
Herbie Ong800c9902018-12-06 15:28:53 -0800658 input: &pb2.Maps{
Herbie Ongcddf8192018-11-28 18:25:20 -0800659 StrToOneofs: map[string]*pb2.Oneofs{
660 "string": &pb2.Oneofs{
661 Union: &pb2.Oneofs_Str{
662 Str: "hello",
663 },
664 },
665 "nested": &pb2.Oneofs{
666 Union: &pb2.Oneofs_Msg{
667 Msg: &pb2.Nested{
668 OptString: scalar.String("nested oneof in map field value"),
669 },
670 },
671 },
672 },
Herbie Ong800c9902018-12-06 15:28:53 -0800673 },
Herbie Ongcddf8192018-11-28 18:25:20 -0800674 want: `str_to_oneofs: {
675 key: "nested"
676 value: {
677 msg: {
678 opt_string: "nested oneof in map field value"
679 }
680 }
681}
682str_to_oneofs: {
683 key: "string"
684 value: {
685 str: "hello"
686 }
687}
688`,
689 }, {
Herbie Ongf5db2df2019-02-07 20:17:45 -0800690 desc: "map field nil message value",
691 input: &pb2.Maps{
692 StrToNested: map[string]*pb2.Nested{
693 "nil": nil,
694 },
695 },
696 want: `str_to_nested: {
697 key: "nil"
698 value: {}
699}
700`,
701 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800702 desc: "proto2 required fields not set",
703 input: &pb2.Requireds{},
704 want: "\n",
705 wantErr: true,
Herbie Ongcddf8192018-11-28 18:25:20 -0800706 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800707 desc: "proto2 required fields partially set",
708 input: &pb2.Requireds{
709 ReqBool: scalar.Bool(false),
710 ReqFixed32: scalar.Uint32(47),
711 ReqSfixed64: scalar.Int64(0xbeefcafe),
712 ReqDouble: scalar.Float64(math.NaN()),
713 ReqString: scalar.String("hello"),
714 ReqEnum: pb2.Enum_FIRST.Enum(),
715 },
716 want: `req_bool: false
717req_fixed32: 47
718req_sfixed64: 3203386110
719req_double: nan
720req_string: "hello"
721req_enum: FIRST
722`,
723 wantErr: true,
724 }, {
725 desc: "proto2 required fields all set",
726 input: &pb2.Requireds{
727 ReqBool: scalar.Bool(false),
728 ReqFixed32: scalar.Uint32(0),
729 ReqFixed64: scalar.Uint64(0),
730 ReqSfixed32: scalar.Int32(0),
731 ReqSfixed64: scalar.Int64(0),
732 ReqFloat: scalar.Float32(0),
733 ReqDouble: scalar.Float64(0),
734 ReqString: scalar.String(""),
735 ReqEnum: pb2.Enum_UNKNOWN.Enum(),
736 ReqBytes: []byte{},
737 ReqNested: &pb2.Nested{},
738 },
739 want: `req_bool: false
740req_fixed32: 0
741req_fixed64: 0
742req_sfixed32: 0
743req_sfixed64: 0
744req_float: 0
745req_double: 0
746req_string: ""
747req_bytes: ""
748req_enum: UNKNOWN
749req_nested: {}
Herbie Ongcddf8192018-11-28 18:25:20 -0800750`,
751 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800752 desc: "indirect required field",
753 input: &pb2.IndirectRequired{
754 OptNested: &pb2.NestedWithRequired{},
755 },
756 want: "opt_nested: {}\n",
757 wantErr: true,
Herbie Ongcddf8192018-11-28 18:25:20 -0800758 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800759 desc: "indirect required field in empty repeated",
760 input: &pb2.IndirectRequired{
761 RptNested: []*pb2.NestedWithRequired{},
762 },
763 want: "\n",
Herbie Ongcddf8192018-11-28 18:25:20 -0800764 }, {
Herbie Ong800c9902018-12-06 15:28:53 -0800765 desc: "indirect required field in repeated",
766 input: &pb2.IndirectRequired{
767 RptNested: []*pb2.NestedWithRequired{
768 &pb2.NestedWithRequired{},
Herbie Ongcddf8192018-11-28 18:25:20 -0800769 },
Herbie Ong800c9902018-12-06 15:28:53 -0800770 },
771 want: "rpt_nested: {}\n",
772 wantErr: true,
773 }, {
774 desc: "indirect required field in empty map",
775 input: &pb2.IndirectRequired{
776 StrToNested: map[string]*pb2.NestedWithRequired{},
777 },
778 want: "\n",
779 }, {
780 desc: "indirect required field in map",
781 input: &pb2.IndirectRequired{
782 StrToNested: map[string]*pb2.NestedWithRequired{
783 "fail": &pb2.NestedWithRequired{},
784 },
785 },
786 want: `str_to_nested: {
787 key: "fail"
788 value: {}
Herbie Ongcddf8192018-11-28 18:25:20 -0800789}
790`,
Herbie Ong800c9902018-12-06 15:28:53 -0800791 wantErr: true,
Herbie Ong20a1d312018-12-11 21:08:58 -0800792 }, {
793 desc: "unknown varint and fixed types",
794 input: &pb2.Scalars{
795 OptString: scalar.String("this message contains unknown fields"),
796 XXX_unrecognized: pack.Message{
797 pack.Tag{101, pack.VarintType}, pack.Bool(true),
798 pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
799 pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
800 pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
801 }.Marshal(),
802 },
803 want: `opt_string: "this message contains unknown fields"
804101: 1
805102: 255
806103: 47
807104: 3735928559
808`,
809 }, {
810 desc: "unknown length-delimited",
811 input: &pb2.Scalars{
812 XXX_unrecognized: pack.Message{
813 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
814 pack.Tag{102, pack.BytesType}, pack.String("hello world"),
815 pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
816 }.Marshal(),
817 },
818 want: `101: "\x01\x00"
819102: "hello world"
820103: "世界"
821`,
822 }, {
823 desc: "unknown group type",
824 input: &pb2.Scalars{
825 XXX_unrecognized: pack.Message{
826 pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
827 pack.Tag{102, pack.StartGroupType},
828 pack.Tag{101, pack.VarintType}, pack.Bool(false),
829 pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
830 pack.Tag{102, pack.EndGroupType},
831 }.Marshal(),
832 },
833 want: `101: {}
834102: {
835 101: 0
836 102: "inside a group"
837}
838`,
839 }, {
840 desc: "unknown unpack repeated field",
841 input: &pb2.Scalars{
842 XXX_unrecognized: pack.Message{
843 pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
844 pack.Tag{102, pack.BytesType}, pack.String("hello"),
845 pack.Tag{101, pack.VarintType}, pack.Bool(true),
846 pack.Tag{102, pack.BytesType}, pack.String("世界"),
847 }.Marshal(),
848 },
849 want: `101: "\x01\x00\x01"
850101: 1
851102: "hello"
852102: "世界"
853`,
Herbie Ongcf253082018-12-17 17:13:07 -0800854 }, {
855 desc: "extensions of non-repeated fields",
856 input: func() proto.Message {
857 m := &pb2.Extensions{
858 OptString: scalar.String("non-extension field"),
859 OptBool: scalar.Bool(true),
860 OptInt32: scalar.Int32(42),
861 }
862 setExtension(m, pb2.E_OptExtBool, true)
863 setExtension(m, pb2.E_OptExtString, "extension field")
864 setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TENTH)
865 setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
866 OptString: scalar.String("nested in an extension"),
867 OptNested: &pb2.Nested{
868 OptString: scalar.String("another nested in an extension"),
869 },
870 })
871 return m
872 }(),
873 want: `opt_string: "non-extension field"
874opt_bool: true
875opt_int32: 42
876[pb2.opt_ext_bool]: true
877[pb2.opt_ext_enum]: TENTH
878[pb2.opt_ext_nested]: {
879 opt_string: "nested in an extension"
880 opt_nested: {
881 opt_string: "another nested in an extension"
882 }
883}
884[pb2.opt_ext_string]: "extension field"
885`,
886 }, {
887 desc: "registered extension but not set",
888 input: func() proto.Message {
889 m := &pb2.Extensions{}
890 setExtension(m, pb2.E_OptExtNested, nil)
891 return m
892 }(),
893 want: "\n",
894 }, {
895 desc: "extensions of repeated fields",
896 input: func() proto.Message {
897 m := &pb2.Extensions{}
898 setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TENTH, 101, pb2.Enum_FIRST})
899 setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
900 setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
901 &pb2.Nested{OptString: scalar.String("one")},
902 &pb2.Nested{OptString: scalar.String("two")},
903 &pb2.Nested{OptString: scalar.String("three")},
904 })
905 return m
906 }(),
907 want: `[pb2.rpt_ext_enum]: TENTH
908[pb2.rpt_ext_enum]: 101
909[pb2.rpt_ext_enum]: FIRST
910[pb2.rpt_ext_fixed32]: 42
911[pb2.rpt_ext_fixed32]: 47
912[pb2.rpt_ext_nested]: {
913 opt_string: "one"
914}
915[pb2.rpt_ext_nested]: {
916 opt_string: "two"
917}
918[pb2.rpt_ext_nested]: {
919 opt_string: "three"
920}
921`,
922 }, {
923 desc: "extensions of non-repeated fields in another message",
924 input: func() proto.Message {
925 m := &pb2.Extensions{}
926 setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
927 setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
928 setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TENTH)
929 setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
930 OptString: scalar.String("nested in an extension"),
931 OptNested: &pb2.Nested{
932 OptString: scalar.String("another nested in an extension"),
933 },
934 })
935 return m
936 }(),
937 want: `[pb2.ExtensionsContainer.opt_ext_bool]: true
938[pb2.ExtensionsContainer.opt_ext_enum]: TENTH
939[pb2.ExtensionsContainer.opt_ext_nested]: {
940 opt_string: "nested in an extension"
941 opt_nested: {
942 opt_string: "another nested in an extension"
943 }
944}
945[pb2.ExtensionsContainer.opt_ext_string]: "extension field"
946`,
947 }, {
948 desc: "extensions of repeated fields in another message",
949 input: func() proto.Message {
950 m := &pb2.Extensions{
951 OptString: scalar.String("non-extension field"),
952 OptBool: scalar.Bool(true),
953 OptInt32: scalar.Int32(42),
954 }
955 setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TENTH, 101, pb2.Enum_FIRST})
956 setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
957 setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
958 &pb2.Nested{OptString: scalar.String("one")},
959 &pb2.Nested{OptString: scalar.String("two")},
960 &pb2.Nested{OptString: scalar.String("three")},
961 })
962 return m
963 }(),
964 want: `opt_string: "non-extension field"
965opt_bool: true
966opt_int32: 42
967[pb2.ExtensionsContainer.rpt_ext_enum]: TENTH
968[pb2.ExtensionsContainer.rpt_ext_enum]: 101
969[pb2.ExtensionsContainer.rpt_ext_enum]: FIRST
970[pb2.ExtensionsContainer.rpt_ext_nested]: {
971 opt_string: "one"
972}
973[pb2.ExtensionsContainer.rpt_ext_nested]: {
974 opt_string: "two"
975}
976[pb2.ExtensionsContainer.rpt_ext_nested]: {
977 opt_string: "three"
978}
979[pb2.ExtensionsContainer.rpt_ext_string]: "hello"
980[pb2.ExtensionsContainer.rpt_ext_string]: "world"
981`,
Herbie Ong6470ea62019-01-07 18:56:57 -0800982 }, {
983 desc: "MessageSet",
984 input: func() proto.Message {
985 m := &pb2.MessageSet{}
986 setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
987 OptString: scalar.String("a messageset extension"),
988 })
989 setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
990 OptString: scalar.String("not a messageset extension"),
991 })
992 setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
993 OptString: scalar.String("just a regular extension"),
994 })
995 return m
996 }(),
997 want: `[pb2.MessageSetExtension]: {
998 opt_string: "a messageset extension"
999}
1000[pb2.MessageSetExtension.ext_nested]: {
1001 opt_string: "just a regular extension"
1002}
1003[pb2.MessageSetExtension.not_message_set_extension]: {
1004 opt_string: "not a messageset extension"
1005}
1006`,
1007 }, {
1008 desc: "not real MessageSet 1",
1009 input: func() proto.Message {
1010 m := &pb2.FakeMessageSet{}
1011 setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
1012 OptString: scalar.String("not a messageset extension"),
1013 })
1014 return m
1015 }(),
1016 want: `[pb2.FakeMessageSetExtension.message_set_extension]: {
1017 opt_string: "not a messageset extension"
1018}
1019`,
1020 }, {
1021 desc: "not real MessageSet 2",
1022 input: func() proto.Message {
1023 m := &pb2.MessageSet{}
1024 setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
1025 OptString: scalar.String("another not a messageset extension"),
1026 })
1027 return m
1028 }(),
1029 want: `[pb2.message_set_extension]: {
1030 opt_string: "another not a messageset extension"
1031}
1032`,
Herbie Ongf42b55f2019-01-02 15:46:07 -08001033 }, {
Herbie Ong66c365c2019-01-04 14:08:41 -08001034 desc: "Any message not expanded",
1035 mo: textpb.MarshalOptions{
1036 Resolver: preg.NewTypes(),
1037 },
Herbie Ongf42b55f2019-01-02 15:46:07 -08001038 input: func() proto.Message {
1039 m := &pb2.Nested{
1040 OptString: scalar.String("embedded inside Any"),
1041 OptNested: &pb2.Nested{
1042 OptString: scalar.String("inception"),
1043 },
1044 }
1045 // TODO: Switch to V2 marshal when ready.
1046 b, err := protoV1.Marshal(m)
1047 if err != nil {
1048 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1049 }
Herbie Ong66c365c2019-01-04 14:08:41 -08001050 return wrapAnyPB(&anypb.Any{
1051 TypeUrl: "pb2.Nested",
Herbie Ongf42b55f2019-01-02 15:46:07 -08001052 Value: b,
Herbie Ong66c365c2019-01-04 14:08:41 -08001053 })
Herbie Ongf42b55f2019-01-02 15:46:07 -08001054 }(),
1055 want: `type_url: "pb2.Nested"
1056value: "\n\x13embedded inside Any\x12\x0b\n\tinception"
1057`,
1058 }, {
Herbie Ong66c365c2019-01-04 14:08:41 -08001059 desc: "Any message expanded",
1060 mo: textpb.MarshalOptions{
1061 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1062 },
Herbie Ongf42b55f2019-01-02 15:46:07 -08001063 input: func() proto.Message {
1064 m := &pb2.Nested{
1065 OptString: scalar.String("embedded inside Any"),
1066 OptNested: &pb2.Nested{
1067 OptString: scalar.String("inception"),
1068 },
1069 }
1070 // TODO: Switch to V2 marshal when ready.
1071 b, err := protoV1.Marshal(m)
1072 if err != nil {
1073 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1074 }
Herbie Ong66c365c2019-01-04 14:08:41 -08001075 return wrapAnyPB(&anypb.Any{
1076 TypeUrl: "foo/pb2.Nested",
Herbie Ongf42b55f2019-01-02 15:46:07 -08001077 Value: b,
Herbie Ong66c365c2019-01-04 14:08:41 -08001078 })
Herbie Ongf42b55f2019-01-02 15:46:07 -08001079 }(),
Herbie Ong66c365c2019-01-04 14:08:41 -08001080 want: `[foo/pb2.Nested]: {
Herbie Ongf42b55f2019-01-02 15:46:07 -08001081 opt_string: "embedded inside Any"
1082 opt_nested: {
1083 opt_string: "inception"
1084 }
1085}
1086`,
1087 }, {
Herbie Ong66c365c2019-01-04 14:08:41 -08001088 desc: "Any message expanded with missing required error",
1089 mo: textpb.MarshalOptions{
1090 Resolver: preg.NewTypes((&pb2.PartialRequired{}).ProtoReflect().Type()),
1091 },
Herbie Ongf42b55f2019-01-02 15:46:07 -08001092 input: func() proto.Message {
1093 m := &pb2.PartialRequired{
1094 OptString: scalar.String("embedded inside Any"),
1095 }
1096 // TODO: Switch to V2 marshal when ready.
1097 b, err := protoV1.Marshal(m)
1098 // Ignore required not set error.
1099 if _, ok := err.(*protoV1.RequiredNotSetError); !ok {
1100 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1101 }
Herbie Ong66c365c2019-01-04 14:08:41 -08001102 return wrapAnyPB(&anypb.Any{
Herbie Ongf42b55f2019-01-02 15:46:07 -08001103 TypeUrl: string(m.ProtoReflect().Type().FullName()),
1104 Value: b,
Herbie Ong66c365c2019-01-04 14:08:41 -08001105 })
Herbie Ongf42b55f2019-01-02 15:46:07 -08001106 }(),
1107 want: `[pb2.PartialRequired]: {
1108 opt_string: "embedded inside Any"
1109}
1110`,
1111 wantErr: true,
1112 }, {
Herbie Ong66c365c2019-01-04 14:08:41 -08001113 desc: "Any message with invalid value",
1114 mo: textpb.MarshalOptions{
1115 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1116 },
1117 input: wrapAnyPB(&anypb.Any{
1118 TypeUrl: "foo/pb2.Nested",
1119 Value: dhex("80"),
1120 }),
1121 want: `type_url: "foo/pb2.Nested"
Herbie Onga94f78c2019-01-03 15:39:58 -08001122value: "\x80"
1123`,
Herbie Ongcddf8192018-11-28 18:25:20 -08001124 }}
1125
1126 for _, tt := range tests {
1127 tt := tt
1128 t.Run(tt.desc, func(t *testing.T) {
1129 t.Parallel()
Herbie Ongf42b55f2019-01-02 15:46:07 -08001130 b, err := tt.mo.Marshal(tt.input)
Herbie Ongcddf8192018-11-28 18:25:20 -08001131 if err != nil && !tt.wantErr {
Herbie Ongf42b55f2019-01-02 15:46:07 -08001132 t.Errorf("Marshal() returned error: %v\n", err)
Herbie Ongcddf8192018-11-28 18:25:20 -08001133 }
Herbie Ong800c9902018-12-06 15:28:53 -08001134 if err == nil && tt.wantErr {
Herbie Ongf42b55f2019-01-02 15:46:07 -08001135 t.Error("Marshal() got nil error, want error\n")
Herbie Ongcddf8192018-11-28 18:25:20 -08001136 }
Herbie Ong800c9902018-12-06 15:28:53 -08001137 got := string(b)
1138 if tt.want != "" && got != tt.want {
1139 t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
1140 if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
Herbie Ongcddf8192018-11-28 18:25:20 -08001141 t.Errorf("Marshal() diff -want +got\n%v\n", diff)
1142 }
1143 }
1144 })
1145 }
1146}