-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexpr_test.go
112 lines (91 loc) · 1.74 KB
/
expr_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
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"testing"
"github.com/antonmedv/expr"
)
func Benchmark_expr(b *testing.B) {
params := createParams()
program, err := expr.Compile(example, expr.Env(params))
if err != nil {
b.Fatal(err)
}
var out interface{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Run(program, params)
}
b.StopTimer()
if err != nil {
b.Fatal(err)
}
if !out.(bool) {
b.Fail()
}
}
func Benchmark_expr_startswith(b *testing.B) {
params := map[string]interface{}{
"name": "/groups/foo/bar",
"group": "foo",
}
program, err := expr.Compile(`name startsWith "/groups/" + group`, expr.Env(params))
if err != nil {
b.Fatal(err)
}
var out interface{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Run(program, params)
}
b.StopTimer()
if err != nil {
b.Fatal(err)
}
if !out.(bool) {
b.Fail()
}
}
func Benchmark_expr_func(b *testing.B) {
join := expr.Function(
"join",
func(params ...interface{}) (interface{}, error) {
return params[0].(string) + params[1].(string), nil
},
)
program, err := expr.Compile(`join("hello", ", world")`, join)
if err != nil {
b.Fatal(err)
}
var out interface{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Run(program, nil)
}
b.StopTimer()
if err != nil {
b.Fatal(err)
}
if out.(string) != "hello, world" {
b.Fail()
}
}
func Benchmark_expr_map(b *testing.B) {
env := map[string]interface{}{
"array": createRange(1, 100),
}
program, err := expr.Compile(`map(array, # * 2)`, expr.Env(env))
if err != nil {
b.Fatal(err)
}
var out interface{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Run(program, env)
}
b.StopTimer()
if err != nil {
b.Fatal(err)
}
if out.([]interface{})[0] != 2 {
b.Fail()
}
}