complete/gocomplete/tests.go

48 lines
1.2 KiB
Go
Raw Normal View History

2017-05-05 16:25:27 -05:00
package main
import (
"os"
"path/filepath"
"regexp"
2017-05-05 16:25:27 -05:00
"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
)
var (
predictBenchmark = funcPredict(regexp.MustCompile("^Benchmark"))
predictTest = funcPredict(regexp.MustCompile("^(Test|Example)"))
)
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 funcPredict(funcRegexp *regexp.Regexp) complete.Predictor {
2017-05-11 12:28:31 -05:00
return complete.PredictFunc(func(a complete.Args) (prediction []string) {
tests := funcNames(funcRegexp)
2017-05-11 12:28:31 -05:00
for _, t := range tests {
2017-05-11 12:48:40 -05:00
if match.Prefix(t, a.Last) {
prediction = append(prediction, t)
2017-05-11 12:28:31 -05:00
}
}
2017-05-11 12:28:31 -05:00
return
})
2017-05-05 16:25:27 -05:00
}
// get all test names in current directory
func funcNames(funcRegexp *regexp.Regexp) (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
tests = append(tests, functionsInFile(path, funcRegexp)...)
2017-05-05 16:25:27 -05:00
return nil
})
return
}