-
Notifications
You must be signed in to change notification settings - Fork 94
/
vm_test.go
62 lines (52 loc) · 1.17 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
package vm_test
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
_ "github.com/ncw/gpython/builtin"
"github.com/ncw/gpython/compile"
"github.com/ncw/gpython/py"
"github.com/ncw/gpython/vm"
)
const testDir = "tests"
// Run the code in str
func run(t *testing.T, prog string) {
f, err := os.Open(prog)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer f.Close()
str, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("ReadAll failed: %v", err)
}
obj, err := compile.Compile(string(str), prog, "exec", 0, true)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}
code := obj.(*py.Code)
module := py.NewModule("__main__", "", nil, nil)
module.Globals["__file__"] = py.String(prog)
res, err := vm.Run(module.Globals, module.Globals, code, nil)
if err != nil {
py.TracebackDump(err)
t.Fatalf("Run failed: %v", err)
}
fmt.Printf("Return = %v\n", res)
}
func TestVm(t *testing.T) {
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
for _, f := range files {
name := path.Join(testDir, f.Name())
if strings.HasSuffix(name, ".py") {
t.Logf("Running %q", name)
run(t, name)
}
}
}