|
1 | 1 | package vm_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "io/ioutil" |
5 |
| - "os" |
6 |
| - "path" |
7 |
| - "strings" |
8 | 4 | "testing"
|
9 | 5 |
|
10 |
| - _ "github.com/ncw/gpython/builtin" |
11 |
| - "github.com/ncw/gpython/compile" |
12 |
| - "github.com/ncw/gpython/py" |
13 |
| - "github.com/ncw/gpython/vm" |
| 6 | + "github.com/ncw/gpython/pytest" |
14 | 7 | )
|
15 | 8 |
|
16 |
| -const testDir = "tests" |
17 |
| - |
18 |
| -// Run the code in str |
19 |
| -func run(t *testing.T, prog string) { |
20 |
| - f, err := os.Open(prog) |
21 |
| - if err != nil { |
22 |
| - t.Fatalf("%s: Open failed: %v", prog, err) |
23 |
| - } |
24 |
| - defer f.Close() |
25 |
| - |
26 |
| - str, err := ioutil.ReadAll(f) |
27 |
| - if err != nil { |
28 |
| - t.Fatalf("%s: ReadAll failed: %v", prog, err) |
29 |
| - } |
30 |
| - |
31 |
| - obj, err := compile.Compile(string(str), prog, "exec", 0, true) |
32 |
| - if err != nil { |
33 |
| - t.Fatalf("%s: Compile failed: %v", prog, err) |
34 |
| - } |
35 |
| - |
36 |
| - code := obj.(*py.Code) |
37 |
| - module := py.NewModule("__main__", "", nil, nil) |
38 |
| - module.Globals["__file__"] = py.String(prog) |
39 |
| - |
40 |
| - _, err = vm.Run(module.Globals, module.Globals, code, nil) |
41 |
| - if err != nil { |
42 |
| - if wantErr, ok := module.Globals["err"]; ok { |
43 |
| - wantErrObj, ok := wantErr.(py.Object) |
44 |
| - if !ok { |
45 |
| - t.Fatalf("%s: want err is not py.Object: %#v", prog, wantErr) |
46 |
| - } |
47 |
| - gotExc, ok := err.(py.ExceptionInfo) |
48 |
| - if !ok { |
49 |
| - t.Fatalf("%s: got err is not ExceptionInfo: %#v", prog, err) |
50 |
| - } |
51 |
| - if gotExc.Value.Type() != wantErrObj.Type() { |
52 |
| - t.Fatalf("%s: Want exception %v got %v", prog, wantErrObj, gotExc.Value) |
53 |
| - } |
54 |
| - t.Logf("%s: matched exception", prog) |
55 |
| - return |
56 |
| - } else { |
57 |
| - py.TracebackDump(err) |
58 |
| - t.Fatalf("%s: Run failed: %v at %q", prog, err, module.Globals["doc"]) |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - // t.Logf("%s: Return = %v", prog, res) |
63 |
| - if doc, ok := module.Globals["doc"]; ok { |
64 |
| - if docStr, ok := doc.(py.String); ok { |
65 |
| - if string(docStr) != "finished" { |
66 |
| - t.Fatalf("%s: Didn't finish at %q", prog, docStr) |
67 |
| - } |
68 |
| - } else { |
69 |
| - t.Fatalf("%s: Set doc variable to non string: %#v", prog, doc) |
70 |
| - } |
71 |
| - } else { |
72 |
| - t.Fatalf("%s: Didn't set doc variable at all", prog) |
73 |
| - } |
74 |
| -} |
75 |
| - |
76 | 9 | func TestVm(t *testing.T) {
|
77 |
| - files, err := ioutil.ReadDir(testDir) |
78 |
| - if err != nil { |
79 |
| - t.Fatalf("ReadDir failed: %v", err) |
80 |
| - } |
81 |
| - for _, f := range files { |
82 |
| - name := f.Name() |
83 |
| - if !strings.HasPrefix(name, "lib") && strings.HasSuffix(name, ".py") { |
84 |
| - name := path.Join(testDir, name) |
85 |
| - t.Logf("%s: Running", name) |
86 |
| - run(t, name) |
87 |
| - } |
88 |
| - } |
| 10 | + pytest.RunTests(t, "tests") |
89 | 11 | }
|
0 commit comments