github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/go/types/check_test.go (about) 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/init0.src"}, 65 {"testdata/init1.src"}, 66 {"testdata/init2.src"}, 67 {"testdata/decls0.src"}, 68 {"testdata/decls1.src"}, 69 {"testdata/decls2a.src", "testdata/decls2b.src"}, 70 {"testdata/decls3.src"}, 71 {"testdata/decls4.src"}, 72 {"testdata/decls5.src"}, 73 {"testdata/const0.src"}, 74 {"testdata/const1.src"}, 75 {"testdata/constdecl.src"}, 76 {"testdata/vardecl.src"}, 77 {"testdata/expr0.src"}, 78 {"testdata/expr1.src"}, 79 {"testdata/expr2.src"}, 80 {"testdata/expr3.src"}, 81 {"testdata/methodsets.src"}, 82 {"testdata/shifts.src"}, 83 {"testdata/builtins.src"}, 84 {"testdata/conversions.src"}, 85 {"testdata/conversions2.src"}, 86 {"testdata/stmt0.src"}, 87 {"testdata/stmt1.src"}, 88 {"testdata/gotos.src"}, 89 {"testdata/labels.src"}, 90 {"testdata/issues.src"}, 91 {"testdata/blank.src"}, 92 } 93 94 var fset = token.NewFileSet() 95 96 // Positioned errors are of the form filename:line:column: message . 97 var posMsgRx = regexp.MustCompile(`^(.*:[0-9]+:[0-9]+): *(.*)`) 98 99 // splitError splits an error's error message into a position string 100 // and the actual error message. If there's no position information, 101 // pos is the empty string, and msg is the entire error message. 102 // 103 func splitError(err error) (pos, msg string) { 104 msg = err.Error() 105 if m := posMsgRx.FindStringSubmatch(msg); len(m) == 3 { 106 pos = m[1] 107 msg = m[2] 108 } 109 return 110 } 111 112 func parseFiles(t *testing.T, filenames []string) ([]*ast.File, []error) { 113 var files []*ast.File 114 var errlist []error 115 for _, filename := range filenames { 116 file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors) 117 if file == nil { 118 t.Fatalf("%s: %s", filename, err) 119 } 120 files = append(files, file) 121 if err != nil { 122 if list, _ := err.(scanner.ErrorList); len(list) > 0 { 123 for _, err := range list { 124 errlist = append(errlist, err) 125 } 126 } else { 127 errlist = append(errlist, err) 128 } 129 } 130 } 131 return files, errlist 132 } 133 134 // ERROR comments must start with text `ERROR "rx"` or `ERROR rx` where 135 // rx is a regular expression that matches the expected error message. 136 // Space around "rx" or rx is ignored. Use the form `ERROR HERE "rx"` 137 // for error messages that are located immediately after rather than 138 // at a token's position. 139 // 140 var errRx = regexp.MustCompile(`^ *ERROR *(HERE)? *"?([^"]*)"?`) 141 142 // errMap collects the regular expressions of ERROR comments found 143 // in files and returns them as a map of error positions to error messages. 144 // 145 func errMap(t *testing.T, testname string, files []*ast.File) map[string][]string { 146 // map of position strings to lists of error message patterns 147 errmap := make(map[string][]string) 148 149 for _, file := range files { 150 filename := fset.Position(file.Package).Filename 151 src, err := ioutil.ReadFile(filename) 152 if err != nil { 153 t.Fatalf("%s: could not read %s", testname, filename) 154 } 155 156 var s scanner.Scanner 157 s.Init(fset.AddFile(filename, -1, len(src)), src, nil, scanner.ScanComments) 158 var prev token.Pos // position of last non-comment, non-semicolon token 159 var here token.Pos // position immediately after the token at position prev 160 161 scanFile: 162 for { 163 pos, tok, lit := s.Scan() 164 switch tok { 165 case token.EOF: 166 break scanFile 167 case token.COMMENT: 168 if lit[1] == '*' { 169 lit = lit[:len(lit)-2] // strip trailing */ 170 } 171 if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 3 { 172 pos := prev 173 if s[1] == "HERE" { 174 pos = here 175 } 176 p := fset.Position(pos).String() 177 errmap[p] = append(errmap[p], strings.TrimSpace(s[2])) 178 } 179 case token.SEMICOLON: 180 // ignore automatically inserted semicolon 181 if lit == "\n" { 182 continue scanFile 183 } 184 fallthrough 185 default: 186 prev = pos 187 var l int // token length 188 if tok.IsLiteral() { 189 l = len(lit) 190 } else { 191 l = len(tok.String()) 192 } 193 here = prev + token.Pos(l) 194 } 195 } 196 } 197 198 return errmap 199 } 200 201 func eliminate(t *testing.T, errmap map[string][]string, errlist []error) { 202 for _, err := range errlist { 203 pos, gotMsg := splitError(err) 204 list := errmap[pos] 205 index := -1 // list index of matching message, if any 206 // we expect one of the messages in list to match the error at pos 207 for i, wantRx := range list { 208 rx, err := regexp.Compile(wantRx) 209 if err != nil { 210 t.Errorf("%s: %v", pos, err) 211 continue 212 } 213 if rx.MatchString(gotMsg) { 214 index = i 215 break 216 } 217 } 218 if index >= 0 { 219 // eliminate from list 220 if n := len(list) - 1; n > 0 { 221 // not the last entry - swap in last element and shorten list by 1 222 list[index] = list[n] 223 errmap[pos] = list[:n] 224 } else { 225 // last entry - remove list from map 226 delete(errmap, pos) 227 } 228 } else { 229 t.Errorf("%s: no error expected: %q", pos, gotMsg) 230 } 231 } 232 } 233 234 func checkFiles(t *testing.T, testfiles []string) { 235 // parse files and collect parser errors 236 files, errlist := parseFiles(t, testfiles) 237 238 pkgName := "<no package>" 239 if len(files) > 0 { 240 pkgName = files[0].Name.Name 241 } 242 243 if *listErrors && len(errlist) > 0 { 244 t.Errorf("--- %s:", pkgName) 245 for _, err := range errlist { 246 t.Error(err) 247 } 248 } 249 250 // typecheck and collect typechecker errors 251 var conf Config 252 // special case for importC.src 253 if len(testfiles) == 1 && testfiles[0] == "testdata/importC.src" { 254 conf.FakeImportC = true 255 } 256 conf.Importer = importer.Default() 257 conf.Error = func(err error) { 258 if *listErrors { 259 t.Error(err) 260 return 261 } 262 // Ignore secondary error messages starting with "\t"; 263 // they are clarifying messages for a primary error. 264 if !strings.Contains(err.Error(), ": \t") { 265 errlist = append(errlist, err) 266 } 267 } 268 conf.Check(pkgName, fset, files, nil) 269 270 if *listErrors { 271 return 272 } 273 274 // match and eliminate errors; 275 // we are expecting the following errors 276 errmap := errMap(t, pkgName, files) 277 eliminate(t, errmap, errlist) 278 279 // there should be no expected errors left 280 if len(errmap) > 0 { 281 t.Errorf("--- %s: %d source positions with expected (but not reported) errors:", pkgName, len(errmap)) 282 for pos, list := range errmap { 283 for _, rx := range list { 284 t.Errorf("%s: %q", pos, rx) 285 } 286 } 287 } 288 } 289 290 func TestCheck(t *testing.T) { 291 testenv.MustHaveGoBuild(t) 292 293 // Declare builtins for testing. 294 DefPredeclaredTestFuncs() 295 296 // If explicit test files are specified, only check those. 297 if files := *testFiles; files != "" { 298 checkFiles(t, strings.Split(files, " ")) 299 return 300 } 301 302 // Otherwise, run all the tests. 303 for _, files := range tests { 304 checkFiles(t, files) 305 } 306 }