forked from posener/complete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.go
47 lines (42 loc) · 1.21 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/posener/complete"
"github.com/posener/complete/match"
)
var (
predictBenchmark = funcPredict(regexp.MustCompile("^Benchmark"))
predictTest = funcPredict(regexp.MustCompile("^(Test|Example)"))
)
// 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 {
return complete.PredictFunc(func(a complete.Args) (prediction []string) {
tests := funcNames(funcRegexp)
for _, t := range tests {
if match.Prefix(t, a.Last) {
prediction = append(prediction, t)
}
}
return
})
}
// get all test names in current directory
func funcNames(funcRegexp *regexp.Regexp) (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, functionsInFile(path, funcRegexp)...)
return nil
})
return
}