forked from the729/lcs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
enum_test.go
76 lines (68 loc) · 1.36 KB
/
enum_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package lcs
import (
"testing"
"github.com/stretchr/testify/assert"
)
type Enum1 interface {
isEnum1()
}
type Enum1Opt0 struct {
Data uint32
}
type Enum1Opt1 bool
type Enum1Opt2 []byte
type Enum1Opt3 []Enum1
func (*Enum1Opt0) isEnum1() {}
func (Enum1Opt1) isEnum1() {}
func (Enum1Opt2) isEnum1() {}
func (Enum1Opt3) isEnum1() {}
func TestInterfaceAsEnum(t *testing.T) {
RegisterEnum((*Enum1)(nil),
(*Enum1Opt0)(nil),
Enum1Opt1(false),
Enum1Opt2(nil),
Enum1Opt3(nil),
)
e0 := Enum1(&Enum1Opt0{3})
e1 := Enum1(Enum1Opt1(true))
e2 := Enum1(Enum1Opt2([]byte{0x11, 0x22}))
e3 := Enum1(Enum1Opt3([]Enum1{e0, e1}))
runTest(t, []*testCase{
{
v: &e0,
b: hexMustDecode("00 03000000"),
name: "struct pointer",
},
{
v: &e1,
b: hexMustDecode("01 01"),
name: "bool",
},
{
v: &e2,
b: hexMustDecode("02 02 1122"),
name: "[]byte",
},
{
v: &e3,
b: hexMustDecode("03 02 00 03000000 01 01"),
name: "enum slice of self",
},
})
}
func TestRegisterNonInterfacePtrShouldPanic(t *testing.T) {
assert.Panics(t, func() {
RegisterEnum(uint32(0))
})
assert.Panics(t, func() {
RegisterEnum(Enum1(nil))
})
assert.Panics(t, func() {
RegisterEnum((*Enum1Opt0)(nil))
})
}
func TestRegisterWrongVariantShouldPanic(t *testing.T) {
assert.Panics(t, func() {
RegisterEnum((*Enum1)(nil), uint32(0))
})
}