-
Notifications
You must be signed in to change notification settings - Fork 56
/
code_test.go
88 lines (69 loc) · 1.64 KB
/
code_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
package py
import (
"testing"
)
type compileCase struct {
exp string
ret string
start StartToken
}
var g_compileCases = []compileCase{
{"1+2", "3", EvalInput},
{"1+2", "None", SingleInput}, // echo
{"1+2", "None", FileInput},
}
func TestCompile(t *testing.T) {
for _, c := range g_compileCases {
code, err := Compile(c.exp, "", c.start)
if err != nil {
t.Fatal("Compile failed:", err)
}
defer code.Decref()
globals := NewDict()
defer globals.Decref()
locals := NewDict()
defer locals.Decref()
ret, err := code.Eval(globals.Obj(), locals.Obj())
if err != nil {
t.Fatal("Eval failed:", err)
}
defer ret.Decref()
if ret.String() != c.ret {
t.Fatal("Eval ret:", ret.String())
}
}
}
type evalLocalGlobalsCase struct {
exp string
globals string
locals string
start StartToken
}
var g_evalLocalGlobalsCases = []evalLocalGlobalsCase{
{"v=1+2", "{}", "{'v': 3}", FileInput},
{"v=1+2", "{}", "{'v': 3}", SingleInput}, // echo
// {"v=1+2", "{}", "{'v': 3}", EvalInput}, // compile error
}
func _TestEvalLocalGlobals(t *testing.T) {
Initialize()
defer Finalize()
for _, c := range g_evalLocalGlobalsCases {
code, err := Compile(c.exp, "", c.start)
if err != nil {
t.Fatal("Compile failed:", c.exp, c.start, err)
}
defer code.Decref()
globals := NewDict()
defer globals.Decref()
locals := NewDict()
defer locals.Decref()
err = code.Run(globals.Obj(), locals.Obj())
if err != nil {
t.Fatal("Run failed:", err)
}
println(globals.String(), locals.String())
if locals.String() != c.locals || globals.String() != c.globals {
t.Fatal("Run:", globals.String(), locals.String())
}
}
}