-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
297 lines (268 loc) · 6.65 KB
/
flag_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
package flaq
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
fixtures := []struct {
args []string
operands []string
expectError bool
foo bool
fooBar bool
bar string
car bool
count int
number int
floatNumber float64
duration time.Duration
}{
{
args: []string{"bar", "op1", "op2"},
operands: []string{"bar", "op1", "op2"},
},
{
args: []string{"op1", "--bar", "ok", "op2"},
operands: []string{"op1", "op2"},
bar: "ok",
},
{
args: []string{"--bar=ok", "op1", "op2"},
operands: []string{"op1", "op2"},
bar: "ok",
},
{
args: []string{"--bar=ok", "op1", "op2"},
operands: []string{"op1", "op2"},
bar: "ok",
},
{
args: []string{"-b", "ok", "op1", "op2"},
operands: []string{"op1", "op2"},
bar: "ok",
},
{
args: []string{"-bok", "op1", "op2"},
operands: []string{"op1", "op2"},
bar: "ok",
},
{
args: []string{"-f"},
foo: true,
},
{
args: []string{"-fc", "op1"},
operands: []string{"op1"},
foo: true,
car: true,
},
{
args: []string{"-b"},
expectError: true,
},
{
args: []string{"-fcb", "ok"},
foo: true,
bar: "ok",
car: true,
},
{
args: []string{"--foo", "--", "op1"},
operands: []string{"op1"},
foo: true,
},
{
args: []string{"--foo-b"},
expectError: true,
},
{
args: []string{"-cbbarval"},
car: true,
bar: "barval",
},
{
args: []string{"--count", "--count"},
count: 2,
},
{
args: []string{"--foo=ok"},
expectError: true,
},
{
args: []string{"--bar"},
expectError: true,
},
{
args: []string{"--number=50"},
number: 50,
},
{
args: []string{"--float-number=3.14159"},
floatNumber: 3.14159,
},
{
args: []string{"--duration=5m"},
duration: time.Duration(5 * time.Minute),
},
}
for _, f := range fixtures {
t.Run(fmt.Sprintf("%q", f.args), func(t *testing.T) {
var car, foo, fooBar bool
var bar string
var count, number int
var floatNumber float64
var duration time.Duration
flags := &FlagSet{}
flags.Bool(&foo, "foo", "f", "", false)
flags.Bool(&fooBar, "foo-bar", "", "", false)
flags.String(&bar, "bar", "b", "")
flags.Bool(&car, "car", "c", "", false)
flags.Count(&count, "count", "", "")
flags.Int(&number, "number", "", "")
flags.Float64(&floatNumber, "float-number", "", "")
flags.Duration(&duration, "duration", "", "")
err := flags.Parse(f.args)
if f.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, f.operands, flags.Args())
}
assert.Equal(t, f.foo, foo)
assert.Equal(t, f.fooBar, fooBar)
assert.Equal(t, f.bar, bar)
assert.Equal(t, f.car, car)
assert.Equal(t, f.count, count)
assert.Equal(t, f.number, number)
assert.Equal(t, f.floatNumber, floatNumber)
assert.Equal(t, f.duration, duration)
})
}
}
func TestParseOrderedArgs(t *testing.T) {
var bar string
flags := &FlagSet{
Ordered: true,
}
flags.String(&bar, "bar", "", "")
err := flags.Parse([]string{"op1", "--bar", "ok", "op2"})
require.NoError(t, err)
require.Equal(t, []string{"op1", "--bar", "ok", "op2"}, flags.Args())
require.Equal(t, "", bar)
}
func TestParseAbbreviation(t *testing.T) {
var bar, foo bool
flags := &FlagSet{
Abbreviations: true,
}
flags.Bool(&bar, "foo-bar", "", "", false)
flags.Bool(&foo, "foo-foo", "", "", false)
err := flags.Parse([]string{"--foo-b"})
require.NoError(t, err)
require.True(t, bar)
}
func TestParseAmbiguousAbbreviation(t *testing.T) {
var bar, foo bool
flags := &FlagSet{
Abbreviations: true,
}
flags.Bool(&bar, "foo-bar", "", "", false)
flags.Bool(&foo, "foo-foo", "", "", false)
err := flags.Parse([]string{"--foo"})
require.Error(t, err)
}
func TestParseStruct(t *testing.T) {
var opts = struct {
Name string `flaq:"-n, --name string name of the person to greet"`
Yell bool `flaq:" --yell whether to yell or not"`
Bool bool `flaq:" --bool bool whether to bool or not"`
Int int `flaq:" --int int whether to int or not"`
Float64 float64 `flaq:" --float64 float64 whether to float64 or not"`
Count int `flaq:"-c, --count count whether to count or not"`
Duration time.Duration `flaq:" --duration duration whether to duration or not"`
RandomJSONField string `json:"randomField"`
}{}
flags := &FlagSet{}
flags.Struct(&opts)
err := flags.Parse([]string{
"--name=ok",
"--yell",
"--bool=true",
"--duration=3s",
"--float64=3.14",
"--int=100",
"-ccc",
})
require.NoError(t, err)
require.Equal(t, "ok", opts.Name)
require.True(t, opts.Yell)
require.True(t, opts.Bool)
require.Equal(t, 3*time.Second, opts.Duration)
require.Equal(t, 100, opts.Int)
require.Equal(t, 3, opts.Count)
require.Equal(t, 3.14, opts.Float64)
}
func TestParseStructFieldTag(t *testing.T) {
fixtures := []struct {
tag string
short string
long string
fieldType string
description string
}{
{
tag: "--yell whether to yell or not",
long: "yell",
description: "whether to yell or not",
},
{
tag: "-y whether to yell or not",
short: "y",
description: "whether to yell or not",
},
{
tag: "-y, --yell bool whether to yell or not",
short: "y",
long: "yell",
fieldType: "bool",
description: "whether to yell or not",
},
{
tag: " --yell whether to yell or not",
long: "yell",
description: "whether to yell or not",
},
}
for _, fixture := range fixtures {
flag, fieldType := parseStructFieldTag(fixture.tag)
require.Equal(t, fixture.short, flag.Short)
require.Equal(t, fixture.long, flag.Long)
require.Equal(t, fixture.fieldType, fieldType)
require.Equal(t, fixture.description, flag.Description)
}
}
func TestUsage(t *testing.T) {
var bar bool
var foo string
flags := &FlagSet{}
flags.String(&foo, "foo", "", "Foo to the foo")
flags.Bool(&bar, "bar", "b", "Foo to the bar", false)
expectedUsage := `Usage: flaq.test [options]
Options
-b, --bar Foo to the bar
--foo <string> Foo to the foo
`
require.Equal(t, expectedUsage, flags.Usage())
}
func TestCustomUsage(t *testing.T) {
customUsage := "This is custooooom"
flags := &FlagSet{
UsageFunc: func(_ *FlagSet) string {
return customUsage
},
}
require.Equal(t, customUsage, flags.Usage())
}