1
0
mirror of https://github.com/golang/go synced 2024-11-26 06:17:57 -07:00

[dev.typeparams] go/types: make TestManual work for directories

This is a port of CL 315769 to go/types, adjusted for the additional
testPkg indirection in go/types on top of testFiles, and to remove the
colDelta argument.

Change-Id: Ieb722d77866313a01645aeec49912c16cb475462
Reviewed-on: https://go-review.googlesource.com/c/go/+/324730
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-06-03 09:45:01 -04:00 committed by Robert Findley
parent d7592ab424
commit 655246f99a
3 changed files with 73 additions and 51 deletions

View File

@ -202,7 +202,7 @@ func asGoVersion(s string) string {
return "" return ""
} }
func checkFiles(t *testing.T, sizes Sizes, goVersion string, filenames []string, srcs [][]byte, manual bool) { func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, manual bool) {
if len(filenames) == 0 { if len(filenames) == 0 {
t.Fatal("no source files") t.Fatal("no source files")
} }
@ -225,6 +225,7 @@ func checkFiles(t *testing.T, sizes Sizes, goVersion string, filenames []string,
} }
// if no Go version is given, consider the package name // if no Go version is given, consider the package name
goVersion := *goVersion
if goVersion == "" { if goVersion == "" {
goVersion = asGoVersion(pkgName) goVersion = asGoVersion(pkgName)
} }
@ -297,29 +298,48 @@ func checkFiles(t *testing.T, sizes Sizes, goVersion string, filenames []string,
} }
} }
// TestManual is for manual testing of input files, provided as a list // TestManual is for manual testing of a package - either provided
// of arguments after the test arguments (and a separating "--"). For // as a list of filenames belonging to the package, or a directory
// instance, to check the files foo.go and bar.go, use: // name containing the package files - after the test arguments
// (and a separating "--"). For instance, to test the package made
// of the files foo.go and bar.go, use:
// //
// go test -run Manual -- foo.go bar.go // go test -run Manual -- foo.go bar.go
// //
// Provide the -verify flag to verify errors against ERROR comments in // If no source arguments are provided, the file testdata/manual.go2
// the input files rather than having a list of errors reported. // is used instead.
// The accepted Go language version can be controlled with the -lang flag. // Provide the -verify flag to verify errors against ERROR comments
// in the input files rather than having a list of errors reported.
// The accepted Go language version can be controlled with the -lang
// flag.
func TestManual(t *testing.T) { func TestManual(t *testing.T) {
testenv.MustHaveGoBuild(t)
filenames := flag.Args() filenames := flag.Args()
if len(filenames) == 0 { if len(filenames) == 0 {
return filenames = []string{filepath.FromSlash("testdata/manual.go2")}
} }
testenv.MustHaveGoBuild(t)
info, err := os.Stat(filenames[0])
if err != nil {
t.Fatalf("TestManual: %v", err)
}
DefPredeclaredTestFuncs() DefPredeclaredTestFuncs()
testPkg(t, filenames, *goVersion, true) if info.IsDir() {
if len(filenames) > 1 {
t.Fatal("TestManual: must have only one directory argument")
}
testDir(t, filenames[0], true)
} else {
testPkg(t, filenames, true)
}
} }
func TestLongConstants(t *testing.T) { func TestLongConstants(t *testing.T) {
format := "package longconst\n\nconst _ = %s\nconst _ = %s // ERROR excessively long constant" format := "package longconst\n\nconst _ = %s\nconst _ = %s // ERROR excessively long constant"
src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001)) src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001))
checkFiles(t, nil, "", []string{"longconst.go"}, [][]byte{[]byte(src)}, false) testFiles(t, nil, []string{"longconst.go"}, [][]byte{[]byte(src)}, false)
} }
// TestIndexRepresentability tests that constant index operands must // TestIndexRepresentability tests that constant index operands must
@ -327,7 +347,7 @@ func TestLongConstants(t *testing.T) {
// represent larger values. // represent larger values.
func TestIndexRepresentability(t *testing.T) { func TestIndexRepresentability(t *testing.T) {
const src = "package index\n\nvar s []byte\nvar _ = s[int64 /* ERROR \"int64\\(1\\) << 40 \\(.*\\) overflows int\" */ (1) << 40]" const src = "package index\n\nvar s []byte\nvar _ = s[int64 /* ERROR \"int64\\(1\\) << 40 \\(.*\\) overflows int\" */ (1) << 40]"
checkFiles(t, &StdSizes{4, 4}, "", []string{"index.go"}, [][]byte{[]byte(src)}, false) testFiles(t, &StdSizes{4, 4}, []string{"index.go"}, [][]byte{[]byte(src)}, false)
} }
func TestIssue46453(t *testing.T) { func TestIssue46453(t *testing.T) {
@ -335,17 +355,17 @@ func TestIssue46453(t *testing.T) {
t.Skip("type params are enabled") t.Skip("type params are enabled")
} }
const src = "package p\ntype _ comparable // ERROR \"undeclared name: comparable\"" const src = "package p\ntype _ comparable // ERROR \"undeclared name: comparable\""
checkFiles(t, nil, "", []string{"issue46453.go"}, [][]byte{[]byte(src)}, false) testFiles(t, nil, []string{"issue46453.go"}, [][]byte{[]byte(src)}, false)
} }
func TestCheck(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, "check") } func TestCheck(t *testing.T) { DefPredeclaredTestFuncs(); testDirFiles(t, "testdata/check", false) }
func TestExamples(t *testing.T) { testDir(t, "examples") } func TestExamples(t *testing.T) { testDirFiles(t, "testdata/examples", false) }
func TestFixedbugs(t *testing.T) { testDir(t, "fixedbugs") } func TestFixedbugs(t *testing.T) { testDirFiles(t, "testdata/fixedbugs", false) }
func testDir(t *testing.T, dir string) { func testDirFiles(t *testing.T, dir string, manual bool) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir = filepath.FromSlash(dir)
dir = filepath.Join("testdata", dir)
fis, err := os.ReadDir(dir) fis, err := os.ReadDir(dir)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -355,28 +375,38 @@ func testDir(t *testing.T, dir string) {
for _, fi := range fis { for _, fi := range fis {
path := filepath.Join(dir, fi.Name()) path := filepath.Join(dir, fi.Name())
// if fi is a directory, its files make up a single package // If fi is a directory, its files make up a single package.
var filenames []string
if fi.IsDir() { if fi.IsDir() {
fis, err := os.ReadDir(path) testDir(t, path, manual)
if err != nil {
t.Error(err)
continue
}
for _, fi := range fis {
filenames = append(filenames, filepath.Join(path, fi.Name()))
}
} else { } else {
filenames = []string{path} t.Run(filepath.Base(path), func(t *testing.T) {
testPkg(t, []string{path}, manual)
})
} }
t.Run(filepath.Base(path), func(t *testing.T) {
testPkg(t, filenames, "", false)
})
} }
} }
func testDir(t *testing.T, dir string, manual bool) {
testenv.MustHaveGoBuild(t)
fis, err := os.ReadDir(dir)
if err != nil {
t.Error(err)
return
}
var filenames []string
for _, fi := range fis {
filenames = append(filenames, filepath.Join(dir, fi.Name()))
}
t.Run(filepath.Base(dir), func(t *testing.T) {
testPkg(t, filenames, manual)
})
}
// TODO(rFindley) reconcile the different test setup in go/types with types2. // TODO(rFindley) reconcile the different test setup in go/types with types2.
func testPkg(t *testing.T, filenames []string, goVersion string, manual bool) { func testPkg(t *testing.T, filenames []string, manual bool) {
srcs := make([][]byte, len(filenames)) srcs := make([][]byte, len(filenames))
for i, filename := range filenames { for i, filename := range filenames {
src, err := os.ReadFile(filename) src, err := os.ReadFile(filename)
@ -385,5 +415,5 @@ func testPkg(t *testing.T, filenames []string, goVersion string, manual bool) {
} }
srcs[i] = src srcs[i] = src
} }
checkFiles(t, nil, goVersion, filenames, srcs, manual) testFiles(t, nil, filenames, srcs, manual)
} }

View File

@ -1,17 +0,0 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is meant as "dumping ground" for debugging code.
package p
// fun test case
type C[P interface{m()}] P
func (r C[P]) m() { r.m() }
func f[T interface{m(); n()}](x T) {
y := C[T](x)
y.m()
}

9
src/go/types/testdata/manual.go2 vendored Normal file
View File

@ -0,0 +1,9 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is tested when running "go test -run Manual"
// without source arguments. Use for one-off debugging.
package p