File: tests.go

package info (click to toggle)
golang-github-posener-complete 1.1%2Bgit20180108.57878c9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 200 kB
  • sloc: sh: 9; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download | duplicates (2)
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
package main

import (
	"os"
	"path/filepath"
	"regexp"
	"strings"

	"github.com/posener/complete"
)

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) []string {
		return funcNames(funcRegexp)
	})
}

// 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
}