forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_test.go
212 lines (183 loc) · 5.16 KB
/
validate_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
package survey
import (
"math/rand"
"testing"
"github.com/Iilun/survey/v2/core"
)
func TestRequired_canSucceedOnPrimitiveTypes(t *testing.T) {
// a string to test
str := "hello"
// if the string is not valid
if valid := Required(str); valid != nil {
//
t.Error("Non null returned an error when one wasn't expected.")
}
}
func TestRequired_canFailOnPrimitiveTypes(t *testing.T) {
// a string to test
str := ""
// if the string is valid
if notValid := Required(str); notValid == nil {
//
t.Error("Non null did not return an error when one was expected.")
}
}
func TestRequired_canSucceedOnMap(t *testing.T) {
// an non-empty map to test
val := map[string]int{"hello": 1}
// if the string is not valid
if valid := Required(val); valid != nil {
//
t.Error("Non null returned an error when one wasn't expected.")
}
}
func TestRequired_passesOnFalse(t *testing.T) {
// a false value to pass to the validator
val := false
// if the boolean is invalid
if notValid := Required(val); notValid != nil {
//
t.Error("False failed a required check.")
}
}
func TestRequired_canFailOnMap(t *testing.T) {
// an non-empty map to test
val := map[string]int{}
// if the string is valid
if notValid := Required(val); notValid == nil {
//
t.Error("Non null did not return an error when one was expected.")
}
}
func TestRequired_canSucceedOnLists(t *testing.T) {
// a string to test
str := []string{"hello"}
// if the string is not valid
if valid := Required(str); valid != nil {
//
t.Error("Non null returned an error when one wasn't expected.")
}
}
func TestRequired_canFailOnLists(t *testing.T) {
// a string to test
str := []string{}
// if the string is not valid
if notValid := Required(str); notValid == nil {
//
t.Error("Non null did not return an error when one was expected.")
}
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
func TestMaxItems(t *testing.T) {
// the list to test
testList := []core.OptionAnswer{
{Value: "a", Index: 0},
{Value: "b", Index: 1},
{Value: "c", Index: 2},
{Value: "d", Index: 3},
{Value: "e", Index: 4},
{Value: "f", Index: 5},
}
// validate the list
if err := MaxItems(4)(testList); err == nil {
t.Error("No error returned with input greater than 6 items.")
}
}
func TestMinItems(t *testing.T) {
// the list to test
testList := []core.OptionAnswer{
{Value: "a", Index: 0},
{Value: "b", Index: 1},
{Value: "c", Index: 2},
{Value: "d", Index: 3},
{Value: "e", Index: 4},
{Value: "f", Index: 5},
}
// validate the list
if err := MinItems(10)(testList); err == nil {
t.Error("No error returned with input less than 10 items.")
}
}
func TestMaxLength(t *testing.T) {
// the string to test
testStr := randString(150)
// validate the string
if err := MaxLength(140)(testStr); err == nil {
t.Error("No error returned with input greater than 150 characters.")
}
// emoji test
emojiStr := "I😍Golang"
// validate visible length with Maxlength
if err := MaxLength(10)(emojiStr); err != nil {
t.Errorf("Error returned with emoji containing 8 characters long input.")
}
}
func TestMinLength(t *testing.T) {
// validate the string
if err := MinLength(12)(randString(10)); err == nil {
t.Error("No error returned with input less than 12 characters.")
}
// emoji test
emojiStr := "I😍Golang"
// validate visibly 8 characters long string with MinLength
if err := MinLength(10)(emojiStr); err == nil {
t.Error("No error returned with emoji containing input less than 10 characters.")
}
}
func TestMinLength_onInt(t *testing.T) {
// validate the string
if err := MinLength(12)(1); err == nil {
t.Error("No error returned when enforcing length on int.")
}
}
func TestMaxLength_onInt(t *testing.T) {
// validate the string
if err := MaxLength(12)(1); err == nil {
t.Error("No error returned when enforcing length on int.")
}
}
func TestComposeValidators_passes(t *testing.T) {
// create a validator that requires a string of no more than 10 characters
valid := ComposeValidators(
Required,
MaxLength(10),
)
str := randString(12)
// if a valid string fails
if err := valid(str); err == nil {
// the test failed
t.Error("Composed validator did not pass. Wanted string less than 10 chars, passed in", str)
}
}
func TestComposeValidators_failsOnFirstError(t *testing.T) {
// create a validator that requires a string of no more than 10 characters
valid := ComposeValidators(
Required,
MaxLength(10),
)
// if an empty string passes
if err := valid(""); err == nil {
// the test failed
t.Error("Composed validator did not fail on first test like expected.")
}
}
func TestComposeValidators_failsOnSubsequentValidators(t *testing.T) {
// create a validator that requires a string of no more than 10 characters
valid := ComposeValidators(
Required,
MaxLength(10),
)
str := randString(12)
// if a string longer than 10 passes
if err := valid(str); err == nil {
// the test failed
t.Error("Composed validator did not fail on second first test like expected. Should fail max length > 10 :", str)
}
}