1 // +build TODO_RSC_REMOVE_THIS
4 // Copyright 2011 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // This file implements a typechecker test harness. The packages specified
9 // in tests are typechecked. Error messages reported by the typechecker are
10 // compared against the error messages expected in the test files.
12 // Expected errors are indicated in the test files by putting a comment
13 // of the form /* ERROR "rx" */ immediately following an offending token.
14 // The harness will verify that an error matching the regular expression
15 // rx is reported at that source position. Consecutive comments may be
16 // used to indicate multiple errors for the same token position.
18 // For instance, the following test file indicates that a "not declared"
19 // error should be reported for the undeclared variable x:
23 // _ = x /* ERROR "not declared" */ + 1
26 // TODO(gri) Also collect strict mode errors of the form /* STRICT ... */
27 // and test against strict mode.
32 "cmd/compile/internal/syntax"
43 . "cmd/compile/internal/types2"
47 haltOnError = flag.Bool("halt", false, "halt on error")
48 listErrors = flag.Bool("errlist", false, "list errors")
49 testFiles = flag.String("files", "", "space-separated list of test files")
52 func parseFiles(t *testing.T, filenames []string) ([]*syntax.File, []error) {
53 var files []*syntax.File
55 errh := func(err error) { errlist = append(errlist, err) }
56 for _, filename := range filenames {
57 file, err := syntax.ParseFile(filename, errh, nil, syntax.AllowGenerics)
59 t.Fatalf("%s: %s", filename, err)
61 files = append(files, file)
66 func unpackError(err error) syntax.Error {
67 switch err := err.(type) {
71 return syntax.Error{Pos: err.Pos, Msg: err.Msg}
73 return syntax.Error{Msg: err.Error()}
77 func delta(x, y uint) uint {
88 func checkFiles(t *testing.T, sources []string, colDelta uint, trace bool) {
89 // parse files and collect parser errors
90 files, errlist := parseFiles(t, sources)
92 pkgName := "<no package>"
94 pkgName = files[0].PkgName.Value
97 if *listErrors && len(errlist) > 0 {
98 t.Errorf("--- %s:", pkgName)
99 for _, err := range errlist {
104 // typecheck and collect typechecker errors
106 conf.AcceptMethodTypeParams = true
107 conf.InferFromConstraints = true
108 // special case for importC.src
109 if len(sources) == 1 && strings.HasSuffix(sources[0], "importC.src") {
110 conf.FakeImportC = true
113 conf.Importer = defaultImporter()
114 conf.Error = func(err error) {
122 // Ignore secondary error messages starting with "\t";
123 // they are clarifying messages for a primary error.
124 if !strings.Contains(err.Error(), ": \t") {
125 errlist = append(errlist, err)
128 conf.Check(pkgName, files, nil)
134 // collect expected errors
135 errmap := make(map[string]map[uint][]syntax.Error)
136 for _, filename := range sources {
137 f, err := os.Open(filename)
142 if m := syntax.ErrorMap(f); len(m) > 0 {
148 // match against found errors
149 for _, err := range errlist {
150 got := unpackError(err)
152 // find list of errors for the respective error line
153 filename := got.Pos.Base().Filename()
154 filemap := errmap[filename]
156 var list []syntax.Error
158 line = got.Pos.Line()
163 // one of errors in list should match the current error
164 index := -1 // list index of matching message, if any
165 for i, want := range list {
166 rx, err := regexp.Compile(want.Msg)
168 t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
171 if rx.MatchString(got.Msg) {
177 t.Errorf("%s: no error expected: %q", got.Pos, got.Msg)
181 // column position must be within expected colDelta
183 if delta(got.Pos.Col(), want.Pos.Col()) > colDelta {
184 t.Errorf("%s: got col = %d; want %d", got.Pos, got.Pos.Col(), want.Pos.Col())
187 // eliminate from list
188 if n := len(list) - 1; n > 0 {
189 // not the last entry - swap in last element and shorten list by 1
190 list[index] = list[n]
191 filemap[line] = list[:n]
193 // last entry - remove list from filemap
194 delete(filemap, line)
197 // if filemap is empty, eliminate from errmap
198 if len(filemap) == 0 {
199 delete(errmap, filename)
203 // there should be no expected errors left
205 t.Errorf("--- %s: unreported errors:", pkgName)
206 for filename, filemap := range errmap {
207 for line, list := range filemap {
208 for _, err := range list {
209 t.Errorf("%s:%d:%d: %s", filename, line, err.Pos.Col(), err.Msg)
216 // TestCheck is for manual testing of selected input files, provided with -files.
217 func TestCheck(t *testing.T) {
218 if *testFiles == "" {
221 testenv.MustHaveGoBuild(t)
222 DefPredeclaredTestFuncs()
223 checkFiles(t, strings.Split(*testFiles, " "), 0, testing.Verbose())
226 func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, 75, "testdata") } // TODO(gri) narrow column tolerance
227 func TestExamples(t *testing.T) { testDir(t, 0, "examples") }
228 func TestFixedbugs(t *testing.T) { testDir(t, 0, "fixedbugs") }
230 func testDir(t *testing.T, colDelta uint, dir string) {
231 testenv.MustHaveGoBuild(t)
233 fis, err := ioutil.ReadDir(dir)
239 for count, fi := range fis {
240 path := filepath.Join(dir, fi.Name())
242 // if fi is a directory, its files make up a single package
244 if testing.Verbose() {
245 fmt.Printf("%3d %s\n", count, path)
247 fis, err := ioutil.ReadDir(path)
252 files := make([]string, len(fis))
253 for i, fi := range fis {
254 // if fi is a directory, checkFiles below will complain
255 files[i] = filepath.Join(path, fi.Name())
256 if testing.Verbose() {
257 fmt.Printf("\t%s\n", files[i])
260 checkFiles(t, files, colDelta, false)
264 // otherwise, fi is a stand-alone file
265 if testing.Verbose() {
266 fmt.Printf("%3d %s\n", count, path)
268 checkFiles(t, []string{path}, colDelta, false)