github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/errMsg_test.go (about) 1 package main 2 3 import ( 4 "github.com/isyscore/isc-gobase/validate" 5 "testing" 6 ) 7 8 type ErrMsgEntity1 struct { 9 Name string `match:"value=zhou" errMsg:"对应的值不合法"` 10 Age int 11 } 12 13 type ErrMsgEntity2 struct { 14 Name string `match:"value=zhou"` 15 Age int `match:"condition=#current > 10" errMsg:"当前的值不合法,应该大于10,当前值为#current,对应的名字为#root.Name"` 16 } 17 18 type ErrMsgEntity3 struct { 19 Name string `match:"value=zhou" errMsg:"当前值不合法,只可为zhou,当前的值为#current,年龄为#root.Age"` 20 Age int `match:"condition=#current > 10" errMsg:"当前的值不合法,应该大于10,当前值为#current,对应的名字为#root.Name"` 21 } 22 23 func TestErrMsg1(t *testing.T) { 24 var value ErrMsgEntity1 25 var result bool 26 var err string 27 28 // 测试 正常情况 29 value = ErrMsgEntity1{Name: "zhou"} 30 result, _ = validate.Check(value, "name") 31 True(t, result) 32 33 // 测试 正常情况 34 value = ErrMsgEntity1{Name: "宋江"} 35 result, err = validate.Check(value, "name") 36 Equal(t, err, "对应的值不合法", result, false) 37 } 38 39 func TestErrMsg2(t *testing.T) { 40 var value ErrMsgEntity2 41 var result bool 42 var err string 43 44 // 测试 正常情况 45 //value = ErrMsgEntity2{Name: "zhou", Age: 12} 46 //result, _ = validate.Check(value) 47 //assert.True(t, result) 48 49 // 测试 正常情况 50 value = ErrMsgEntity2{Name: "zhou", Age: 2} 51 result, err = validate.Check(value) 52 Equal(t, err, "当前的值不合法,应该大于10,当前值为2,对应的名字为zhou", result, false) 53 } 54 55 func TestErrMsg3(t *testing.T) { 56 var value ErrMsgEntity3 57 var result bool 58 var err string 59 60 // 测试 正常情况 61 value = ErrMsgEntity3{Name: "zhou", Age: 12} 62 result, _ = validate.Check(value) 63 True(t, result) 64 65 // 测试 正常情况 66 value = ErrMsgEntity3{Name: "zhou", Age: 2} 67 result, err = validate.Check(value) 68 Equal(t, err, "当前的值不合法,应该大于10,当前值为2,对应的名字为zhou", result, false) 69 70 // 测试 正常情况 71 value = ErrMsgEntity3{Name: "宋江", Age: 12} 72 result, err = validate.Check(value, "name") 73 Equal(t, err, "当前值不合法,只可为zhou,当前的值为宋江,年龄为12", result, false) 74 75 // 测试 正常情况 76 value = ErrMsgEntity3{Name: "宋江", Age: 3} 77 result, err = validate.Check(value, "age") 78 Equal(t, err, "当前的值不合法,应该大于10,当前值为3,对应的名字为宋江", result, false) 79 }