-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
218 lines (182 loc) · 5.93 KB
/
parser_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
package gommander
import (
"testing"
)
func TestParseBasic(t *testing.T) {
clearCache()
cmd := NewCommand("test").Flag("-v --version", "Version flag").Option("-p --port <port-no>", "Port option")
parser := NewParser(cmd)
matches, _ := parser.parse([]string{"-v", "-p", "90"})
v, _ := matches.GetOptionValue("--port")
assertEq(t, v, "90", "Option arg parsing not working correctly")
assert(t, matches.ContainsFlag("-v"), "Flag parsing has some errors")
}
func TestParseStandard(t *testing.T) {
clearCache()
app := NewCommand("echo")
app.SubCommand("first").Flag("-v --verbose", "Set verbose").Option("-n --name <value>", "Some name")
parser := NewParser(app)
matches, _ := parser.parse([]string{"first", "-v", "-n", "one", "-n", "two"})
assert(t, matches.ContainsFlag("verbose"), "Flag parsing has some errors")
assertEq(t, matches.GetMatchedCommand().name, "first", "Subcommand resolution has some errors")
assertDeepEq(t, matches.GetAllOptionInstances("name"), []string{"one", "two"}, "Multiple option argument parsing failed")
}
func TestParseComplex(t *testing.T) {
clearCache() // workaound for test mode
app := NewCommand("echo").Version("0.1.0").Help("A test CLI")
app.SubCommand("image").
Argument("<image-name>", "Provide an image name").
Alias("i").
Flag("--all", "Ran across all variants").
Help("A first value subcommand").
AddFlag(
NewFlag("test").
Short('t').
Help("A simple test flag"),
).
AddOption(
NewOption("port").
Short('p').
Required(true).
Help("Pass the port number").
AddArgument(
NewArgument("port-number").
Required(true),
),
)
parser := NewParser(app)
matches, _ := parser.parse([]string{"i", "image-one", "--all", "-p", "800", "--", "ng", "serve"})
assert(t, matches.ContainsFlag("all"), "Flag parsing has some errors")
assertEq(t, matches.GetMatchedCommand().name, "image", "Subcommand resolution has some errors")
assertDeepEq(t, matches.GetPositionalArgs(), []string{"ng", "serve"}, "Positional args parsing failed")
v, _ := matches.GetArgValue("<image-name>")
v2, _ := matches.GetOptionValue("--port")
assertEq(t, v, "image-one", "Command argument parsing")
assertEq(t, v2, "800", "Option arg parsing not working correctly")
}
func TestParseOptionSyntaxes(t *testing.T) {
clearCache()
app := NewCommand("basic").Option("-p --port <port-number>", "Port option")
parser := NewParser(app)
m1, _ := parser.parse([]string{"-p", "9000"})
m2, _ := parser.parse([]string{"--port", "9000"})
m3, _ := parser.parse([]string{"--port=9000"})
a1, _ := m1.GetOptionValue("port")
a2, _ := m2.GetOptionValue("port")
a3, _ := m3.GetOptionValue("port")
assertEq(t, a1, a2, "Short option parsing and long option parsing out of sync")
assertEq(t, a2, a3, "Long option syntax with `=` parsing failed")
}
func _assertParserError(t *testing.T, app *Command, parserArgs, errorArgs []string, event Event, msg string) {
clearCache()
parser := NewParser(app)
_, err := parser.parse(parserArgs)
expected := generateError(app, event, errorArgs)
assertDeepEq(t, *err, expected, msg)
}
func TestParserErrors(t *testing.T) {
clearCache()
app := NewCommand("echo").Version("0.1.0").Help("A test CLI")
app.SubCommand("image").
Argument("<image-name>", "Provide an image name").
Alias("i").
Flag("--all", "Ran across all variants").
Help("A first value subcommand").
AddOption(
NewOption("port").
Short('p').
Argument("<int:port-no>").
Help("option argument").
Required(true),
).
AddFlag(
NewFlag("test").
Short('t').
Help("A simple test flag"),
)
app.SubCommand("basic").
Flag("-a --all", "All flag").
Flag("-V --verbose", "Verbosity").
AddArgument(
NewArgument("<lang>").
Help("Language").
ValidateWith([]string{"eng", "fre", "spa", "swa", "ru"}),
)
// test missing required argument errors
_assertParserError(t, app,
[]string{"i"},
[]string{"<image-name>"},
MissingRequiredArgument,
"Missing required arg error detection failed",
)
_assertParserError(t, app,
[]string{"i", "-p", "90"},
[]string{"<image-name>", "-p"},
MissingRequiredArgument,
"Expected required arg but found option error detection failed",
)
_assertParserError(t, app,
[]string{"i", "imageOne"},
[]string{"--port"},
MissingRequiredOption,
"Missing required option error detection failed",
)
// test unknown command errors
_assertParserError(t, app,
[]string{"invalid"},
[]string{"invalid"},
UnknownCommand,
"Unknown command error detection failed",
)
// test unknown option errors
_assertParserError(t, app,
[]string{"i", "imageOne", "--port=90", "-x"},
[]string{"-x"},
UnknownOption,
"Unknown option error detection failed",
)
_assertParserError(t, app,
[]string{"i", "imageOne", "--extra=90"},
[]string{"--extra", "--extra=90"},
UnknownOption,
"Unknown option(with long syntax) error detection failed",
)
_assertParserError(t, app,
[]string{"basic", "eng", "-Vx"},
[]string{"x", "-Vx", ""},
UnknownOption,
"Unknown option(with posix flag syntax) error detection failed",
)
_assertParserError(t, app,
[]string{"i", "imageOne", "-p=90"},
[]string{},
UnknownOption,
"Short option synatx not supported error detection failed",
)
// test unresolved args errors
_assertParserError(t, app,
[]string{"i", "imageOne", "--port=90", "invalid"},
[]string{"invalid"},
UnresolvedArgument,
"Unresolved argument error detection failed",
)
// test invalid args values
_assertParserError(t, app,
[]string{"i", "imageOne", "--port=hello"},
[]string{"hello", "`hello` is not a valid integer"},
InvalidArgumentValue,
"invalid argument error detection failed",
)
_assertParserError(t, app,
[]string{"basic", "urdu"},
[]string{"urdu", "eng", "fre", "spa", "swa", "ru"},
InvalidArgumentValue,
"invalid argument(with default values) error detection failed",
)
}
func BenchmarkParseEmpty(b *testing.B) {
for i := 0; i < b.N; i++ {
parser := NewParser(NewCommand("empty"))
parser.parse([]string{})
}
}