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. 4 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. 8 // 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. 14 // 15 // For instance, the following test file indicates that a "not declared" 16 // error should be reported for the undeclared variable x: 17 // 18 // package p 19 // func f() { 20 // _ = x /* ERROR "not declared" */ + 1 21 // } 22 23 // TODO(gri) Also collect strict mode errors of the form /* STRICT ... */ 24 // and test against strict mode. 25 26 package types_test 27 28 import ( 29 "flag" 30 "go/ast" 31 "go/importer" 32 "go/parser" 33 "go/scanner" 34 "go/token" 35 "internal/testenv" 36 "io/ioutil" 37 "regexp" 38 "strings" 39 "testing" 40 41 . "go/types" 42 ) 43 44 var ( 45 listErrors = flag.Bool("errlist", false, "list errors") 46 testFiles = flag.String("files", "", "space-separated list of test files") 47 ) 48 49 // The test filenames do not end in .go so that they are invisible 50 // to gofmt since they contain comments that must not change their 51 // positions relative to surrounding tokens. 52 53 // Each tests entry is list of files belonging to the same package. 54 var tests = [][]string{ 55 {"testdata/errors.src"}, 56 {"testdata/importdecl0a.src", "testdata/importdecl0b.src"}, 57 {"testdata/importdecl1a.src", "testdata/importdecl1b.src"}, 58 {"testdata/importC.src"}, // special handling in checkFiles 59 {"testdata/cycles.src"}, 60 {"testdata/cycles1.src"}, 61 {"testdata/cycles2.src"}, 62 {"testdata/cycles3.src"}, 63 {"testdata/cycles4.src"}, 64 {"testdata/cycles5.src"}, 65 {"testdata/init0.src"}, 66 {"testdata/init1.src"}, 67 {"testdata/init2.src"}, 68 {"testdata/decls0.src"}, 69 {"testdata/decls1.src"}, 70 {"testdata/decls2a.src", "testdata/decls2b.src"}, 71 {"testdata/decls3.src"}, 72 {"testdata/decls4.src"}, 73 {"testdata/decls5.src"}, 74 {"testdata/const0.src"}, 75 {"testdata/const1.src"}, 76 {"testdata/constdecl.src"}, 77 {"testdata/vardecl.src"}, 78 {"testdata/expr0.src"}, 79 {"testdata/expr1.src"}, 80 {"testdata/expr2.src"}, 81 {"testdata/expr3.src"}, 82 {"testdata/methodsets.src"}, 83 {"testdata/shifts.src"}, 84 {"testdata/builtins.src"}, 85 {"testdata/conversions.src"}, 86 {"testdata/conversions2.src"}, 87 {"testdata/stmt0.src"}, 88 {"testdata/stmt1.src"}, 89 {"testdata/gotos.src"}, 90 {"testdata/labels.src"}, 91 {"testdata/literals.src"}, 92 {"testdata/issues.src"}, 93 {"testdata/blank.src"}, 94 {"testdata/issue25008b.src", "testdata/issue25008a.src"}, // order (b before a) is crucial! 95 {"testdata/issue26390.src"}, // stand-alone test to ensure case is triggered 96 {"testdata/issue23203a.src"}, 97 {"testdata/issue23203b.src"}, 98 {"testdata/issue28251.src"}, 99 } 100 101 var fset = token.NewFileSet() 102 103 // Positioned errors are of the form filename:line:column: message . 104 var posMsgRx = regexp.MustCompile(`^(.*:[0-9]+:[0-9]+): *(.*)`) 105 106 // splitError splits an error's error message into a position string 107 // and the actual error message. If there's no position information, 108 // pos is the empty string, and msg is the entire error message. 109 // 110 func splitError(err error) (pos, msg string) { 111 msg = err.Error() 112 if m := posMsgRx.FindStringSubmatch(msg); len(m) == 3 { 113 pos = m[1] 114 msg = m[2] 115 } 116 return 117 } 118 119 func parseFiles(t *testing.T, filenames []string) ([]*ast.File, []error) { 120 var files []*ast.File 121 var errlist []error 122 for _, filename := range filenames { 123 file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors) 124 if file == nil { 125 t.Fatalf("%s: %s", filename, err) 126 } 127 files = append(files, file) 128 if err != nil { 129 if list, _ := err.(scanner.ErrorList); len(list) > 0 { 130 for _, err := range list { 131 errlist = append(errlist, err) 132 } 133 } else { 134 errlist = append(errlist, err) 135 } 136 } 137 } 138 return files, errlist 139 } 140 141 // ERROR comments must start with text `ERROR "rx"` or `ERROR rx` where 142 // rx is a regular expression that matches the expected error message. 143 // Space around "rx" or rx is ignored. Use the form `ERROR HERE "rx"` 144 // for error messages that are located immediately after rather than 145 // at a token's position. 146 // 147 var errRx = regexp.MustCompile(`^ *ERROR *(HERE)? *"?([^"]*)"?`) 148 149 // errMap collects the regular expressions of ERROR comments found 150 // in files and returns them as a map of error positions to error messages. 151 // 152 func errMap(t *testing.T, testname string, files []*ast.File) map[string][]string { 153 // map of position strings to lists of error message patterns 154 errmap := make(map[string][]string) 155 156 for _, file := range files { 157 filename := fset.Position(file.Package).Filename 158 src, err := ioutil.ReadFile(filename) 159 if err != nil { 160 t.Fatalf("%s: could not read %s", testname, filename) 161 } 162 163 var s scanner.Scanner 164 s.Init(fset.AddFile(filename, -1, len(src)), src, nil, scanner.ScanComments) 165 var prev token.Pos // position of last non-comment, non-semicolon token 166 var here token.Pos // position immediately after the token at position prev 167 168 scanFile: 169 for { 170 pos, tok, lit := s.Scan() 171 switch tok { 172 case token.EOF: 173 break scanFile 174 case token.COMMENT: 175 if lit[1] == '*' { 176 lit = lit[:len(lit)-2] // strip trailing */ 177 } 178 if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 3 { 179 pos := prev 180 if s[1] == "HERE" { 181 pos = here 182 } 183 p := fset.Position(pos).String() 184 errmap[p] = append(errmap[p], strings.TrimSpace(s[2])) 185 } 186 case token.SEMICOLON: 187 // ignore automatically inserted semicolon 188 if lit == "\n" { 189 continue scanFile 190 } 191 fallthrough 192 default: 193 prev = pos 194 var l int // token length 195 if tok.IsLiteral() { 196 l = len(lit) 197 } else { 198 l = len(tok.String()) 199 } 200 here = prev + token.Pos(l) 201 } 202 } 203 } 204 205 return errmap 206 } 207 208 func eliminate(t *testing.T, errmap map[string][]string, errlist []error) { 209 for _, err := range errlist { 210 pos, gotMsg := splitError(err) 211 list := errmap[pos] 212 index := -1 // list index of matching message, if any 213 // we expect one of the messages in list to match the error at pos 214 for i, wantRx := range list { 215 rx, err := regexp.Compile(wantRx) 216 if err != nil { 217 t.Errorf("%s: %v", pos, err) 218 continue 219 } 220 if rx.MatchString(gotMsg) { 221 index = i 222 break 223 } 224 } 225 if index >= 0 { 226 // eliminate from list 227 if n := len(list) - 1; n > 0 { 228 // not the last entry - swap in last element and shorten list by 1 229 list[index] = list[n] 230 errmap[pos] = list[:n] 231 } else { 232 // last entry - remove list from map 233 delete(errmap, pos) 234 } 235 } else { 236 t.Errorf("%s: no error expected: %q", pos, gotMsg) 237 } 238 } 239 } 240 241 func checkFiles(t *testing.T, testfiles []string) { 242 // parse files and collect parser errors 243 files, errlist := parseFiles(t, testfiles) 244 245 pkgName := "<no package>" 246 if len(files) > 0 { 247 pkgName = files[0].Name.Name 248 } 249 250 if *listErrors && len(errlist) > 0 { 251 t.Errorf("--- %s:", pkgName) 252 for _, err := range errlist { 253 t.Error(err) 254 } 255 } 256 257 // typecheck and collect typechecker errors 258 var conf Config 259 // special case for importC.src 260 if len(testfiles) == 1 && testfiles[0] == "testdata/importC.src" { 261 conf.FakeImportC = true 262 } 263 conf.Importer = importer.Default() 264 conf.Error = func(err error) { 265 if *listErrors { 266 t.Error(err) 267 return 268 } 269 // Ignore secondary error messages starting with "\t"; 270 // they are clarifying messages for a primary error. 271 if !strings.Contains(err.Error(), ": \t") { 272 errlist = append(errlist, err) 273 } 274 } 275 conf.Check(pkgName, fset, files, nil) 276 277 if *listErrors { 278 return 279 } 280 281 // match and eliminate errors; 282 // we are expecting the following errors 283 errmap := errMap(t, pkgName, files) 284 eliminate(t, errmap, errlist) 285 286 // there should be no expected errors left 287 if len(errmap) > 0 { 288 t.Errorf("--- %s: %d source positions with expected (but not reported) errors:", pkgName, len(errmap)) 289 for pos, list := range errmap { 290 for _, rx := range list { 291 t.Errorf("%s: %q", pos, rx) 292 } 293 } 294 } 295 } 296 297 func TestCheck(t *testing.T) { 298 testenv.MustHaveGoBuild(t) 299 300 // Declare builtins for testing. 301 DefPredeclaredTestFuncs() 302 303 // If explicit test files are specified, only check those. 304 if files := *testFiles; files != "" { 305 checkFiles(t, strings.Split(files, " ")) 306 return 307 } 308 309 // Otherwise, run all the tests. 310 for _, files := range tests { 311 checkFiles(t, files) 312 } 313 } 314
View as plain text