-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_test.go
185 lines (148 loc) · 4.49 KB
/
command_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package commander
import (
"github.com/WindomZ/testify/assert"
"testing"
)
func TestCommand_CommandsString(t *testing.T) {
c := newCommand(true)
c.Command("test").
Version("0.0.1").
Description("this is a test cli.")
c.Command("add <x> <y>").
Description("this addition operation")
c.Command("sub <x> <y>").
Description("this subtraction operation").
Command("again").
Description("this subtraction operation again")
assert.Equal(t, c.CommandsString(""),
[]string{
"add this addition operation",
"sub this subtraction operation",
"sub again this subtraction operation again",
})
}
func TestCommand_HelpMessage(t *testing.T) {
c := newCommand(true)
assert.Equal(t, c.HelpMessage(), "")
c.Usage("cmd <x>", "this is description").
Option("-c, --config", "config description").
Option("-d, --drop", "drop description")
c.Command("cmd2").
Option("-a, --about", "cmd2 about description").
Option("-t, --test", "cmd2 test description")
c.Command("cmd3 [y]").
Option("-b=<kn>, --bold=<kn>", "cmd3 bold description").
Option("-c, --count", "cmd3 count description")
assert.Equal(t, c.UsagesString(),
[]string{
"cmd <x> [-c|--config] [-d|--drop]",
"cmd cmd2 [-a|--about] [-t|--test]",
"cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]",
})
opts := c.OptionsString()
assert.Equal(t, opts["-c|--config"], "-c --config config description")
assert.Equal(t, opts["-d|--drop"], "-d --drop drop description")
assert.Equal(t, opts["-a|--about"], "-a --about cmd2 about description")
assert.Equal(t, opts["-t|--test"], "-t --test cmd2 test description")
assert.Equal(t, opts["-b|--bold"], "-b=<kn> --bold=<kn>\n cmd3 bold description")
assert.Equal(t, opts["-c|--count"], "-c --count cmd3 count description")
assert.Equal(t, c.HelpMessage(),
` this is description
Usage:
cmd <x> [-c|--config] [-d|--drop]
cmd cmd2 [-a|--about] [-t|--test]
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
Options:
-a --about cmd2 about description
-b=<kn> --bold=<kn>
cmd3 bold description
-c --config config description
-c --count cmd3 count description
-d --drop drop description
-t --test cmd2 test description
`)
c.Annotation("Example", []string{
"cmd xxx -c",
"cmd cmd2 -a",
"cmd cmd3 y -b",
})
assert.Equal(t, c.HelpMessage(),
` this is description
Usage:
cmd <x> [-c|--config] [-d|--drop]
cmd cmd2 [-a|--about] [-t|--test]
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
Options:
-a --about cmd2 about description
-b=<kn> --bold=<kn>
cmd3 bold description
-c --config config description
-c --count cmd3 count description
-d --drop drop description
-t --test cmd2 test description
Example:
cmd xxx -c
cmd cmd2 -a
cmd cmd3 y -b
`)
c.Doc(` Usage:
cmd <x> [-c|--config] [-d|--drop]
cmd cmd2 [-a|--about] [-t|--test]
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
`)
assert.Equal(t, c.HelpMessage(), ` Usage:
cmd <x> [-c|--config] [-d|--drop]
cmd cmd2 [-a|--about] [-t|--test]
cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]
`)
}
func TestCommand_Aliases(t *testing.T) {
c := newCommand(true)
c.Command("cmd").
Aliases([]string{"c0", "cmd0"}).
Option("-a, --about", "cmd about description").
Option("-t, --test", "cmd test description")
assert.Equal(t, c.HelpMessage(), ` Usage:
(cmd|c0|cmd0) [-a|--about] [-t|--test]
Options:
-a --about cmd about description
-t --test cmd test description
`)
}
func TestCommand_ShowVersion(t *testing.T) {
c := newCommand(true)
c.Version("v0.0.1")
assert.Empty(t, c.ShowVersion())
}
func TestCommand_ShowHelpMessage(t *testing.T) {
c := newCommand(true)
c.Version("v0.0.1").Description("this is a test cli.")
assert.NotEmpty(t, c.ShowHelpMessage())
assert.Equal(t, c.HelpMessage(),
` this is a test cli.
`)
}
func TestCommand_ErrorHandling(t *testing.T) {
c := newCommand(true)
c.ErrorHandling(func(err error) {
assert.NoError(t, err)
})
c.Command("test").Action(func() error {
return newError("this is a test error")
})
if _, err := c.Parse([]string{"go-commander", "test"}); err != nil {
assert.Error(t, err)
}
}
func TestCommand_Command(t *testing.T) {
c := newCommand(true)
assert.Empty(t, c.Name())
c.Command("go-commander")
c.Version("0.0.1")
c.Command("test")
c.Line("[opt]").Command("cmd")
defer func() {
assert.NotEmpty(t, recover())
}()
c.Command("")
}