mirror of
https://github.com/golang/go
synced 2024-11-26 07:17:59 -07:00
[dev.typeparams] go/types: import dev.go2go changes to check tests
Import changes from go2go to automatically discover testdata-driven check tests. Tests for generics will be added in a subsequent CL. Change-Id: I50d55141750caebf15f1f382e139edfe9920c14e Reviewed-on: https://go-review.googlesource.com/c/go/+/283132 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:
parent
9e746e4255
commit
6a56c6c870
@ -27,12 +27,14 @@ package types_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/importer"
|
"go/importer"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/scanner"
|
"go/scanner"
|
||||||
"go/token"
|
"go/token"
|
||||||
"internal/testenv"
|
"internal/testenv"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -48,54 +50,6 @@ var (
|
|||||||
testFiles = flag.String("files", "", "space-separated list of test files")
|
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()
|
var fset = token.NewFileSet()
|
||||||
|
|
||||||
// Positioned errors are of the form filename:line:column: message .
|
// Positioned errors are of the form filename:line:column: message .
|
||||||
@ -236,9 +190,9 @@ 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) {
|
||||||
// parse files and collect parser errors
|
// parse files and collect parser errors
|
||||||
files, errlist := parseFiles(t, testfiles)
|
files, errlist := parseFiles(t, sources)
|
||||||
|
|
||||||
pkgName := "<no package>"
|
pkgName := "<no package>"
|
||||||
if len(files) > 0 {
|
if len(files) > 0 {
|
||||||
@ -254,10 +208,14 @@ func checkFiles(t *testing.T, testfiles []string) {
|
|||||||
|
|
||||||
// typecheck and collect typechecker errors
|
// typecheck and collect typechecker errors
|
||||||
var conf Config
|
var conf Config
|
||||||
|
// TODO(rFindley) parse generics when given a .go2 suffix.
|
||||||
|
|
||||||
// special case for importC.src
|
// 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
|
conf.FakeImportC = true
|
||||||
}
|
}
|
||||||
|
// TODO(rFindley) we may need to use the source importer when adding generics
|
||||||
|
// tests.
|
||||||
conf.Importer = importer.Default()
|
conf.Importer = importer.Default()
|
||||||
conf.Error = func(err error) {
|
conf.Error = func(err error) {
|
||||||
if *haltOnError {
|
if *haltOnError {
|
||||||
@ -306,44 +264,61 @@ func checkFiles(t *testing.T, testfiles []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestCheck is for manual testing of selected input files, provided with -files.
|
||||||
func TestCheck(t *testing.T) {
|
func TestCheck(t *testing.T) {
|
||||||
testenv.MustHaveGoBuild(t)
|
if *testFiles == "" {
|
||||||
|
|
||||||
// Declare builtins for testing.
|
|
||||||
DefPredeclaredTestFuncs()
|
|
||||||
|
|
||||||
// If explicit test files are specified, only check those.
|
|
||||||
if files := *testFiles; files != "" {
|
|
||||||
checkFiles(t, strings.Split(files, " "))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
testenv.MustHaveGoBuild(t)
|
||||||
// Otherwise, run all the tests.
|
DefPredeclaredTestFuncs()
|
||||||
for _, files := range tests {
|
checkFiles(t, strings.Split(*testFiles, " "))
|
||||||
checkFiles(t, files)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFixedBugs(t *testing.T) { testDir(t, "fixedbugs") }
|
func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, "testdata") }
|
||||||
|
|
||||||
|
// TODO(rFindley) add go2 examples.
|
||||||
|
// func TestExamples(t *testing.T) { testDir(t, "examples") }
|
||||||
|
|
||||||
|
func TestFixedbugs(t *testing.T) { testDir(t, "fixedbugs") }
|
||||||
|
|
||||||
func testDir(t *testing.T, dir string) {
|
func testDir(t *testing.T, dir string) {
|
||||||
testenv.MustHaveGoBuild(t)
|
testenv.MustHaveGoBuild(t)
|
||||||
|
|
||||||
dirs, err := os.ReadDir(dir)
|
fis, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range dirs {
|
for count, fi := range fis {
|
||||||
testname := filepath.Base(d.Name())
|
path := filepath.Join(dir, fi.Name())
|
||||||
testname = strings.TrimSuffix(testname, filepath.Ext(testname))
|
|
||||||
t.Run(testname, func(t *testing.T) {
|
// if fi is a directory, its files make up a single package
|
||||||
filename := filepath.Join(dir, d.Name())
|
if fi.IsDir() {
|
||||||
if d.IsDir() {
|
if testing.Verbose() {
|
||||||
t.Errorf("skipped directory %q", filename)
|
fmt.Printf("%3d %s\n", count, path)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
checkFiles(t, []string{filename})
|
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, files)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, fi is a stand-alone file
|
||||||
|
if testing.Verbose() {
|
||||||
|
fmt.Printf("%3d %s\n", count, path)
|
||||||
|
}
|
||||||
|
checkFiles(t, []string{path})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user