forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_typed_test.go
More file actions
345 lines (276 loc) · 13.4 KB
/
Copy pathvalidator_typed_test.go
File metadata and controls
345 lines (276 loc) · 13.4 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package valgo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidatorTypedNot(t *testing.T) {
v := Is(Typed(10).Not().Passing(func(val int) bool {
return val == 11
}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedNilValid(t *testing.T) {
var v *Validation
var nilInterface interface{}
v = Is(Typed(nilInterface).Nil())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
v = Is(Typed[interface{}](nil).Nil())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
var a *int
v = Is(Typed(a).Nil())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
type X struct{}
var x *X
v = Is(Typed(x).Nil())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedNilInvalid(t *testing.T) {
var v *Validation
v = Is(Typed(0).Nil())
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be nil",
v.Errors()["value_0"].Messages()[0])
type X struct{}
x := X{}
v = Is(Typed(&x).Nil())
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be nil",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorTypedPassingValid(t *testing.T) {
var v *Validation
valTen := 10
v = Is(Typed(valTen).Passing(func(val int) bool {
return val == 10
}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
type Status string
status := Status("running")
// Test valid status
v = Is(Typed(status).Passing(func(s Status) bool {
return s == "running" || s == "paused"
}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedPassingInvalid(t *testing.T) {
var v *Validation
valTen := 10
v = Is(Typed(valTen).Passing(func(val int) bool {
return val == 9
}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
type Status string
status := Status("stopped")
v = Is(Typed(status).Passing(func(s Status) bool {
return s == "running" || s == "paused"
}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorTypedOrOperatorWithIs(t *testing.T) {
var v *Validation
var _true = true
var _false = false
// Testing Or operation with two valid conditions
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, _true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left invalid and right valid conditions
v = Is(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left valid and right invalid conditions
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with two invalid conditions
v = Is(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, _false || false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing And operation (default when no Or() function is used) with left valid and right invalid conditions
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing combination of Not and Or operators with left valid and right invalid conditions
v = Is(Typed(true).Not().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, !false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing combination of Not and Or operators with left invalid and right valid conditions
v = Is(Typed(true).Not().Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, !true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the first condition being valid
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, true || _false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the last condition being valid
v = Is(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, _false || false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid Or operation then valid And operation
v = Is(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, false || _true && true, v.Valid())
assert.Empty(t, v.Errors())
// Testing valid Or operation then invalid And operation
v = Is(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, false || true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing valid And operation then invalid Or operation
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, _true && true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid And operation then valid Or operation
v = Is(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, true && false || true, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedOrOperatorWithCheck(t *testing.T) {
var v *Validation
// Check are Non-Short-circuited operations
var _true = true
var _false = false
// Testing Or operation with two valid conditions
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, _true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left invalid and right valid conditions
v = Check(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left valid and right invalid conditions
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with two invalid conditions
v = Check(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, _false || false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing And operation (default when no Or() function is used) with left valid and right invalid conditions
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing combination of Not and Or operators with left valid and right invalid conditions
v = Check(Typed(true).Not().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, !false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing combination of Not and Or operators with left invalid and right valid conditions
v = Check(Typed(true).Not().Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, !true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the first condition being valid
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, true || _false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the last condition being valid
v = Check(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, _false || false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid Or operation then valid And operation
v = Check(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, false || _true && true, v.Valid())
assert.Empty(t, v.Errors())
// Testing valid Or operation then invalid And operation
v = Check(Typed(true).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }))
assert.False(t, v.Valid())
assert.Equal(t, false || true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing valid And operation then invalid Or operation
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == true }).Or().Passing(func(val bool) bool { return val == false }))
assert.True(t, v.Valid())
assert.Equal(t, _true && true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid And operation then valid Or operation
v = Check(Typed(true).Passing(func(val bool) bool { return val == true }).Passing(func(val bool) bool { return val == false }).Or().Passing(func(val bool) bool { return val == true }))
assert.True(t, v.Valid())
assert.Equal(t, true && false || true, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedOrElseOperatorWithIs(t *testing.T) {
var v *Validation
// Testing OrElse with left side valid - should short-circuit (key behavior)
var input *string
v = Is(Typed(input).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Testing OrElse with left side invalid - should continue to right side
testStr := "test"
v = Is(Typed(&testStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Testing OrElse with left invalid and right side fails
otherStr := "other"
v = Is(Typed(&otherStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing OrElse with both sides invalid
v = Is(Typed(&otherStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing OrElse with Not() - left valid should short-circuit
v = Is(Typed(&testStr).Not().Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "other" }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Testing OrElse with Not() - left invalid should continue to right
v = Is(Typed(input).Not().Nil().OrElse().Passing(func(s *string) bool { return s == nil }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorTypedOrElseOperatorWithCheck(t *testing.T) {
var v *Validation
// Testing OrElse with left side valid - should short-circuit
var input *string
v = Check(Typed(input).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Testing OrElse with left side invalid - should continue to right side
testStr := "test"
v = Check(Typed(&testStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Testing OrElse with left invalid and right side fails
otherStr := "other"
v = Check(Typed(&otherStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing OrElse with both sides invalid
v = Check(Typed(&otherStr).Nil().OrElse().Passing(func(s *string) bool { return s != nil && *s == "test" }))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
}