-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsexpression_test.go
128 lines (107 loc) · 2.52 KB
/
jsexpression_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package vpl
import (
"fmt"
"testing"
"github.com/dop251/goja"
"github.com/robertkrimen/otto/parser"
"github.com/zbysir/vpl/internal/lib/log"
)
type DataGet struct {
data map[string]interface{}
}
func (d DataGet) Get(k string) interface{} {
x, _, _ := ShouldLookInterface(d.data, k)
return x
}
func TestRunJs(t *testing.T) {
cases := []struct {
Code string
Value interface{}
}{
{Code: "1+1", Value: 2},
{Code: "a+1", Value: 2},
{Code: "a-1", Value: 0},
{Code: "-a", Value: -1},
{Code: "!a", Value: false},
{Code: "!!a", Value: true},
{Code: "!0", Value: true},
{Code: "!(a+1)", Value: false},
{Code: "!(a-1)", Value: true},
{Code: "2 > 1", Value: true},
{Code: "2 >= 1", Value: true},
{Code: "1 < 2", Value: true},
{Code: "1 <= 2", Value: true},
{Code: "info.sex", Value: 26},
{Code: "info.sex+1", Value: 27},
{Code: "info.sexkey", Value: "sex"},
{Code: "info[info.sexkey]", Value: 26},
{Code: "{'abc': 'abc'}['abc']", Value: "abc"},
{Code: "{2: 3}['2']", Value: "3"},
// getter
{Code: "getter.a", Value: "1"},
// call function
{Code: "concat(1,2)", Value: "12"},
}
scope := NewScope(nil)
scope.Value = map[string]interface{}{
"a": 1,
"info": map[string]interface{}{
"sex": 26,
"sexkey": "sex",
},
"getter": DataGet{
data: map[string]interface{}{
"a": 1,
},
},
"concat": func(ctx *RenderCtx, args ...interface{}) interface{} {
return fmt.Sprintf("%+v%+v", args[0], args[1])
},
}
for _, c := range cases {
p, err := parser.ParseFile(nil, "", "("+c.Code+")", 0)
if err != nil {
err = fmt.Errorf("GetAst err: %w, code:%s", err, c.Code)
log.Warning(err)
t.Fatal(err)
}
v, err := runJsExpression(p.Body[0], &RenderCtx{
Scope: scope,
Store: nil,
})
if err != nil {
log.Warningf("runJsExpression err:%v", err)
t.Fatal(err)
}
if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", c.Value) {
t.Fatal(fmt.Sprintf("code %s, want:%+v, get:%+v", c.Code, c.Value, v))
}
}
t.Logf("OK")
}
func TestInterfaceToBool(t *testing.T) {
var a int64 = 0
if false != interfaceToBool(a) {
t.Fatalf("%v , want false, but:%v", a, interfaceToBool(a))
}
}
func TestGoja(t *testing.T) {
vm := goja.New()
v, err := vm.RunString("2 + 2")
if err != nil {
panic(err)
}
if num := v.Export().(int64); num != 4 {
panic(num)
}
}
func TestGojaEs6(t *testing.T) {
vm := goja.New()
// panic
// not support es6 feature: Destructuring
v, err := vm.RunString("var a = {a:1}; var b = {...a, c:1}; b")
if err != nil {
panic(err)
}
t.Log(v.Export())
}