d19de30d by miraclesu

add test

1 parent 6d05163c
...@@ -72,7 +72,7 @@ func (r *ValidationResult) Message(message string, args ...interface{}) *Validat ...@@ -72,7 +72,7 @@ func (r *ValidationResult) Message(message string, args ...interface{}) *Validat
72 if len(args) == 0 { 72 if len(args) == 0 {
73 r.Error.Message = message 73 r.Error.Message = message
74 } else { 74 } else {
75 r.Error.Message = fmt.Sprintf(message, args) 75 r.Error.Message = fmt.Sprintf(message, args...)
76 } 76 }
77 } 77 }
78 return r 78 return r
...@@ -107,15 +107,15 @@ func (v *Validation) Length(obj interface{}, n int, key string) *ValidationResul ...@@ -107,15 +107,15 @@ func (v *Validation) Length(obj interface{}, n int, key string) *ValidationResul
107 return v.apply(Length{n, key}, obj) 107 return v.apply(Length{n, key}, obj)
108 } 108 }
109 109
110 func (v *Validation) Alpha(obj interface{}, n int, key string) *ValidationResult { 110 func (v *Validation) Alpha(obj interface{}, key string) *ValidationResult {
111 return v.apply(Alpha{key}, obj) 111 return v.apply(Alpha{key}, obj)
112 } 112 }
113 113
114 func (v *Validation) Numeric(obj interface{}, n int, key string) *ValidationResult { 114 func (v *Validation) Numeric(obj interface{}, key string) *ValidationResult {
115 return v.apply(Numeric{key}, obj) 115 return v.apply(Numeric{key}, obj)
116 } 116 }
117 117
118 func (v *Validation) AlphaNumeric(obj interface{}, n int, key string) *ValidationResult { 118 func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult {
119 return v.apply(AlphaNumeric{key}, obj) 119 return v.apply(AlphaNumeric{key}, obj)
120 } 120 }
121 121
......
1 package validation
2
3 import (
4 "regexp"
5 "testing"
6 "time"
7 )
8
9 func TestRequired(t *testing.T) {
10 valid := Validation{}
11
12 if valid.Required(nil, "nil").Ok {
13 t.Error("nil object should be false")
14 }
15 if valid.Required("", "string").Ok {
16 t.Error("\"'\" string should be false")
17 }
18 if !valid.Required("astaxie", "string").Ok {
19 t.Error("string should be true")
20 }
21 if valid.Required(0, "zero").Ok {
22 t.Error("Integer should not be equal 0")
23 }
24 if !valid.Required(1, "int").Ok {
25 t.Error("Integer except 0 should be true")
26 }
27 if !valid.Required(time.Now(), "time").Ok {
28 t.Error("time should be true")
29 }
30 if valid.Required([]string{}, "emptySlice").Ok {
31 t.Error("empty slice should be false")
32 }
33 if !valid.Required([]interface{}{"ok"}, "slice").Ok {
34 t.Error("slice should be equal true")
35 }
36 }
37
38 func TestMin(t *testing.T) {
39 valid := Validation{}
40
41 if valid.Min(-1, 0, "min0").Ok {
42 t.Error("-1 is less than the minimum value of 0 should be false")
43 }
44 if !valid.Min(1, 0, "min0").Ok {
45 t.Error("1 is greater or equal than the minimum value of 0 should be true")
46 }
47 }
48
49 func TestMax(t *testing.T) {
50 valid := Validation{}
51
52 if valid.Max(1, 0, "max0").Ok {
53 t.Error("1 is greater than the minimum value of 0 should be false")
54 }
55 if !valid.Max(-1, 0, "max0").Ok {
56 t.Error("-1 is less or equal than the maximum value of 0 should be true")
57 }
58 }
59
60 func TestRange(t *testing.T) {
61 valid := Validation{}
62
63 if valid.Range(-1, 0, 1, "range0_1").Ok {
64 t.Error("-1 is bettween 0 and 1 should be false")
65 }
66 if !valid.Range(1, 0, 1, "range0_1").Ok {
67 t.Error("1 is bettween 0 and 1 should be true")
68 }
69 }
70
71 func TestMinSize(t *testing.T) {
72 valid := Validation{}
73
74 if valid.MinSize("", 1, "minSize1").Ok {
75 t.Error("the length of \"\" is less than the minimum value of 1 should be false")
76 }
77 if !valid.MinSize("ok", 1, "minSize1").Ok {
78 t.Error("the length of \"ok\" is greater or equal than the minimum value of 1 should be true")
79 }
80 if valid.MinSize([]string{}, 1, "minSize1").Ok {
81 t.Error("the length of empty slice is less than the minimum value of 1 should be false")
82 }
83 if !valid.MinSize([]interface{}{"ok"}, 1, "minSize1").Ok {
84 t.Error("the length of [\"ok\"] is greater or equal than the minimum value of 1 should be true")
85 }
86 }
87
88 func TestMaxSize(t *testing.T) {
89 valid := Validation{}
90
91 if valid.MaxSize("ok", 1, "maxSize1").Ok {
92 t.Error("the length of \"ok\" is greater than the maximum value of 1 should be false")
93 }
94 if !valid.MaxSize("", 1, "maxSize1").Ok {
95 t.Error("the length of \"\" is less or equal than the maximum value of 1 should be true")
96 }
97 if valid.MaxSize([]interface{}{"ok", false}, 1, "maxSize1").Ok {
98 t.Error("the length of [\"ok\", false] is greater than the maximum value of 1 should be false")
99 }
100 if !valid.MaxSize([]string{}, 1, "maxSize1").Ok {
101 t.Error("the length of empty slice is less or equal than the maximum value of 1 should be true")
102 }
103 }
104
105 func TestLength(t *testing.T) {
106 valid := Validation{}
107
108 if valid.Length("", 1, "length1").Ok {
109 t.Error("the length of \"\" must equal 1 should be false")
110 }
111 if !valid.Length("1", 1, "length1").Ok {
112 t.Error("the length of \"1\" must equal 1 should be true")
113 }
114 if valid.Length([]string{}, 1, "length1").Ok {
115 t.Error("the length of empty slice must equal 1 should be false")
116 }
117 if !valid.Length([]interface{}{"ok"}, 1, "length1").Ok {
118 t.Error("the length of [\"ok\"] must equal 1 should be true")
119 }
120 }
121
122 func TestAlpha(t *testing.T) {
123 valid := Validation{}
124
125 if valid.Alpha("a,1-@ $", "alpha").Ok {
126 t.Error("\"a,1-@ $\" are valid alpha characters should be false")
127 }
128 if !valid.Alpha("abCD", "alpha").Ok {
129 t.Error("\"abCD\" are valid alpha characters should be true")
130 }
131 }
132
133 func TestNumeric(t *testing.T) {
134 valid := Validation{}
135
136 if valid.Numeric("a,1-@ $", "numeric").Ok {
137 t.Error("\"a,1-@ $\" are valid numeric characters should be false")
138 }
139 if !valid.Numeric("1234", "numeric").Ok {
140 t.Error("\"1234\" are valid numeric characters should be true")
141 }
142 }
143
144 func TestAlphaNumeric(t *testing.T) {
145 valid := Validation{}
146
147 if valid.AlphaNumeric("a,1-@ $", "alphaNumeric").Ok {
148 t.Error("\"a,1-@ $\" are valid alpha or numeric characters should be false")
149 }
150 if !valid.AlphaNumeric("1234aB", "alphaNumeric").Ok {
151 t.Error("\"1234aB\" are valid alpha or numeric characters should be true")
152 }
153 }
154
155 func TestMatch(t *testing.T) {
156 valid := Validation{}
157
158 if valid.Match("suchuangji@gmail", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
159 t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be false")
160 }
161 if !valid.Match("suchuangji@gmail.com", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
162 t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be true")
163 }
164 }
165
166 func TestNoMatch(t *testing.T) {
167 valid := Validation{}
168
169 if valid.NoMatch("123@gmail", regexp.MustCompile("[^\\w\\d]"), "nomatch").Ok {
170 t.Error("\"123@gmail\" not match \"[^\\w\\d]\" should be false")
171 }
172 if !valid.NoMatch("123gmail", regexp.MustCompile("[^\\w\\d]"), "match").Ok {
173 t.Error("\"123@gmail\" not match \"[^\\w\\d@]\" should be true")
174 }
175 }
176
177 func TestAlphaDash(t *testing.T) {
178 valid := Validation{}
179
180 if valid.AlphaDash("a,1-@ $", "alphaDash").Ok {
181 t.Error("\"a,1-@ $\" are valid alpha or numeric or dash(-_) characters should be false")
182 }
183 if !valid.AlphaDash("1234aB-_", "alphaDash").Ok {
184 t.Error("\"1234aB\" are valid alpha or numeric or dash(-_) characters should be true")
185 }
186 }
187
188 func TestEmail(t *testing.T) {
189 valid := Validation{}
190
191 if valid.Email("not@a email", "email").Ok {
192 t.Error("\"not@a email\" is a valid email address should be false")
193 }
194 if !valid.Email("suchuangji@gmail.com", "email").Ok {
195 t.Error("\"suchuangji@gmail.com\" is a valid email address should be true")
196 }
197 }
198
199 func TestIP(t *testing.T) {
200 valid := Validation{}
201
202 if valid.IP("11.255.255.256", "IP").Ok {
203 t.Error("\"11.255.255.256\" is a valid ip address should be false")
204 }
205 if !valid.IP("01.11.11.11", "IP").Ok {
206 t.Error("\"suchuangji@gmail.com\" is a valid ip address should be true")
207 }
208 }
209
210 func TestBase64(t *testing.T) {
211 valid := Validation{}
212
213 if valid.Base64("suchuangji@gmail.com", "base64").Ok {
214 t.Error("\"suchuangji@gmail.com\" are a valid base64 characters should be false")
215 }
216 if !valid.Base64("c3VjaHVhbmdqaUBnbWFpbC5jb20=", "base64").Ok {
217 t.Error("\"c3VjaHVhbmdqaUBnbWFpbC5jb20=\" are a valid base64 characters should be true")
218 }
219 }
...@@ -196,6 +196,7 @@ func (a Alpha) IsSatisfied(obj interface{}) bool { ...@@ -196,6 +196,7 @@ func (a Alpha) IsSatisfied(obj interface{}) bool {
196 return false 196 return false
197 } 197 }
198 } 198 }
199 return true
199 } 200 }
200 return false 201 return false
201 } 202 }
...@@ -219,6 +220,7 @@ func (n Numeric) IsSatisfied(obj interface{}) bool { ...@@ -219,6 +220,7 @@ func (n Numeric) IsSatisfied(obj interface{}) bool {
219 return false 220 return false
220 } 221 }
221 } 222 }
223 return true
222 } 224 }
223 return false 225 return false
224 } 226 }
...@@ -242,6 +244,7 @@ func (a AlphaNumeric) IsSatisfied(obj interface{}) bool { ...@@ -242,6 +244,7 @@ func (a AlphaNumeric) IsSatisfied(obj interface{}) bool {
242 return false 244 return false
243 } 245 }
244 } 246 }
247 return true
245 } 248 }
246 return false 249 return false
247 } 250 }
...@@ -284,7 +287,7 @@ func (n NoMatch) IsSatisfied(obj interface{}) bool { ...@@ -284,7 +287,7 @@ func (n NoMatch) IsSatisfied(obj interface{}) bool {
284 } 287 }
285 288
286 func (n NoMatch) DefaultMessage() string { 289 func (n NoMatch) DefaultMessage() string {
287 return fmt.Sprintln("Must no match", n.Regexp) 290 return fmt.Sprintln("Must not match", n.Regexp)
288 } 291 }
289 292
290 func (n NoMatch) GetKey() string { 293 func (n NoMatch) GetKey() string {
...@@ -299,7 +302,7 @@ type AlphaDash struct { ...@@ -299,7 +302,7 @@ type AlphaDash struct {
299 } 302 }
300 303
301 func (a AlphaDash) DefaultMessage() string { 304 func (a AlphaDash) DefaultMessage() string {
302 return fmt.Sprintln("Must be valid characters") 305 return fmt.Sprintln("Must be valid alpha or numeric or dash(-_) characters")
303 } 306 }
304 307
305 func (a AlphaDash) GetKey() string { 308 func (a AlphaDash) GetKey() string {
...@@ -321,7 +324,7 @@ func (e Email) GetKey() string { ...@@ -321,7 +324,7 @@ func (e Email) GetKey() string {
321 return e.Key 324 return e.Key
322 } 325 }
323 326
324 var ipPattern = regexp.MustCompile("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)") 327 var ipPattern = regexp.MustCompile("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$")
325 328
326 type IP struct { 329 type IP struct {
327 Match 330 Match
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!