-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper_test.go
72 lines (59 loc) · 1.33 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package comment_test
import (
"bytes"
"go/ast"
"go/parser"
"go/token"
"testing"
"golang.org/x/tools/txtar"
"github.com/gostaticanalysis/comment"
)
func parse(t *testing.T, fset *token.FileSet, path string) []*ast.File {
t.Helper()
ar, err := txtar.ParseFile(path)
if err != nil {
t.Fatal("unexpected error:", err)
}
files := make([]*ast.File, len(ar.Files))
for i := range ar.Files {
n, d := ar.Files[i].Name, ar.Files[i].Data
f, err := parser.ParseFile(fset, n, d, parser.ParseComments)
if err != nil {
t.Fatal("unexpected error:", err)
}
files[i] = f
}
return files
}
func maps(t *testing.T, fset *token.FileSet, path string) comment.Maps {
t.Helper()
files := parse(t, fset, path)
return comment.New(fset, files)
}
// pos find position of `_` in source codes as a token.Pos.
func pos(t *testing.T, fset *token.FileSet, path string) token.Pos {
t.Helper()
ar, err := txtar.ParseFile(path)
if err != nil {
t.Fatal("unexpected error:", err)
}
for i := range ar.Files {
n, d := ar.Files[i].Name, ar.Files[i].Data
index := bytes.Index(d, []byte("_"))
if index == -1 {
continue
}
var pos token.Pos
fset.Iterate(func(f *token.File) bool {
if n == f.Name() {
pos = f.Pos(index)
return false
}
return true
})
if pos != token.NoPos {
return pos
}
}
return token.NoPos
}