github.com/monk-io/complete@v1.1.2/gocomplete/tests.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/posener/complete"
    10  )
    11  
    12  var (
    13  	predictBenchmark = funcPredict(regexp.MustCompile("^Benchmark"))
    14  	predictTest      = funcPredict(regexp.MustCompile("^(Test|Example)"))
    15  )
    16  
    17  // predictTest predict test names.
    18  // it searches in the current directory for all the go test files
    19  // and then all the relevant function names.
    20  // for test names use prefix of 'Test' or 'Example', and for benchmark
    21  // test names use 'Benchmark'
    22  func funcPredict(funcRegexp *regexp.Regexp) complete.Predictor {
    23  	return complete.PredictFunc(func(a complete.Args) []string {
    24  		return funcNames(funcRegexp)
    25  	})
    26  }
    27  
    28  // get all test names in current directory
    29  func funcNames(funcRegexp *regexp.Regexp) (tests []string) {
    30  	filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
    31  		// if not a test file, skip
    32  		if !strings.HasSuffix(path, "_test.go") {
    33  			return nil
    34  		}
    35  		// inspect test file and append all the test names
    36  		tests = append(tests, functionsInFile(path, funcRegexp)...)
    37  		return nil
    38  	})
    39  	return
    40  }