-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_test.go
More file actions
36 lines (33 loc) · 954 Bytes
/
Copy pathscript_test.go
File metadata and controls
36 lines (33 loc) · 954 Bytes
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
package ruleflow
import "testing"
func TestEvalScriptReturn(t *testing.T) {
// 数值运算
v, err := evalScript("return a + b", map[string]any{"a": 1, "b": 2})
if err != nil {
t.Fatal(err)
}
if f, ok := toFloat(v); !ok || f != 3 {
t.Errorf("应返回 3, got %v", v)
}
// 布尔
v, err = evalScript("return temperature > 30", map[string]any{"temperature": 35})
if err != nil {
t.Fatal(err)
}
if b, ok := v.(bool); !ok || !b {
t.Errorf("应返回 true, got %v", v)
}
// inputs 数组按下标访问(由 scriptVars 作为一个键放入 vars)
v, err = evalScript("return inputs[0].x * 2", map[string]any{"inputs": []any{map[string]any{"x": 5}}})
if err != nil {
t.Fatal(err)
}
if f, ok := toFloat(v); !ok || f != 10 {
t.Errorf("应返回 10, got %v", v)
}
}
func TestEvalScriptError(t *testing.T) {
if _, err := evalScript("this is not valid js !!!", nil); err == nil {
t.Error("语法错误脚本应返回 error")
}
}