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. 4 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. 9// 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. 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 23package parser 24 25import ( 26 "go/scanner" 27 "go/token" 28 "os" 29 "path/filepath" 30 "regexp" 31 "strings" 32 "testing" 33) 34 35const testdata = "testdata" 36 37// getFile assumes that each filename occurs at most once 38func getFile(fset *token.FileSet, filename string) (file *token.File) { 39 fset.Iterate(func(f *token.File) bool { 40 if f.Name() == filename { 41 if file != nil { 42 panic(filename + " used multiple times") 43 } 44 file = f 45 } 46 return true 47 }) 48 return file 49} 50 51func getPos(fset *token.FileSet, filename string, offset int) token.Pos { 52 if f := getFile(fset, filename); f != nil { 53 return f.Pos(offset) 54 } 55 return token.NoPos 56} 57 58// ERROR comments must be of the form /* ERROR "rx" */ and rx is 59// a regular expression that matches the expected error message. 60// The special form /* ERROR HERE "rx" */ must be used for error 61// messages that appear immediately after a token, rather than at 62// a token's position. 63// 64var errRx = regexp.MustCompile(`^/\* *ERROR *(HERE)? *"([^"]*)" *\*/$`) 65 66// expectedErrors collects the regular expressions of ERROR comments found 67// in files and returns them as a map of error positions to error messages. 68// 69func expectedErrors(fset *token.FileSet, filename string, src []byte) map[token.Pos]string { 70 errors := make(map[token.Pos]string) 71 72 var s scanner.Scanner 73 // file was parsed already - do not add it again to the file 74 // set otherwise the position information returned here will 75 // not match the position information collected by the parser 76 s.Init(getFile(fset, filename), src, nil, scanner.ScanComments) 77 var prev token.Pos // position of last non-comment, non-semicolon token 78 var here token.Pos // position immediately after the token at position prev 79 80 for { 81 pos, tok, lit := s.Scan() 82 switch tok { 83 case token.EOF: 84 return errors 85 case token.COMMENT: 86 s := errRx.FindStringSubmatch(lit) 87 if len(s) == 3 { 88 pos := prev 89 if s[1] == "HERE" { 90 pos = here 91 } 92 errors[pos] = string(s[2]) 93 } 94 case token.SEMICOLON: 95 // don't use the position of auto-inserted (invisible) semicolons 96 if lit != ";" { 97 break 98 } 99 fallthrough 100 default: 101 prev = pos 102 var l int // token length 103 if tok.IsLiteral() { 104 l = len(lit) 105 } else { 106 l = len(tok.String()) 107 } 108 here = prev + token.Pos(l) 109 } 110 } 111} 112 113// compareErrors compares the map of expected error messages with the list 114// of found errors and reports discrepancies. 115// 116func compareErrors(t *testing.T, fset *token.FileSet, expected map[token.Pos]string, found scanner.ErrorList) { 117 for _, error := range found { 118 // error.Pos is a token.Position, but we want 119 // a token.Pos so we can do a map lookup 120 pos := getPos(fset, error.Pos.Filename, error.Pos.Offset) 121 if msg, found := expected[pos]; found { 122 // we expect a message at pos; check if it matches 123 rx, err := regexp.Compile(msg) 124 if err != nil { 125 t.Errorf("%s: %v", error.Pos, err) 126 continue 127 } 128 if match := rx.MatchString(error.Msg); !match { 129 t.Errorf("%s: %q does not match %q", error.Pos, error.Msg, msg) 130 continue 131 } 132 // we have a match - eliminate this error 133 delete(expected, pos) 134 } else { 135 // To keep in mind when analyzing failed test output: 136 // If the same error position occurs multiple times in errors, 137 // this message will be triggered (because the first error at 138 // the position removes this position from the expected errors). 139 t.Errorf("%s: unexpected error: %s", error.Pos, error.Msg) 140 } 141 } 142 143 // there should be no expected errors left 144 if len(expected) > 0 { 145 t.Errorf("%d errors not reported:", len(expected)) 146 for pos, msg := range expected { 147 t.Errorf("%s: %s\n", fset.Position(pos), msg) 148 } 149 } 150} 151 152func checkErrors(t *testing.T, filename string, input interface{}) { 153 src, err := readSource(filename, input) 154 if err != nil { 155 t.Error(err) 156 return 157 } 158 159 fset := token.NewFileSet() 160 _, err = ParseFile(fset, filename, src, DeclarationErrors|AllErrors) 161 found, ok := err.(scanner.ErrorList) 162 if err != nil && !ok { 163 t.Error(err) 164 return 165 } 166 found.RemoveMultiples() 167 168 // we are expecting the following errors 169 // (collect these after parsing a file so that it is found in the file set) 170 expected := expectedErrors(fset, filename, src) 171 172 // verify errors returned by the parser 173 compareErrors(t, fset, expected, found) 174} 175 176func TestErrors(t *testing.T) { 177 list, err := os.ReadDir(testdata) 178 if err != nil { 179 t.Fatal(err) 180 } 181 for _, d := range list { 182 name := d.Name() 183 if !d.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".src") { 184 checkErrors(t, filepath.Join(testdata, name), nil) 185 } 186 } 187} 188