-
Notifications
You must be signed in to change notification settings - Fork 142
/
spec_func_test.go
100 lines (90 loc) · 2.26 KB
/
spec_func_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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"
"github.com/stretchr/testify/assert"
)
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)
}
}
}
func TestRangeIn(t *testing.T) {
var vm = tagexpr.New("te")
type S struct {
F []string `te:"range($, in(#v, '', 'ttp', 'euttp'))"`
}
a := []string{"ttp", "", "euttp"}
r := vm.MustRun(S{
F: a,
// F: b,
})
assert.Equal(t, []interface{}{true, true, true}, r.Eval("F"))
}