add more unit test cases for package gvalid

This commit is contained in:
John 2019-03-11 22:58:21 +08:00
parent 733c5db228
commit 79a3aa5916
3 changed files with 45 additions and 31 deletions

View File

@ -67,35 +67,4 @@ type CustomMsg = map[string]interface{}
func parseSequenceTag(tag string) (name, rule, msg string) {
match, _ := gregex.MatchString(`\s*((\w+)\s*@){0,1}\s*([^#]+)\s*(#\s*(.*)){0,1}\s*`, tag)
return strings.TrimSpace(match[2]), strings.TrimSpace(match[3]), strings.TrimSpace(match[5])
}
// 解析sequence tag为标准校验规则及自定义错误
func parseSequenceTags(tags []string) (rules map[string]string, msgs map[string]interface{}) {
rules = make(map[string]string)
msgs = make(map[string]interface{})
for _, tag := range tags {
name, rule, msg := parseSequenceTag(tag)
// 校验规则
if len(name) == 0 {
continue
}
rules[name] = rule
// 错误提示
if len(msg) > 0 {
ruleArray := strings.Split(rule, "|")
msgArray := strings.Split(msg, "|")
for k, v := range ruleArray {
if len(msgArray[k]) == 0 {
continue
}
// 关联校验会有":"符号
array := strings.Split(v, ":")
if _, ok := msgs[name]; !ok {
msgs[name] = make(map[string]string)
}
msgs[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
}
}
}
return
}

View File

@ -400,6 +400,9 @@ func Test_QQ(t *testing.T) {
}
func Test_Ip(t *testing.T) {
if m := gvalid.Check("10.0.0.1", "ip", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check("10.0.0.1", "ipv4", nil); m != nil {
t.Error(m)
}
@ -409,12 +412,21 @@ func Test_Ip(t *testing.T) {
if m := gvalid.Check("1920.0.0.0", "ipv4", nil); m == nil {
t.Error("ipv4校验失败")
}
if m := gvalid.Check("1920.0.0.0", "ip", nil); m == nil {
t.Error("ipv4校验失败")
}
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
t.Error(m)
}
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
t.Error(m)
}
}
func Test_IPv4(t *testing.T) {

View File

@ -7,6 +7,7 @@
package gvalid_test
import (
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gvalid"
"testing"
)
@ -65,3 +66,35 @@ func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) {
}
}
func Test_Sequence(t *testing.T) {
gtest.Case(t, func() {
params := map[string]interface{} {
"passport" : "",
"password" : "123456",
"password2" : "1234567",
}
rules := []string {
"passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间",
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等",
"password2@required|length:6,16#",
}
err := gvalid.CheckMap(params, rules)
gtest.AssertNE(err, nil)
gtest.Assert(len(err.Map()), 2)
gtest.Assert(err.Map()["required"], "账号不能为空")
gtest.Assert(err.Map()["length"], "账号长度应当在6到16之间")
gtest.Assert(len(err.Maps()), 2)
gtest.Assert(err.String(), "账号不能为空; 账号长度应当在6到16之间; 两次密码输入不相等")
gtest.Assert(err.Strings(), []string{"账号不能为空", "账号长度应当在6到16之间", "两次密码输入不相等"})
k, m := err.FirstItem()
gtest.Assert(k, "passport")
gtest.Assert(m, err.Map())
r, s := err.FirstRule()
gtest.Assert(r, "required")
gtest.Assert(s, "账号不能为空")
})
}