-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinspect_test.go
200 lines (161 loc) · 4.98 KB
/
inspect_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package inspect
import (
"bytes"
"go/ast"
"go/parser"
"go/token"
"log"
"testing"
)
var fset = token.NewFileSet()
var file *ast.File
const (
tf1FuncName = "ExportedFunctionOne"
tf1FuncSig = "func ExportedFunctionOne() string"
tf1FuncDoc = "I'm a comment for ExportedFunctionOne"
tf1UnexportedFuncName = "unexportedFunctionOne"
tf1UnexportedFuncSig = "func unexportedFunctionOne() string"
tf1UnexportedFuncDoc = "I'm a comment for unexportedFunctionOne"
tf1Path = "testfiles/testfile1.go"
tfPkgName = "testfiles"
)
func init() {
var err error
file, err = parser.ParseFile(fset, tf1Path, nil, parser.ParseComments)
if err != nil {
log.Fatalln(err)
}
}
func TestParseFileImports(t *testing.T) {
imports := ParseFileImports(file)
if len(imports) != 2 {
t.Errorf("%d imports found. expected 2", len(imports))
}
if imports[0] != "fmt" {
t.Errorf("expected imports[0] to be `fmt`, got %s", imports[0])
}
}
func TestParseFunction(t *testing.T) {
funcs := []*Function{}
bb := new(bytes.Buffer)
for _, d := range file.Decls {
if fnc, ok := d.(*ast.FuncDecl); ok {
f := ParseFunction(fset, fnc, bb)
if f != nil {
funcs = append(funcs, f)
}
}
}
if funcs[0].Name != tf1FuncName {
t.Errorf("function name incorrect, expected %s, got %s",
tf1FuncName, funcs[0].Name)
}
if funcs[0].Signature != tf1FuncSig {
t.Errorf("function signature incorrect, expected %s, got %s",
tf1FuncSig, funcs[0].Signature)
}
if funcs[0].Documentation != tf1FuncDoc {
t.Errorf("function documentation incorrect, expected %s, got %s",
tf1FuncDoc, funcs[0].Documentation)
}
}
func TestParseFileFuncsExported(t *testing.T) {
// Only parse exported functions.
funcs := ParseFileFuncs(fset, file, FuncExported)
if len(funcs) > 1 {
t.Errorf("expected to find 1 function, found %d", len(funcs))
}
if funcs[0].Name != tf1FuncName {
t.Errorf("function name incorrect, expected %s, got %s",
tf1FuncName, funcs[0].Name)
}
if funcs[0].Signature != tf1FuncSig {
t.Errorf("function signature incorrect, expected %s, got %s",
tf1FuncSig, funcs[0].Signature)
}
if funcs[0].Documentation != tf1FuncDoc {
t.Errorf("function documentation incorrect, expected %s, got %s",
tf1FuncDoc, funcs[0].Documentation)
}
}
func TestParseFileFuncsUnexported(t *testing.T) {
// Only parse unexported functions.
funcs := ParseFileFuncs(fset, file, FuncUnexported)
if len(funcs) > 1 {
t.Errorf("expected to find 1 function, found %d", len(funcs))
}
if funcs[0].Name != tf1UnexportedFuncName {
t.Errorf("function name incorrect, expected %s, got %s",
tf1UnexportedFuncName, funcs[0].Name)
}
if funcs[0].Signature != tf1UnexportedFuncSig {
t.Errorf("function signature incorrect, expected %s, got %s",
tf1UnexportedFuncSig, funcs[0].Signature)
}
if funcs[0].Documentation != tf1UnexportedFuncDoc {
t.Errorf("function documentation incorrect, expected %s, got %s",
tf1UnexportedFuncDoc, funcs[0].Documentation)
}
}
func TestParseFileFuncsBoth(t *testing.T) {
// Parse both exported and unexported functions.
funcs := ParseFileFuncs(fset, file, FuncBoth)
if len(funcs) != 2 {
t.Errorf("expected to find 2 function, found %d", len(funcs))
}
if funcs[1].Name != tf1UnexportedFuncName {
t.Errorf("function name incorrect, expected %s, got %s",
tf1UnexportedFuncName, funcs[1].Name)
}
if funcs[1].Signature != tf1UnexportedFuncSig {
t.Errorf("function signature incorrect, expected %s, got %s",
tf1UnexportedFuncSig, funcs[1].Signature)
}
if funcs[1].Documentation != tf1UnexportedFuncDoc {
t.Errorf("function documentation incorrect, expected %s, got %s",
tf1UnexportedFuncDoc, funcs[1].Documentation)
}
}
func TestIsExported(t *testing.T) {
// Parse file and return only exported functions.
funcs := ParseFileFuncs(fset, file, FuncExported)
for _, fnc := range funcs {
if !fnc.IsExported() {
t.Errorf("expected no unexported functions, %s is unexported", fnc.Name)
}
}
}
func TestIsNotExported(t *testing.T) {
// Parse file and return only exported functions.
funcs := ParseFileFuncs(fset, file, FuncUnexported)
for _, fnc := range funcs {
if fnc.IsExported() {
t.Errorf("expected no exported functions, %s is exported", fnc.Name)
}
}
}
func TestParsePackage(t *testing.T) {
pkgs, err := parser.ParseDir(fset, "testfiles", FilterIgnoreTests, parser.ParseComments)
if err != nil {
t.Error(err)
}
if len(pkgs) != 1 {
t.Errorf("expected 1 package, found %d", len(pkgs))
}
if _, exists := pkgs[tfPkgName]; !exists {
t.Errorf("package %s not found", tfPkgName)
}
if len(pkgs[tfPkgName].Files) != 2 {
t.Errorf("expected 2 package files, found %d", len(pkgs[tfPkgName].Files))
}
pkg := ParsePackage(fset, pkgs[tfPkgName], FuncExported)
if pkg.Name != tfPkgName {
t.Errorf("expected package name %s, got %s", pkg.Name, pkgs[tfPkgName].Name)
}
if len(pkg.Imports) != 3 {
t.Errorf("expected to find 3 imports, found %d", len(pkg.Imports))
}
if len(pkg.Funcs) != 2 {
t.Errorf("expected to find 2 functions, found %d", len(pkg.Funcs))
}
}