-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_test.go
104 lines (82 loc) · 2.18 KB
/
evaluate_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
package jsont
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)
func TestEvalExpression(t *testing.T) {
expr := "$select.example:($test.something:literal)"
input := gjson.Parse(`{"select":{"example":"test"},"test":{"something":"value"}}`)
e := newExpressionEvaluator(input)
res, err := e.EvaluateExpression(expr)
t.Log(err)
t.Log(res)
}
func TestEvaluateExpression(t *testing.T) {
testFile := "testdata/input2.json"
file, _ := ioutil.ReadFile(testFile)
input := gjson.ParseBytes( /*json*/ file)
e := newExpressionEvaluator( /*inputData*/ input)
testData := []struct {
id string
input string
expectedRes interface{}
expectedErr error
}{
{
id: "single selector",
input: "$firstName",
expectedRes: "John",
},
{
id: "single path",
input: "$address.streetAddress",
expectedRes: "21 2nd Street",
},
{
id: "single operation with constant",
input: "$age + 5",
expectedRes: 30.,
},
{
id: "operation with two values",
input: "$firstName : $lastName",
expectedRes: "JohnSmith",
},
{
id: "malformed path",
input: ".data",
expectedRes: nil,
expectedErr: errIllegalPathSeparator,
},
{
id: "missing closing parenthesis",
input: "($firstName",
expectedRes: nil,
expectedErr: errMissingRightParenthesis,
},
{
id: "missing opening parenthesis",
input: "$firstName)",
expectedRes: nil,
expectedErr: errMissingLeftParenthesis,
},
}
for _, tt := range testData {
t.Run(tt.id, func(t *testing.T) {
res, err := e.EvaluateExpression( /*expr*/ tt.input)
assert.Equal(t, tt.expectedErr, err, "expected error returned by evaluation to match")
if res == nil {
res = &gjson.Result{}
}
assert.Equal(t, tt.expectedRes, res.Value(), "expected correct result of evaluation")
})
}
}
func jsonFromString(in string) *gjson.Result {
return &gjson.Result{Type: gjson.String, Str: in, Index: 0, Num: 0, Raw: `"` + in + `"`}
}
func jsonFromNumber(in float64) *gjson.Result {
return &gjson.Result{Type: gjson.Number, Num: in}
}