This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathvalidate_test.go
151 lines (131 loc) · 3.69 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
package survey
import (
"math/rand"
"testing"
)
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_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 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.")
}
}
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.")
}
}
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)
}
}