-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonnata_test.go
113 lines (89 loc) · 2.33 KB
/
jsonnata_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
113
package jsonata
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompile(t *testing.T) {
exp, err := Compile("$sum(example.value)")
assert.NoError(t, err)
assert.NotNil(t, exp)
}
func TestExpression_Evaluate(t *testing.T) {
data := map[string]any{
"example": []map[string]any{
{"value": 4},
{"value": 7},
{"value": 13},
},
}
exp := MustCompile("$sum(example.value)")
output, err := exp.Evaluate(data)
assert.NoError(t, err)
assert.Equal(t, int64(24), output)
}
func TestExpression_Assign(t *testing.T) {
exp := MustCompile("$greet")
err := exp.Assign("greet", "Hello world")
assert.NoError(t, err)
output, err := exp.Evaluate(nil)
assert.NoError(t, err)
assert.Equal(t, "Hello world", output)
}
func TestExpression_RegisterFunction(t *testing.T) {
exp := MustCompile("$greet()")
err := exp.RegisterFunction("greet", func(f *Focus, args ...any) (any, error) { return "Hello world", nil })
assert.NoError(t, err)
output, err := exp.Evaluate(nil)
assert.NoError(t, err)
assert.Equal(t, "Hello world", output)
}
func TestExpression_Ast(t *testing.T) {
exp := MustCompile("$sum(example.value)")
ast, err := exp.Ast()
assert.NoError(t, err)
assert.NotNil(t, ast)
}
func BenchmarkExpression_Compile(b *testing.B) {
for i := 0; i < b.N; i++ {
exp, err := Compile("$sum(example.value)")
assert.NoError(b, err)
assert.NotNil(b, exp)
}
}
func BenchmarkExpression_Evaluate(b *testing.B) {
data := map[string]any{
"example": []map[string]any{
{"value": 4},
{"value": 7},
{"value": 13},
},
}
exp := MustCompile("$sum(example.value)")
for i := 0; i < b.N; i++ {
output, err := exp.Evaluate(data)
assert.NoError(b, err)
assert.Equal(b, int64(24), output)
}
}
func BenchmarkExpression_Assign(b *testing.B) {
exp := MustCompile("$greet")
for i := 0; i < b.N; i++ {
err := exp.Assign("greet", "Hello world")
assert.NoError(b, err)
}
}
func BenchmarkExpression_RegisterFunction(b *testing.B) {
exp := MustCompile("$greet()")
for i := 0; i < b.N; i++ {
err := exp.RegisterFunction("greet", func(f *Focus, args ...any) (any, error) { return "Hello world", nil })
assert.NoError(b, err)
}
}
func BenchmarkExpression_Ast(b *testing.B) {
exp := MustCompile("$sum(example.value)")
for i := 0; i < b.N; i++ {
ast, err := exp.Ast()
assert.NoError(b, err)
assert.NotNil(b, ast)
}
}