Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 2f548ec

Browse files
committed
more tests on enum type
1 parent 04a41b6 commit 2f548ec

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

codec_test.go

+61-6
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ type Option0 struct {
402402
}
403403
type Option1 struct{}
404404
type Option2 bool
405+
type Option3 []byte
405406
type isOption interface {
406407
isOption()
407408
}
@@ -415,6 +416,7 @@ type OptionalOption struct {
415416
func (*Option0) isOption() {}
416417
func (Option1) isOption() {}
417418
func (Option2) isOption() {}
419+
func (Option3) isOption() {}
418420

419421
var optionEnumDef = []EnumVariant{
420422
{
@@ -432,6 +434,11 @@ var optionEnumDef = []EnumVariant{
432434
Value: 2,
433435
Template: Option2(false),
434436
},
437+
{
438+
Name: "option",
439+
Value: 3,
440+
Template: Option3(nil),
441+
},
435442
}
436443

437444
func (*Option) EnumTypes() []EnumVariant { return optionEnumDef }
@@ -460,6 +467,20 @@ func TestEnum(t *testing.T) {
460467
b: hexMustDecode("02 01"),
461468
name: "ptr to struct with real value as enum variant",
462469
},
470+
{
471+
v: &Option{
472+
Option: Option3([]byte{0x11, 0x22}),
473+
},
474+
b: hexMustDecode("03 02 11 22"),
475+
name: "ptr to struct with slice as enum variant",
476+
},
477+
{
478+
v: &Option{
479+
Option: Option3([]byte{}),
480+
},
481+
b: hexMustDecode("03 00"),
482+
name: "ptr to struct with nil slice as enum variant",
483+
},
463484
{
464485
v: Option{
465486
Option: Option1{},
@@ -481,11 +502,45 @@ func TestEnum(t *testing.T) {
481502
})
482503
}
483504

484-
type Wrapper struct {
505+
type OptionWrap struct {
506+
OptionStruct Option
507+
OptionStructPtr *Option
508+
}
509+
510+
func TestEnumInStruct(t *testing.T) {
511+
runTest(t, []*testCase{
512+
{
513+
v: &OptionWrap{
514+
OptionStruct: Option{
515+
Option: Option3([]byte{0x11, 0x22}),
516+
},
517+
OptionStructPtr: &Option{
518+
Option: Option1{},
519+
},
520+
},
521+
b: hexMustDecode("03 02 1122 01"),
522+
name: "enum struct in struct",
523+
},
524+
{
525+
v: &OptionWrap{
526+
OptionStructPtr: &Option{
527+
Option: Option3([]byte{0x11, 0x22}),
528+
},
529+
OptionStruct: Option{
530+
Option: Option1{},
531+
},
532+
},
533+
b: hexMustDecode("01 03 02 1122"),
534+
name: "enum struct in struct 2",
535+
},
536+
})
537+
}
538+
539+
type OptionSlice struct {
485540
Option []isOption `lcs:"enum=option"`
486541
}
487542

488-
func (*Wrapper) EnumTypes() []EnumVariant {
543+
func (*OptionSlice) EnumTypes() []EnumVariant {
489544
return []EnumVariant{
490545
{
491546
Name: "option",
@@ -508,7 +563,7 @@ func (*Wrapper) EnumTypes() []EnumVariant {
508563
func TestEnumSlice(t *testing.T) {
509564
runTest(t, []*testCase{
510565
{
511-
v: &Wrapper{
566+
v: &OptionSlice{
512567
Option: []isOption{
513568
&Option0{5},
514569
Option1{},
@@ -521,11 +576,11 @@ func TestEnumSlice(t *testing.T) {
521576
})
522577
}
523578

524-
type Wrapper2 struct {
579+
type OptionSlice2D struct {
525580
Option [][]isOption `lcs:"enum=option"`
526581
}
527582

528-
func (*Wrapper2) EnumTypes() []EnumVariant {
583+
func (*OptionSlice2D) EnumTypes() []EnumVariant {
529584
return []EnumVariant{
530585
{
531586
Name: "option",
@@ -548,7 +603,7 @@ func (*Wrapper2) EnumTypes() []EnumVariant {
548603
func TestEnum2DSlice(t *testing.T) {
549604
runTest(t, []*testCase{
550605
{
551-
v: &Wrapper2{
606+
v: &OptionSlice2D{
552607
Option: [][]isOption{
553608
{
554609
&Option0{5},

0 commit comments

Comments
 (0)