-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorule_test.go
95 lines (92 loc) · 1.68 KB
/
gorule_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
package gorule
import "testing"
func TestGoRule(t *testing.T) {
type A0 struct {
Name string
UserId string
Tags []string
Config map[string]bool
}
args := []Argument{
{
Name: "a0",
Val: A0{
Name: "zbh255",
UserId: "change",
Tags: []string{
"new-user", "inner",
},
Config: map[string]bool{
"Show": true,
"DataSync": true,
},
},
},
{
Name: "a2",
Val: true,
},
}
t.Run("Base", func(t *testing.T) {
ok, err := ExecuteSimpleBoolExpr("(a0.Name == \"zbh255\" || a0.UserId == \"change\") || a2 ", args...)
if err != nil {
t.Fatal(err)
}
t.Log(ok)
})
t.Run("FuncCall", func(t *testing.T) {
ok, err := ExecuteSimpleBoolExpr("len(a0.Tags) == 2 && a0.Tags[1] == \"inner\"", args...)
if err != nil {
t.Fatal(err)
}
t.Log(ok)
})
t.Run("MapVisit", func(t *testing.T) {
ok, err := ExecuteSimpleBoolExpr("len(a0.Config) == 2 && a0.Config[\"Show\"]", args...)
if err != nil {
t.Fatal(err)
}
t.Log(ok)
})
t.Run("TypeConvert", func(t *testing.T) {
ok, err := ExecuteSimpleBoolExpr("uint64(2) == uint64(a0)", Argument{
Name: "a0",
Val: 2.33,
})
if err != nil {
t.Fatal(err)
}
t.Log(ok)
})
}
func BenchmarkGoRule(b *testing.B) {
type A0 struct {
Name string
UserId string
Tags []string
}
args := []Argument{
{
Name: "a0",
Val: A0{
Name: "zbh255",
UserId: "change",
Tags: []string{
"new-user", "inner",
},
},
},
{
Name: "a2",
Val: true,
},
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ok, err := ExecuteSimpleBoolExpr("len(a0.Tags) == 2 && a0.Tags[1] == \"inner\"", args...)
if err != nil {
b.Fatal(err)
}
_ = ok
}
}