forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
323 lines (302 loc) · 7.35 KB
/
index_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package godoc
import (
"bytes"
"reflect"
"sort"
"strings"
"testing"
"golang.org/x/tools/godoc/vfs/mapfs"
)
func newCorpus(t *testing.T) *Corpus {
c := NewCorpus(mapfs.New(map[string]string{
"src/foo/foo.go": `// Package foo is an example.
package foo
import "bar"
const Pi = 3.1415
var Foos []Foo
// Foo is stuff.
type Foo struct{}
func New() *Foo {
return new(Foo)
}
`,
"src/bar/bar.go": `// Package bar is another example to test races.
package bar
`,
"src/other/bar/bar.go": `// Package bar is another bar package.
package bar
func X() {}
`,
"src/skip/skip.go": `// Package skip should be skipped.
package skip
func Skip() {}
`,
"src/bar/readme.txt": `Whitelisted text file.
`,
"src/bar/baz.zzz": `Text file not whitelisted.
`,
}))
c.IndexEnabled = true
c.IndexDirectory = func(dir string) bool {
return !strings.Contains(dir, "skip")
}
if err := c.Init(); err != nil {
t.Fatal(err)
}
return c
}
func TestIndex(t *testing.T) {
for _, docs := range []bool{true, false} {
for _, goCode := range []bool{true, false} {
for _, fullText := range []bool{true, false} {
c := newCorpus(t)
c.IndexDocs = docs
c.IndexGoCode = goCode
c.IndexFullText = fullText
c.UpdateIndex()
ix, _ := c.CurrentIndex()
if ix == nil {
t.Fatal("no index")
}
t.Logf("docs, goCode, fullText = %v,%v,%v", docs, goCode, fullText)
testIndex(t, c, ix)
}
}
}
}
func TestIndexWriteRead(t *testing.T) {
type key struct {
docs, goCode, fullText bool
}
type val struct {
buf *bytes.Buffer
c *Corpus
}
m := map[key]val{}
for _, docs := range []bool{true, false} {
for _, goCode := range []bool{true, false} {
for _, fullText := range []bool{true, false} {
k := key{docs, goCode, fullText}
c := newCorpus(t)
c.IndexDocs = docs
c.IndexGoCode = goCode
c.IndexFullText = fullText
c.UpdateIndex()
ix, _ := c.CurrentIndex()
if ix == nil {
t.Fatal("no index")
}
var buf bytes.Buffer
nw, err := ix.WriteTo(&buf)
if err != nil {
t.Fatalf("Index.WriteTo: %v", err)
}
m[k] = val{bytes.NewBuffer(buf.Bytes()), c}
ix2 := new(Index)
nr, err := ix2.ReadFrom(&buf)
if err != nil {
t.Fatalf("Index.ReadFrom: %v", err)
}
if nr != nw {
t.Errorf("Wrote %d bytes to index but read %d", nw, nr)
}
testIndex(t, c, ix)
}
}
}
// Test CompatibleWith
for k1, v1 := range m {
ix := new(Index)
if _, err := ix.ReadFrom(v1.buf); err != nil {
t.Fatalf("Index.ReadFrom: %v", err)
}
for k2, v2 := range m {
if got, want := ix.CompatibleWith(v2.c), k1 == k2; got != want {
t.Errorf("CompatibleWith = %v; want %v for %v, %v", got, want, k1, k2)
}
}
}
}
func testIndex(t *testing.T, c *Corpus, ix *Index) {
if _, ok := ix.words["Skip"]; ok {
t.Errorf("the word Skip was found; expected it to be skipped")
}
checkStats(t, c, ix)
checkImportCount(t, c, ix)
checkPackagePath(t, c, ix)
checkExports(t, c, ix)
checkIdents(t, c, ix)
}
// checkStats checks the Index's statistics.
// Some statistics are only set when we're indexing Go code.
func checkStats(t *testing.T, c *Corpus, ix *Index) {
want := Statistics{}
if c.IndexFullText {
want.Bytes = 314
want.Files = 4
want.Lines = 21
} else if c.IndexDocs || c.IndexGoCode {
want.Bytes = 291
want.Files = 3
want.Lines = 20
}
if c.IndexGoCode {
want.Words = 8
want.Spots = 12
}
if got := ix.Stats(); !reflect.DeepEqual(got, want) {
t.Errorf("Stats = %#v; want %#v", got, want)
}
}
// checkImportCount checks the Index's import count map.
// It is only set when we're indexing Go code.
func checkImportCount(t *testing.T, c *Corpus, ix *Index) {
want := map[string]int{}
if c.IndexGoCode {
want = map[string]int{
"bar": 1,
}
}
if got := ix.ImportCount(); !reflect.DeepEqual(got, want) {
t.Errorf("ImportCount = %v; want %v", got, want)
}
}
// checkPackagePath checks the Index's package path map.
// It is set if at least one of the indexing options is enabled.
func checkPackagePath(t *testing.T, c *Corpus, ix *Index) {
want := map[string]map[string]bool{}
if c.IndexDocs || c.IndexGoCode || c.IndexFullText {
want = map[string]map[string]bool{
"foo": {
"foo": true,
},
"bar": {
"bar": true,
"other/bar": true,
},
}
}
if got := ix.PackagePath(); !reflect.DeepEqual(got, want) {
t.Errorf("PackagePath = %v; want %v", got, want)
}
}
// checkExports checks the Index's exports map.
// It is only set when we're indexing Go code.
func checkExports(t *testing.T, c *Corpus, ix *Index) {
want := map[string]map[string]SpotKind{}
if c.IndexGoCode {
want = map[string]map[string]SpotKind{
"foo": {
"Pi": ConstDecl,
"Foos": VarDecl,
"Foo": TypeDecl,
"New": FuncDecl,
},
"other/bar": {
"X": FuncDecl,
},
}
}
if got := ix.Exports(); !reflect.DeepEqual(got, want) {
t.Errorf("Exports = %v; want %v", got, want)
}
}
// checkIdents checks the Index's indents map.
// It is only set when we're indexing documentation.
func checkIdents(t *testing.T, c *Corpus, ix *Index) {
want := map[SpotKind]map[string][]Ident{}
if c.IndexDocs {
want = map[SpotKind]map[string][]Ident{
PackageClause: {
"bar": {
{"bar", "bar", "bar", "Package bar is another example to test races."},
{"other/bar", "bar", "bar", "Package bar is another bar package."},
},
"foo": {{"foo", "foo", "foo", "Package foo is an example."}},
"other": {{"other/bar", "bar", "bar", "Package bar is another bar package."}},
},
ConstDecl: {
"Pi": {{"foo", "foo", "Pi", ""}},
},
VarDecl: {
"Foos": {{"foo", "foo", "Foos", ""}},
},
TypeDecl: {
"Foo": {{"foo", "foo", "Foo", "Foo is stuff."}},
},
FuncDecl: {
"New": {{"foo", "foo", "New", ""}},
"X": {{"other/bar", "bar", "X", ""}},
},
}
}
if got := ix.Idents(); !reflect.DeepEqual(got, want) {
t.Errorf("Idents = %v; want %v", got, want)
}
}
func TestIdentResultSort(t *testing.T) {
ic := map[string]int{
"/a/b/pkg1": 10,
"/a/b/pkg2": 2,
"/b/d/pkg3": 20,
}
for _, tc := range []struct {
ir []Ident
exp []Ident
}{
{
ir: []Ident{
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
},
exp: []Ident{
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
},
},
{
ir: []Ident{
{"/a/a/pkg1", "pkg1", "MyFunc1", ""},
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
},
exp: []Ident{
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
{"/a/a/pkg1", "pkg1", "MyFunc1", ""},
},
},
} {
if sort.Sort(byImportCount{tc.ir, ic}); !reflect.DeepEqual(tc.ir, tc.exp) {
t.Errorf("got: %v, want %v", tc.ir, tc.exp)
}
}
}
func TestIdentFilter(t *testing.T) {
ic := map[string]int{}
for _, tc := range []struct {
ir []Ident
pak string
exp []Ident
}{
{
ir: []Ident{
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
{"/b/d/pkg3", "pkg3", "MyFunc3", ""},
{"/a/b/pkg1", "pkg1", "MyFunc1", ""},
},
pak: "pkg2",
exp: []Ident{
{"/a/b/pkg2", "pkg2", "MyFunc2", ""},
},
},
} {
res := byImportCount{tc.ir, ic}.filter(tc.pak)
if !reflect.DeepEqual(res, tc.exp) {
t.Errorf("got: %v, want %v", res, tc.exp)
}
}
}