diff options
author | Rob Findley <rfindley@google.com> | 2021-02-04 11:06:15 -0500 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-02-04 17:13:55 +0000 |
commit | bc451b5770dc99b6a74934c26fd11a8cdc172bb1 (patch) | |
tree | a8d2b08902e1b662df817a7b6c035691caf1292e | |
parent | afd67f333466fc67cd37433e45ecdb190efc8f51 (diff) | |
download | go-bc451b5770dc99b6a74934c26fd11a8cdc172bb1.tar.gz go-bc451b5770dc99b6a74934c26fd11a8cdc172bb1.zip |
[dev.regabi] go/types: port check_test.go ergonomics from dev.typeparams
On the dev.typeparams and dev.go2go branches, check_test.go has been
updated to automatically discover test data. This is convenient, so port
it to dev.regabi.
Change-Id: I5da9a9a5139c35a2693e64364eb9928ece1cd7c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/289713
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>
-rw-r--r-- | src/go/types/check_test.go | 121 | ||||
-rw-r--r-- | src/go/types/testdata/decls2/decls2a.src (renamed from src/go/types/testdata/decls2a.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/decls2/decls2b.src (renamed from src/go/types/testdata/decls2b.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/importdecl0/importdecl0a.src (renamed from src/go/types/testdata/importdecl0a.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/importdecl0/importdecl0b.src (renamed from src/go/types/testdata/importdecl0b.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/importdecl1/importdecl1a.src (renamed from src/go/types/testdata/importdecl1a.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/importdecl1/importdecl1b.src (renamed from src/go/types/testdata/importdecl1b.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/issue25008/issue25008a.src (renamed from src/go/types/testdata/issue25008a.src) | 0 | ||||
-rw-r--r-- | src/go/types/testdata/issue25008/issue25008b.src (renamed from src/go/types/testdata/issue25008b.src) | 0 |
9 files changed, 45 insertions, 76 deletions
diff --git a/src/go/types/check_test.go b/src/go/types/check_test.go index ce31dab68b..47d749b3a3 100644 --- a/src/go/types/check_test.go +++ b/src/go/types/check_test.go @@ -27,12 +27,14 @@ package types_test import ( "flag" + "fmt" "go/ast" "go/importer" "go/parser" "go/scanner" "go/token" "internal/testenv" + "io/ioutil" "os" "path/filepath" "regexp" @@ -48,54 +50,6 @@ var ( testFiles = flag.String("files", "", "space-separated list of test files") ) -// The test filenames do not end in .go so that they are invisible -// to gofmt since they contain comments that must not change their -// positions relative to surrounding tokens. - -// Each tests entry is list of files belonging to the same package. -var tests = [][]string{ - {"testdata/errors.src"}, - {"testdata/importdecl0a.src", "testdata/importdecl0b.src"}, - {"testdata/importdecl1a.src", "testdata/importdecl1b.src"}, - {"testdata/importC.src"}, // special handling in checkFiles - {"testdata/cycles.src"}, - {"testdata/cycles1.src"}, - {"testdata/cycles2.src"}, - {"testdata/cycles3.src"}, - {"testdata/cycles4.src"}, - {"testdata/cycles5.src"}, - {"testdata/init0.src"}, - {"testdata/init1.src"}, - {"testdata/init2.src"}, - {"testdata/decls0.src"}, - {"testdata/decls1.src"}, - {"testdata/decls2a.src", "testdata/decls2b.src"}, - {"testdata/decls3.src"}, - {"testdata/decls4.src"}, - {"testdata/decls5.src"}, - {"testdata/const0.src"}, - {"testdata/const1.src"}, - {"testdata/constdecl.src"}, - {"testdata/vardecl.src"}, - {"testdata/expr0.src"}, - {"testdata/expr1.src"}, - {"testdata/expr2.src"}, - {"testdata/expr3.src"}, - {"testdata/methodsets.src"}, - {"testdata/shifts.src"}, - {"testdata/builtins.src"}, - {"testdata/conversions.src"}, - {"testdata/conversions2.src"}, - {"testdata/stmt0.src"}, - {"testdata/stmt1.src"}, - {"testdata/gotos.src"}, - {"testdata/labels.src"}, - {"testdata/literals.src"}, - {"testdata/issues.src"}, - {"testdata/blank.src"}, - {"testdata/issue25008b.src", "testdata/issue25008a.src"}, // order (b before a) is crucial! -} - var fset = token.NewFileSet() // Positioned errors are of the form filename:line:column: message . @@ -236,9 +190,13 @@ func eliminate(t *testing.T, errmap map[string][]string, errlist []error) { } } -func checkFiles(t *testing.T, testfiles []string) { +func checkFiles(t *testing.T, sources []string) { + if len(sources) == 0 { + t.Fatal("no source files") + } + // parse files and collect parser errors - files, errlist := parseFiles(t, testfiles) + files, errlist := parseFiles(t, sources) pkgName := "<no package>" if len(files) > 0 { @@ -254,10 +212,13 @@ func checkFiles(t *testing.T, testfiles []string) { // typecheck and collect typechecker errors var conf Config + // special case for importC.src - if len(testfiles) == 1 && strings.HasSuffix(testfiles[0], "importC.src") { + if len(sources) == 1 && strings.HasSuffix(sources[0], "importC.src") { conf.FakeImportC = true } + // TODO(rFindley) we may need to use the source importer when adding generics + // tests. conf.Importer = importer.Default() conf.Error = func(err error) { if *haltOnError { @@ -306,44 +267,52 @@ func checkFiles(t *testing.T, testfiles []string) { } } +// TestCheck is for manual testing of selected input files, provided with -files. func TestCheck(t *testing.T) { - testenv.MustHaveGoBuild(t) - - // Declare builtins for testing. - DefPredeclaredTestFuncs() - - // If explicit test files are specified, only check those. - if files := *testFiles; files != "" { - checkFiles(t, strings.Split(files, " ")) + if *testFiles == "" { return } - - // Otherwise, run all the tests. - for _, files := range tests { - checkFiles(t, files) - } + testenv.MustHaveGoBuild(t) + DefPredeclaredTestFuncs() + checkFiles(t, strings.Split(*testFiles, " ")) } -func TestFixedBugs(t *testing.T) { testDir(t, "fixedbugs") } +func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, "testdata") } +func TestFixedbugs(t *testing.T) { testDir(t, "fixedbugs") } func testDir(t *testing.T, dir string) { testenv.MustHaveGoBuild(t) - dirs, err := os.ReadDir(dir) + fis, err := os.ReadDir(dir) if err != nil { - t.Fatal(err) + t.Error(err) + return } - for _, d := range dirs { - testname := filepath.Base(d.Name()) - testname = strings.TrimSuffix(testname, filepath.Ext(testname)) - t.Run(testname, func(t *testing.T) { - filename := filepath.Join(dir, d.Name()) - if d.IsDir() { - t.Errorf("skipped directory %q", filename) - return + for _, fi := range fis { + path := filepath.Join(dir, fi.Name()) + + // if fi is a directory, its files make up a single package + var files []string + if fi.IsDir() { + fis, err := ioutil.ReadDir(path) + if err != nil { + t.Error(err) + continue + } + files = make([]string, len(fis)) + for i, fi := range fis { + // if fi is a directory, checkFiles below will complain + files[i] = filepath.Join(path, fi.Name()) + if testing.Verbose() { + fmt.Printf("\t%s\n", files[i]) + } } - checkFiles(t, []string{filename}) + } else { + files = []string{path} + } + t.Run(filepath.Base(path), func(t *testing.T) { + checkFiles(t, files) }) } } diff --git a/src/go/types/testdata/decls2a.src b/src/go/types/testdata/decls2/decls2a.src index bdbecd9dbb..bdbecd9dbb 100644 --- a/src/go/types/testdata/decls2a.src +++ b/src/go/types/testdata/decls2/decls2a.src diff --git a/src/go/types/testdata/decls2b.src b/src/go/types/testdata/decls2/decls2b.src index 5c55750a10..5c55750a10 100644 --- a/src/go/types/testdata/decls2b.src +++ b/src/go/types/testdata/decls2/decls2b.src diff --git a/src/go/types/testdata/importdecl0a.src b/src/go/types/testdata/importdecl0/importdecl0a.src index e96fca3cdd..e96fca3cdd 100644 --- a/src/go/types/testdata/importdecl0a.src +++ b/src/go/types/testdata/importdecl0/importdecl0a.src diff --git a/src/go/types/testdata/importdecl0b.src b/src/go/types/testdata/importdecl0/importdecl0b.src index 6844e70982..6844e70982 100644 --- a/src/go/types/testdata/importdecl0b.src +++ b/src/go/types/testdata/importdecl0/importdecl0b.src diff --git a/src/go/types/testdata/importdecl1a.src b/src/go/types/testdata/importdecl1/importdecl1a.src index d377c01638..d377c01638 100644 --- a/src/go/types/testdata/importdecl1a.src +++ b/src/go/types/testdata/importdecl1/importdecl1a.src diff --git a/src/go/types/testdata/importdecl1b.src b/src/go/types/testdata/importdecl1/importdecl1b.src index ee70bbd8e7..ee70bbd8e7 100644 --- a/src/go/types/testdata/importdecl1b.src +++ b/src/go/types/testdata/importdecl1/importdecl1b.src diff --git a/src/go/types/testdata/issue25008a.src b/src/go/types/testdata/issue25008/issue25008a.src index cf71ca10e4..cf71ca10e4 100644 --- a/src/go/types/testdata/issue25008a.src +++ b/src/go/types/testdata/issue25008/issue25008a.src diff --git a/src/go/types/testdata/issue25008b.src b/src/go/types/testdata/issue25008/issue25008b.src index f132b7fab3..f132b7fab3 100644 --- a/src/go/types/testdata/issue25008b.src +++ b/src/go/types/testdata/issue25008/issue25008b.src |