forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_test.go
261 lines (204 loc) · 5.43 KB
/
vm_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package vm_test
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/expr-lang/expr/internal/testify/require"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/checker"
"github.com/expr-lang/expr/compiler"
"github.com/expr-lang/expr/conf"
"github.com/expr-lang/expr/parser"
"github.com/expr-lang/expr/vm"
)
func TestRun_NilProgram(t *testing.T) {
_, err := vm.Run(nil, nil)
require.Error(t, err)
}
func TestRun_ReuseVM(t *testing.T) {
node, err := parser.Parse(`map(1..2, {#})`)
require.NoError(t, err)
program, err := compiler.Compile(node, nil)
require.NoError(t, err)
reuse := vm.VM{}
_, err = reuse.Run(program, nil)
require.NoError(t, err)
_, err = reuse.Run(program, nil)
require.NoError(t, err)
}
func TestRun_ReuseVM_for_different_variables(t *testing.T) {
v := vm.VM{}
program, err := expr.Compile(`let a = 1; a + 1`)
require.NoError(t, err)
out, err := v.Run(program, nil)
require.NoError(t, err)
require.Equal(t, 2, out)
program, err = expr.Compile(`let a = 2; a + 1`)
require.NoError(t, err)
out, err = v.Run(program, nil)
require.NoError(t, err)
require.Equal(t, 3, out)
program, err = expr.Compile(`let a = 2; let b = 2; a + b`)
require.NoError(t, err)
out, err = v.Run(program, nil)
require.NoError(t, err)
require.Equal(t, 4, out)
}
func TestRun_Cast(t *testing.T) {
input := `1`
tree, err := parser.Parse(input)
require.NoError(t, err)
program, err := compiler.Compile(tree, &conf.Config{Expect: reflect.Float64})
require.NoError(t, err)
out, err := vm.Run(program, nil)
require.NoError(t, err)
require.Equal(t, float64(1), out)
}
func TestRun_Helpers(t *testing.T) {
values := []any{
uint(1),
uint8(1),
uint16(1),
uint32(1),
uint64(1),
1,
int8(1),
int16(1),
int32(1),
int64(1),
float32(1),
float64(1),
}
ops := []string{"+", "-", "*", "/", "%", "==", ">=", "<=", "<", ">"}
for _, a := range values {
for _, b := range values {
for _, op := range ops {
if op == "%" {
switch a.(type) {
case float32, float64:
continue
}
switch b.(type) {
case float32, float64:
continue
}
}
input := fmt.Sprintf("a %v b", op)
env := map[string]any{
"a": a,
"b": b,
}
config := conf.CreateNew()
tree, err := parser.Parse(input)
require.NoError(t, err)
_, err = checker.Check(tree, config)
require.NoError(t, err)
program, err := compiler.Compile(tree, config)
require.NoError(t, err)
_, err = vm.Run(program, env)
require.NoError(t, err)
}
}
}
}
type ErrorEnv struct {
InnerEnv InnerEnv
}
type InnerEnv struct{}
func (ErrorEnv) WillError(param string) (bool, error) {
if param == "yes" {
return false, errors.New("error")
}
return true, nil
}
func (InnerEnv) WillError(param string) (bool, error) {
if param == "yes" {
return false, errors.New("inner error")
}
return true, nil
}
func TestRun_MethodWithError(t *testing.T) {
input := `WillError("yes")`
tree, err := parser.Parse(input)
require.NoError(t, err)
env := ErrorEnv{}
funcConf := conf.New(env)
_, err = checker.Check(tree, funcConf)
require.NoError(t, err)
program, err := compiler.Compile(tree, funcConf)
require.NoError(t, err)
out, err := vm.Run(program, env)
require.EqualError(t, err, "error (1:1)\n | WillError(\"yes\")\n | ^")
require.Equal(t, nil, out)
selfErr := errors.Unwrap(err)
require.NotNil(t, err)
require.Equal(t, "error", selfErr.Error())
}
func TestRun_FastMethods(t *testing.T) {
input := `hello() + world()`
tree, err := parser.Parse(input)
require.NoError(t, err)
env := map[string]any{
"hello": func(...any) any { return "hello " },
"world": func(...any) any { return "world" },
}
funcConf := conf.New(env)
_, err = checker.Check(tree, funcConf)
require.NoError(t, err)
program, err := compiler.Compile(tree, funcConf)
require.NoError(t, err)
out, err := vm.Run(program, env)
require.NoError(t, err)
require.Equal(t, "hello world", out)
}
func TestRun_InnerMethodWithError(t *testing.T) {
input := `InnerEnv.WillError("yes")`
tree, err := parser.Parse(input)
require.NoError(t, err)
env := ErrorEnv{}
funcConf := conf.New(env)
program, err := compiler.Compile(tree, funcConf)
require.NoError(t, err)
out, err := vm.Run(program, env)
require.EqualError(t, err, "inner error (1:10)\n | InnerEnv.WillError(\"yes\")\n | .........^")
require.Equal(t, nil, out)
}
func TestRun_InnerMethodWithError_NilSafe(t *testing.T) {
input := `InnerEnv?.WillError("yes")`
tree, err := parser.Parse(input)
require.NoError(t, err)
env := ErrorEnv{}
funcConf := conf.New(env)
program, err := compiler.Compile(tree, funcConf)
require.NoError(t, err)
out, err := vm.Run(program, env)
require.EqualError(t, err, "inner error (1:11)\n | InnerEnv?.WillError(\"yes\")\n | ..........^")
require.Equal(t, nil, out)
}
func TestRun_TaggedFieldName(t *testing.T) {
input := `value`
tree, err := parser.Parse(input)
require.NoError(t, err)
env := struct {
V string `expr:"value"`
}{
V: "hello world",
}
funcConf := conf.New(env)
_, err = checker.Check(tree, funcConf)
require.NoError(t, err)
program, err := compiler.Compile(tree, funcConf)
require.NoError(t, err)
out, err := vm.Run(program, env)
require.NoError(t, err)
require.Equal(t, "hello world", out)
}
func TestRun_OpInvalid(t *testing.T) {
program := &vm.Program{
Bytecode: []vm.Opcode{vm.OpInvalid},
Arguments: []int{0},
}
_, err := vm.Run(program, nil)
require.EqualError(t, err, "invalid opcode")
}