-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_int_test.go
250 lines (233 loc) · 5.19 KB
/
spec_int_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
package vd
import (
"testing"
)
// type IntNotAllowZero struct {
// Age int
// }
// func (v IntNotAllowZero) VD(r *Rule) (err error) {
// r.Int(v.Age, IntSpec{
// Name: "年龄",
// })
// }
// func TestIntNotAllowZero(t *testing.T) {
//
// checker := NewCN()
// CheckEqualAndNoError(t, checker, IntNotAllowZero{Age:0}, Report{
// Fail: true,
// Message: "年龄不允许为0",
// })
// CheckEqualAndNoError(t, checker, IntNotAllowZero{Age:1}, Report{
// Fail: false,
// Message: "",
// })
// }
// type IntAllowZero struct {
// Age int
// }
// func (v IntAllowZero) VD(r *Rule) (err error) {
// r.Int(v.Age, IntSpec{
// Name: "年龄",
// AllowZero: true,
// })
// }
// func TestIntAllowZero(t *testing.T) {
//
// checker := NewCN()
// CheckEqualAndNoError(t, checker, IntAllowZero{Age:0}, Report{
// Fail: false,
// Message: "",
// })
// CheckEqualAndNoError(t, checker, IntAllowZero{Age:1}, Report{
// Fail: false,
// Message: "",
// })
// }
type IntMin struct {
Age int
}
func (v IntMin) VD(r *Rule) (err error) {
r.Int(v.Age, IntSpec{
Name: "年龄",
Min: Int(18),
})
return nil
}
func TestIntMin(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntMin{Age:17}, Report{
Fail: true,
Message: "年龄不能小于18",
})
CheckEqualAndNoError(t, checker, IntMin{Age:18}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMin{Age:19}, Report{
Fail: false,
Message: "",
})
}
type IntMinMessage struct {
Age int
}
func (v IntMinMessage) VD(r *Rule) (err error) {
r.Int(v.Age, IntSpec{
Name: "年龄",
Min: Int(18),
MinMessage:"年龄不可以小于{{Min}}",
})
return nil
}
func TestIntMinMessage(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntMinMessage{Age:17}, Report{
Fail: true,
Message: "年龄不可以小于18",
})
CheckEqualAndNoError(t, checker, IntMinMessage{Age:18}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMinMessage{Age:19}, Report{
Fail: false,
Message: "",
})
}
type IntMax struct {
Age int
}
func (v IntMax) VD(r *Rule) (err error) {
r.Int(v.Age, IntSpec{
Name: "年龄",
Max: Int(18),
})
return nil
}
func TestIntMax(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntMax{Age:17}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMax{Age:18}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMax{Age:19}, Report{
Fail: true,
Message: "年龄不能大于18",
})
}
type IntMaxMessage struct {
Age int
}
func (v IntMaxMessage) VD(r *Rule) (err error) {
r.Int(v.Age, IntSpec{
Name: "年龄",
Max: Int(18),
MaxMessage:"年龄不可以大于{{Max}}",
})
return nil
}
func TestIntMaxMessage(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntMaxMessage{Age:17}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMaxMessage{Age:18}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMaxMessage{Age:19}, Report{
Fail: true,
Message: "年龄不可以大于18",
})
}
type IntMinMax struct {
Age int
}
func (v IntMinMax) VD (r *Rule) (err error) {
r.Int(v.Age, IntSpec{
Name: "年龄",
Min: Int(2),
Max: Int(4),
})
return nil
}
func TestIntMinMax(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntMinMax{Age: 0}, Report{
Fail: true,
Message: "年龄不能小于2",
})
CheckEqualAndNoError(t, checker, IntMinMax{Age: 1}, Report{
Fail: true,
Message: "年龄不能小于2",
})
CheckEqualAndNoError(t, checker, IntMinMax{Age: 2}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMinMax{Age: 3}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMinMax{Age: 4}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntMinMax{Age: 5}, Report{
Fail: true,
Message: "年龄不能大于4",
})
}
type IntPattern struct {
Number int
}
func (v IntPattern) VD (r *Rule) (err error) {
r.Int(v.Number, IntSpec{
Name: "号码",
Pattern: []string{`^138`},
PatternMessage: "{{Name}}必须以138开头",
})
return nil
}
func TestIntPattern(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntPattern{Number: 11384}, Report{
Fail: true,
Message: "号码必须以138开头",
})
CheckEqualAndNoError(t, checker, IntPattern{Number: 138}, Report{
Fail: false,
Message: "",
})
}
type IntBanPattern struct {
Number int
}
func (v IntBanPattern) VD (r *Rule) (err error) {
r.Int(v.Number, IntSpec{
Name: "号码",
BanPattern: []string{`^138`, `^178`},
PatternMessage: "{{Name}}不允许以138和178开头",
})
return nil
}
func TestIntBanPattern(t *testing.T) {
checker := NewCN()
CheckEqualAndNoError(t, checker, IntBanPattern{Number: 11384}, Report{
Fail: false,
Message: "",
})
CheckEqualAndNoError(t, checker, IntBanPattern{Number: 138}, Report{
Fail: true,
Message: "号码不允许以138和178开头",
})
CheckEqualAndNoError(t, checker, IntBanPattern{Number: 178}, Report{
Fail: true,
Message: "号码不允许以138和178开头",
})
}