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