-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexpression_test.go
70 lines (65 loc) · 1.41 KB
/
expression_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
package tdtl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Expression(t *testing.T) {
tests := []struct {
name string
path string
expression string
params map[string]Node
want string
}{
{
name: "test add",
path: "metrics.cpu_used",
expression: "20 + 8",
want: "28",
},
{
name: "test sub",
path: "metrics.cpu_used",
expression: "20 - 8",
want: "12",
},
{
name: "test mul",
path: "metrics.cpu_used",
expression: "20 * 8",
want: "160",
},
{
name: "test div",
path: "metrics.cpu_used",
expression: "20 / 8",
want: "2",
},
{
name: "test1",
path: "metrics.cpu_used",
expression: "device234.metrics.cpu_used / 20",
params: map[string]Node{
"device234.metrics.cpu_used": NewInt64(100),
},
want: "5",
},
{
name: "test2",
path: "sysField._spacePath",
expression: "device234.sysField._spacePath + '/tomas'",
params: map[string]Node{
"device234.sysField._spacePath": NewString("iotd-device123"),
},
want: "iotd-device123/tomas",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
exprIns, err := NewExpr(test.expression, nil)
assert.Nil(t, err)
result := exprIns.Eval(test.params)
assert.Equal(t, test.want, string(result.String()))
})
}
}