1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements a typechecker test harness. The packages specified
6 // in tests are typechecked. Error messages reported by the typechecker are
7 // compared against the error messages expected in the test files.
9 // Expected errors are indicated in the test files by putting a comment
10 // of the form /* ERROR "rx" */ immediately following an offending token.
11 // The harness will verify that an error matching the regular expression
12 // rx is reported at that source position. Consecutive comments may be
13 // used to indicate multiple errors for the same token position.
15 // For instance, the following test file indicates that a "not declared"
16 // error should be reported for the undeclared variable x:
20 // _ = x /* ERROR "not declared" */ + 1
26 "cmd/compile/internal/syntax"
37 . "cmd/compile/internal/types2"
41 haltOnError = flag.Bool("halt", false, "halt on error")
42 verifyErrors = flag.Bool("verify", false, "verify errors (rather than list them) in TestManual")
43 goVersion = flag.String("lang", "", "Go language version (e.g. \"go1.12\")")
46 func parseFiles(t *testing.T, filenames []string, mode syntax.Mode) ([]*syntax.File, []error) {
47 var files []*syntax.File
49 errh := func(err error) { errlist = append(errlist, err) }
50 for _, filename := range filenames {
51 file, err := syntax.ParseFile(filename, errh, nil, mode)
53 t.Fatalf("%s: %s", filename, err)
55 files = append(files, file)
60 func unpackError(err error) syntax.Error {
61 switch err := err.(type) {
65 return syntax.Error{Pos: err.Pos, Msg: err.Msg}
67 return syntax.Error{Msg: err.Error()}
71 // delta returns the absolute difference between x and y.
72 func delta(x, y uint) uint {
83 // goVersionRx matches a Go version string using '_', e.g. "go1_12".
84 var goVersionRx = regexp.MustCompile(`^go[1-9][0-9]*_(0|[1-9][0-9]*)$`)
86 // asGoVersion returns a regular Go language version string
87 // if s is a Go version string using '_' rather than '.' to
88 // separate the major and minor version numbers (e.g. "go1_12").
89 // Otherwise it returns the empty string.
90 func asGoVersion(s string) string {
91 if goVersionRx.MatchString(s) {
92 return strings.Replace(s, "_", ".", 1)
97 // excludedForUnifiedBuild lists files that cannot be tested
98 // when using the unified build's export data.
99 // TODO(gri) enable as soon as the unified build supports this.
100 var excludedForUnifiedBuild = map[string]bool{
101 "issue47818.go2": true,
102 "issue49705.go2": true,
105 func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
106 if len(filenames) == 0 {
107 t.Fatal("no source files")
110 if buildcfg.Experiment.Unified {
111 for _, f := range filenames {
112 if excludedForUnifiedBuild[filepath.Base(f)] {
113 t.Logf("%s cannot be tested with unified build - skipped", f)
120 if strings.HasSuffix(filenames[0], ".go2") || manual {
121 mode |= syntax.AllowGenerics | syntax.AllowMethodTypeParams
123 // parse files and collect parser errors
124 files, errlist := parseFiles(t, filenames, mode)
126 pkgName := "<no package>"
128 pkgName = files[0].PkgName.Value
131 // if no Go version is given, consider the package name
132 goVersion := *goVersion
134 goVersion = asGoVersion(pkgName)
137 listErrors := manual && !*verifyErrors
138 if listErrors && len(errlist) > 0 {
139 t.Errorf("--- %s:", pkgName)
140 for _, err := range errlist {
145 // typecheck and collect typechecker errors
147 conf.GoVersion = goVersion
148 // special case for importC.src
149 if len(filenames) == 1 && strings.HasSuffix(filenames[0], "importC.src") {
150 conf.FakeImportC = true
152 conf.Trace = manual && testing.Verbose()
153 conf.Importer = defaultImporter()
154 conf.Error = func(err error) {
162 errlist = append(errlist, err)
164 conf.Check(pkgName, files, nil)
170 // sort errlist in source order
171 sort.Slice(errlist, func(i, j int) bool {
172 pi := unpackError(errlist[i]).Pos
173 pj := unpackError(errlist[j]).Pos
174 return pi.Cmp(pj) < 0
177 // collect expected errors
178 errmap := make(map[string]map[uint][]syntax.Error)
179 for _, filename := range filenames {
180 f, err := os.Open(filename)
185 if m := syntax.ErrorMap(f); len(m) > 0 {
191 // match against found errors
192 for _, err := range errlist {
193 got := unpackError(err)
195 // find list of errors for the respective error line
196 filename := got.Pos.Base().Filename()
197 filemap := errmap[filename]
198 line := got.Pos.Line()
199 var list []syntax.Error
205 // one of errors in list should match the current error
206 index := -1 // list index of matching message, if any
207 for i, want := range list {
208 rx, err := regexp.Compile(want.Msg)
210 t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
213 if rx.MatchString(got.Msg) {
219 t.Errorf("%s: no error expected: %q", got.Pos, got.Msg)
223 // column position must be within expected colDelta
225 if delta(got.Pos.Col(), want.Pos.Col()) > colDelta {
226 t.Errorf("%s: got col = %d; want %d", got.Pos, got.Pos.Col(), want.Pos.Col())
229 // eliminate from list
230 if n := len(list) - 1; n > 0 {
231 // not the last entry - slide entries down (don't reorder)
232 copy(list[index:], list[index+1:])
233 filemap[line] = list[:n]
235 // last entry - remove list from filemap
236 delete(filemap, line)
239 // if filemap is empty, eliminate from errmap
240 if len(filemap) == 0 {
241 delete(errmap, filename)
245 // there should be no expected errors left
247 t.Errorf("--- %s: unreported errors:", pkgName)
248 for filename, filemap := range errmap {
249 for line, list := range filemap {
250 for _, err := range list {
251 t.Errorf("%s:%d:%d: %s", filename, line, err.Pos.Col(), err.Msg)
258 // TestManual is for manual testing of a package - either provided
259 // as a list of filenames belonging to the package, or a directory
260 // name containing the package files - after the test arguments
261 // (and a separating "--"). For instance, to test the package made
262 // of the files foo.go and bar.go, use:
264 // go test -run Manual -- foo.go bar.go
266 // If no source arguments are provided, the file testdata/manual.go2
268 // Provide the -verify flag to verify errors against ERROR comments
269 // in the input files rather than having a list of errors reported.
270 // The accepted Go language version can be controlled with the -lang
272 func TestManual(t *testing.T) {
273 testenv.MustHaveGoBuild(t)
275 filenames := flag.Args()
276 if len(filenames) == 0 {
277 filenames = []string{filepath.FromSlash("testdata/manual.go2")}
280 info, err := os.Stat(filenames[0])
282 t.Fatalf("TestManual: %v", err)
285 DefPredeclaredTestFuncs()
287 if len(filenames) > 1 {
288 t.Fatal("TestManual: must have only one directory argument")
290 testDir(t, filenames[0], 0, true)
292 testFiles(t, filenames, 0, true)
296 // TODO(gri) go/types has extra TestLongConstants and TestIndexRepresentability tests
298 func TestCheck(t *testing.T) { DefPredeclaredTestFuncs(); testDirFiles(t, "testdata/check", 55, false) } // TODO(gri) narrow column tolerance
299 func TestSpec(t *testing.T) { DefPredeclaredTestFuncs(); testDirFiles(t, "testdata/spec", 0, false) }
300 func TestExamples(t *testing.T) { testDirFiles(t, "testdata/examples", 0, false) }
301 func TestFixedbugs(t *testing.T) { testDirFiles(t, "testdata/fixedbugs", 0, false) }
303 func testDirFiles(t *testing.T, dir string, colDelta uint, manual bool) {
304 testenv.MustHaveGoBuild(t)
305 dir = filepath.FromSlash(dir)
307 fis, err := os.ReadDir(dir)
313 for _, fi := range fis {
314 path := filepath.Join(dir, fi.Name())
316 // If fi is a directory, its files make up a single package.
318 testDir(t, path, colDelta, manual)
320 t.Run(filepath.Base(path), func(t *testing.T) {
321 testFiles(t, []string{path}, colDelta, manual)
327 func testDir(t *testing.T, dir string, colDelta uint, manual bool) {
328 fis, err := os.ReadDir(dir)
334 var filenames []string
335 for _, fi := range fis {
336 filenames = append(filenames, filepath.Join(dir, fi.Name()))
339 t.Run(filepath.Base(dir), func(t *testing.T) {
340 testFiles(t, filenames, colDelta, manual)