-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgommander_test.go
192 lines (159 loc) · 4.95 KB
/
gommander_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
package gommander
import (
"testing"
)
func TestCommandMetadata(t *testing.T) {
cmd := NewCommand("basic").
Help("Basic command").
Alias("b").
Author("vndaba").
Version("0.1.0")
assertEq(t, cmd.GetHelp(), "Basic command", "Cmd help field set incorrectly")
assertEq(t, cmd.GetAuthor(), "vndaba", "Cmd author set wrongly")
assertEq(t, cmd.GetVersion(), "0.1.0", "Cmd Version set wrongly")
assertDeepEq(t, cmd.GetAliases(), []string{"b"}, "Cmd aliases set wrongly")
assertDeepEq(t, cmd.GetFlags()[0], helpFlag(), "Help flag not added automatically")
}
func TestCommandSettings(t *testing.T) {
clearCache()
app := App()
app.SubCommand("dummy").Argument("<uint:count>", "a count arg")
// assertStructArrEq[*Flag](t, app.GetFlags(), []*Flag{helpFlag(), versionFlag()}, "Help and version flags not set correctly")
assertEq(t, len(app.GetFlags()), 2, "Help and version flags not set correctly")
assertDeepEq(t, app.GetFlags()[1], versionFlag(), "Version flag not set correctly")
app.Set(DisableVersionFlag, true)
app.Set(IncludeHelpSubcommand, true)
app.Set(AllowNegativeNumbers, true)
// TODO: Complete the other settings
app._init()
assertEq(t, len(app.GetFlags()), 1, "Failed to disable the version flag")
assertEq(t, app.GetSubCommands()[1].GetName(), "help", "Include Help subcommand setting failed")
}
func TestEventListeners(t *testing.T) {
app := App()
app.SubCommand("test")
app.Override(UnknownCommand, func(ec *EventConfig) {
assertEq(t, len(ec.GetArgs()), 1, "Incorrect number of args passed along")
assertEq(t, ec.GetEvent(), UnknownCommand, "Event on EventCfg set incorrectly")
assertEq(t, ec.GetApp(), app, "app reference passed incorrectly")
assertEq(t, ec.GetError().message, "no such subcommand found: `new`", "Error message configured wrongly")
assertEq(t, ec.GetExitCode(), 40, "Wrong exit code found")
})
app.Set(IncludeHelpSubcommand, true)
app.ParseFrom([]string{"my_bin", "new"})
}
func TestRootCmdArgs(t *testing.T) {
// Test single required arg
{
clearCache()
app := App()
app.Argument("<file>", "file to open").
Action(func(pm *ParserMatches) {
val, _ := pm.GetArgValue("file")
assertEq(t, val, "happy.png", "Root cmd arg parsing for required args is faulty")
})
app.ParseFrom([]string{"my_bin", "happy.png"})
}
// Test required arg error
{
clearCache()
app := App()
app.Argument("<file>", "file to open")
expectedError := generateError(app, MissingRequiredArgument, []string{"<file>"})
exec := func() {
app.ParseFrom([]string{"my_bin"})
}
assertStdOut(t, expectedError.GetErrorString(app), exec, "Error throwing for missing required arg on root cmd faulty")
}
// Test optional args parsing
{
clearCache()
app := App()
app.Argument("[file]", "file to open").
Action(func(pm *ParserMatches) {
val, _ := pm.GetArgValue("file")
assertEq(t, val, "happy.png", "Root cmd arg parsing for required args is faulty")
})
app.ParseFrom([]string{"my_bin", "happy.png"})
}
// Test multiple root args
{
app := App()
app.Argument("<arg1>", "first arg").
Argument("<arg2>", "second arg").
Argument("<arg3>", "third arg").
Action(func(pm *ParserMatches) {
val1, _ := pm.GetArgValue("arg1")
val2, _ := pm.GetArgValue("arg2")
val3, _ := pm.GetArgValue("arg3")
assertEq(t, val1, "one", "Root cmd multi-arg parsing failed")
assertEq(t, val2, "two", "Root cmd multi-arg parsing failed")
assertEq(t, val3, "three", "Root cmd multi-arg parsing failed")
})
app.ParseFrom([]string{"my_bin", "one", "two", "three"})
}
}
func BenchmarkBuildEmptyCmd(b *testing.B) {
for i := 0; i < b.N; i++ {
NewCommand("empty")
}
}
func BenchmarkBuildEmptyApp(b *testing.B) {
for i := 0; i < b.N; i++ {
App()
}
}
func _compareVariants(b *testing.B, vars ...func(*Command)) {
for i, fn := range vars {
name := ""
switch i {
case 0:
name = "constructor"
case 1:
name = "builder"
case 2:
name = "composite-func"
}
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
fn(NewCommand("empty"))
}
})
}
}
func BenchmarkBuildWithFlags(b *testing.B) {
constructor := func(a *Command) {
a.AddFlag(&Flag{
Name: "verbose",
LongVal: "--verbose",
ShortVal: "-V",
HelpStr: "Verbosity flag",
})
}
builder := func(a *Command) {
a.AddFlag(NewFlag("verbose").Help("Verbosity flag").Short('V'))
}
composite := func(a *Command) {
a.Flag("-V --verbose", "Verbosity flag")
}
_compareVariants(b, constructor, builder, composite)
}
func BenchmarkBuildWithArgs(b *testing.B) {
constructor := func(a *Command) {
a.AddArgument(&Argument{
Name: "arg1",
HelpStr: "Argument one",
RawValue: "<arg1...>",
ArgType: str,
IsVariadic: true,
IsRequired: true,
})
}
buidler := func(a *Command) {
a.AddArgument(NewArgument("arg1").Help("Argument one").Required(true).Variadic(true))
}
composite := func(a *Command) {
a.Argument("<arg1...>", "Argument one")
}
_compareVariants(b, constructor, buidler, composite)
}