加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
spec_func_test.go 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
andeyalee 提交于 2021-02-02 11:01 . chore:fix import path
// Copyright 2019 Bytedance Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tagexpr_test
import (
"regexp"
"testing"
"github.com/bytedance/go-tagexpr/v2"
)
func TestFunc(t *testing.T) {
var emailRegexp = regexp.MustCompile(
"^([A-Za-z0-9_\\-\\.\u4e00-\u9fa5])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,8})$",
)
tagexpr.RegFunc("email", func(args ...interface{}) interface{} {
if len(args) == 0 {
return false
}
s, ok := args[0].(string)
if !ok {
return false
}
t.Log(s)
return emailRegexp.MatchString(s)
})
var vm = tagexpr.New("te")
type T struct {
Email string `te:"email($)"`
}
var cases = []struct {
email string
expect bool
}{
{"", false},
{"henrylee2cn@gmail.com", true},
}
obj := new(T)
for _, c := range cases {
obj.Email = c.email
te := vm.MustRun(obj)
got := te.EvalBool("Email")
if got != c.expect {
t.Fatalf("email: %s, expect: %v, but got: %v", c.email, c.expect, got)
}
}
// test len
type R struct {
Str string `vd:"mblen($)<6"`
}
var lenCases = []struct {
str string
expect bool
}{
{"123", true},
{"一二三四五六七", false},
{"一二三四五", true},
}
lenObj := new(R)
vm = tagexpr.New("vd")
for _, lenCase := range lenCases {
lenObj.Str = lenCase.str
te := vm.MustRun(lenObj)
got := te.EvalBool("Str")
if got != lenCase.expect {
t.Fatalf("string: %v, expect: %v, but got: %v", lenCase.str, lenCase.expect, got)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化