-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcmd_test.go
399 lines (363 loc) · 10 KB
/
subcmd_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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package subcmd
import (
"context"
"fmt"
"strconv"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestNoArgs(t *testing.T) {
err := Run(context.Background(), &command{}, []string{"y"})
if err != nil {
t.Error(err)
}
}
func TestOptionalArg(t *testing.T) {
cmd := new(command)
err := Run(context.Background(), cmd, []string{"z"})
if err != nil {
t.Fatal(err)
}
if cmd.zgotopt != 7 {
t.Errorf("got %d, want 7", cmd.zgotopt)
}
err = Run(context.Background(), cmd, []string{"z", "17"})
if err != nil {
t.Fatal(err)
}
if cmd.zgotopt != 17 {
t.Errorf("got %d, want 17", cmd.zgotopt)
}
}
func TestRun(t *testing.T) {
var (
ctx = context.Background()
args = []string{"x"}
cmd command
)
cmd.boolflagtest(ctx, t, args)
}
func (cmd *command) boolflagtest(ctx context.Context, t *testing.T, args []string) {
for _, boolopt := range []bool{false, true} {
cmd.boolopt = boolopt
t.Run(fmt.Sprintf("bool_%v", boolopt), func(t *testing.T) {
if boolopt {
args = append(args, "-boolopt")
}
cmd.intflagtest(ctx, t, args)
})
}
}
func (cmd *command) intflagtest(ctx context.Context, t *testing.T, args []string) {
for _, intopt := range []int{0, 1} {
cmd.intopt = intopt
t.Run(fmt.Sprintf("int_%d", intopt), func(t *testing.T) {
if intopt != 0 {
args = append(args, "-intopt", strconv.Itoa(intopt))
}
cmd.int64flagtest(ctx, t, args)
})
}
}
func (cmd *command) int64flagtest(ctx context.Context, t *testing.T, args []string) {
for _, int64opt := range []int64{0, 2} {
cmd.int64opt = int64opt
t.Run(fmt.Sprintf("int64_%d", int64opt), func(t *testing.T) {
if int64opt != 0 {
args = append(args, "-int64opt", strconv.FormatInt(int64opt, 10))
}
cmd.uintflagtest(ctx, t, args)
})
}
}
func (cmd *command) uintflagtest(ctx context.Context, t *testing.T, args []string) {
for _, uintopt := range []uint{0, 3} {
cmd.uintopt = uintopt
t.Run(fmt.Sprintf("uint_%d", uintopt), func(t *testing.T) {
if uintopt != 0 {
args = append(args, "-uintopt", strconv.FormatUint(uint64(uintopt), 10))
}
cmd.uint64flagtest(ctx, t, args)
})
}
}
func (cmd *command) uint64flagtest(ctx context.Context, t *testing.T, args []string) {
for _, uint64opt := range []uint64{0, 4} {
cmd.uint64opt = uint64opt
t.Run(fmt.Sprintf("uint64_%d", uint64opt), func(t *testing.T) {
if uint64opt != 0 {
args = append(args, "-uint64opt", strconv.FormatUint(uint64opt, 10))
}
cmd.strflagtest(ctx, t, args)
})
}
}
func (cmd *command) strflagtest(ctx context.Context, t *testing.T, args []string) {
for _, stropt := range []string{"", "foo"} {
cmd.stropt = stropt
t.Run(fmt.Sprintf("str%s", stropt), func(t *testing.T) {
if stropt != "" {
args = append(args, "-stropt", stropt)
}
cmd.float64flagtest(ctx, t, args)
})
}
}
func (cmd *command) float64flagtest(ctx context.Context, t *testing.T, args []string) {
for _, float64opt := range []float64{0, 3.14} {
cmd.float64opt = float64opt
t.Run(fmt.Sprintf("float%f", float64opt), func(t *testing.T) {
if float64opt != 0 {
args = append(args, "-float64opt", strconv.FormatFloat(float64opt, 'f', -1, 64))
}
cmd.durflagtest(ctx, t, args)
})
}
}
func (cmd *command) durflagtest(ctx context.Context, t *testing.T, args []string) {
for _, duropt := range []time.Duration{0, time.Minute} {
cmd.duropt = duropt
t.Run(fmt.Sprintf("dur%s", duropt), func(t *testing.T) {
if duropt != 0 {
args = append(args, "-duropt", duropt.String())
}
cmd.boolpostest(ctx, t, args)
})
}
}
func (cmd *command) boolpostest(ctx context.Context, t *testing.T, args []string) {
cmd.boolpos = true
t.Run(fmt.Sprintf("boolpos_%v", cmd.boolpos), func(t *testing.T) {
args = append(args, "true")
cmd.intpostest(ctx, t, args)
})
}
func (cmd *command) intpostest(ctx context.Context, t *testing.T, args []string) {
cmd.intpos = 412
t.Run(fmt.Sprintf("intpos_%d", cmd.intpos), func(t *testing.T) {
args = append(args, "412")
cmd.int64postest(ctx, t, args)
})
}
func (cmd *command) int64postest(ctx context.Context, t *testing.T, args []string) {
cmd.int64pos = 733
t.Run(fmt.Sprintf("int64pos_%d", cmd.int64pos), func(t *testing.T) {
args = append(args, "733")
cmd.uintpostest(ctx, t, args)
})
}
func (cmd *command) uintpostest(ctx context.Context, t *testing.T, args []string) {
cmd.uintpos = 31178
t.Run(fmt.Sprintf("uintpos_%d", cmd.uintpos), func(t *testing.T) {
args = append(args, "31178")
cmd.uint64postest(ctx, t, args)
})
}
func (cmd *command) uint64postest(ctx context.Context, t *testing.T, args []string) {
cmd.uint64pos = 2134
t.Run(fmt.Sprintf("uint64pos_%d", cmd.uint64pos), func(t *testing.T) {
args = append(args, "2134")
cmd.strpostest(ctx, t, args)
})
}
func (cmd *command) strpostest(ctx context.Context, t *testing.T, args []string) {
cmd.strpos = "plugh"
t.Run(fmt.Sprintf("strpos%s", cmd.strpos), func(t *testing.T) {
args = append(args, "plugh")
cmd.float64postest(ctx, t, args)
})
}
func (cmd *command) float64postest(ctx context.Context, t *testing.T, args []string) {
cmd.float64pos = 2.718
t.Run(fmt.Sprintf("floatpos%f", cmd.float64pos), func(t *testing.T) {
args = append(args, "2.718")
cmd.durpostest(ctx, t, args)
})
}
func (cmd *command) durpostest(ctx context.Context, t *testing.T, args []string) {
cmd.durpos = 7 * time.Second
t.Run(fmt.Sprintf("durpos%s", cmd.durpos), func(t *testing.T) {
args = append(args, "7s")
cmd.t = t
err := Run(ctx, cmd, args)
if err != nil {
t.Fatal(err)
}
})
}
type command struct {
t *testing.T
boolopt bool
intopt int
int64opt int64
uintopt uint
uint64opt uint64
stropt string
float64opt float64
duropt time.Duration
boolpos bool
intpos int
int64pos int64
uintpos uint
uint64pos uint64
strpos string
float64pos float64
durpos time.Duration
zgotopt int
}
func (cmd *command) Subcmds() Map {
return Commands(
"x", cmd.xcmd, "x", Params(
"-boolopt", Bool, false, "bool flag",
"-intopt", Int, 0, "int flag",
"-int64opt", Int64, 0, "int64 flag",
"-uintopt", Uint, 0, "uint flag",
"-uint64opt", Uint64, 0, "uint64 flag",
"-stropt", String, "", "str flag",
"-float64opt", Float64, 0, "float64 flag",
"-duropt", Duration, 0, "dur flag",
"boolpos", Bool, false, "bool pos",
"intpos", Int, 0, "int pos",
"int64pos", Int64, 0, "int64 pos",
"uintpos", Uint, 0, "uint pos",
"uint64pos", Uint64, 0, "uint64 pos",
"strpos", String, "", "str pos",
"float64pos", Float64, 0, "float64 pos",
"durpos", Duration, 0, "dur pos",
),
"y", cmd.ycmd, "y", nil,
"z", cmd.zcmd, "z", Params(
"opt?", Int, 7, "optional int",
),
)
}
func (cmd *command) xcmd(
_ context.Context,
boolopt bool,
intopt int,
int64opt int64,
uintopt uint,
uint64opt uint64,
stropt string,
float64opt float64,
duropt time.Duration,
boolpos bool,
intpos int,
int64pos int64,
uintpos uint,
uint64pos uint64,
strpos string,
float64pos float64,
durpos time.Duration,
_ []string,
) error {
if boolopt != cmd.boolopt {
cmd.t.Errorf("boolopt: got %v, want %v", boolopt, cmd.boolopt)
}
if intopt != cmd.intopt {
cmd.t.Errorf("intopt: got %d, want %d", intopt, cmd.intopt)
}
if int64opt != cmd.int64opt {
cmd.t.Errorf("int64opt: got %d, want %d", int64opt, cmd.int64opt)
}
if uintopt != cmd.uintopt {
cmd.t.Errorf("uintopt: got %d, want %d", uintopt, cmd.uintopt)
}
if uint64opt != cmd.uint64opt {
cmd.t.Errorf("uint64opt: got %d, want %d", uint64opt, cmd.uint64opt)
}
if stropt != cmd.stropt {
cmd.t.Errorf(`stropt: got "%s", want "%s"`, stropt, cmd.stropt)
}
if float64opt != cmd.float64opt {
cmd.t.Errorf("float64opt: got %f, want %f", float64opt, cmd.float64opt)
}
if duropt != cmd.duropt {
cmd.t.Errorf("duropt: got %s, want %s", duropt, cmd.duropt)
}
if boolpos != cmd.boolpos {
cmd.t.Errorf("boolpos: got %v, want %v", boolpos, cmd.boolpos)
}
if intpos != cmd.intpos {
cmd.t.Errorf("intpos: got %d, want %d", intpos, cmd.intpos)
}
if int64pos != cmd.int64pos {
cmd.t.Errorf("int64pos: got %d, want %d", int64pos, cmd.int64pos)
}
if uintpos != cmd.uintpos {
cmd.t.Errorf("uintpos: got %d, want %d", uintpos, cmd.uintpos)
}
if uint64pos != cmd.uint64pos {
cmd.t.Errorf("uint64pos: got %d, want %d", uint64pos, cmd.uint64pos)
}
if strpos != cmd.strpos {
cmd.t.Errorf(`strpos: got "%s", want "%s"`, strpos, cmd.strpos)
}
if float64pos != cmd.float64pos {
cmd.t.Errorf("float64pos: got %f, want %f", float64pos, cmd.float64pos)
}
if durpos != cmd.durpos {
cmd.t.Errorf("durpos: got %s, want %s", durpos, cmd.durpos)
}
return nil
}
func TestCommands(t *testing.T) {
baz := Subcmd{
F: bazcmd,
Desc: "baz command",
}
got := Commands(
"foo", foocmd, "foo command", Params(
"a", Bool, false, "flag a",
"b", Int, 0, "flag b",
),
"bar", barcmd, "bar command", nil,
"baz", baz,
)
want := Map{
"foo": Subcmd{
F: foocmd,
Params: []Param{{
Name: "a",
Type: Bool,
Default: false,
Doc: "flag a",
}, {
Name: "b",
Type: Int,
Default: 0,
Doc: "flag b",
}},
Desc: "foo command",
},
"bar": Subcmd{
F: barcmd,
Desc: "bar command",
},
"baz": Subcmd{
F: bazcmd,
Desc: "baz command",
},
}
if diff := cmp.Diff(want, got, fooopt, baropt); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func (*command) ycmd(_ context.Context, _ []string) error { return nil }
func (cmd *command) zcmd(_ context.Context, opt int, _ []string) error {
cmd.zgotopt = opt
return nil
}
// The following is needed because cmp.Diff
// (and reflect.DeepEqual for that matter)
// do not work as expected with function pointers.
func foocmd(context.Context, bool, int, []string) {}
func barcmd(context.Context, []string) {}
func bazcmd(context.Context, []string) {}
var (
foocomparer = func(_, _ func(context.Context, bool, int, []string)) bool { return true }
barcomparer = func(_, _ func(context.Context, []string)) bool { return true }
fooopt = cmp.FilterValues(foocomparer, cmp.Comparer(foocomparer))
baropt = cmp.FilterValues(barcomparer, cmp.Comparer(barcomparer))
)