This repository was archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_test.go
More file actions
320 lines (288 loc) · 7.67 KB
/
Copy pathcommand_test.go
File metadata and controls
320 lines (288 loc) · 7.67 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package cli
import (
"flag"
"fmt"
"strings"
"testing"
)
type RunTest struct {
Name string
Args []string
Command *Command
SubCommands []*Command
ExpectedCommandName string
}
type GetCurrentCommandTest struct {
Name string
Command *Command
SubCommands []*Command
Args []string
ExpectedCurrentCommand string
}
type SubCommandsTest struct {
Name string
Command *Command
ExpectedSubCommands int
}
type ParameterTest struct {
Name string
Command *Command
Parameter Parameter
Args []string
ExpectedParameterValue interface{}
}
type FindHelpTest struct {
Name string
Args []string
Command *Command
SubCommands []*Command
ExpectedStrings []string
}
func TestRun(t *testing.T) {
tests := []RunTest{
{
Name: "simple do command",
Args: []string{"do"},
Command: NewCommand("seo", "seo [command]", ""),
ExpectedCommandName: "do",
SubCommands: []*Command{
{
Name: "do",
Use: "seo do [flags]",
Parameters: []Parameter{
{
Name: "message",
Shortname: "m",
},
},
Flagset: flag.NewFlagSet("cli", flag.ContinueOnError),
},
},
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
var commandName string
for _, cmd := range test.SubCommands {
cmd.Do(func(cmd *Command) {
if cmd.Name == test.ExpectedCommandName {
commandName = cmd.Name
} else {
t.Error("called the wrong command's handler function")
}
})
test.Command.AddCommand(cmd)
}
cmd, err := test.Command.Parse(test.Args)
if err != nil {
t.Fatal(err)
}
cmd.Run()
if commandName == "" {
t.Error("no any command's handler function is called")
}
})
}
}
func TestFindHelp(t *testing.T) {
tests := []FindHelpTest{
{
Name: "a simple help test",
Args: []string{"help", "do"},
Command: NewCommand("seo", "seo [command]", "seo command"),
ExpectedStrings: []string{"message value"},
SubCommands: []*Command{
{
Name: "do",
Use: "seo do [flags]",
Parameters: []Parameter{
{
Name: "message",
Shortname: "m",
Description: "message value",
},
},
Flagset: flag.NewFlagSet("cli", flag.ContinueOnError),
},
},
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
for _, cmd := range test.SubCommands {
test.Command.AddCommand(cmd)
}
help, err := test.Command.FindHelp(test.Args)
if err != nil {
t.Fatal(err)
}
all := help.Usage + help.Parameters + help.Commands
for _, expectedString := range test.ExpectedStrings {
if !strings.Contains(all, expectedString) {
t.Errorf("help string is not contains any expected string")
}
}
})
}
}
func TestGetCurrentCommand(t *testing.T) {
tests := []GetCurrentCommandTest{
{
Name: "one simple command",
Args: []string{"do"},
Command: NewCommand("seo", "seo [command]", ""),
SubCommands: []*Command{NewCommand("do", "seo do [command]", "")},
ExpectedCurrentCommand: "do",
},
{
Name: "a subcommand",
Args: []string{"do", "concurrent"},
Command: NewCommand("seo", "seo [command]", ""),
SubCommands: []*Command{
{
Name: "do",
Use: "seo do [command]",
Commands: []*Command{
{
Name: "concurrent",
Use: "seo do concurrent",
},
},
},
},
ExpectedCurrentCommand: "concurrent",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
for _, cmd := range test.SubCommands {
test.Command.AddCommand(cmd)
}
command, _, err := test.Command.getCurrentCommand(test.Args)
if err != nil {
t.Fatal(err)
}
if command.Name != test.ExpectedCurrentCommand {
t.Errorf("command name %s is not equal to the expected command name %s", command.Name, test.ExpectedCurrentCommand)
}
})
}
}
func TestParameters(t *testing.T) {
tests := []ParameterTest{
{
Name: "a string parameter test",
Command: NewCommand("do", "seo do", ""),
Parameter: Parameter{
Name: "message",
Shortname: "m",
},
Args: []string{"--message", "seo do rocks"},
ExpectedParameterValue: "seo do rocks",
},
{
Name: "a boolean parameter test",
Command: NewCommand("do", "seo do", ""),
Parameter: Parameter{
Name: "verbose",
Shortname: "v",
},
Args: []string{"--verbose"},
ExpectedParameterValue: true,
},
{
Name: "an integer parameter test",
Command: NewCommand("do", "seo do", ""),
Parameter: Parameter{
Name: "count",
Shortname: "c",
},
Args: []string{"--count", "16"},
ExpectedParameterValue: 16,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
switch expectedValue := test.ExpectedParameterValue.(type) {
case string:
var value string
test.Command.AddStringParameter(&test.Parameter, &value, "")
err := test.Command.Flagset.Parse(test.Args)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value %s is not equal to the expected value %s", value, expectedValue)
}
value, err = test.Command.GetString(test.Parameter.Name)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value taken by GetString %s is not equal to the expected value %s", value, expectedValue)
}
case int:
var value int
test.Command.AddIntParameter(&test.Parameter, &value, 0)
err := test.Command.Flagset.Parse(test.Args)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value %d is not equal to the expected value %d", value, expectedValue)
}
value, err = test.Command.GetInt(test.Parameter.Name)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value taken by GetInt %d is not equal to the expected value %d", value, expectedValue)
}
case bool:
var value bool
test.Command.AddBoolParameter(&test.Parameter, &value, false)
err := test.Command.Flagset.Parse(test.Args)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value %t is not equal to the expected value %t", value, expectedValue)
}
value, err = test.Command.GetBool(test.Parameter.Name)
if err != nil {
t.Fatal(err)
}
if value != expectedValue {
t.Errorf("parsed value taken by GetBool %t is not equal to the expected value %t", value, expectedValue)
}
}
})
}
}
func TestSubCommands(t *testing.T) {
tests := []SubCommandsTest{
{
Name: "a test with 10 sub commands",
Command: NewCommand("do", "seo do", ""),
ExpectedSubCommands: 10,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
for i := 0; i < test.ExpectedSubCommands; i++ {
test.Command.AddCommand(
NewCommand(fmt.Sprintf("command_%d", i), "", ""),
)
}
if len(test.Command.Commands) != test.ExpectedSubCommands {
t.Errorf("command's subcommands length %d is not equal to the expected sub commands length %d", len(test.Command.Commands), test.ExpectedSubCommands)
}
for i := 0; i < test.ExpectedSubCommands; i++ {
_, exists := test.Command.FindCommand(fmt.Sprintf("command_%d", i))
if !exists {
t.Errorf("find command method can't find the added command")
}
}
})
}
}