diff options
author | Rob Findley <rfindley@google.com> | 2021-02-11 10:23:41 -0500 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-02-13 00:40:18 +0000 |
commit | b81efb7ec4348951211058cf4fdfc045c75255d6 (patch) | |
tree | 262b756be7647299688e37d153cefaa180eb570f /src/go/types/check_test.go | |
parent | a7e9b4b94804a1fbefc0c012ec510f4ee0837ffa (diff) | |
download | go-b81efb7ec4348951211058cf4fdfc045c75255d6.tar.gz go-b81efb7ec4348951211058cf4fdfc045c75255d6.zip |
[dev.regabi] go/types: add support for language version checking
This is a port of CL 289509 to go/types. It differs from that CL in
codes added to errors, to fit the new factoring of check_test.go, and to
allow go/types to import regexp in deps_test.go
For #31793
Change-Id: Ia9e4c7f5aac1493001189184227c2ebc79a76e77
Reviewed-on: https://go-review.googlesource.com/c/go/+/291317
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/go/types/check_test.go')
-rw-r--r-- | src/go/types/check_test.go | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/go/types/check_test.go b/src/go/types/check_test.go index 7292f7bcb2..ca7d926ca9 100644 --- a/src/go/types/check_test.go +++ b/src/go/types/check_test.go @@ -47,7 +47,8 @@ import ( var ( haltOnError = flag.Bool("halt", false, "halt on error") listErrors = flag.Bool("errlist", false, "list errors") - testFiles = flag.String("files", "", "space-separated list of test files") + testFiles = flag.String("files", "", "comma-separated list of test files") + goVersion = flag.String("lang", "", "Go language version (e.g. \"go1.12\"") ) var fset = token.NewFileSet() @@ -188,7 +189,21 @@ func eliminate(t *testing.T, errmap map[string][]string, errlist []error) { } } -func checkFiles(t *testing.T, filenames []string, srcs [][]byte) { +// goVersionRx matches a Go version string using '_', e.g. "go1_12". +var goVersionRx = regexp.MustCompile(`^go[1-9][0-9]*_(0|[1-9][0-9]*)$`) + +// asGoVersion returns a regular Go language version string +// if s is a Go version string using '_' rather than '.' to +// separate the major and minor version numbers (e.g. "go1_12"). +// Otherwise it returns the empty string. +func asGoVersion(s string) string { + if goVersionRx.MatchString(s) { + return strings.Replace(s, "_", ".", 1) + } + return "" +} + +func checkFiles(t *testing.T, goVersion string, filenames []string, srcs [][]byte) { if len(filenames) == 0 { t.Fatal("no source files") } @@ -201,6 +216,11 @@ func checkFiles(t *testing.T, filenames []string, srcs [][]byte) { pkgName = files[0].Name.Name } + // if no Go version is given, consider the package name + if goVersion == "" { + goVersion = asGoVersion(pkgName) + } + if *listErrors && len(errlist) > 0 { t.Errorf("--- %s:", pkgName) for _, err := range errlist { @@ -210,6 +230,7 @@ func checkFiles(t *testing.T, filenames []string, srcs [][]byte) { // typecheck and collect typechecker errors var conf Config + conf.GoVersion = goVersion // special case for importC.src if len(filenames) == 1 { @@ -267,19 +288,20 @@ func checkFiles(t *testing.T, filenames []string, srcs [][]byte) { } // TestCheck is for manual testing of selected input files, provided with -files. +// The accepted Go language version can be controlled with the -lang flag. func TestCheck(t *testing.T) { if *testFiles == "" { return } testenv.MustHaveGoBuild(t) DefPredeclaredTestFuncs() - testPkg(t, strings.Split(*testFiles, " ")) + testPkg(t, strings.Split(*testFiles, ","), *goVersion) } func TestLongConstants(t *testing.T) { format := "package longconst\n\nconst _ = %s\nconst _ = %s // ERROR excessively long constant" src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001)) - checkFiles(t, []string{"longconst.go"}, [][]byte{[]byte(src)}) + checkFiles(t, "", []string{"longconst.go"}, [][]byte{[]byte(src)}) } func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, "testdata") } @@ -312,12 +334,12 @@ func testDir(t *testing.T, dir string) { filenames = []string{path} } t.Run(filepath.Base(path), func(t *testing.T) { - testPkg(t, filenames) + testPkg(t, filenames, "") }) } } -func testPkg(t *testing.T, filenames []string) { +func testPkg(t *testing.T, filenames []string, goVersion string) { srcs := make([][]byte, len(filenames)) for i, filename := range filenames { src, err := os.ReadFile(filename) @@ -326,5 +348,5 @@ func testPkg(t *testing.T, filenames []string) { } srcs[i] = src } - checkFiles(t, filenames, srcs) + checkFiles(t, goVersion, filenames, srcs) } |