-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_test.go
More file actions
535 lines (483 loc) · 14.6 KB
/
coverage_test.go
File metadata and controls
535 lines (483 loc) · 14.6 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package cli
import (
"context"
"testing"
"time"
)
// TestCommandVisibility tests Show/Hidden methods
func TestCommandVisibility(t *testing.T) {
cmd := Cmd("test").Hidden()
if !cmd.IsHidden() {
t.Error("command should be hidden")
}
cmd.Show()
if cmd.IsHidden() {
t.Error("command should be visible after Show()")
}
}
// TestHelpConfiguration tests help flag customization
func TestHelpConfiguration(t *testing.T) {
cmd := Root("test")
// Test IsHelpEnabled (default true)
if !cmd.IsHelpEnabled() {
t.Error("help should be enabled by default")
}
// Test DisableHelp
cmd.DisableHelp()
if cmd.IsHelpEnabled() {
t.Error("help should be disabled after DisableHelp()")
}
// Test EnableHelp
cmd.EnableHelp()
if !cmd.IsHelpEnabled() {
t.Error("help should be enabled after EnableHelp()")
}
// Test SetHelpFlag
cmd.SetHelpFlag("assist", "a")
if cmd.helpFlag != "assist" || cmd.helpShort != "a" {
t.Errorf("expected helpFlag='assist', helpShort='a', got helpFlag='%s', helpShort='%s'", cmd.helpFlag, cmd.helpShort)
}
}
// TestFlagGetters tests flag getter methods
func TestFlagGetters(t *testing.T) {
var port int
var verbose bool
cmd := Root("test").
Flag(&port, "port", "p", 8080, "Port number").
Flag(&verbose, "verbose", "v", false, "Verbose output")
flags := cmd.flags.GetFlags()
if len(flags) != 2 {
t.Fatalf("expected 2 flags, got %d", len(flags))
}
// Test GetNames
portFlag := cmd.flags.GetFlag("port")
names := portFlag.GetNames()
if len(names) != 2 || names[0] != "port" || names[1] != "p" {
t.Errorf("expected names [port, p], got %v", names)
}
// Test GetType
flagType := portFlag.GetType()
if flagType != "int" {
t.Errorf("expected type 'int', got %q", flagType)
}
// Test PrimaryName
if portFlag.PrimaryName() != "port" {
t.Errorf("expected primary name 'port', got %q", portFlag.PrimaryName())
}
// Test ShortName
if portFlag.ShortName() != "p" {
t.Errorf("expected short name 'p', got %q", portFlag.ShortName())
}
// Test GetDefault
if portFlag.GetDefault() != 8080 {
t.Errorf("expected default 8080, got %v", portFlag.GetDefault())
}
// Test GetUsage
if portFlag.GetUsage() != "Port number" {
t.Errorf("expected usage 'Port number', got %q", portFlag.GetUsage())
}
// Test GetValue before setting
val := portFlag.GetValue()
if val != 8080 {
t.Errorf("expected value 8080, got %v", val)
}
}
// TestFlagShortNameHandling tests flags with no short name
func TestFlagShortNameHandling(t *testing.T) {
var config string
cmd := Root("test").Flag(&config, "config", "", "default.yaml", "Config file")
flag := cmd.flags.GetFlag("config")
if flag.ShortName() != "" {
t.Errorf("expected empty short name, got %q", flag.ShortName())
}
if flag.PrimaryName() != "config" {
t.Errorf("expected primary name 'config', got %q", flag.PrimaryName())
}
}
// TestCompletionShells tests all shell completion implementations
func TestCompletionShells(t *testing.T) {
root := Root("test").Description("Test app")
deploy := Cmd("deploy").Description("Deploy")
root.AddCommand(deploy)
tests := []struct {
name string
completion ShellCompletion
}{
{"bash", &BashCompletion{}},
{"zsh", &ZshCompletion{}},
{"fish", &FishCompletion{}},
{"powershell", &PowerShellCompletion{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test GetCompletions with empty args
words := tt.completion.GetCompletions(root, []string{})
found := false
for _, word := range words {
if word == "deploy" {
found = true
break
}
}
if !found {
t.Errorf("%s completion should include 'deploy' command", tt.name)
}
// Test GenerateScript (just verify it doesn't panic and returns non-empty)
script := tt.completion.GenerateScript(root)
if len(script) == 0 {
t.Errorf("%s GenerateScript returned empty script", tt.name)
}
// Test Register (verify it doesn't panic)
tt.completion.Register(root)
})
}
}
// TestStructBasedFlags tests Flags() method with struct tags
func TestStructBasedFlags(t *testing.T) {
type Config struct {
Host string `cli:"host,h" default:"localhost" usage:"Server host"`
Port int `cli:"port,p" default:"8080" usage:"Server port"`
Verbose bool `cli:"verbose,v" default:"false" usage:"Verbose output"`
Timeout time.Duration `cli:"timeout,t" default:"30s" usage:"Request timeout"`
}
var config Config
cmd := Root("test").Flags(&config)
// Verify flags were added
hostFlag := cmd.flags.GetFlag("host")
if hostFlag == nil {
t.Fatal("host flag not found")
}
if hostFlag.PrimaryName() != "host" {
t.Errorf("expected primary name 'host', got %q", hostFlag.PrimaryName())
}
if hostFlag.ShortName() != "h" {
t.Errorf("expected short name 'h', got %q", hostFlag.ShortName())
}
portFlag := cmd.flags.GetFlag("port")
if portFlag == nil {
t.Fatal("port flag not found")
}
verboseFlag := cmd.flags.GetFlag("verbose")
if verboseFlag == nil {
t.Fatal("verbose flag not found")
}
timeoutFlag := cmd.flags.GetFlag("timeout")
if timeoutFlag == nil {
t.Fatal("timeout flag not found")
}
// Test execution with struct flags
err := cmd.ExecuteWithArgs([]string{"--host=example.com", "--port=9000", "--verbose", "--timeout=1m"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
if config.Host != "example.com" {
t.Errorf("expected host 'example.com', got %q", config.Host)
}
if config.Port != 9000 {
t.Errorf("expected port 9000, got %d", config.Port)
}
if !config.Verbose {
t.Error("expected verbose to be true")
}
if config.Timeout != time.Minute {
t.Errorf("expected timeout 1m, got %v", config.Timeout)
}
}
// TestFlagTypeConversions tests all type conversions in setValue
func TestFlagTypeConversions(t *testing.T) {
tests := []struct {
name string
setup func() (*Command, interface{})
args []string
validate func(t *testing.T, val interface{})
}{
{
name: "uint conversion",
setup: func() (*Command, interface{}) {
var count uint
cmd := Root("test").
Flag(&count, "count", "c", uint(10), "Count").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &count
},
args: []string{"--count=42"},
validate: func(t *testing.T, val interface{}) {
if *val.(*uint) != 42 {
t.Errorf("expected 42, got %d", *val.(*uint))
}
},
},
{
name: "float32 conversion",
setup: func() (*Command, interface{}) {
var ratio float32
cmd := Root("test").
Flag(&ratio, "ratio", "r", float32(1.5), "Ratio").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &ratio
},
args: []string{"--ratio=3.14"},
validate: func(t *testing.T, val interface{}) {
expected := float32(3.14)
actual := *val.(*float32)
if actual < expected-0.01 || actual > expected+0.01 {
t.Errorf("expected ~3.14, got %f", actual)
}
},
},
{
name: "float64 conversion",
setup: func() (*Command, interface{}) {
var ratio float64
cmd := Root("test").
Flag(&ratio, "ratio", "r", 1.5, "Ratio").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &ratio
},
args: []string{"--ratio=2.71828"},
validate: func(t *testing.T, val interface{}) {
if *val.(*float64) != 2.71828 {
t.Errorf("expected 2.71828, got %f", *val.(*float64))
}
},
},
{
name: "int8 conversion",
setup: func() (*Command, interface{}) {
var level int8
cmd := Root("test").
Flag(&level, "level", "l", int8(1), "Level").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &level
},
args: []string{"--level=127"},
validate: func(t *testing.T, val interface{}) {
if *val.(*int8) != 127 {
t.Errorf("expected 127, got %d", *val.(*int8))
}
},
},
{
name: "int16 conversion",
setup: func() (*Command, interface{}) {
var port int16
cmd := Root("test").
Flag(&port, "port", "p", int16(8080), "Port").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &port
},
args: []string{"--port=32767"},
validate: func(t *testing.T, val interface{}) {
if *val.(*int16) != 32767 {
t.Errorf("expected 32767, got %d", *val.(*int16))
}
},
},
{
name: "int32 conversion",
setup: func() (*Command, interface{}) {
var count int32
cmd := Root("test").
Flag(&count, "count", "c", int32(100), "Count").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &count
},
args: []string{"--count=2147483647"},
validate: func(t *testing.T, val interface{}) {
if *val.(*int32) != 2147483647 {
t.Errorf("expected 2147483647, got %d", *val.(*int32))
}
},
},
{
name: "int64 conversion",
setup: func() (*Command, interface{}) {
var size int64
cmd := Root("test").
Flag(&size, "size", "s", int64(1000), "Size").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &size
},
args: []string{"--size=9223372036854775807"},
validate: func(t *testing.T, val interface{}) {
if *val.(*int64) != 9223372036854775807 {
t.Errorf("expected 9223372036854775807, got %d", *val.(*int64))
}
},
},
{
name: "uint8 conversion",
setup: func() (*Command, interface{}) {
var level uint8
cmd := Root("test").
Flag(&level, "level", "l", uint8(0), "Level").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &level
},
args: []string{"--level=255"},
validate: func(t *testing.T, val interface{}) {
if *val.(*uint8) != 255 {
t.Errorf("expected 255, got %d", *val.(*uint8))
}
},
},
{
name: "uint16 conversion",
setup: func() (*Command, interface{}) {
var port uint16
cmd := Root("test").
Flag(&port, "port", "p", uint16(8080), "Port").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &port
},
args: []string{"--port=65535"},
validate: func(t *testing.T, val interface{}) {
if *val.(*uint16) != 65535 {
t.Errorf("expected 65535, got %d", *val.(*uint16))
}
},
},
{
name: "uint32 conversion",
setup: func() (*Command, interface{}) {
var count uint32
cmd := Root("test").
Flag(&count, "count", "c", uint32(100), "Count").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &count
},
args: []string{"--count=4294967295"},
validate: func(t *testing.T, val interface{}) {
if *val.(*uint32) != 4294967295 {
t.Errorf("expected 4294967295, got %d", *val.(*uint32))
}
},
},
{
name: "uint64 conversion",
setup: func() (*Command, interface{}) {
var size uint64
cmd := Root("test").
Flag(&size, "size", "s", uint64(1000), "Size").
Action(func(ctx context.Context, cmd *Command) error { return nil })
return cmd, &size
},
args: []string{"--size=18446744073709551615"},
validate: func(t *testing.T, val interface{}) {
if *val.(*uint64) != 18446744073709551615 {
t.Errorf("expected 18446744073709551615, got %d", *val.(*uint64))
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, valPtr := tt.setup()
err := cmd.ExecuteWithArgs(tt.args)
if err != nil {
t.Fatalf("execution failed: %v", err)
}
tt.validate(t, valPtr)
})
}
}
// TestVariadicActionEdgeCases tests edge cases in variadic action handling
func TestVariadicActionEdgeCases(t *testing.T) {
t.Run("variadic with fixed args", func(t *testing.T) {
var result []string
cmd := Root("test").
Arg("first", "First arg", true).
Arg("rest", "Rest args", false).
Action(func(ctx context.Context, c *Command, first string, rest ...string) error {
result = append([]string{first}, rest...)
return nil
})
err := cmd.ExecuteWithArgs([]string{"one", "two", "three"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
expected := []string{"one", "two", "three"}
if len(result) != len(expected) {
t.Errorf("expected %d results, got %d", len(expected), len(result))
}
for i, v := range expected {
if result[i] != v {
t.Errorf("at index %d: expected %q, got %q", i, v, result[i])
}
}
})
t.Run("variadic with no optional args", func(t *testing.T) {
var result []string
cmd := Root("test").
Arg("first", "First arg", true).
Action(func(ctx context.Context, c *Command, first string, rest ...string) error {
result = append([]string{first}, rest...)
return nil
})
err := cmd.ExecuteWithArgs([]string{"only"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
if len(result) != 1 || result[0] != "only" {
t.Errorf("expected [only], got %v", result)
}
})
}
// TestInferType tests type inference for struct fields
func TestInferType(t *testing.T) {
type TestStruct struct {
StrField string `cli:"str-field,s" usage:"String field"`
IntField int `cli:"int-field,i" usage:"Int field"`
BoolField bool `cli:"bool-field,b" usage:"Bool field"`
FloatField float64 `cli:"float-field,f" usage:"Float field"`
DurField time.Duration `cli:"dur-field,d" usage:"Duration field"`
Uint64Field uint64 `cli:"uint64-field,u" usage:"Uint64 field"`
}
var ts TestStruct
cmd := Root("test")
// This will internally call inferType for each field
cmd.Flags(&ts)
// Verify flags were created with correct types
strFlag := cmd.flags.GetFlag("str-field")
if strFlag == nil {
t.Fatal("str-field flag not created")
}
if strFlag.GetType() != "string" {
t.Errorf("expected type 'string', got %q", strFlag.GetType())
}
intFlag := cmd.flags.GetFlag("int-field")
if intFlag == nil {
t.Fatal("int-field flag not created")
}
if intFlag.GetType() != "int" {
t.Errorf("expected type 'int', got %q", intFlag.GetType())
}
boolFlag := cmd.flags.GetFlag("bool-field")
if boolFlag == nil {
t.Fatal("bool-field flag not created")
}
if boolFlag.GetType() != "bool" {
t.Errorf("expected type 'bool', got %q", boolFlag.GetType())
}
floatFlag := cmd.flags.GetFlag("float-field")
if floatFlag == nil {
t.Fatal("float-field flag not created")
}
if floatFlag.GetType() != "float" {
t.Errorf("expected type 'float', got %q", floatFlag.GetType())
}
durFlag := cmd.flags.GetFlag("dur-field")
if durFlag == nil {
t.Fatal("dur-field flag not created")
}
if durFlag.GetType() != "duration" {
t.Errorf("expected type 'duration', got %q", durFlag.GetType())
}
uint64Flag := cmd.flags.GetFlag("uint64-field")
if uint64Flag == nil {
t.Fatal("uint64-field flag not created")
}
if uint64Flag.GetType() != "uint" {
t.Errorf("expected type 'uint', got %q", uint64Flag.GetType())
}
}