forked from go-ozzo/ozzo-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
length_test.go
90 lines (79 loc) · 2.64 KB
/
length_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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package validation
import (
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLength(t *testing.T) {
var v *string
tests := []struct {
tag string
min, max int
value interface{}
err string
}{
{"t1", 2, 4, "abc", ""},
{"t2", 2, 4, "", ""},
{"t3", 2, 4, "abcdf", "the length must be between 2 and 4"},
{"t4", 0, 4, "ab", ""},
{"t5", 0, 4, "abcde", "the length must be no more than 4"},
{"t6", 2, 0, "ab", ""},
{"t7", 2, 0, "a", "the length must be no less than 2"},
{"t8", 2, 0, v, ""},
{"t9", 2, 0, 123, "cannot get the length of int"},
{"t10", 2, 4, sql.NullString{String: "abc", Valid: true}, ""},
{"t11", 2, 4, sql.NullString{String: "", Valid: true}, ""},
{"t12", 2, 4, &sql.NullString{String: "abc", Valid: true}, ""},
}
for _, test := range tests {
r := Length(test.min, test.max)
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
}
}
func TestRuneLength(t *testing.T) {
var v *string
tests := []struct {
tag string
min, max int
value interface{}
err string
}{
{"t1", 2, 4, "abc", ""},
{"t1.1", 2, 3, "💥💥", ""},
{"t1.2", 2, 3, "💥💥💥", ""},
{"t1.3", 2, 3, "💥", "the length must be between 2 and 3"},
{"t1.4", 2, 3, "💥💥💥💥", "the length must be between 2 and 3"},
{"t2", 2, 4, "", ""},
{"t3", 2, 4, "abcdf", "the length must be between 2 and 4"},
{"t4", 0, 4, "ab", ""},
{"t5", 0, 4, "abcde", "the length must be no more than 4"},
{"t6", 2, 0, "ab", ""},
{"t7", 2, 0, "a", "the length must be no less than 2"},
{"t8", 2, 0, v, ""},
{"t9", 2, 0, 123, "cannot get the length of int"},
{"t10", 2, 4, sql.NullString{String: "abc", Valid: true}, ""},
{"t11", 2, 4, sql.NullString{String: "", Valid: true}, ""},
{"t12", 2, 4, &sql.NullString{String: "abc", Valid: true}, ""},
{"t13", 2, 3, &sql.NullString{String: "💥💥", Valid: true}, ""},
{"t14", 2, 3, &sql.NullString{String: "💥", Valid: true}, "the length must be between 2 and 3"},
}
for _, test := range tests {
r := RuneLength(test.min, test.max)
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
}
}
func Test_LengthRule_Error(t *testing.T) {
r := Length(10, 20)
assert.Equal(t, "the length must be between 10 and 20", r.message)
r = Length(0, 20)
assert.Equal(t, "the length must be no more than 20", r.message)
r = Length(10, 0)
assert.Equal(t, "the length must be no less than 10", r.message)
r.Error("123")
assert.Equal(t, "123", r.message)
}