-
Notifications
You must be signed in to change notification settings - Fork 224
/
minmax_test.go
148 lines (133 loc) · 4.86 KB
/
minmax_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
// 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 (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMin(t *testing.T) {
date0 := time.Time{}
date20000101 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
date20001201 := time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)
date20000601 := time.Date(2000, 6, 1, 0, 0, 0, 0, time.UTC)
tests := []struct {
tag string
threshold interface{}
exclusive bool
value interface{}
err string
}{
// int cases
{"t1.1", 1, false, 1, ""},
{"t1.2", 1, false, 2, ""},
{"t1.3", 1, false, -1, "must be no less than 1"},
{"t1.4", 1, false, 0, ""},
{"t1.5", 1, true, 1, "must be greater than 1"},
{"t1.6", 1, false, "1", "cannot convert string to int64"},
{"t1.7", "1", false, 1, "type not supported: string"},
// uint cases
{"t2.1", uint(2), false, uint(2), ""},
{"t2.2", uint(2), false, uint(3), ""},
{"t2.3", uint(2), false, uint(1), "must be no less than 2"},
{"t2.4", uint(2), false, uint(0), ""},
{"t2.5", uint(2), true, uint(2), "must be greater than 2"},
{"t2.6", uint(2), false, "1", "cannot convert string to uint64"},
// float cases
{"t3.1", float64(2), false, float64(2), ""},
{"t3.2", float64(2), false, float64(3), ""},
{"t3.3", float64(2), false, float64(1), "must be no less than 2"},
{"t3.4", float64(2), false, float64(0), ""},
{"t3.5", float64(2), true, float64(2), "must be greater than 2"},
{"t3.6", float64(2), false, "1", "cannot convert string to float64"},
// Time cases
{"t4.1", date20000601, false, date20000601, ""},
{"t4.2", date20000601, false, date20001201, ""},
{"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.4", date20000601, false, date0, ""},
{"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.6", date20000601, true, 1, "cannot convert int to time.Time"},
{"t4.7", struct{}{}, false, 1, "type not supported: struct {}"},
{"t4.8", date0, false, date20000601, ""},
}
for _, test := range tests {
r := Min(test.threshold)
if test.exclusive {
r = r.Exclusive()
}
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
}
}
func TestMinError(t *testing.T) {
r := Min(10)
assert.Equal(t, "must be no less than 10", r.Validate(9).Error())
r = r.Error("123")
assert.Equal(t, "123", r.err.Message())
}
func TestMax(t *testing.T) {
date0 := time.Time{}
date20000101 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
date20001201 := time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)
date20000601 := time.Date(2000, 6, 1, 0, 0, 0, 0, time.UTC)
tests := []struct {
tag string
threshold interface{}
exclusive bool
value interface{}
err string
}{
// int cases
{"t1.1", 2, false, 2, ""},
{"t1.2", 2, false, 1, ""},
{"t1.3", 2, false, 3, "must be no greater than 2"},
{"t1.4", 2, false, 0, ""},
{"t1.5", 2, true, 2, "must be less than 2"},
{"t1.6", 2, false, "1", "cannot convert string to int64"},
{"t1.7", "1", false, 1, "type not supported: string"},
// uint cases
{"t2.1", uint(2), false, uint(2), ""},
{"t2.2", uint(2), false, uint(1), ""},
{"t2.3", uint(2), false, uint(3), "must be no greater than 2"},
{"t2.4", uint(2), false, uint(0), ""},
{"t2.5", uint(2), true, uint(2), "must be less than 2"},
{"t2.6", uint(2), false, "1", "cannot convert string to uint64"},
// float cases
{"t3.1", float64(2), false, float64(2), ""},
{"t3.2", float64(2), false, float64(1), ""},
{"t3.3", float64(2), false, float64(3), "must be no greater than 2"},
{"t3.4", float64(2), false, float64(0), ""},
{"t3.5", float64(2), true, float64(2), "must be less than 2"},
{"t3.6", float64(2), false, "1", "cannot convert string to float64"},
// Time cases
{"t4.1", date20000601, false, date20000601, ""},
{"t4.2", date20000601, false, date20000101, ""},
{"t4.3", date20000601, false, date20001201, "must be no greater than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.4", date20000601, false, date0, ""},
{"t4.5", date20000601, true, date20000601, "must be less than 2000-06-01 00:00:00 +0000 UTC"},
{"t4.6", date20000601, true, 1, "cannot convert int to time.Time"},
}
for _, test := range tests {
r := Max(test.threshold)
if test.exclusive {
r = r.Exclusive()
}
err := r.Validate(test.value)
assertError(t, test.err, err, test.tag)
}
}
func TestMaxError(t *testing.T) {
r := Max(10)
assert.Equal(t, "must be no greater than 10", r.Validate(11).Error())
r = r.Error("123")
assert.Equal(t, "123", r.err.Message())
}
func TestThresholdRule_ErrorObject(t *testing.T) {
r := Max(10)
err := NewError("code", "abc")
r = r.ErrorObject(err)
assert.Equal(t, err, r.err)
assert.Equal(t, err.Code(), r.err.Code())
assert.Equal(t, err.Message(), r.err.Message())
}