-
Notifications
You must be signed in to change notification settings - Fork 2
/
args_test.go
205 lines (180 loc) · 6.45 KB
/
args_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
package gommander
import (
"fmt"
"strconv"
"testing"
)
func Test_init(t *testing.T) {
setGommanderTestMode()
}
func TestArgCreation(t *testing.T) {
arg := NewArgument("<test>").Help("Test argument").Variadic(true)
argB := newArgument("<test...>", "Test argument")
assertDeepEq(t, arg, argB, "Arg creation methods out of sync")
assert(t, arg.IsRequired, "Failed to make arg required")
assert(t, arg.IsVariadic, "Failed to make arg variadic")
assertEq(t, arg.Name, "test", "Arg name not set correctly")
assertEq(t, arg.HelpStr, "Test argument", "Arg help string wrongly set")
}
func TestArgMetadata(t *testing.T) {
arg := NewArgument("<basic>").
Variadic(true).
Help("Test argument").
ValidateWith([]string{"ONE", "TWO"}).
Type(str)
assertNe(t, arg.Name, "<basic>", "Enclosures not stripped from name")
assert(t, arg.testValue("one"), "Arg validation working incorrectly")
assert(t, arg.testValue("TWO"), "Arg validation working incorrectly")
assertEq(t, arg.getRawValue(), "<basic...>", "Raw value return function working incorrectly")
assertEq(t, arg.ArgType, str, "Arg builder Type method not working correctly")
assertEq(t, len(arg.ValidatorFns), 0, "Unnecessary validator func added for strings")
expLeading := "<basic...>"
expFloating := "Test argument"
gotLeading, gotFloating := arg.generate(App())
assertEq(t, expLeading, gotLeading, "The arg generate function is problematic")
assertEq(t, expFloating, gotFloating, "The arg generate function is problematic")
}
func TestOptionalArgs(t *testing.T) {
arg := NewArgument("[optional]").Default("DEFAULT").Help("Optional value with default")
assert(t, !arg.IsRequired, "Failed to set argument as optional")
assert(t, arg.hasDefaultValue(), "Failed to set default value")
assert(t, arg.DefaultValue == "DEFAULT", "Failed to set default value")
expLeading := "[optional]"
expFloating := "Optional value with default (default: DEFAULT)"
gotLeading, gotFloating := arg.generate(App())
assertEq(t, expLeading, gotLeading, "The arg generate function is problematic")
assertEq(t, expFloating, gotFloating, "The arg generate function is problematic")
}
func TestArgValidValues(t *testing.T) {
// valid values validator
arg := NewArgument("<lang>").
DisplayAs("language").
ValidateWith([]string{"ENG", "SPA", "RUS", "FRE"})
assert(t, !arg.testValue("else"), "Values validation not working properly")
assertEq(t, arg.getRawValue(), "language", "Failed to set raw arg value using DisplayAs method")
exec := func() {
arg.Default("NEW")
}
expected := fmt.Sprintf("error occurred when setting default value for argument: %v \n. the passed value %v does not match the valid values: %v", arg.Name, "NEW", arg.ValidValues)
assertStdOut(t, expected, exec, "Argument validation for arguments with valid values is buggy")
}
func TestArgValidatorFunc(t *testing.T) {
arg := NewArgument("<age>").
DisplayAs("int").
ValidatorFunc(func(s string) error {
_, err := strconv.Atoi(s)
if err != nil {
return err
}
return nil
})
assertEq(t, arg.getRawValue(), "int", "Failed to set raw arg value using DisplayAs method")
assert(t, arg.testValue("2"), "Strconv validator function working incorrectly")
exec := func() {
arg.Default("notInt")
}
expected := fmt.Sprintf("you tried to set a default value for argument: %v, but the validator function returned an error for values: %v", arg.Name, "notInt")
assertStdOut(t, expected, exec, "Argument validation for arguments with validator functions is buggy")
}
func TestArgRegexValidator(t *testing.T) {
clearCache()
{
arg := NewArgument("version").ValidatorRegex(`^v[\d\.]`)
assert(t, arg.testValue("v0.1.0"), "Regex validation is buggy")
assert(t, !arg.testValue("1.1"), "Regex validation working incorrectly")
assert(t, !arg.testValue("vA.1"), "Regex validation working incorrectly")
}
}
func TestArgTypeValidation(t *testing.T) {
{
arg := NewArgument("<int:count>")
assert(t, arg.testValue("2"), "Integer arg validation faulty")
assert(t, arg.testValue("-2"), "Integer arg validation faulty")
assertEq(t, arg.testValue("2.0"), false, "Integer arg validation faulty against float")
assertEq(t, arg.testValue("two"), false, "Integer arg validation faulty against string")
}
{
arg := NewArgument("<uint:count>")
assert(t, arg.testValue("4"), "Uinteger arg validation is faulty")
assert(t, !arg.testValue("-4"), "Uinteger arg validation is faulty")
}
{
arg := NewArgument("<float:count>")
assert(t, arg.testValue("2.0"), "Float arg validation faulty")
assertEq(t, arg.testValue("two"), false, "Float arg validation faulty against string")
}
{
arg := NewArgument("<bool:count>")
assert(t, arg.testValue("true"), "Boolean arg validation faulty")
assertEq(t, arg.testValue("2"), false, "Boolean arg validation faulty against int")
assertEq(t, arg.testValue("2.0"), false, "Boolean arg validation faulty against float")
}
{
arg := NewArgument("<file:path>")
assert(t, !arg.testValue("fake.png"), "Filename arg validation faulty")
assert(t, arg.testValue("go.mod"), "Filename arg validation faulty")
}
{
exec := func() {
NewArgument("<fake:arg>")
}
expected := "found unknown argument type: `fake` for argument: `<arg>`\n"
assertStdOut(t, expected, exec, "Unknown arg types pass on undetected")
}
}
func BenchmarkArgFunc(b *testing.B) {
for i := 0; i < b.N; i++ {
newArgument("<test>", "A test argument")
}
}
func BenchmarkArgBuilder(b *testing.B) {
for i := 0; i < b.N; i++ {
NewArgument("test").
Help("A test argument").
Required(true).
ValidateWith([]string{"ONE", "TWO", "THREE"}).
Variadic(true)
}
}
func BenchmarkArgBuilderFull(b *testing.B) {
for i := 0; i < b.N; i++ {
NewArgument("complex").
Help("Arg with many options").
DisplayAs("Something").
Required(true).
ValidateWith([]string{"ONE", "TEN"}).
Default("TEN").
Variadic(false).
ValidatorFunc(func(s string) error {
_, err := strconv.Atoi(s)
if err != nil {
return err
}
return nil
})
}
}
func BenchmarkArgConstructor(b *testing.B) {
fn := func(a Argument) {}
for i := 0; i < b.N; i++ {
fn(Argument{
Name: "test",
HelpStr: "A test argument",
IsRequired: true,
ValidValues: []string{"ONE", "TWO", "THREE"},
IsVariadic: true,
RawValue: "<test...>",
DefaultValue: "TEN",
ArgType: integer,
ValidatorFns: [](func(s string) error){
func(s string) error {
_, err := strconv.Atoi(s)
if err != nil {
return err
}
return nil
},
},
})
}
}