-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag_test.go
More file actions
228 lines (204 loc) · 5.65 KB
/
flag_test.go
File metadata and controls
228 lines (204 loc) · 5.65 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
package cli
import (
"testing"
"time"
)
// TestFlagCreation tests basic flag creation
func TestFlagCreation(t *testing.T) {
var verbose bool
var count int
var timeout time.Duration
cmd := Root("test").
Flag(&verbose, "verbose", "v", false, "Enable verbose").
Flag(&count, "count", "c", 5, "Number of items").
Flag(&timeout, "timeout", "t", 30*time.Second, "Timeout duration")
flags := cmd.flags.GetAll()
if len(flags) != 3 {
t.Fatalf("expected 3 flags, got %d", len(flags))
}
// Check verbose flag
vFlag := cmd.flags.GetFlag("verbose")
if vFlag == nil {
t.Fatal("verbose flag not found")
}
if vFlag.GetType() != "bool" {
t.Errorf("verbose flag type: expected bool, got %s", vFlag.GetType())
}
if vFlag.PrimaryName() != "verbose" {
t.Errorf("verbose primary name: expected 'verbose', got %s", vFlag.PrimaryName())
}
if vFlag.ShortName() != "v" {
t.Errorf("verbose short name: expected 'v', got %s", vFlag.ShortName())
}
// Check count flag
cFlag := cmd.flags.GetFlag("count")
if cFlag == nil {
t.Fatal("count flag not found")
}
if cFlag.GetType() != "int" {
t.Errorf("count flag type: expected int, got %s", cFlag.GetType())
}
// Check timeout flag
tFlag := cmd.flags.GetFlag("timeout")
if tFlag == nil {
t.Fatal("timeout flag not found")
}
if tFlag.GetType() != "duration" {
t.Errorf("timeout flag type: expected duration, got %s", tFlag.GetType())
}
}
// TestFlagParsing tests flag value parsing
func TestFlagParsing(t *testing.T) {
tests := []struct {
name string
args []string
checkFunc func(*testing.T, *Command)
}{
{
name: "boolean flag",
args: []string{"--verbose"},
checkFunc: func(t *testing.T, cmd *Command) {
var verbose bool
cmd.Flag(&verbose, "verbose", "", false, "")
cmd.ExecuteWithArgs([]string{"--verbose"})
flag := cmd.flags.GetFlag("verbose")
if val, _ := flag.GetValue().(bool); !val {
t.Error("verbose should be true")
}
},
},
{
name: "int flag",
args: []string{"--count=42"},
checkFunc: func(t *testing.T, cmd *Command) {
var count int
cmd.Flag(&count, "count", "", 0, "")
cmd.ExecuteWithArgs([]string{"--count=42"})
flag := cmd.flags.GetFlag("count")
if val, _ := flag.GetValue().(int); val != 42 {
t.Errorf("count should be 42, got %d", val)
}
},
},
{
name: "short flag",
args: []string{"-v"},
checkFunc: func(t *testing.T, cmd *Command) {
var verbose bool
cmd.Flag(&verbose, "verbose", "v", false, "")
cmd.ExecuteWithArgs([]string{"-v"})
flag := cmd.flags.GetFlag("verbose")
if val, _ := flag.GetValue().(bool); !val {
t.Error("verbose should be true")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := Root("test")
tt.checkFunc(t, cmd)
})
}
}
// TestFlagRequired tests required flag validation
func TestFlagRequired(t *testing.T) {
var apiKey string
cmd := Root("test").
FlagRequired(&apiKey, "api-key", "", "", "API key").
Action(func(ctx interface{}, c *Command) error {
return nil
})
// Should fail without required flag
err := cmd.ExecuteWithArgs([]string{})
if err == nil {
t.Error("expected error for missing required flag")
}
if _, ok := err.(*FlagError); !ok {
t.Errorf("expected FlagError, got %T", err)
}
}
// TestFlagHidden tests hidden flags
func TestFlagHidden(t *testing.T) {
var debug bool
cmd := Root("test").
FlagHidden(&debug, "debug", "", false, "Debug mode")
flag := cmd.flags.GetFlag("debug")
if flag == nil {
t.Fatal("debug flag not found")
}
if !flag.IsHidden() {
t.Error("debug flag should be hidden")
}
}
// TestArrayFlags tests array flag handling
func TestArrayFlags(t *testing.T) {
var tags []string
cmd := Root("test").
Flag(&tags, "tag", "t", []string{}, "Tags").
Action(func(ctx interface{}, c *Command, args ...string) error {
return nil
})
err := cmd.ExecuteWithArgs([]string{"--tag=v1", "--tag=v2", "--tag=v3"})
if err != nil {
t.Fatalf("execution failed: %v", err)
}
flag := cmd.flags.GetFlag("tag")
values := flag.GetValue().([]string)
if len(values) != 3 {
t.Fatalf("expected 3 tag values, got %d", len(values))
}
if values[0] != "v1" || values[1] != "v2" || values[2] != "v3" {
t.Errorf("unexpected tag values: %v", values)
}
}
// TestFlagByShortName tests finding flags by short name
func TestFlagByShortName(t *testing.T) {
var verbose bool
cmd := Root("test").
Flag(&verbose, "verbose", "v", false, "Verbose output")
flag := cmd.flags.GetFlag("v")
if flag == nil {
t.Fatal("should find flag by short name")
}
if flag.PrimaryName() != "verbose" {
t.Error("short name should map to primary flag")
}
}
// TestFlagUsage tests flag usage strings
func TestFlagUsage(t *testing.T) {
var verbose bool
cmd := Root("test").
Flag(&verbose, "verbose", "v", false, "Enable verbose output")
flag := cmd.flags.GetFlag("verbose")
if flag.GetUsage() != "Enable verbose output" {
t.Errorf("unexpected usage: %s", flag.GetUsage())
}
}
// TestFlagDefault tests flag default values
func TestFlagDefault(t *testing.T) {
var count int
cmd := Root("test").
Flag(&count, "count", "", 42, "Item count")
flag := cmd.flags.GetFlag("count")
defaultVal := flag.GetDefault().(int)
if defaultVal != 42 {
t.Errorf("expected default 42, got %d", defaultVal)
}
}
// TestFlagHasName tests the HasName method
func TestFlagHasName(t *testing.T) {
var verbose bool
cmd := Root("test").
Flag(&verbose, "verbose", "v", false, "Verbose")
flag := cmd.flags.GetFlag("verbose")
if !flag.HasName("verbose") {
t.Error("should have name 'verbose'")
}
if !flag.HasName("v") {
t.Error("should have name 'v'")
}
if flag.HasName("other") {
t.Error("should not have name 'other'")
}
}