-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_test.go
More file actions
71 lines (60 loc) · 1.46 KB
/
helper_test.go
File metadata and controls
71 lines (60 loc) · 1.46 KB
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
package astquery_test
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
"testing"
"github.com/gostaticanalysis/astquery"
"golang.org/x/tools/txtar"
)
func nodeType(t *testing.T, n ast.Node) string {
return strings.TrimPrefix(fmt.Sprintf("%T", n), "*ast.")
}
func nodesType(t *testing.T, ns []ast.Node) []string {
t.Helper()
if ns == nil {
return nil
}
s := make([]string, len(ns))
for i := range s {
s[i] = nodeType(t, ns[i])
}
return s
}
func newInspector(t *testing.T, stmtStr string) (ast.Stmt, *astquery.Inspector) {
t.Helper()
src := fmt.Sprintf("package a\nfunc f() { _ = 10; %s }", stmtStr)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "a.go", src, 0)
if err != nil {
t.Fatal("unexpected error:", err)
}
stmt := f.Decls[0].(*ast.FuncDecl).Body.List[1]
in := astquery.NewInspector([]*ast.File{f})
return stmt, in
}
func newEvaluator(t *testing.T, path string) *astquery.Evaluator {
t.Helper()
fset := token.NewFileSet()
files := parse(t, fset, path)
return astquery.New(fset, files, nil)
}
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, 0)
if err != nil {
t.Fatal("unexpected error:", err)
}
files[i] = f
}
return files
}