| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package syntax |
|
|
| import ( |
| "flag" |
| "fmt" |
| "internal/testenv" |
| "os" |
| "path/filepath" |
| "regexp" |
| "sort" |
| "strings" |
| "testing" |
| ) |
|
|
| const testdata = "testdata" |
|
|
| var print = flag.Bool("print", false, "only print errors") |
|
|
| |
| type position struct { |
| line, col uint |
| } |
|
|
| func (pos position) String() string { |
| return fmt.Sprintf("%d:%d", pos.line, pos.col) |
| } |
|
|
| func sortedPositions(m map[position]string) []position { |
| list := make([]position, len(m)) |
| i := 0 |
| for pos := range m { |
| list[i] = pos |
| i++ |
| } |
| sort.Slice(list, func(i, j int) bool { |
| a, b := list[i], list[j] |
| return a.line < b.line || a.line == b.line && a.col < b.col |
| }) |
| return list |
| } |
|
|
| |
| |
| |
| |
| func declaredErrors(t *testing.T, filename string) map[position]string { |
| f, err := os.Open(filename) |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer f.Close() |
|
|
| declared := make(map[position]string) |
|
|
| var s scanner |
| var pattern string |
| s.init(f, func(line, col uint, msg string) { |
| |
| switch { |
| case strings.HasPrefix(msg, "// ERROR "): |
| |
| declared[position{s.line, 0}] = strings.TrimSpace(msg[9:]) |
| case strings.HasPrefix(msg, "/* ERROR "): |
| |
| pattern = strings.TrimSpace(msg[9 : len(msg)-2]) |
| } |
| }, comments) |
|
|
| |
| for { |
| s.next() |
| if pattern != "" { |
| declared[position{s.line, s.col}] = pattern |
| pattern = "" |
| } |
| if s.tok == _EOF { |
| break |
| } |
| } |
|
|
| return declared |
| } |
|
|
| func testSyntaxErrors(t *testing.T, filename string) { |
| declared := declaredErrors(t, filename) |
| if *print { |
| fmt.Println("Declared errors:") |
| for _, pos := range sortedPositions(declared) { |
| fmt.Printf("%s:%s: %s\n", filename, pos, declared[pos]) |
| } |
|
|
| fmt.Println() |
| fmt.Println("Reported errors:") |
| } |
|
|
| f, err := os.Open(filename) |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer f.Close() |
|
|
| ParseFile(filename, func(err error) { |
| e, ok := err.(Error) |
| if !ok { |
| return |
| } |
|
|
| if *print { |
| fmt.Println(err) |
| return |
| } |
|
|
| orig := position{e.Pos.Line(), e.Pos.Col()} |
| pos := orig |
| pattern, found := declared[pos] |
| if !found { |
| |
| pos = position{e.Pos.Line(), 0} |
| pattern, found = declared[pos] |
| } |
| if found { |
| rx, err := regexp.Compile(pattern) |
| if err != nil { |
| t.Errorf("%s:%s: %v", filename, pos, err) |
| return |
| } |
| if match := rx.MatchString(e.Msg); !match { |
| t.Errorf("%s:%s: %q does not match %q", filename, pos, e.Msg, pattern) |
| return |
| } |
| |
| delete(declared, pos) |
| } else { |
| t.Errorf("%s:%s: unexpected error: %s", filename, orig, e.Msg) |
| } |
| }, nil, CheckBranches) |
|
|
| if *print { |
| fmt.Println() |
| return |
| } |
|
|
| |
| for pos, pattern := range declared { |
| t.Errorf("%s:%s: missing error: %s", filename, pos, pattern) |
| } |
| } |
|
|
| func TestSyntaxErrors(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| list, err := os.ReadDir(testdata) |
| if err != nil { |
| t.Fatal(err) |
| } |
| for _, fi := range list { |
| name := fi.Name() |
| if !fi.IsDir() && !strings.HasPrefix(name, ".") { |
| testSyntaxErrors(t, filepath.Join(testdata, name)) |
| } |
| } |
| } |
|
|