加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tagexpr_test.go 17.81 KB
一键复制 编辑 原始数据 按行查看 历史
andeyalee 提交于 2021-08-05 21:34 . feat: support 'range' tpl function #12
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
// 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
import (
"reflect"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func BenchmarkTagExpr(b *testing.B) {
type T struct {
a int `bench:"$%3"`
}
vm := New("bench")
vm.MustRun(new(T)) // warm up
b.ReportAllocs()
b.ResetTimer()
var t = &T{10}
for i := 0; i < b.N; i++ {
tagExpr, err := vm.Run(t)
if err != nil {
b.FailNow()
}
if tagExpr.EvalFloat("a") != 1 {
b.FailNow()
}
}
}
func BenchmarkReflect(b *testing.B) {
type T struct {
a int `remainder:"3"`
}
b.ReportAllocs()
b.ResetTimer()
var t = &T{1}
for i := 0; i < b.N; i++ {
v := reflect.ValueOf(t).Elem()
ft, ok := v.Type().FieldByName("a")
if !ok {
b.FailNow()
}
x, err := strconv.ParseInt(ft.Tag.Get("remainder"), 10, 64)
if err != nil {
b.FailNow()
}
fv := v.FieldByName("a")
if fv.Int()%x != 1 {
b.FailNow()
}
}
}
func Test(t *testing.T) {
g := &struct {
_ int
h string `tagexpr:"$"`
s []string
m map[string][]string
}{
h: "haha",
s: []string{"1"},
m: map[string][]string{"0": {"2"}},
}
d := "ddd"
e := new(int)
*e = 3
type iface interface{}
var cases = []struct {
tagName string
structure interface{}
tests map[string]interface{}
}{
{
tagName: "tagexpr",
structure: &struct {
A int `tagexpr:"$>0&&$<10&&!''&&!!!0&&!nil&&$"`
A2 int `tagexpr:"@:$>0&&$<10"`
b string `tagexpr:"is:$=='test';msg:sprintf('expect: test, but got: %s',$)"`
c float32 `tagexpr:"(A)$+$"`
d *string `tagexpr:"$"`
e **int `tagexpr:"$"`
f *[3]int `tagexpr:"x:len($)"`
g string `tagexpr:"x:!regexp('xxx',$);y:regexp('g\\d{3}$')"`
h []string `tagexpr:"x:$[1];y:$[10]"`
i map[string]int `tagexpr:"x:$['a'];y:$[0];z:$==nil"`
i2 *map[string]int `tagexpr:"x:$['a'];y:$[0];z:$"`
j, j2 iface `tagexpr:"@:$==1;y:$"`
k *iface `tagexpr:"$==nil"`
m *struct{ i int } `tagexpr:"@:$;x:$['a']['x']"`
}{
A: 5.0,
A2: 5.0,
b: "x",
c: 1,
d: &d,
e: &e,
f: new([3]int),
g: "g123",
h: []string{"", "hehe"},
i: map[string]int{"a": 7},
j2: iface(1),
m: &struct{ i int }{1},
},
tests: map[string]interface{}{
"A": true,
"A2": true,
"b@is": false,
"b@msg": "expect: test, but got: x",
"c": 6.0,
"d": d,
"e": float64(*e),
"f@x": float64(3),
"g@x": true,
"g@y": true,
"h@x": "hehe",
"h@y": nil,
"i@x": 7.0,
"i@y": nil,
"i@z": false,
"i2@x": nil,
"i2@y": nil,
"i2@z": nil,
"j": false,
"j@y": nil,
"j2": true,
"j2@y": 1.0,
"k": true,
"m": &struct{ i int }{1},
"m@x": nil,
},
},
{
tagName: "tagexpr",
structure: &struct {
A int `tagexpr:"$>0&&$<10"`
b string `tagexpr:"is:$=='test';msg:sprintf('expect: test, but got: %s',$)"`
c struct {
_ int
d bool `tagexpr:"$"`
}
e *struct {
_ int
f bool `tagexpr:"$"`
}
g **struct {
_ int
h string `tagexpr:"$"`
s []string
m map[string][]string
} `tagexpr:"$['h']"`
i string `tagexpr:"(g.s)$[0]+(g.m)$['0'][0]==$"`
j bool `tagexpr:"!$"`
k int `tagexpr:"!$"`
m *int `tagexpr:"$==nil"`
n *bool `tagexpr:"$==nil"`
p *string `tagexpr:"$"`
}{
A: 5,
b: "x",
c: struct {
_ int
d bool `tagexpr:"$"`
}{d: true},
e: &struct {
_ int
f bool `tagexpr:"$"`
}{f: true},
g: &g,
i: "12",
},
tests: map[string]interface{}{
"A": true,
"b@is": false,
"b@msg": "expect: test, but got: x",
"c.d": true,
"e.f": true,
"g": "haha",
"g.h": "haha",
"i": true,
"j": true,
"k": true,
"m": true,
"n": true,
"p": nil,
},
},
{
tagName: "p",
structure: &struct {
q *struct {
x int
} `p:"(q.x)$"`
}{},
tests: map[string]interface{}{
"q": nil,
},
},
}
for i, c := range cases {
vm := New(c.tagName)
// vm.WarmUp(c.structure)
tagExpr, err := vm.Run(c.structure)
if err != nil {
t.Fatal(err)
}
for selector, value := range c.tests {
val := tagExpr.Eval(selector)
if !reflect.DeepEqual(val, value) {
t.Fatalf("Eval Serial: %d, selector: %q, got: %v, expect: %v", i, selector, val, value)
}
}
tagExpr.Range(func(eh *ExprHandler) error {
es := eh.ExprSelector()
t.Logf("Range selector: %s, field: %q exprName: %q", es, es.Field(), es.Name())
value := c.tests[es.String()]
val := eh.Eval()
if !reflect.DeepEqual(val, value) {
t.Fatalf("Range NO: %d, selector: %q, got: %v, expect: %v", i, es, val, value)
}
return nil
})
}
}
func TestFieldNotInit(t *testing.T) {
g := &struct {
_ int
h string
s []string
m map[string][]string
}{
h: "haha",
s: []string{"1"},
m: map[string][]string{"0": {"2"}},
}
structure := &struct {
A int
b string
c struct {
_ int
d *bool `expr:"test:nil"`
}
e *struct {
_ int
f bool
}
g **struct {
_ int
h string
s []string
m map[string][]string
}
i string
j bool
k int
m *int
n *bool
p *string
}{
A: 5,
b: "x",
e: &struct {
_ int
f bool
}{f: true},
g: &g,
i: "12",
}
vm := New("expr")
e, err := vm.Run(structure)
if err != nil {
t.Fatal(err)
}
cases := []struct {
fieldSelector string
value interface{}
}{
{"A", structure.A},
{"b", structure.b},
{"c", structure.c},
{"c._", 0},
{"c.d", structure.c.d},
{"e", structure.e},
{"e._", 0},
{"e.f", structure.e.f},
{"g", structure.g},
{"g._", 0},
{"g.h", (*structure.g).h},
{"g.s", (*structure.g).s},
{"g.m", (*structure.g).m},
{"i", structure.i},
{"j", structure.j},
{"k", structure.k},
{"m", structure.m},
{"n", structure.n},
{"p", structure.p},
}
for _, c := range cases {
fh, _ := e.Field(c.fieldSelector)
val := fh.Value(false).Interface()
assert.Equal(t, c.value, val, c.fieldSelector)
}
var i int
e.RangeFields(func(fh *FieldHandler) bool {
val := fh.Value(false).Interface()
if fh.StringSelector() == "c.d" {
assert.NotNil(t, fh.EvalFuncs()["c.d@test"])
}
assert.Equal(t, cases[i].value, val, fh.StringSelector())
i++
return true
})
var wall uint64 = 1024
unix := time.Unix(1549186325, int64(wall))
e, err = vm.Run(&unix)
if err != nil {
t.Fatal(err)
}
fh, _ := e.Field("wall")
val := fh.Value(false).Interface()
if !reflect.DeepEqual(val, wall) {
t.Fatalf("Time.wall: got: %v(%[1]T), expect: %v(%[2]T)", val, wall)
}
}
func TestFieldInitZero(t *testing.T) {
g := &struct {
_ int
h string
s []string
m map[string][]string
}{
h: "haha",
s: []string{"1"},
m: map[string][]string{"0": {"2"}},
}
structure := &struct {
A int
b string
c struct {
_ int
d *bool
}
e *struct {
_ int
f bool
}
g **struct {
_ int
h string
s []string
m map[string][]string
}
g2 ****struct {
_ int
h string
s []string
m map[string][]string
}
i string
j bool
k int
m *int
n *bool
p *string
}{
A: 5,
b: "x",
e: &struct {
_ int
f bool
}{f: true},
g: &g,
i: "12",
}
vm := New("")
e, err := vm.Run(structure)
if err != nil {
t.Fatal(err)
}
cases := []struct {
fieldSelector string
value interface{}
}{
{"A", structure.A},
{"b", structure.b},
{"c", struct {
_ int
d *bool
}{}},
{"c._", 0},
{"c.d", new(bool)},
{"e", structure.e},
{"e._", 0},
{"e.f", structure.e.f},
{"g", structure.g},
{"g._", 0},
{"g.h", (*structure.g).h},
{"g.s", (*structure.g).s},
{"g.m", (*structure.g).m},
{"g2.m", (map[string][]string)(nil)},
{"i", structure.i},
{"j", structure.j},
{"k", structure.k},
{"m", new(int)},
{"n", new(bool)},
{"p", new(string)},
}
for _, c := range cases {
fh, _ := e.Field(c.fieldSelector)
val := fh.Value(true).Interface()
assert.Equal(t, c.value, val, c.fieldSelector)
}
}
func TestOperator(t *testing.T) {
type Tmp1 struct {
A string `tagexpr:$=="1"||$=="2"||$="3"`
B []int `tagexpr:len($)>=10&&$[0]<10`
C interface{}
}
type Tmp2 struct {
A *Tmp1
B interface{}
}
type Target struct {
A int `tagexpr:"-$+$<=10"`
B int `tagexpr:"+$-$<=10"`
C int `tagexpr:"-$+(M)$*(N)$/$%(D.B)$[2]+$==1"`
D *Tmp1 `tagexpr:"(D.A)$!=nil"`
E string `tagexpr:"((D.A)$=='1'&&len($)>1)||((D.A)$=='2'&&len($)>2)||((D.A)$=='3'&&len($)>3)"`
F map[string]int `tagexpr:"x:len($);y:$['a']>10&&$['b']>1"`
G *map[string]int `tagexpr:"x:$['a']+(F)$['a']>20"`
H []string `tagexpr:"len($)>=1&&len($)<10&&$[0]=='123'&&$[1]!='456'"`
I interface{} `tagexpr:"$!=nil"`
K *string `tagexpr:"len((D.A)$)+len($)<10&&len((D.A)$+$)<10"`
L **string `tagexpr:"false"`
M float64 `tagexpr:"$/2>10&&$%2==0"`
N *float64 `tagexpr:"($+$*$-$/$+1)/$==$+1"`
O *[3]float64 `tagexpr:"$[0]>10&&$[0]<20||$[0]>20&&$[0]<30"`
P *Tmp2 `tagexpr:"x:$!=nil;y:len((P.A.A)$)<=1&&(P.A.B)$[0]==1;z:$['A']['C']==nil;w:$['A']['B'][0]==1;r:$[0][1][2]==3;s1:$[2]==nil;s2:$[0][3]==nil;s3:(ZZ)$;s4:(P.B)$!=nil"`
Q *Tmp2 `tagexpr:"s1:$['A']['B']!=nil;s2:(Q.A)$['B']!=nil;s3:$['A']['C']==nil;s4:(Q.A)$['C']==nil;s5:(Q.A)$['B'][0]==1;s6:$['X']['Z']==nil"`
}
k := "123456"
n := float64(-12.5)
o := [3]float64{15, 9, 9}
var cases = []struct {
tagName string
structure interface{}
tests map[string]interface{}
}{
{
tagName: "tagexpr",
structure: &Target{
A: 5,
B: 10,
C: -10,
D: &Tmp1{A: "3", B: []int{1, 2, 3}},
E: "1234",
F: map[string]int{"a": 11, "b": 9},
G: &map[string]int{"a": 11},
H: []string{"123", "45"},
I: struct{}{},
K: &k,
L: nil,
M: float64(30),
N: &n,
O: &o,
P: &Tmp2{A: &Tmp1{A: "3", B: []int{1, 2, 3}}, B: struct{}{}},
Q: &Tmp2{A: &Tmp1{A: "3", B: []int{1, 2, 3}}, B: struct{}{}},
},
tests: map[string]interface{}{
"A": true,
"B": true,
"C": true,
"D": true,
"E": true,
"F@x": float64(2),
"F@y": true,
"G@x": true,
"H": true,
"I": true,
"K": true,
"L": false,
"M": true,
"N": true,
"O": true,
"P@x": true,
"P@y": true,
"P@z": true,
"P@w": true,
"P@r": true,
"P@s1": true,
"P@s2": true,
"P@s3": nil,
"P@s4": true,
"Q@s1": true,
"Q@s2": true,
"Q@s3": true,
"Q@s4": true,
"Q@s5": true,
"Q@s6": true,
},
},
}
for i, c := range cases {
vm := New(c.tagName)
// vm.WarmUp(c.structure)
tagExpr, err := vm.Run(c.structure)
if err != nil {
t.Fatal(err)
}
for selector, value := range c.tests {
val := tagExpr.Eval(selector)
if !reflect.DeepEqual(val, value) {
t.Fatalf("Eval NO: %d, selector: %q, got: %v, expect: %v", i, selector, val, value)
}
}
tagExpr.Range(func(eh *ExprHandler) error {
es := eh.ExprSelector()
t.Logf("Range selector: %s, field: %q exprName: %q", es, es.Field(), es.Name())
value := c.tests[es.String()]
val := eh.Eval()
if !reflect.DeepEqual(val, value) {
t.Fatalf("Range NO: %d, selector: %q, got: %v, expect: %v", i, es, val, value)
}
return nil
})
}
}
func TestStruct(t *testing.T) {
type A struct {
B struct {
C struct {
D struct {
X string `vd:"$"`
}
} `vd:"@:$['D']['X']"`
C2 string `vd:"@:(C)$['D']['X']"`
C3 string `vd:"@:(C.D.X)$"`
}
}
a := new(A)
a.B.C.D.X = "xxx"
vm := New("vd")
expr := vm.MustRun(a)
assert.Equal(t, "xxx", expr.EvalString("B.C2"))
assert.Equal(t, "xxx", expr.EvalString("B.C3"))
assert.Equal(t, "xxx", expr.EvalString("B.C"))
assert.Equal(t, "xxx", expr.EvalString("B.C.D.X"))
expr.Range(func(eh *ExprHandler) error {
es := eh.ExprSelector()
t.Logf("Range selector: %s, field: %q exprName: %q", es, es.Field(), es.Name())
if eh.Eval().(string) != "xxx" {
t.FailNow()
}
return nil
})
}
func TestStruct2(t *testing.T) {
type IframeBlock struct {
XBlock struct {
BlockType string `vd:"$"`
}
Props struct {
Data struct {
DataType string `vd:"$"`
}
}
}
b := new(IframeBlock)
b.XBlock.BlockType = "BlockType"
b.Props.Data.DataType = "DataType"
vm := New("vd")
expr := vm.MustRun(b)
if expr.EvalString("XBlock.BlockType") != "BlockType" {
t.Fatal(expr.EvalString("XBlock.BlockType"))
}
if expr.EvalString("Props.Data.DataType") != "DataType" {
t.Fatal(expr.EvalString("Props.Data.DataType"))
}
}
func TestStruct3(t *testing.T) {
type Data struct {
DataType string `vd:"$"`
}
type Prop struct {
PropType string `vd:"$"`
Datas []*Data `vd:"$"`
Datas2 []*Data `vd:"$"`
DataMap map[int]Data `vd:"$"`
DataMap2 map[int]Data `vd:"$"`
}
type IframeBlock struct {
XBlock struct {
BlockType string `vd:"$"`
}
Props []Prop `vd:"$"`
Props1 [2]Prop `vd:"$"`
Props2 []Prop `vd:"$"`
PropMap map[int]*Prop `vd:"$"`
PropMap2 map[int]*Prop `vd:"$"`
}
b := new(IframeBlock)
b.XBlock.BlockType = "BlockType"
p1 := Prop{
PropType: "p1",
Datas: []*Data{
{"p1s1"},
{"p1s2"},
nil,
},
DataMap: map[int]Data{
1: {"p1m1"},
2: {"p1m2"},
0: Data{},
},
}
b.Props = []Prop{p1}
p2 := &Prop{
PropType: "p2",
Datas: []*Data{
{"p2s1"},
{"p2s2"},
nil,
},
DataMap: map[int]Data{
1: {"p2m1"},
2: {"p2m2"},
0: Data{},
},
}
b.Props1 = [2]Prop{p1, Prop{}}
b.PropMap = map[int]*Prop{
9: p2,
}
vm := New("vd")
expr := vm.MustRun(b)
if expr.EvalString("XBlock.BlockType") != "BlockType" {
t.Fatal(expr.EvalString("XBlock.BlockType"))
}
err := expr.Range(func(eh *ExprHandler) error {
es := eh.ExprSelector()
t.Logf("Range selector: %s, field: %q exprName: %q, eval: %v", eh.Path(), es.Field(), es.Name(), eh.Eval())
return nil
})
assert.NoError(t, err)
}
func TestNilField(t *testing.T) {
type P struct {
X **struct {
A *[]uint16 `tagexpr:"$"`
} `tagexpr:"$"`
Y **struct{} `tagexpr:"$"`
}
vm := New("tagexpr")
te := vm.MustRun(&P{})
te.Range(func(eh *ExprHandler) error {
r := eh.Eval()
assert.Nil(t, r, eh.Path())
return nil
})
type G struct {
// Nil1 *int `tagexpr:"nil!=$"`
Nil2 *int `tagexpr:"$!=nil"`
}
g := &G{
// Nil1: new(int),
Nil2: new(int),
}
vm.MustRun(g).Range(func(eh *ExprHandler) error {
r, ok := eh.Eval().(bool)
assert.True(t, ok, eh.Path())
assert.True(t, r, eh.Path())
return nil
})
return
type (
N struct {
X string `tagexpr:"len($)>0"`
S []*N `tagexpr:"?"`
M map[string]*N `tagexpr:"?"`
M2 map[string]*N `tagexpr:"?"`
I interface{} `tagexpr:"-"`
MI map[string]interface{} `tagexpr:"?"`
SI []interface{}
*N `tagexpr:"?"`
N2 *N `tagexpr:"?"`
}
M struct {
X string `tagexpr:"len($)>0"`
}
)
n := &N{
X: "n",
S: []*N{nil},
M: map[string]*N{"": nil},
M2: map[string]*N{"": {X: "nn"}},
I: new(N),
MI: map[string]interface{}{"": (*M)(nil)},
SI: []interface{}{&M{X: "nn"}},
}
var cnt int
vm.MustRun(n).Range(func(eh *ExprHandler) error {
r := eh.EvalBool()
assert.True(t, r, eh.Path())
t.Log("path:", eh.Path(), "es:", eh.ExprSelector(), "val:", r)
cnt++
return nil
})
assert.Equal(t, 3, cnt)
}
func TestDeepNested(t *testing.T) {
type testInner struct {
Address string `tagexpr:"name:$"`
}
type struct1 struct {
I *testInner
A []*testInner
X interface{}
}
type struct2 struct {
S *struct1
}
type Data struct {
S1 *struct2
S2 *struct2
}
data := &Data{
S1: &struct2{
S: &struct1{
I: &testInner{Address: "I:address"},
A: []*testInner{{Address: "A:address"}},
X: []*testInner{{Address: "X:address"}},
},
},
S2: &struct2{
S: &struct1{
A: []*testInner{nil},
},
},
}
expectKey := [...]interface{}{"S1.S.I.Address@name", "S2.S.I.Address@name", "S1.S.A[0].Address@name", "S2.S.A[0].Address@name", "S1.S.X[0].Address@name"}
expectValue := [...]interface{}{"I:address", nil, "A:address", nil, "X:address"}
var i int
vm := New("tagexpr")
vm.MustRun(data).Range(func(eh *ExprHandler) error {
assert.Equal(t, expectKey[i], eh.Path())
assert.Equal(t, expectValue[i], eh.Eval())
i++
t.Log(eh.Path(), eh.ExprSelector(), eh.Eval())
return nil
})
assert.Equal(t, 5, i)
}
func TestIssue3(t *testing.T) {
type C struct {
Id string
Index int32 `vd:"$"`
P *int `vd:"$!=nil"`
}
type A struct {
F1 *C
F2 *C
}
a := &A{
F1: &C{
Id: "test",
Index: 1,
P: new(int),
},
}
vm := New("vd")
err := vm.MustRun(a).Range(func(eh *ExprHandler) error {
switch eh.Path() {
case "F1.Index":
assert.Equal(t, float64(1), eh.Eval(), eh.Path())
case "F2.Index":
assert.Equal(t, nil, eh.Eval(), eh.Path())
case "F1.P":
assert.Equal(t, true, eh.Eval(), eh.Path())
case "F2.P":
assert.Equal(t, false, eh.Eval(), eh.Path())
}
return nil
})
assert.NoError(t, err)
}
func TestIssue4(t *testing.T) {
type T struct {
A *string `te:"len($)+mblen($)"`
B *string `te:"len($)+mblen($)"`
C *string `te:"len($)+mblen($)"`
}
c := "c"
v := &T{
B: new(string),
C: &c,
}
vm := New("te")
err := vm.MustRun(v).Range(func(eh *ExprHandler) error {
t.Logf("eval:%v, path:%s", eh.EvalFloat(), eh.Path())
return nil
})
assert.NoError(t, err)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化