-
Notifications
You must be signed in to change notification settings - Fork 1
/
call_test.go
251 lines (234 loc) · 5.22 KB
/
call_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
package starlet_test
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/1set/starlet"
"go.starlark.net/starlark"
)
func TestMachine_Call_Preconditions(t *testing.T) {
m := starlet.NewDefault()
// test: if name == ""
_, err := m.Call("")
expectErr(t, err, "starlet: call: no function name")
// test: if m.thread == nil
_, err = m.Call("no_thread")
expectErr(t, err, "starlet: call: no function loaded")
// test: if m.predeclared == nil
m.SetGlobals(map[string]interface{}{"x": 1})
_, err = m.Call("no_globals")
expectErr(t, err, "starlet: call: no function loaded")
// prepare: run a script to load a function if exists
_, err = m.RunScript([]byte(`y = 2`), map[string]interface{}{
"println": fmt.Println,
})
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// test: if no such function
_, err = m.Call("no_such_function")
expectErr(t, err, "starlet: call: no such function: no_such_function")
// test: if mistyped function
_, err = m.Call("y")
expectErr(t, err, "starlet: call: mistyped function: y")
ei := err.(starlet.ExecError).Unwrap()
expectErr(t, ei, "mistyped function: y")
// test: if builtin function
_, err = m.Call("println", "hello")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestMachine_Call_Functions(t *testing.T) {
type mockData struct {
Apple string `json:"app"`
Banana int64 `json:"bana"`
Coconut bool `json:"coco"`
}
tests := []struct {
name string
code string
args []interface{}
want interface{}
wantErr string
}{
{
name: "no args nor return",
code: `
def work():
pass
`,
want: nil,
},
{
name: "no args but return",
code: `
def work():
return 1
`,
want: int64(1),
},
{
name: "args but no return",
code: `
def work(x, y):
pass
`,
args: []interface{}{1, 2},
want: nil,
},
{
name: "args and return",
code: `
def work(x, y):
return x + y
`,
args: []interface{}{1, 2},
want: int64(3),
},
{
name: "lambda",
code: `
work = lambda x, y: x * y
`,
args: []interface{}{2, 3},
want: int64(6),
},
{
name: "multiple return",
code: `
def work(x, y):
return x + 1, y + 2
`,
args: []interface{}{1, 2},
want: []interface{}{int64(2), int64(4)},
},
{
name: "multiple return with tuple",
code: `
def work(x, y):
return (x + 1, y + 2)
`,
args: []interface{}{1, 2},
want: []interface{}{int64(2), int64(4)},
},
{
name: "multiple return with list",
code: `
def work(x, y):
return [x + 1, y + 2]
`,
args: []interface{}{1, 2},
want: []interface{}{int64(2), int64(4)},
},
{
name: "convert args fail",
code: `
def work(x, y):
return x + y
`,
args: []interface{}{1, make(chan int64)},
wantErr: `starlight: convert args: type chan int64 is not a supported starlark type`,
},
{
name: "invalid args",
code: `
def work(x, y):
return x + y
`,
args: []interface{}{1, "two"},
wantErr: `starlark: call: unknown binary op: int + string`,
},
{
name: "func runtime error",
code: `
def work(x, y):
fail("oops")
`,
args: []interface{}{1, 2},
wantErr: `starlark: call: fail: oops`,
},
{
name: "func runtime panic",
code: `
def work(x, y):
panic("outside starlark")
`,
args: []interface{}{1, 2},
wantErr: `starlark: call: panic: as expected`,
},
{
name: "custom tag for arg",
args: []interface{}{
mockData{Apple: "red", Banana: 2, Coconut: true},
},
code: `
def work(x):
return "apple is " + x.app + ", banana is " + str(x.bana) + ", coconut is " + str(x.coco)
`,
want: "apple is red, banana is 2, coconut is True",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// prepare to load
m := starlet.NewDefault()
m.SetCustomTag("json")
_, err := m.RunScript([]byte(tt.code), map[string]interface{}{
"panic": starlark.NewBuiltin("panic", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
panic(errors.New("as expected"))
}),
})
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
// call and check
got, err := m.Call("work", tt.args...)
if err != nil {
if tt.wantErr == "" {
t.Errorf("expected no error, got %v", err)
} else {
expectErr(t, err, tt.wantErr)
}
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("expected %v (%T), got %v (%T)", tt.want, tt.want, got, got)
}
})
}
}
func TestMachine_Call_Convert(t *testing.T) {
m := starlet.NewDefault()
m.SetCustomTag("json")
_, err := m.RunScript([]byte(`
def work(x, y):
return x * y
`), nil)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
// call and check --- conversion is enabled by default
got, err := m.Call("work", 10, 20)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
if got != int64(200) {
t.Errorf("expected 200, got %v", got)
}
// not convert
m.SetOutputConversionEnabled(false)
got, err = m.Call("work", 5, 6)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
if v, ok := got.(starlark.Value); !ok {
t.Errorf("expected starlark Value, got %T", got)
return
} else if v != starlark.MakeInt(30) {
t.Errorf("got unexpected value: %v", v)
}
}