1 // Copyright 2012 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 parser test harness. The files in the testdata
6 // directory are parsed and the errors reported are compared against the
7 // error messages expected in the test files. The test files must end in
8 // .src rather than .go so that they are not disturbed by gofmt runs.
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.
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
35 const testdata = "testdata"
37 var fsetErrs *token.FileSet
39 // getFile assumes that each filename occurs at most once
40 func getFile(filename string) (file *token.File) {
41 fsetErrs.Iterate(func(f *token.File) bool {
42 if f.Name() == filename {
44 panic(filename + " used multiple times")
53 func getPos(filename string, offset int) token.Pos {
54 if f := getFile(filename); f != nil {
60 // ERROR comments must be of the form /* ERROR "rx" */ and rx is
61 // a regular expression that matches the expected error message.
63 var errRx = regexp.MustCompile(`^/\* *ERROR *"([^"]*)" *\*/$`)
65 // expectedErrors collects the regular expressions of ERROR comments found
66 // in files and returns them as a map of error positions to error messages.
68 func expectedErrors(t *testing.T, filename string, src []byte) map[token.Pos]string {
69 errors := make(map[token.Pos]string)
72 // file was parsed already - do not add it again to the file
73 // set otherwise the position information returned here will
74 // not match the position information collected by the parser
75 s.Init(getFile(filename), src, nil, scanner.ScanComments)
76 var prev token.Pos // position of last non-comment, non-semicolon token
79 pos, tok, lit := s.Scan()
84 s := errRx.FindStringSubmatch(lit)
86 errors[prev] = string(s[1])
96 // compareErrors compares the map of expected error messages with the list
97 // of found errors and reports discrepancies.
99 func compareErrors(t *testing.T, expected map[token.Pos]string, found scanner.ErrorList) {
100 for _, error := range found {
101 // error.Pos is a token.Position, but we want
102 // a token.Pos so we can do a map lookup
103 pos := getPos(error.Pos.Filename, error.Pos.Offset)
104 if msg, found := expected[pos]; found {
105 // we expect a message at pos; check if it matches
106 rx, err := regexp.Compile(msg)
108 t.Errorf("%s: %v", error.Pos, err)
111 if match := rx.MatchString(error.Msg); !match {
112 t.Errorf("%s: %q does not match %q", error.Pos, error.Msg, msg)
115 // we have a match - eliminate this error
116 delete(expected, pos)
118 // To keep in mind when analyzing failed test output:
119 // If the same error position occurs multiple times in errors,
120 // this message will be triggered (because the first error at
121 // the position removes this position from the expected errors).
122 t.Errorf("%s: unexpected error: %s", error.Pos, error.Msg)
126 // there should be no expected errors left
127 if len(expected) > 0 {
128 t.Errorf("%d errors not reported:", len(expected))
129 for pos, msg := range expected {
130 t.Errorf("%s: %s\n", fsetErrs.Position(pos), msg)
135 func checkErrors(t *testing.T, filename string, input interface{}) {
136 src, err := readSource(filename, input)
142 _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors)
143 found, ok := err.(scanner.ErrorList)
144 if err != nil && !ok {
149 // we are expecting the following errors
150 // (collect these after parsing a file so that it is found in the file set)
151 expected := expectedErrors(t, filename, src)
153 // verify errors returned by the parser
154 compareErrors(t, expected, found)
157 func TestErrors(t *testing.T) {
158 fsetErrs = token.NewFileSet()
159 list, err := ioutil.ReadDir(testdata)
163 for _, fi := range list {
165 if !fi.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") {
166 checkErrors(t, filepath.Join(testdata, name), nil)