2 // Copyright 2011 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
6 // This file implements a typechecker test harness. The packages specified
7 // in tests are typechecked. Error messages reported by the typechecker are
8 // compared against the error messages expected in the test files.
10 // Expected errors are indicated in the test files by putting a comment
11 // of the form /* ERROR "rx" */ immediately following an offending token.
12 // The harness will verify that an error matching the regular expression
13 // rx is reported at that source position. Consecutive comments may be
14 // used to indicate multiple errors for the same token position.
16 // For instance, the following test file indicates that a "not declared"
17 // error should be reported for the undeclared variable x:
21 // _ = x /* ERROR "not declared" */ + 1
24 // TODO(gri) Also collect strict mode errors of the form /* STRICT ... */
25 // and test against strict mode.
30 "cmd/compile/internal/syntax"
41 . "cmd/compile/internal/types2"
45 haltOnError = flag.Bool("halt", false, "halt on error")
46 listErrors = flag.Bool("errlist", false, "list errors")
47 testFiles = flag.String("files", "", "space-separated list of test files")
50 func parseFiles(t *testing.T, filenames []string) ([]*syntax.File, []error) {
51 var files []*syntax.File
53 errh := func(err error) { errlist = append(errlist, err) }
54 for _, filename := range filenames {
55 file, err := syntax.ParseFile(filename, errh, nil, syntax.AllowGenerics)
57 t.Fatalf("%s: %s", filename, err)
59 files = append(files, file)
64 func unpackError(err error) syntax.Error {
65 switch err := err.(type) {
69 return syntax.Error{Pos: err.Pos, Msg: err.Msg}
71 return syntax.Error{Msg: err.Error()}
75 func delta(x, y uint) uint {
86 func checkFiles(t *testing.T, sources []string, colDelta uint, trace bool) {
87 // parse files and collect parser errors
88 files, errlist := parseFiles(t, sources)
90 pkgName := "<no package>"
92 pkgName = files[0].PkgName.Value
95 if *listErrors && len(errlist) > 0 {
96 t.Errorf("--- %s:", pkgName)
97 for _, err := range errlist {
102 // typecheck and collect typechecker errors
104 conf.AcceptMethodTypeParams = true
105 conf.InferFromConstraints = true
106 // special case for importC.src
107 if len(sources) == 1 && strings.HasSuffix(sources[0], "importC.src") {
108 conf.FakeImportC = true
111 conf.Importer = defaultImporter()
112 conf.Error = func(err error) {
120 // Ignore secondary error messages starting with "\t";
121 // they are clarifying messages for a primary error.
122 if !strings.Contains(err.Error(), ": \t") {
123 errlist = append(errlist, err)
126 conf.Check(pkgName, files, nil)
132 // collect expected errors
133 errmap := make(map[string]map[uint][]syntax.Error)
134 for _, filename := range sources {
135 f, err := os.Open(filename)
140 if m := syntax.ErrorMap(f); len(m) > 0 {
146 // match against found errors
147 for _, err := range errlist {
148 got := unpackError(err)
150 // find list of errors for the respective error line
151 filename := got.Pos.Base().Filename()
152 filemap := errmap[filename]
154 var list []syntax.Error
156 line = got.Pos.Line()
161 // one of errors in list should match the current error
162 index := -1 // list index of matching message, if any
163 for i, want := range list {
164 rx, err := regexp.Compile(want.Msg)
166 t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
169 if rx.MatchString(got.Msg) {
175 t.Errorf("%s: no error expected: %q", got.Pos, got.Msg)
179 // column position must be within expected colDelta
181 if delta(got.Pos.Col(), want.Pos.Col()) > colDelta {
182 t.Errorf("%s: got col = %d; want %d", got.Pos, got.Pos.Col(), want.Pos.Col())
185 // eliminate from list
186 if n := len(list) - 1; n > 0 {
187 // not the last entry - swap in last element and shorten list by 1
188 list[index] = list[n]
189 filemap[line] = list[:n]
191 // last entry - remove list from filemap
192 delete(filemap, line)
195 // if filemap is empty, eliminate from errmap
196 if len(filemap) == 0 {
197 delete(errmap, filename)
201 // there should be no expected errors left
203 t.Errorf("--- %s: unreported errors:", pkgName)
204 for filename, filemap := range errmap {
205 for line, list := range filemap {
206 for _, err := range list {
207 t.Errorf("%s:%d:%d: %s", filename, line, err.Pos.Col(), err.Msg)
214 // TestCheck is for manual testing of selected input files, provided with -files.
215 func TestCheck(t *testing.T) {
216 if *testFiles == "" {
219 testenv.MustHaveGoBuild(t)
220 DefPredeclaredTestFuncs()
221 checkFiles(t, strings.Split(*testFiles, " "), 0, testing.Verbose())
224 func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, 75, "testdata") } // TODO(gri) narrow column tolerance
225 func TestExamples(t *testing.T) { testDir(t, 0, "examples") }
226 func TestFixedbugs(t *testing.T) { testDir(t, 0, "fixedbugs") }
228 func testDir(t *testing.T, colDelta uint, dir string) {
229 testenv.MustHaveGoBuild(t)
231 fis, err := ioutil.ReadDir(dir)
237 for count, fi := range fis {
238 path := filepath.Join(dir, fi.Name())
240 // if fi is a directory, its files make up a single package
242 if testing.Verbose() {
243 fmt.Printf("%3d %s\n", count, path)
245 fis, err := ioutil.ReadDir(path)
250 files := make([]string, len(fis))
251 for i, fi := range fis {
252 // if fi is a directory, checkFiles below will complain
253 files[i] = filepath.Join(path, fi.Name())
254 if testing.Verbose() {
255 fmt.Printf("\t%s\n", files[i])
258 checkFiles(t, files, colDelta, false)
262 // otherwise, fi is a stand-alone file
263 if testing.Verbose() {
264 fmt.Printf("%3d %s\n", count, path)
266 checkFiles(t, []string{path}, colDelta, false)