-
Notifications
You must be signed in to change notification settings - Fork 142
/
tagparser_test.go
79 lines (75 loc) · 1.48 KB
/
tagparser_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
package tagexpr
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTagparser(t *testing.T) {
cases := []struct {
tag reflect.StructTag
expect map[string]string
fail bool
}{
{
tag: `tagexpr:"$>0"`,
expect: map[string]string{
"@": "$>0",
},
}, {
tag: `tagexpr:"$>0;'xxx'"`,
fail: true,
}, {
tag: `tagexpr:"$>0;b:sprintf('%[1]T; %[1]v',(X)$)"`,
expect: map[string]string{
"@": `$>0`,
"b": `sprintf('%[1]T; %[1]v',(X)$)`,
},
}, {
tag: `tagexpr:"a:$=='0;1;';b:sprintf('%[1]T; %[1]v',(X)$)"`,
expect: map[string]string{
"a": `$=='0;1;'`,
"b": `sprintf('%[1]T; %[1]v',(X)$)`,
},
}, {
tag: `tagexpr:"a:1;;b:2"`,
expect: map[string]string{
"a": `1`,
"b": `2`,
},
}, {
tag: `tagexpr:";a:1;;b:2;;;"`,
expect: map[string]string{
"a": `1`,
"b": `2`,
},
}, {
tag: `tagexpr:";a:'123\\'';;b:'1\\'23';c:'1\\'2\\'3';;"`,
expect: map[string]string{
"a": `'123\''`,
"b": `'1\'23'`,
"c": `'1\'2\'3'`,
},
}, {
tag: `tagexpr:"email($)"`,
expect: map[string]string{
"@": `email($)`,
},
}, {
tag: `tagexpr:"false"`,
expect: map[string]string{
"@": `false`,
},
},
}
for _, c := range cases {
r, e := parseTag(c.tag.Get("tagexpr"))
if e != nil == c.fail {
assert.Equal(t, c.expect, r, c.tag)
} else {
assert.Failf(t, string(c.tag), "kvs:%v, err:%v", r, e)
}
if e != nil {
t.Logf("tag:%q, errMsg:%v", c.tag, e)
}
}
}