2017-05-05 16:25:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/posener/complete"
|
2017-05-07 11:53:55 -05:00
|
|
|
"github.com/posener/complete/match"
|
2017-05-05 16:25:27 -05:00
|
|
|
)
|
|
|
|
|
2017-05-07 22:41:37 -05:00
|
|
|
// predictTest predict test names.
|
|
|
|
// it searches in the current directory for all the go test files
|
|
|
|
// and then all the relevant function names.
|
|
|
|
// for test names use prefix of 'Test' or 'Example', and for benchmark
|
|
|
|
// test names use 'Benchmark'
|
|
|
|
func predictTest(funcPrefix ...string) complete.Predicate {
|
2017-05-07 11:53:55 -05:00
|
|
|
return func(last string) []match.Matcher {
|
2017-05-07 22:41:37 -05:00
|
|
|
tests := testNames(funcPrefix)
|
2017-05-07 11:53:55 -05:00
|
|
|
options := make([]match.Matcher, len(tests))
|
2017-05-06 12:07:50 -05:00
|
|
|
for i := range tests {
|
2017-05-07 11:53:55 -05:00
|
|
|
options[i] = match.Prefix(tests[i])
|
2017-05-06 12:07:50 -05:00
|
|
|
}
|
|
|
|
return options
|
2017-05-05 16:25:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all test names in current directory
|
2017-05-07 22:41:37 -05:00
|
|
|
func testNames(funcPrefix []string) (tests []string) {
|
2017-05-05 16:25:27 -05:00
|
|
|
filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
|
|
|
|
// if not a test file, skip
|
|
|
|
if !strings.HasSuffix(path, "_test.go") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// inspect test file and append all the test names
|
2017-05-07 22:41:37 -05:00
|
|
|
tests = append(tests, testsInFile(funcPrefix, path)...)
|
2017-05-05 16:25:27 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-07 22:41:37 -05:00
|
|
|
func testsInFile(funcPrefix []string, path string) (tests []string) {
|
2017-05-05 16:25:27 -05:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, path, nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
complete.Log("Failed parsing %s: %s", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
if f, ok := d.(*ast.FuncDecl); ok {
|
|
|
|
name := f.Name.String()
|
2017-05-07 22:41:37 -05:00
|
|
|
for _, prefix := range funcPrefix {
|
|
|
|
if strings.HasPrefix(name, prefix) {
|
|
|
|
tests = append(tests, name)
|
|
|
|
break
|
|
|
|
}
|
2017-05-05 16:25:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|