-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast_test.go
156 lines (139 loc) · 4.32 KB
/
ast_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package astquery
import (
"go/ast"
"go/build"
"go/parser"
"go/token"
"reflect"
"regexp"
"testing"
)
type nodeInfo struct {
Name string
Type reflect.Type
}
func TestSetFilter(t *testing.T) {
servicePkg := getTestPkg(t)
serviceTypes := Find([]ast.Node{servicePkg}, SetFilter{
Names: []string{"ServiceOne", "ServiceTwo"},
Type: reflect.TypeOf((*ast.TypeSpec)(nil)),
})
expServiceTypes := []nodeInfo{
{Name: "ServiceOne", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
{Name: "ServiceTwo", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
}
checkNodesExpected(t, expServiceTypes, serviceTypes)
}
func TestRegexpFilter(t *testing.T) {
servicePkg := getTestPkg(t)
serviceTypes := Find([]ast.Node{servicePkg}, RegexpFilter{
Pattern: regexp.MustCompile(`^Service[A-Za-z]*$`),
Type: reflect.TypeOf((*ast.TypeSpec)(nil)),
})
expServiceTypes := []nodeInfo{
{Name: "ServiceOne", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
{Name: "ServiceTwo", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
}
checkNodesExpected(t, expServiceTypes, serviceTypes)
}
func TestNestedFilters(t *testing.T) {
servicePkg := getTestPkg(t)
type expMethodInfo struct {
method nodeInfo
calls []nodeInfo
}
type expServiceInfo struct {
service nodeInfo
methods []expMethodInfo
}
testcases := []expServiceInfo{{
service: nodeInfo{Name: "ServiceOne", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
methods: []expMethodInfo{{
method: nodeInfo{Name: "Get", Type: reflect.TypeOf((*ast.FuncDecl)(nil))},
calls: []nodeInfo{{Name: "Check", Type: reflect.TypeOf((*ast.SelectorExpr)(nil))}},
}, {
method: nodeInfo{Name: "List", Type: reflect.TypeOf((*ast.FuncDecl)(nil))},
calls: []nodeInfo{{Name: "Check", Type: reflect.TypeOf((*ast.SelectorExpr)(nil))}},
}},
}, {
service: nodeInfo{Name: "ServiceTwo", Type: reflect.TypeOf((*ast.TypeSpec)(nil))},
methods: []expMethodInfo{{
method: nodeInfo{Name: "Get", Type: reflect.TypeOf((*ast.FuncDecl)(nil))},
calls: []nodeInfo{{Name: "Check", Type: reflect.TypeOf((*ast.SelectorExpr)(nil))}},
}, {
method: nodeInfo{Name: "List", Type: reflect.TypeOf((*ast.FuncDecl)(nil))},
calls: []nodeInfo{{Name: "Check", Type: reflect.TypeOf((*ast.SelectorExpr)(nil))}},
}, {
method: nodeInfo{Name: "UncheckedMeth", Type: reflect.TypeOf((*ast.FuncDecl)(nil))},
calls: []nodeInfo{},
}},
}}
for _, test := range testcases {
service_ := Find([]ast.Node{servicePkg},
SetFilter{Names: []string{test.service.Name}, Type: reflect.TypeOf((*ast.TypeSpec)(nil))})
if len(service_) != 1 {
t.Fatalf("expected to get 1 AST node back, but got %d: %v", len(service_), service_)
}
service := service_[0]
serviceName, _ := GetName(service)
expMethods := make([]nodeInfo, len(test.methods))
for i, m := range test.methods {
expMethods[i] = m.method
}
actMethods := Find([]ast.Node{servicePkg}, MethodFilter{
ReceiverType: serviceName,
ExportedOnly: true,
})
checkNodesExpected(t, expMethods, actMethods)
for _, method := range actMethods {
methodInfo := nodeInfoFromNode(method)
var expCalls []nodeInfo
for _, expMethodInfo := range test.methods {
if expMethodInfo.method == methodInfo {
expCalls = expMethodInfo.calls
}
}
calls := Find([]ast.Node{method}, RegexpFilter{Pattern: regexp.MustCompile(`.*`), Type: reflect.TypeOf((*ast.SelectorExpr)(nil))})
checkNodesExpected(t, expCalls, calls)
}
}
}
//
// Helpers
//
func nodeInfoFromNode(node ast.Node) nodeInfo {
var info nodeInfo
if name, nameExists := GetName(node); nameExists {
info.Name = name
}
info.Type = reflect.TypeOf(node)
return info
}
func checkNodesExpected(t *testing.T, exp []nodeInfo, actual []ast.Node) {
exp_ := make(map[nodeInfo]bool)
for _, e := range exp {
exp_[e] = true
}
actual_ := make(map[nodeInfo]bool)
for _, node := range actual {
actual_[nodeInfoFromNode(node)] = true
}
if !reflect.DeepEqual(exp_, actual_) {
t.Errorf("expected nodes %+v, but got %+v", exp_, actual_)
}
}
func getTestPkg(t *testing.T) *ast.Package {
pkg, err := build.Import("github.com/beyang/go-astquery/testpkg", "", build.FindOnly)
if err != nil {
t.Fatal(err)
}
pkgs, err := parser.ParseDir(token.NewFileSet(), pkg.Dir, nil, parser.AllErrors)
if err != nil {
t.Fatal(err)
}
servicePkg, in := pkgs["service"]
if !in {
t.Fatal("service package not found")
}
return servicePkg
}