-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclop_callback_test.go
65 lines (51 loc) · 1.38 KB
/
clop_callback_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
package clop
import (
"testing"
"github.com/stretchr/testify/assert"
)
type TestCallback struct {
Size int `clop:"short;long;callback=ParseSize" usage:"parse size"`
Max int `clop:"short;long"`
}
func (t *TestCallback) ParseSize(val string) {
t.Size = 1024 * 1024
}
// 指定函数名
func Test_Callback_SpecifyTheFunctionName(t *testing.T) {
got := TestCallback{}
need := TestCallback{Size: 1024 * 1024, Max: 10}
p := New([]string{"--size", "1MB", "--max", "10"}).SetExit(false)
err := p.Bind(&got)
assert.Equal(t, got, need)
assert.NoError(t, err)
}
type TestCallbackDefault struct {
Size int `clop:"short;long;callback" usage:"parse size"`
}
// 这是默认函数名
func (t *TestCallbackDefault) Parse(val string) {
t.Size = 1024 * 1024
}
// 指定函数名
func Test_Callback_Default(t *testing.T) {
got := TestCallback{}
need := TestCallback{Size: 1024 * 1024, Max: 10}
p := New([]string{"--size", "1MB", "--max", "10"}).SetExit(false)
err := p.Bind(&got)
assert.Equal(t, got, need)
assert.NoError(t, err)
}
type TestCallbackPanic struct {
Size int `clop:"short;long;callback" usage:"parse size"`
}
// 这是默认函数名
func (t *TestCallbackPanic) Parse() {
t.Size = 1024 * 1024
}
func Test_Callback_Panic(t *testing.T) {
got := TestCallbackPanic{}
assert.Panics(t, func() {
p := New([]string{"--size", "1MB", "--max", "10"}).SetExit(false)
p.Bind(&got)
})
}