Created
September 26, 2010 17:41
-
-
Save nsf/598145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "io/ioutil" | |
| "bytes" | |
| "exec" | |
| "path" | |
| "fmt" | |
| "os" | |
| ) | |
| const RED = "\033[31m" | |
| const GRN = "\033[32m" | |
| const NC = "\033[0m" | |
| const PASS = GRN + "PASS!" + NC | |
| const FAIL = RED + "FAIL!" + NC | |
| const LINE = "████████████████████████████████████████████████████████████████████████" | |
| type Stats struct { | |
| total, ok, fail int | |
| } | |
| var stats Stats | |
| func print_fail_report(t string, out, outexpected []byte) { | |
| fmt.Printf("%s: %s\n", t, FAIL) | |
| fmt.Printf("-----------------------------------------------------------------\n") | |
| fmt.Printf("Got:\n%s\n", string(out)) | |
| fmt.Printf("-----------------------------------------------------------------\n") | |
| fmt.Printf("Expected:\n%s\n", string(outexpected)) | |
| fmt.Printf("-----------------------------------------------------------------\n") | |
| } | |
| func print_pass_report(t string) { | |
| fmt.Printf("%s: %s\n", t, PASS) | |
| } | |
| func print_stats() { | |
| fmt.Printf("\nSummary (total: %d)\n", stats.total) | |
| fmt.Printf(GRN + " PASS" + NC + ": %d\n", stats.ok) | |
| fmt.Printf(RED + " FAIL" + NC + ": %d\n", stats.fail) | |
| if stats.fail == 0 { | |
| fmt.Print(GRN) | |
| } else { | |
| fmt.Print(RED) | |
| } | |
| fmt.Println(LINE + NC) | |
| } | |
| func autocomplete(filename string, cursor string) []byte { | |
| path, err := exec.LookPath("gocode") | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| cwd, err := os.Getwd() | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| p, err := exec.Run(path, []string{"gocode", "-in", filename, "autocomplete", filename, cursor}, | |
| os.Environ(), cwd, exec.Pipe, exec.Pipe, exec.Pipe) | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| defer p.Close() | |
| data, err := ioutil.ReadAll(p.Stdout) | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| return data | |
| } | |
| func get_cursor(t string) string { | |
| filesInDir, err := ioutil.ReadDir(t) | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| for _, stat := range filesInDir { | |
| ok, _ := path.Match("cursor.*", stat.Name) | |
| if ok { | |
| return path.Ext(stat.Name)[1:] | |
| } | |
| } | |
| panic("Can't find cursor.* file!") | |
| return "" | |
| } | |
| func get_expected(t string) []byte { | |
| data, err := ioutil.ReadFile(t + "/out.expected") | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| return data | |
| } | |
| func run_test(t string) { | |
| stats.total++ | |
| cursor := get_cursor(t) | |
| outexpected := get_expected(t) | |
| filename := t + "/test.go" | |
| out := autocomplete(filename, cursor) | |
| if bytes.Equal(out, outexpected) { | |
| print_pass_report(t) | |
| stats.ok++ | |
| } else { | |
| print_fail_report(t, out, outexpected) | |
| stats.fail++ | |
| } | |
| } | |
| func main() { | |
| filesInDir, err := ioutil.ReadDir(".") | |
| if err != nil { | |
| panic(err.String()) | |
| } | |
| for _, stat := range filesInDir { | |
| ok, _ := path.Match("test.*", stat.Name) | |
| if ok { | |
| run_test(stat.Name) | |
| } | |
| } | |
| print_stats() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment