@@ -18,8 +18,8 @@ import (
18
18
"github.com/go-python/gpython/vm"
19
19
)
20
20
21
- // Run the code in str
22
- func Run (t * testing.T , prog string ) {
21
+ // Compile the program in the file prog to code in the module that is returned
22
+ func compileProgram (t testing.TB , prog string ) ( * py. Module , * py. Code ) {
23
23
f , err := os .Open (prog )
24
24
if err != nil {
25
25
t .Fatalf ("%s: Open failed: %v" , prog , err )
@@ -43,55 +43,80 @@ func Run(t *testing.T, prog string) {
43
43
code := obj .(* py.Code )
44
44
module := py .NewModule ("__main__" , "" , nil , nil )
45
45
module .Globals ["__file__" ] = py .String (prog )
46
+ return module , code
47
+ }
46
48
47
- _ , err = vm .Run (module .Globals , module .Globals , code , nil )
49
+ // Run the code in the module
50
+ func run (t testing.TB , module * py.Module , code * py.Code ) {
51
+ _ , err := vm .Run (module .Globals , module .Globals , code , nil )
48
52
if err != nil {
49
53
if wantErr , ok := module .Globals ["err" ]; ok {
50
54
wantErrObj , ok := wantErr .(py.Object )
51
55
if ! ok {
52
- t .Fatalf ("%s: want err is not py.Object: %#v" , prog , wantErr )
56
+ t .Fatalf ("want err is not py.Object: %#v" , wantErr )
53
57
}
54
58
gotExc , ok := err .(py.ExceptionInfo )
55
59
if ! ok {
56
- t .Fatalf ("%s: got err is not ExceptionInfo: %#v" , prog , err )
60
+ t .Fatalf ("got err is not ExceptionInfo: %#v" , err )
57
61
}
58
62
if gotExc .Value .Type () != wantErrObj .Type () {
59
- t .Fatalf ("%s: Want exception %v got %v" , prog , wantErrObj , gotExc .Value )
63
+ t .Fatalf ("Want exception %v got %v" , wantErrObj , gotExc .Value )
60
64
}
61
- t .Logf ("%s: matched exception" , prog )
65
+ // t.Logf("matched exception")
62
66
return
63
67
} else {
64
68
py .TracebackDump (err )
65
- t .Fatalf ("%s: Run failed: %v at %q" , prog , err , module .Globals ["doc" ])
69
+ t .Fatalf ("Run failed: %v at %q" , err , module .Globals ["doc" ])
66
70
}
67
71
}
68
72
69
73
// t.Logf("%s: Return = %v", prog, res)
70
74
if doc , ok := module .Globals ["doc" ]; ok {
71
75
if docStr , ok := doc .(py.String ); ok {
72
76
if string (docStr ) != "finished" {
73
- t .Fatalf ("%s: Didn't finish at %q" , prog , docStr )
77
+ t .Fatalf ("Didn't finish at %q" , docStr )
74
78
}
75
79
} else {
76
- t .Fatalf ("%s: Set doc variable to non string: %#v" , prog , doc )
80
+ t .Fatalf ("Set doc variable to non string: %#v" , doc )
77
81
}
78
82
} else {
79
- t .Fatalf ("%s: Didn't set doc variable at all" , prog )
83
+ t .Fatalf ("Didn't set doc variable at all" )
80
84
}
81
85
}
82
86
83
- // Runs the tests in the directory passed in
84
- func RunTests (t * testing.T , testDir string ) {
87
+ // find the python files in the directory passed in
88
+ func findFiles (t testing.TB , testDir string ) ( names [] string ) {
85
89
files , err := ioutil .ReadDir (testDir )
86
90
if err != nil {
87
91
t .Fatalf ("ReadDir failed: %v" , err )
88
92
}
89
93
for _ , f := range files {
90
94
name := f .Name ()
91
95
if ! strings .HasPrefix (name , "lib" ) && strings .HasSuffix (name , ".py" ) {
92
- name := path .Join (testDir , name )
93
- t .Logf ("%s: Running" , name )
94
- Run (t , name )
96
+ names = append (names , name )
95
97
}
96
98
}
99
+ return names
100
+ }
101
+
102
+ // RunTests runs the tests in the directory passed in
103
+ func RunTests (t * testing.T , testDir string ) {
104
+ for _ , name := range findFiles (t , testDir ) {
105
+ t .Run (name , func (t * testing.T ) {
106
+ module , code := compileProgram (t , path .Join (testDir , name ))
107
+ run (t , module , code )
108
+ })
109
+ }
110
+ }
111
+
112
+ // RunBenchmarks runs the benchmarks in the directory passed in
113
+ func RunBenchmarks (b * testing.B , testDir string ) {
114
+ for _ , name := range findFiles (b , testDir ) {
115
+ module , code := compileProgram (b , path .Join (testDir , name ))
116
+ b .Run (name , func (b * testing.B ) {
117
+ for i := 0 ; i < b .N ; i ++ {
118
+ run (b , module , code )
119
+ }
120
+ })
121
+ }
97
122
}
0 commit comments