@@ -18,8 +18,8 @@ import (
1818 "github.com/go-python/gpython/vm"
1919)
2020
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 ) {
2323 f , err := os .Open (prog )
2424 if err != nil {
2525 t .Fatalf ("%s: Open failed: %v" , prog , err )
@@ -43,55 +43,80 @@ func Run(t *testing.T, prog string) {
4343 code := obj .(* py.Code )
4444 module := py .NewModule ("__main__" , "" , nil , nil )
4545 module .Globals ["__file__" ] = py .String (prog )
46+ return module , code
47+ }
4648
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 )
4852 if err != nil {
4953 if wantErr , ok := module .Globals ["err" ]; ok {
5054 wantErrObj , ok := wantErr .(py.Object )
5155 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 )
5357 }
5458 gotExc , ok := err .(py.ExceptionInfo )
5559 if ! ok {
56- t .Fatalf ("%s: got err is not ExceptionInfo: %#v" , prog , err )
60+ t .Fatalf ("got err is not ExceptionInfo: %#v" , err )
5761 }
5862 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 )
6064 }
61- t .Logf ("%s: matched exception" , prog )
65+ // t.Logf("matched exception")
6266 return
6367 } else {
6468 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" ])
6670 }
6771 }
6872
6973 // t.Logf("%s: Return = %v", prog, res)
7074 if doc , ok := module .Globals ["doc" ]; ok {
7175 if docStr , ok := doc .(py.String ); ok {
7276 if string (docStr ) != "finished" {
73- t .Fatalf ("%s: Didn't finish at %q" , prog , docStr )
77+ t .Fatalf ("Didn't finish at %q" , docStr )
7478 }
7579 } 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 )
7781 }
7882 } else {
79- t .Fatalf ("%s: Didn't set doc variable at all" , prog )
83+ t .Fatalf ("Didn't set doc variable at all" )
8084 }
8185}
8286
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 ) {
8589 files , err := ioutil .ReadDir (testDir )
8690 if err != nil {
8791 t .Fatalf ("ReadDir failed: %v" , err )
8892 }
8993 for _ , f := range files {
9094 name := f .Name ()
9195 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 )
9597 }
9698 }
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+ }
97122}
0 commit comments