github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/regex_test.go (about) 1 package main 2 3 import ( 4 "github.com/isyscore/isc-gobase/validate" 5 "testing" 6 ) 7 8 type ValueRegexEntity struct { 9 Name string `match:"regex=^zhou.*zhen$"` 10 Age int `match:"regex=^\\d+$"` 11 } 12 13 func TestRegex(t *testing.T) { 14 var value ValueRegexEntity 15 var result bool 16 var err string 17 18 // 测试 正常情况 19 value = ValueRegexEntity{Name: "zhouOKzhen"} 20 result, err = validate.Check(value, "name") 21 TrueErr(t, result, err) 22 23 // 测试 正常情况 24 value = ValueRegexEntity{Age: 13} 25 result, err = validate.Check(value, "age") 26 TrueErr(t, result, err) 27 28 // 测试 异常情况 29 value = ValueRegexEntity{Name: "chenzhen"} 30 result, err = validate.Check(value, "name") 31 Equal(t, "属性 Name 的值 chenzhen 没命中只允许的正则表达式 ^zhou.*zhen$ ", err, result, false) 32 } 33 34 // Regex的基准测试 35 func Benchmark_Regex(b *testing.B) { 36 var value ValueRegexEntity 37 for i := 0; i < b.N; i++ { 38 value = ValueRegexEntity{Name: "chenzhen"} 39 validate.Check(value, "name") 40 } 41 }