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

Commit

Permalink
update tests and example of enum type without value.
Browse files Browse the repository at this point in the history
  • Loading branch information
the729 committed Sep 24, 2019
1 parent 0347833 commit 7c98b09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ type isOption interface {
type Option0 struct {
Data uint32
}
type Option1 struct {
Data uint64
}
type Option1 struct {} // Empty enum variant
type Option2 bool
// Variants should implement isOption
func (*Option0) isOption() {}
func (*Option1) isOption() {}
func (Option1) isOption() {}
func (Option2) isOption() {}

// MyStruct contains the enum type Option
Expand All @@ -110,8 +108,8 @@ type MyStruct struct {
List2D [][]isOption `lcs:"enum:dummy"` // support multi-dim slices
}

// EnumTypes implement lcs.EnumTypeUser. It returns the ingredients used for
// all enum types.
// EnumTypes implement lcs.EnumTypeUser. It returns all the ingredients that can be
// used for all enum fields in the receiver struct type.
func (*Option) EnumTypes() []EnumVariant {
return []EnumVariant{
{
Expand All @@ -122,7 +120,7 @@ func (*Option) EnumTypes() []EnumVariant {
{
Name: "dummy",
Value: 1,
Template: (*Option1)(nil),
Template: Option1{},
},
{
Name: "dummy",
Expand Down
35 changes: 19 additions & 16 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func TestBasicStruct(t *testing.T) {
}

runTest(t, []*testCase{
{
v: struct{}{},
b: nil,
name: "empty struct",
},
{
v: MyStruct{
Boolean: true,
Expand Down Expand Up @@ -338,9 +343,7 @@ func TestMap(t *testing.T) {
type Option0 struct {
Data uint32
}
type Option1 struct {
Data uint64
}
type Option1 struct{}
type Option2 bool
type isOption interface {
isOption()
Expand All @@ -350,7 +353,7 @@ type Option struct {
}

func (*Option0) isOption() {}
func (*Option1) isOption() {}
func (Option1) isOption() {}
func (Option2) isOption() {}
func (*Option) EnumTypes() []EnumVariant {
return []EnumVariant{
Expand All @@ -362,7 +365,7 @@ func (*Option) EnumTypes() []EnumVariant {
{
Name: "option",
Value: 1,
Template: (*Option1)(nil),
Template: Option1{},
},
{
Name: "option",
Expand All @@ -379,28 +382,28 @@ func TestEnum(t *testing.T) {
Option: &Option0{5},
},
b: hexMustDecode("0000 0000 0500 0000"),
name: "ptr to struct with ptr interface 0",
name: "ptr to struct with ptr enum variant",
},
{
v: &Option{
Option: &Option1{6},
Option: Option1{},
},
b: hexMustDecode("0100 0000 0600 0000 0000 0000"),
name: "ptr to struct with ptr interface 1",
b: hexMustDecode("0100 0000"),
name: "ptr to struct with non-ptr empty variant",
},
{
v: &Option{
Option: Option2(true),
},
b: hexMustDecode("0200 0000 01"),
name: "ptr to struct with real value as interface",
name: "ptr to struct with real value as enum variant",
},
{
v: Option{
Option: &Option1{6},
Option: Option1{},
},
b: hexMustDecode("0100 0000 0600 0000 0000 0000"),
name: "struct with enum with real struct interface",
b: hexMustDecode("0100 0000"),
name: "non-ptr struct with variant",
},
})
}
Expand All @@ -419,7 +422,7 @@ func (*Wrapper) EnumTypes() []EnumVariant {
{
Name: "option",
Value: 1,
Template: (*Option1)(nil),
Template: Option1{},
},
{
Name: "option",
Expand All @@ -435,11 +438,11 @@ func TestEnumSlice(t *testing.T) {
v: &Wrapper{
Option: []isOption{
&Option0{5},
&Option1{6},
Option1{},
Option2(true),
},
},
b: hexMustDecode("03000000 00000000 05000000 01000000 0600000000000000 02000000 01"),
b: hexMustDecode("03000000 00000000 05000000 01000000 02000000 01"),
name: "enum slice",
},
})
Expand Down

0 comments on commit 7c98b09

Please sign in to comment.