func run(t *testing.T, dir, input string) bool { fmt.Printf("Input: %s\n", input) start := time.Now() var inputs []string for _, i := range strings.Split(input, " ") { inputs = append(inputs, dir+i) } imp := importer.New(&importer.Config{Build: &build.Default}) // TODO(adonovan): use LoadInitialPackages, then un-export ParseFiles. files, err := importer.ParseFiles(imp.Fset, ".", inputs...) if err != nil { t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error()) return false } // Print a helpful hint if we don't make it to the end. var hint string defer func() { if hint != "" { fmt.Println("FAIL") fmt.Println(hint) } else { fmt.Println("PASS") } }() hint = fmt.Sprintf("To dump SSA representation, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=CFP %s\n", input) mainInfo := imp.LoadMainPackage(files...) prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) if err := prog.CreatePackages(imp); err != nil { t.Errorf("CreatePackages failed: %s", err) return false } prog.BuildAll() mainPkg := prog.Package(mainInfo.Pkg) mainPkg.CreateTestMainFunction() // (no-op if main already exists) hint = fmt.Sprintf("To trace execution, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=C -run --interp=T %s\n", input) if exitCode := interp.Interpret(mainPkg, 0, inputs[0], []string{}); exitCode != 0 { t.Errorf("interp.Interpret(%s) exited with code %d, want zero", inputs, exitCode) return false } hint = "" // call off the hounds if false { fmt.Println(input, time.Since(start)) // test profiling } return true }
// CreateTestMainPackage should return nil if there were no tests. func TestNullTestmainPackage(t *testing.T) { imp := importer.New(&importer.Config{Build: &build.Default}) files, err := importer.ParseFiles(imp.Fset, ".", "testdata/b_test.go") if err != nil { t.Fatalf("ParseFiles failed: %s", err) } mainInfo := imp.CreatePackage("b", files...) prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) if err := prog.CreatePackages(imp); err != nil { t.Fatalf("CreatePackages failed: %s", err) } mainPkg := prog.Package(mainInfo.Pkg) if mainPkg.Func("main") != nil { t.Fatalf("unexpected main function") } if prog.CreateTestMainPackage(mainPkg) != nil { t.Fatalf("CreateTestMainPackage returned non-nil") } }
func run(t *testing.T, dir, input string, success successPredicate) bool { fmt.Printf("Input: %s\n", input) start := time.Now() var inputs []string for _, i := range strings.Split(input, " ") { inputs = append(inputs, dir+i) } imp := importer.New(&importer.Config{Build: &build.Default}) // TODO(adonovan): use LoadInitialPackages, then un-export ParseFiles. // Then add the following packages' tests, which pass: // "flag", "unicode", "unicode/utf8", "testing", "log", "path". files, err := importer.ParseFiles(imp.Fset, ".", inputs...) if err != nil { t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error()) return false } // Print a helpful hint if we don't make it to the end. var hint string defer func() { if hint != "" { fmt.Println("FAIL") fmt.Println(hint) } else { fmt.Println("PASS") } interp.CapturedOutput = nil }() hint = fmt.Sprintf("To dump SSA representation, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=CFP %s\n", input) mainInfo := imp.CreatePackage(files[0].Name.Name, files...) if _, err := imp.LoadPackage("runtime"); err != nil { t.Errorf("LoadPackage(runtime) failed: %s", err) } prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) if err := prog.CreatePackages(imp); err != nil { t.Errorf("CreatePackages failed: %s", err) return false } prog.BuildAll() mainPkg := prog.Package(mainInfo.Pkg) if mainPkg.Func("main") == nil { testmainPkg := prog.CreateTestMainPackage(mainPkg) if testmainPkg == nil { t.Errorf("CreateTestMainPackage(%s) returned nil", mainPkg) return false } if testmainPkg.Func("main") == nil { t.Errorf("synthetic testmain package has no main") return false } mainPkg = testmainPkg } var out bytes.Buffer interp.CapturedOutput = &out hint = fmt.Sprintf("To trace execution, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=C -run --interp=T %s\n", input) exitCode := interp.Interpret(mainPkg, 0, inputs[0], []string{}) // The definition of success varies with each file. if err := success(exitCode, out.String()); err != nil { t.Errorf("interp.Interpret(%s) failed: %s", inputs, err) return false } hint = "" // call off the hounds if false { fmt.Println(input, time.Since(start)) // test profiling } return true }