-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
225 lines (223 loc) · 5.38 KB
/
builder_test.go
File metadata and controls
225 lines (223 loc) · 5.38 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
package cli
import (
"reflect"
"testing"
)
func TestBuilder(t *testing.T) {
for ttIdx, tt := range []struct {
name string
builds []func()
expPanicVals []any
}{
{
name: "control test clean ones",
builds: []func(){
func() { NewCmd("a") },
func() { c := NewCmd("a"); c.Parse() },
},
expPanicVals: []any{nil, nil},
}, {
name: "mixing positional args and subcommands",
builds: []func(){
func() {
NewCmd("root").
Subcmd(NewCmd("sc")).
Arg(NewArg("a1"))
},
func() {
NewCmd("root").
Arg(NewArg("a1")).
Subcmd(NewCmd("sc"))
},
},
expPanicVals: []any{
errMixingPosArgsAndSubcmds,
errMixingPosArgsAndSubcmds,
},
}, {
name: "empty command name",
builds: []func(){
func() { NewCmd("") },
func() { NewCmd("root").Subcmd(NewCmd("")) },
},
expPanicVals: []any{
errEmptyCmdName,
errEmptyCmdName,
},
}, {
name: "command name with whitespace",
builds: []func(){
func() { NewCmd(" ") },
func() { NewCmd("a b") },
func() { NewCmd("ab").Subcmd(NewCmd("c\td")) },
},
expPanicVals: []any{
"invalid command name ' ': cannot contain whitespace",
"invalid command name 'a b': cannot contain whitespace",
"invalid command name 'c\td': cannot contain whitespace",
},
}, {
name: "empty input ids",
builds: []func(){
func() { NewCmd("root").Opt(NewOpt("")) },
func() { NewCmd("root").Arg(NewArg("")) },
},
expPanicVals: []any{
errEmptyInputID,
errEmptyInputID,
},
}, {
name: "empty option names",
builds: []func(){func() { NewCmd("root").Opt(NewArg("a1")) }},
expPanicVals: []any{errEmptyOptNames},
}, {
name: "duplicate option ids",
builds: []func(){
func() {
NewCmd("root").
Opt(NewOpt("o1")).
Opt(NewOpt("o2")).
Opt(NewOpt("o3")).
Subcmd(NewCmd("one").
Opt(NewOpt("o1")).
Opt(NewOpt("o2")).
Opt(NewOpt("o3")).
Opt(NewOpt("o1")).
Opt(NewOpt("o5"))).
ParseOrExit()
},
func() {
NewCmd("root").
Arg(NewArg("a1")).
Arg(NewArg("a1")).
Arg(NewArg("a2")).
Arg(NewArg("a3")).
ParseOrExit()
},
},
expPanicVals: []any{
"command 'root one' contains duplicate input ids 'o1'",
"command 'root' contains duplicate input ids 'a1'",
},
}, {
name: "duplicate option short names",
builds: []func(){
func() {
NewCmd("root").
Opt(NewOpt("aa").Short('a')).
Opt(NewOpt("bb").Short('b')).
ParseTheseOrExit()
},
func() {
NewCmd("root").
Opt(NewOpt("aa").Short('a')).
Opt(NewOpt("bb").Short('a')).
ParseOrExit()
},
func() {
NewCmd("root").
Opt(NewOpt("aa").ShortOnly('b')).
Opt(NewOpt("bb").Short('b')).
ParseOrExit()
},
},
expPanicVals: []any{
nil,
"command 'root' contains duplicate option short name 'a'",
"command 'root' contains duplicate option short name 'b'",
},
}, {
name: "duplicate option long names",
builds: []func(){
func() {
NewCmd("root").
Opt(NewOpt("aa").Long("aaa")).
Opt(NewOpt("bb").Long("bbb")).
Opt(NewOpt("cc").Long("aaa")).
ParseOrExit()
},
},
expPanicVals: []any{
"command 'root' contains duplicate option long name 'aaa'",
},
}, {
name: "options as positional arguments",
builds: []func(){
func() { NewCmd("root").Arg(NewOpt("o1")) },
func() { NewCmd("root").Subcmd(NewCmd("sc").Arg(NewOpt("o1"))) },
},
expPanicVals: []any{
errOptAsPosArg,
errOptAsPosArg,
},
}, {
name: "required positional arguments coming after optional ones",
builds: []func(){
func() {
NewCmd("root").
Arg(NewArg("a")).
Arg(NewArg("b").Required())
},
func() {
NewCmd("root").
Subcmd(NewCmd("subcmd").
Arg(NewArg("a")).
Arg(NewArg("b").Required()))
},
},
expPanicVals: []any{
errReqArgAfterOptional,
errReqArgAfterOptional,
},
}, {
name: "duplicate subcommand names",
builds: []func(){
func() {
NewCmd("root").
Subcmd(NewCmd("bb")).
Subcmd(NewCmd("bb")).
ParseOrExit()
},
func() {
NewCmd("root").
Subcmd(NewCmd("subcmd").
Subcmd(NewCmd("aa")).
Subcmd(NewCmd("bb")).
Subcmd(NewCmd("aa")).
Subcmd(NewCmd("cc"))).
ParseOrExit()
},
},
expPanicVals: []any{
"command 'root' contains duplicate subcommand name 'bb'",
"command 'root subcmd' contains duplicate subcommand name 'aa'",
},
}, {
name: "using NewVersionOpt with neither a short or long name",
builds: []func(){
func() { NewCmd("root").Opt(NewVersionOpt(0, "", VersionOptConfig{})) },
func() { NewCmd("root").Opt(NewVersionOpt('-', "", VersionOptConfig{})) },
},
expPanicVals: []any{
"must provide at least either a long or short name for the version option",
"must provide at least either a long or short name for the version option",
},
},
} {
t.Run("prevent "+tt.name, func(t *testing.T) {
for i, build := range tt.builds {
expPanicVal := tt.expPanicVals[i]
func() {
defer func() {
gotPanicVal := recover()
if !reflect.DeepEqual(gotPanicVal, expPanicVal) {
t.Fatalf("tt[%d]: panic values don't match\nexpected: %+#v\n got: %+#v",
ttIdx, expPanicVal, gotPanicVal)
}
}()
build()
}()
}
})
}
}