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-06 11:08:47 -05:00
|
|
|
func predictTest(testType string) *complete.Predicate {
|
|
|
|
return &complete.Predicate{
|
2017-05-06 11:47:27 -05:00
|
|
|
Predictor: func(last string) []complete.Option {
|
2017-05-05 16:25:27 -05:00
|
|
|
tests := testNames(testType)
|
|
|
|
options := make([]complete.Option, len(tests))
|
|
|
|
for i := range tests {
|
|
|
|
options[i] = complete.Arg(tests[i])
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all test names in current directory
|
|
|
|
func testNames(testType string) (tests []string) {
|
|
|
|
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
|
|
|
|
tests = append(tests, testsInFile(testType, path)...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func testsInFile(testType, path string) (tests []string) {
|
|
|
|
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()
|
|
|
|
if strings.HasPrefix(name, testType) {
|
|
|
|
tests = append(tests, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|