-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparam_test.go
214 lines (186 loc) · 5.89 KB
/
param_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
package broccli
import (
"log"
"os"
"testing"
)
func h(c *CLI) int {
return 0
}
// TestParamValidationBasic tests basic validation for specific types.
func TestParamValidationBasic(t *testing.T) {
p := ¶m{}
if p.validateValue("") != nil {
t.Errorf("Empty param should validate")
}
for _, typ := range []int64{TypeInt, TypeFloat, TypeAlphanumeric, TypePathFile} {
p.valueType = typ
if p.validateValue("") != nil {
t.Errorf("Empty param should validate")
}
}
p.valueType = TypeInt
if p.validateValue("48") != nil {
t.Errorf("Int param should validate")
}
if p.validateValue("aa") == nil {
t.Errorf("Int param should not validate string")
}
p.valueType = TypeFloat
if p.validateValue("48.998") != nil {
t.Errorf("Float param should validate")
}
if p.validateValue("48") == nil {
t.Errorf("Float param should not validate int")
}
if p.validateValue("aa") == nil {
t.Errorf("Float param should not validate string")
}
p.valueType = TypeAlphanumeric
if p.validateValue("a123aaAEz") != nil {
t.Errorf("Alphanumeric param should validate")
}
if p.validateValue("a.z") == nil {
t.Errorf("Alphanumeric param should not validate")
}
p.valueType = TypePathFile
if p.validateValue("anything/here") != nil {
t.Errorf("TypePathFile param should validate")
}
}
// TestParamValidationRequired tests IsRequired flag.
func TestParamValidationRequired(t *testing.T) {
p := ¶m{
flags: IsRequired,
}
if p.validateValue("") == nil {
t.Errorf("Empty param with IsRequired should not validate")
}
if p.validateValue("aa") != nil {
t.Errorf("Param with IsRequired should validate")
}
}
// TestParamValidationExtraChars tests flags such as AllowUnderscore that allow TypeAlphanumeric to contain
// extra characters.
func TestParamValidationExtraChars(t *testing.T) {
p := ¶m{
valueType: TypeAlphanumeric,
flags: AllowDots,
}
if p.validateValue("aZ09.az") != nil {
t.Errorf("Alphanumeric param with extra chars should validate")
}
p.flags = AllowUnderscore
if p.validateValue("aZ09_09") != nil {
t.Errorf("Alphanumeric param with extra chars should validate")
}
p.flags = AllowHyphen
if p.validateValue("aZ09-09") != nil {
t.Errorf("Alphanumeric param with extra chars should validate")
}
if p.validateValue("aZ09-_09") == nil {
t.Errorf("Alphanumeric param with extra chars should fail")
}
p.flags = AllowUnderscore | AllowHyphen
if p.validateValue("aZ09_0-9") != nil {
t.Errorf("Alphanumeric param with extra chars should validate")
}
}
// TestParamValidationMultipleValues tests params that allow multiple values.
func TestParamValidationMultipleValues(t *testing.T) {
p := ¶m{
valueType: TypeAlphanumeric,
flags: AllowMultipleValues | AllowDots,
}
if p.validateValue("aZ09.az,x00,y2.3") != nil {
t.Errorf("Alphanumeric param with extra chars and multiple values should validate")
}
if p.validateValue("aZ09.az,x-00,y2.3") == nil {
t.Errorf("Alphanumeric param with extra chars and multiple values should fail")
}
p.flags = AllowMultipleValues | AllowHyphen | AllowUnderscore | SeparatorSemiColon
if p.validateValue("aZ09_-az;x0-0;y2_-3") != nil {
t.Errorf("Alphanumeric param with extra chars and multiple values should validate")
}
if p.validateValue("aZ09az,x-00,y2_3") == nil {
t.Errorf("Alphanumeric param with extra chars and multiple values should fail")
}
p.valueType = TypeFloat
if p.validateValue("5.8;2.3") != nil {
t.Errorf("Alphanumeric param with extra chars and multiple values should validate")
}
}
// TestParamValidationFiles creates param of TypePathFile and tests additional validation flags related to checking
// if file is a regular file, if it exists etc.
func TestParamValidationFiles(t *testing.T) {
f, err := os.CreateTemp("", "example")
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name())
p := ¶m{
valueType: TypePathFile,
}
if p.validateValue("/non-existing/path") != nil {
t.Errorf("PathFile param should validate")
}
p.flags = IsNotExistent
if p.validateValue("/non-existing/path") != nil {
t.Errorf("PathFile param with IsNotExistent should validate")
}
if p.validateValue(f.Name()) == nil {
t.Errorf("PathFile param with IsNotExistent should fail")
}
if p.validateValue("") != nil {
t.Errorf("Empty PathFile param with IsNotExistent should validate")
}
p.flags = IsExistent
if p.validateValue("/non-existing/path") == nil {
t.Errorf("PathFile param with IsExistent should fail")
}
if p.validateValue(f.Name()) != nil {
t.Errorf("PathFile param with IsNotExistent should validate")
}
if p.validateValue("") != nil {
t.Errorf("Empty PathFile param with IsExistent should validate")
}
p.flags = IsRegularFile
if p.validateValue("") != nil {
t.Errorf("Empty PathFile param with IsRegularFile should validate")
}
if p.validateValue(f.Name()) != nil {
t.Errorf("PathFile param with IsRegularFile should validate")
}
p.flags = IsDirectory
if p.validateValue("") != nil {
t.Errorf("Empty PathFile param with IsDirectory should validate")
}
if p.validateValue("") != nil {
t.Errorf("Empty PathFile param with IsDirectory should validate")
}
p.flags = IsExistent | IsValidJSON
if p.validateValue(f.Name()) != nil {
t.Errorf("PathFile param with IsExistent and IsValidJSON should validate")
}
p.flags = IsExistent | IsRegularFile | IsValidJSON
if p.validateValue(f.Name()) == nil {
t.Errorf("PathFile param with IsExistent should fail")
}
if _, err := f.Write([]byte("{\"valid\":\"json\"}")); err != nil {
log.Fatal(err)
}
p.flags = IsExistent | IsRegularFile | IsValidJSON
if p.validateValue(f.Name()) != nil {
t.Errorf("PathFile param with IsExistent should validate")
}
if _, err := f.Write([]byte("in{\"valid\":\"json\"}")); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
p.flags = IsExistent | IsRegularFile | IsValidJSON
if p.validateValue(f.Name()) == nil {
t.Errorf("PathFile param with IsExistent should fail")
}
}