-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_test.go
72 lines (64 loc) · 1.82 KB
/
eval_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 astquery_test
import (
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestEvaluator_Select(t *testing.T) {
t.Parallel()
//astquery.DebugON(t)
S := func(s ...string) []string { return s }
TD := func(f string) string { return filepath.Join("testdata", "TestEvaluator_Select", f) }
cases := map[string]struct {
path string
xpath string
want []string
}{
"single": {TD("single.go"), "/*/Decls[1]/Body/*", S("ReturnStmt")},
"multi": {TD("multi.go"), "/*/Decls[1]/Body/*", S("AssignStmt", "ReturnStmt")},
"filename": {TD("single.go"), "/a.go/Decls[1]/Body/*", S("ReturnStmt")},
"attr": {TD("attr.go"), "//*[@type='CallExpr']/Fun[@type='Ident' and @Name='print']", S("Ident", "Ident", "Ident")},
}
for n, tt := range cases {
tt := tt
t.Run(n, func(t *testing.T) {
t.Parallel()
e := newEvaluator(t, tt.path)
ns, err := e.Select(tt.xpath)
if err != nil {
t.Fatal("unexpected error:", err)
}
got := nodesType(t, ns)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Error(diff)
}
})
}
}
func TestEvaluator_Eval(t *testing.T) {
t.Parallel()
//astquery.DebugON(t)
TD := func(f string) string { return filepath.Join("testdata", "TestEvaluator_Eval", f) }
cases := map[string]struct {
path string
xpath string
want interface{}
}{
"attr": {TD("attr.go"), "//*[@type='CallExpr']/Fun[@type='Ident']/@Name", []interface{}{"print", "print", "println", "print"}},
"src": {TD("attr.go"), "//*[@src='print']/@Name", []interface{}{"print", "print", "print"}},
}
for n, tt := range cases {
tt := tt
t.Run(n, func(t *testing.T) {
t.Parallel()
e := newEvaluator(t, tt.path)
got, err := e.Eval(tt.xpath)
if err != nil {
t.Fatal("unexpected error:", err)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Error(diff)
}
})
}
}