-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation_test.go
236 lines (216 loc) · 4.95 KB
/
validation_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package validation
import (
"testing"
"github.com/stretchr/testify/require"
)
type testcase[T any] struct {
name string
f func(T) error
input T
errExpected bool
}
func testWithInput[T any](t *testing.T, tcases []testcase[T]) {
r := require.New(t)
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
err := tc.f(tc.input)
if tc.errExpected {
r.Error(err)
} else {
r.NoError(err)
}
})
}
}
func TestMin(t *testing.T) {
inputs := []testcase[int]{
{
name: "min lower limit",
f: Min[int](10),
input: 10,
},
{
name: "min normal",
f: Min[int](10),
input: 11,
},
{
name: "min failure",
f: Min[int](10),
input: 8,
errExpected: true,
},
}
testWithInput[int](t, inputs)
}
func TestMax(t *testing.T) {
inputs := []testcase[float64]{
{
name: "max upper limit",
f: Max[float64](10.0),
input: 10.0,
},
{
name: "max normal",
f: Max[float64](10.0),
input: 9.0,
},
{
name: "max failure",
f: Max[float64](10.0),
input: 10.00001,
errExpected: true,
},
}
testWithInput[float64](t, inputs)
}
func TestRange(t *testing.T) {
inputs := []testcase[uint64]{
{
name: "lower limit",
f: RangeInclusive[uint64](10, 16),
input: 10,
},
{
name: "upper limit",
f: RangeInclusive[uint64](10, 16),
input: 16,
},
{
name: "lower limit failure",
f: RangeInclusive[uint64](10, 16),
input: 9,
errExpected: true,
},
{
name: "upper limit failure",
f: RangeInclusive[uint64](10, 16),
input: 17,
errExpected: true,
},
{
name: "normal",
f: RangeInclusive[uint64](10, 16),
input: 12,
},
}
testWithInput[uint64](t, inputs)
}
func TestDisjointRange(t *testing.T) {
inputs := []testcase[int16]{
{
name: "lower limit lower range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 10,
},
{
name: "upper limit lower range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 16,
},
{
name: "lower limit upper range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 56,
},
{
name: "upper limit upper range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 67,
},
{
name: "normal lower range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 13,
},
{
name: "normal upper range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 60,
},
{
name: "failure below lower range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 1,
errExpected: true,
},
{
name: "failure in between lower and upper range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 20,
errExpected: true,
},
{
name: "failure above upper range",
f: ValidationChainAny[int16](RangeInclusive[int16](10, 16), RangeInclusive[int16](56, 67)),
input: 70,
errExpected: true,
},
}
testWithInput[int16](t, inputs)
}
func TestEnum(t *testing.T) {
inputs := []testcase[string]{
{
name: "valid value for enum - 1",
f: Enum[string]("hello", "bar", "foo"),
input: "hello",
},
{
name: "valid value for enum - 2",
f: Enum[string]("hello", "bar", "foo"),
input: "bar",
},
{
name: "invalid value for enum",
f: Enum[string]("hello", "bar", "foo"),
input: "helloee",
errExpected: true,
},
}
testWithInput[string](t, inputs)
}
func TestRegex(t *testing.T) {
type myString string
inputs := []testcase[myString]{
{
name: "valid value regex match - 1",
f: Regex[myString]("foo[1-7].*y"),
input: "foo1y",
},
{
name: "valid value regex match - 2",
f: Regex[myString]("foo[1-7].*y"),
input: "foo1ttthsyy",
},
{
name: "invalid value regex match - 1",
f: Regex[myString]("foo[1-7].*y"),
input: "fooy",
errExpected: true,
},
{
name: "invalid value regex match - 2",
f: Regex[myString]("foo[1-7].*y"),
input: "fooOy",
errExpected: true,
},
}
testWithInput[myString](t, inputs)
}
func TestSliceValidator(t *testing.T) {
inputs := []testcase[[]uint32]{
{
name: "valid values slices",
f: SliceValidator[uint32](Min[uint32](7)),
input: []uint32{9, 10, 18, 14},
},
{
name: "invalid value slice - 1",
f: SliceValidator[uint32](Min[uint32](7)),
input: []uint32{9, 10, 6, 14},
errExpected: true,
},
}
testWithInput[[]uint32](t, inputs)
}