-
Notifications
You must be signed in to change notification settings - Fork 7
/
string_test.go
379 lines (275 loc) · 8.8 KB
/
string_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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package zog
import (
"regexp"
"testing"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
func TestStringOptionalByDefault(t *testing.T) {
field := String().Len(3).Contains("foo").HasPrefix("pre").HasSuffix("fix")
var dest string
errs := field.Parse("", &dest)
assert.Empty(t, errs)
assert.Equal(t, "", dest)
field = field.Required()
errs = field.Parse("", &dest)
assert.NotEmpty(t, errs)
field.Required().Optional()
}
func TestStringOptional(t *testing.T) {
field := String().Required().Optional()
var dest string
errs := field.Parse("", &dest)
assert.Empty(t, errs)
assert.Equal(t, "", dest)
errs = field.Parse("foo", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
}
func TestStringPreTransform(t *testing.T) {
field := String().Required().Len(3).PreTransform(func(val any, ctx ParseCtx) (any, error) {
return "foo", nil
})
var dest string
errs := field.Parse("", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
}
func TestStringTrim(t *testing.T) {
field := String().Required().Trim()
var dest string
errs := field.Parse(" foo ", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
errs = field.Parse(123, &dest)
assert.Empty(t, errs)
assert.Equal(t, "123", dest)
}
func TestStringPostTransform(t *testing.T) {
field := String().Required().PostTransform(func(val any, ctx ParseCtx) error {
s := val.(*string)
*s = *s + "_transformed"
return nil
})
var dest string
errs := field.Parse("hello", &dest)
assert.Empty(t, errs)
assert.Equal(t, "hello_transformed", dest)
// Test that PostTransform is not applied when there's an error
field = String().Required().Len(1).PostTransform(func(val any, ctx ParseCtx) error {
s := val.(*string)
*s = *s + "_transformed"
return nil
})
errs = field.Parse("short", &dest)
assert.NotEmpty(t, errs)
assert.NotEqual(t, "short_transformed", dest)
}
func TestStringRequiredAborts(t *testing.T) {
field := String().Required().Len(3)
var dest string
errs := field.Parse("", &dest)
assert.NotEmpty(t, errs)
assert.Len(t, errs, 1)
}
func TestStringUserTests(t *testing.T) {
field := String().Test(TestFunc("test", func(val any, ctx ParseCtx) bool {
return val == "test"
}), Message("Invalid"))
var dest string
errs := field.Parse("test", &dest)
assert.Empty(t, errs)
assert.Equal(t, "test", dest)
errs = field.Parse("not test", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "Invalid", errs[0].Message())
}
func TestStringRequired(t *testing.T) {
field := String().Required(Message("a"))
var dest string
errs := field.Parse("", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, errs[0].Message(), "a")
errs = field.Parse("foo", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
}
func TestStringDefault(t *testing.T) {
field := String().Default("bar")
var dest string
errs := field.Parse("", &dest)
assert.Empty(t, errs)
assert.Equal(t, "bar", dest)
errs = field.Parse("foo", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
}
func TestStringCatch(t *testing.T) {
field := String().Required().Min(5).Catch("error")
var dest string
errs := field.Parse("x", &dest)
assert.Empty(t, errs)
assert.Equal(t, "error", dest)
errs = field.Parse("not error", &dest)
assert.Empty(t, errs)
assert.Equal(t, "not error", dest)
}
// VALIDATORS / Tests / Validators
func TestStringLength(t *testing.T) {
field := String().Len(3, Message("custom length"))
var dest string
errs := field.Parse("foo", &dest)
assert.Empty(t, errs)
assert.Equal(t, "foo", dest)
errs = field.Parse("foobar", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom length", errs[0].Message())
field = String().Min(5, Message("custom min")).Max(7, Message("custom max"))
errs = field.Parse("123456789", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom max", errs[0].Message())
assert.Equal(t, "123456789", dest)
field = String().Min(5, Message("custom min")).Max(7, Message("custom max"))
errs = field.Parse("1234567", &dest)
assert.Empty(t, errs)
assert.Equal(t, "1234567", dest)
}
func TestStringEmail(t *testing.T) {
field := String().Email(Message("custom email"))
var dest string
errs := field.Parse("not an email", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom email", errs[0].Message())
errs = field.Parse("[email protected]", &dest)
assert.Empty(t, errs)
assert.Equal(t, "[email protected]", dest)
}
func TestStringURL(t *testing.T) {
field := String().URL(Message("custom url"))
var dest string
errs := field.Parse("not a url", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom url", errs[0].Message())
errs = field.Parse("http://example.com", &dest)
assert.Empty(t, errs)
assert.Equal(t, "http://example.com", dest)
}
func TestStringHasPrefix(t *testing.T) {
field := String().HasPrefix("pre", Message("custom prefix"))
var dest string
errs := field.Parse("not prefixed", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom prefix", errs[0].Message())
errs = field.Parse("prefix", &dest)
assert.Empty(t, errs)
assert.Equal(t, "prefix", dest)
}
func TestStringHasPostfix(t *testing.T) {
field := String().HasSuffix("fix", Message("custom suffix"))
var dest string
errs := field.Parse("not postfixed", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom suffix", errs[0].Message())
errs = field.Parse("postfix", &dest)
assert.Empty(t, errs)
assert.Equal(t, "postfix", dest)
}
func TestStringContains(t *testing.T) {
field := String().Contains("contains", Message("custom contains"))
var dest string
errs := field.Parse("does not contain", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom contains", errs[0].Message())
errs = field.Parse("contains", &dest)
assert.Empty(t, errs)
assert.Equal(t, "contains", dest)
}
func TestStringContainsDigit(t *testing.T) {
field := String().ContainsDigit(Message("custom digit"))
var dest string
errs := field.Parse("no digit here", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom digit", errs[0].Message())
errs = field.Parse("1234", &dest)
assert.Empty(t, errs)
assert.Equal(t, "1234", dest)
}
func TestStringContainsUpper(t *testing.T) {
field := String().ContainsUpper(Message("custom upper"))
var dest string
errs := field.Parse("no uppercase here", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom upper", errs[0].Message())
errs = field.Parse("UPPERCASE", &dest)
assert.Empty(t, errs)
assert.Equal(t, "UPPERCASE", dest)
}
func TestStringContainsSpecial(t *testing.T) {
field := String().ContainsSpecial(Message("custom special"))
var dest string
errs := field.Parse("no special character here", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom special", errs[0].Message())
errs = field.Parse("!@#$%", &dest)
assert.Empty(t, errs)
assert.Equal(t, "!@#$%", dest)
}
func TestStringOneOf(t *testing.T) {
field := String().OneOf([]string{"apple", "banana", "cherry"}, Message("custom one of")).Required(Message("custom required"))
var dest string
errs := field.Parse("orange", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom one of", errs[0].Message())
errs = field.Parse("banana", &dest)
assert.Empty(t, errs)
assert.Equal(t, "banana", dest)
// Test with non-string input
errs = field.Parse(123, &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom one of", errs[0].Message())
// Test with empty string
errs = field.Parse("", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom required", errs[0].Message())
// Test with nil
errs = field.Parse(nil, &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom required", errs[0].Message())
}
func TestStringUUID(t *testing.T) {
field := String().UUID(Message("custom uuid msg"))
var dest string
errs := field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom uuid msg", errs[0].Message())
errs = field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", &dest)
assert.Empty(t, errs)
assert.Equal(t, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", dest)
errs = field.Parse("F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6", &dest)
assert.Empty(t, errs)
assert.Equal(t, "F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6", dest)
}
func TestStringRegex(t *testing.T) {
r := regexp.MustCompile("^[0-9]{2}$")
field := String().Match(r, Message("custom regex msg"))
var dest string
errs := field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom regex msg", errs[0].Message())
errs = field.Parse("00", &dest)
assert.Empty(t, errs)
assert.Equal(t, "00", dest)
}
func TestStringSchemaOption(t *testing.T) {
s := String(WithCoercer(func(original any) (value any, err error) {
return "coerced", nil
}))
var result string
err := s.Parse(123, &result)
assert.Nil(t, err)
assert.Equal(t, "coerced", result)
}
func TestStringGetType(t *testing.T) {
s := String()
assert.Equal(t, zconst.TypeString, s.getType())
}