complete/gocomplete/tests.go

41 lines
1.0 KiB
Go
Raw Permalink 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"
2019-11-17 17:25:16 -06:00
"github.com/posener/complete/v2"
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 {
2019-11-13 22:51:44 -06:00
return complete.PredictFunc(func(prefix string) []string {
return funcNames(funcRegexp)
2017-05-11 12:28:31 -05:00
})
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
}