-
Notifications
You must be signed in to change notification settings - Fork 10
/
clop_merge_test.go
86 lines (65 loc) · 2.01 KB
/
clop_merge_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
77
78
79
80
81
82
83
84
85
86
package clop
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// 测试用
type Asr struct {
ThreadNum int `clop:"long" usage:"thread number" valid:"required"`
OpenVad bool `clop:"long" usage:"open vad" valid:"required"`
}
// 测试用
type Server struct {
ServerAddress string `clop:"long" usage:"Server address" valid:"required"`
Rate time.Duration `clop:"long" usage:"The speed at which audio is sent" valid:"required"`
}
// 1.测试多结构体串联的help功能
func Test_Merge_Help(t *testing.T) {
type TestA struct {
Aa string `clop:"long" usage:"a"`
Bb string `clop:"long" usage:"b"`
}
type TestB struct {
Cc string `clop:"long" usage:"c"`
Dd string `clop:"long" usage:"c"`
}
var out bytes.Buffer
p := New([]string{"-h"}).SetExit(false).SetOutput(&out)
p.Register(&TestA{})
p.Bind(&TestB{})
assert.Equal(t, 1, bytes.Count(out.Bytes(), []byte("aa")))
assert.Equal(t, 1, bytes.Count(out.Bytes(), []byte("bb")))
assert.Equal(t, 1, bytes.Count(out.Bytes(), []byte("cc")))
assert.Equal(t, 1, bytes.Count(out.Bytes(), []byte("dd")))
}
// 2.测试多结构体串联的parse功能
func Test_Merge_Parse(t *testing.T) {
p := New([]string{"--server-address", ":8080", "--rate", "1s", "--thread-num", "20", "--open-vad"}).SetExit(false)
asr := Asr{}
ser := Server{}
p.Register(&asr)
p.Bind(&ser)
assert.Equal(t, asr.ThreadNum, 20)
assert.True(t, asr.OpenVad)
assert.Equal(t, ser.ServerAddress, ":8080")
assert.Equal(t, ser.Rate, time.Second)
}
// 3.测试MustRegister接口
func Test_Merge_MustRegister(t *testing.T) {
assert.Panics(t, func() {
MustRegister(nil)
})
}
// 4.测试结构体串联的数据校验功能
func Test_Merge_Valid(t *testing.T) {
var usage bytes.Buffer
p := New([]string{"--server-address", ":8080", "--rate", "1s"}).SetExit(false).SetOutput(&usage)
asr := Asr{}
ser := Server{}
p.Register(&asr)
p.Bind(&ser)
//os.Stdout.Write(usage.Bytes())
assert.True(t, bytes.Contains(usage.Bytes(), []byte("must have a value")))
}