| 1 |
// Copyright 2020 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 |
package syntax |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCommentMap(t *testing.T) { |
| 15 |
const src = `/* ERROR "0:0" */ /* ERROR "0:0" */ // ERROR "0:0" |
| 16 |
// ERROR "0:0" |
| 17 |
x /* ERROR "3:1" */ // ignore automatically inserted semicolon here |
| 18 |
/* ERROR "3:1" */ // position of x on previous line |
| 19 |
x /* ERROR "5:4" */ ; // do not ignore this semicolon |
| 20 |
/* ERROR "5:24" */ // position of ; on previous line |
| 21 |
package /* ERROR "7:2" */ // indented with tab |
| 22 |
import /* ERROR "8:9" */ // indented with blanks |
| 23 |
` |
| 24 |
m := CommentMap(strings.NewReader(src), regexp.MustCompile("^ ERROR ")) |
| 25 |
found := 0 // number of errors found |
| 26 |
for line, errlist := range m { |
| 27 |
for _, err := range errlist { |
| 28 |
if err.Pos.Line() != line { |
| 29 |
t.Errorf("%v: got map line %d; want %d", err, err.Pos.Line(), line) |
| 30 |
continue |
| 31 |
} |
| 32 |
// err.Pos.Line() == line |
| 33 |
|
| 34 |
got := strings.TrimSpace(err.Msg[len(" ERROR "):]) |
| 35 |
want := fmt.Sprintf(`"%d:%d"`, line, err.Pos.Col()) |
| 36 |
if got != want { |
| 37 |
t.Errorf("%v: got msg %q; want %q", err, got, want) |
| 38 |
continue |
| 39 |
} |
| 40 |
found++ |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
want := strings.Count(src, " ERROR ") |
| 45 |
if found != want { |
| 46 |
t.Errorf("CommentMap got %d errors; want %d", found, want) |
| 47 |
} |
| 48 |
} |