-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_length_validator_test.go
77 lines (60 loc) · 1.75 KB
/
string_length_validator_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
// Copyright 2021 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package validator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStringLengthValidatorWithBadType(t *testing.T) {
v := NewStringLengthValidator(StringLength{})
assert.EqualError(t, v.Validate(123), "invalid \"int\" type given. String expected")
}
func TestStringLengthValidatorWithMaxOption(t *testing.T) {
v := NewStringLengthValidator(StringLength{
Max: 6,
})
assert.Nil(t, v.Validate("Test"))
assert.EqualError(t, v.Validate("Testing"), "the input is more than 6 characters long")
}
func TestStringLengthValidatorWithMinOption(t *testing.T) {
v := NewStringLengthValidator(StringLength{
Min: 5,
})
assert.EqualError(t, v.Validate("Test"), "the input is less than 5 characters long")
assert.Nil(t, v.Validate("Testing"))
}
func TestStringLengthValidatorWithMinAndMaxOption(t *testing.T) {
v := NewStringLengthValidator(StringLength{
Min: 5,
Max: 6,
})
assert.EqualError(t, v.Validate("Test"), "the input is less than 5 characters long")
assert.EqualError(t, v.Validate("Testing"), "the input is more than 6 characters long")
assert.Nil(t, v.Validate("good t"))
}
func BenchmarkStringLengthValidatorMin(b *testing.B) {
v := NewStringLengthValidator(StringLength{
Min: 6,
})
for i := 0; i < b.N; i++ {
v.Validate("Test")
}
}
func BenchmarkStringLengthValidatorMax(b *testing.B) {
v := NewStringLengthValidator(StringLength{
Max: 6,
})
for i := 0; i < b.N; i++ {
v.Validate("Test")
}
}
func BenchmarkStringLengthValidatorMinMax(b *testing.B) {
v := NewStringLengthValidator(StringLength{
Min: 3,
Max: 6,
})
for i := 0; i < b.N; i++ {
v.Validate("Test")
}
}