1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 "golang.org/x/tools/godoc/vfs/mapfs"
17 func newCorpus(t *testing.T) *Corpus {
18 c := NewCorpus(mapfs.New(map[string]string{
19 "src/foo/foo.go": `// Package foo is an example.
35 "src/bar/bar.go": `// Package bar is another example to test races.
38 "src/other/bar/bar.go": `// Package bar is another bar package.
42 "src/skip/skip.go": `// Package skip should be skipped.
46 "src/bar/readme.txt": `Whitelisted text file.
48 "src/bar/baz.zzz": `Text file not whitelisted.
52 c.IndexDirectory = func(dir string) bool {
53 return !strings.Contains(dir, "skip")
56 if err := c.Init(); err != nil {
62 func TestIndex(t *testing.T) {
63 for _, docs := range []bool{true, false} {
64 for _, goCode := range []bool{true, false} {
65 for _, fullText := range []bool{true, false} {
68 c.IndexGoCode = goCode
69 c.IndexFullText = fullText
71 ix, _ := c.CurrentIndex()
75 t.Logf("docs, goCode, fullText = %v,%v,%v", docs, goCode, fullText)
82 func TestIndexWriteRead(t *testing.T) {
84 docs, goCode, fullText bool
92 for _, docs := range []bool{true, false} {
93 for _, goCode := range []bool{true, false} {
94 for _, fullText := range []bool{true, false} {
95 k := key{docs, goCode, fullText}
98 c.IndexGoCode = goCode
99 c.IndexFullText = fullText
101 ix, _ := c.CurrentIndex()
106 nw, err := ix.WriteTo(&buf)
108 t.Fatalf("Index.WriteTo: %v", err)
110 m[k] = val{bytes.NewBuffer(buf.Bytes()), c}
112 nr, err := ix2.ReadFrom(&buf)
114 t.Fatalf("Index.ReadFrom: %v", err)
117 t.Errorf("Wrote %d bytes to index but read %d", nw, nr)
123 // Test CompatibleWith
124 for k1, v1 := range m {
126 if _, err := ix.ReadFrom(v1.buf); err != nil {
127 t.Fatalf("Index.ReadFrom: %v", err)
129 for k2, v2 := range m {
130 if got, want := ix.CompatibleWith(v2.c), k1 == k2; got != want {
131 t.Errorf("CompatibleWith = %v; want %v for %v, %v", got, want, k1, k2)
137 func testIndex(t *testing.T, c *Corpus, ix *Index) {
138 if _, ok := ix.words["Skip"]; ok {
139 t.Errorf("the word Skip was found; expected it to be skipped")
142 checkImportCount(t, c, ix)
143 checkPackagePath(t, c, ix)
144 checkExports(t, c, ix)
145 checkIdents(t, c, ix)
148 // checkStats checks the Index's statistics.
149 // Some statistics are only set when we're indexing Go code.
150 func checkStats(t *testing.T, c *Corpus, ix *Index) {
156 } else if c.IndexDocs || c.IndexGoCode {
165 if got := ix.Stats(); !reflect.DeepEqual(got, want) {
166 t.Errorf("Stats = %#v; want %#v", got, want)
170 // checkImportCount checks the Index's import count map.
171 // It is only set when we're indexing Go code.
172 func checkImportCount(t *testing.T, c *Corpus, ix *Index) {
173 want := map[string]int{}
175 want = map[string]int{
179 if got := ix.ImportCount(); !reflect.DeepEqual(got, want) {
180 t.Errorf("ImportCount = %v; want %v", got, want)
184 // checkPackagePath checks the Index's package path map.
185 // It is set if at least one of the indexing options is enabled.
186 func checkPackagePath(t *testing.T, c *Corpus, ix *Index) {
187 want := map[string]map[string]bool{}
188 if c.IndexDocs || c.IndexGoCode || c.IndexFullText {
189 want = map[string]map[string]bool{
199 if got := ix.PackagePath(); !reflect.DeepEqual(got, want) {
200 t.Errorf("PackagePath = %v; want %v", got, want)
204 // checkExports checks the Index's exports map.
205 // It is only set when we're indexing Go code.
206 func checkExports(t *testing.T, c *Corpus, ix *Index) {
207 want := map[string]map[string]SpotKind{}
209 want = map[string]map[string]SpotKind{
221 if got := ix.Exports(); !reflect.DeepEqual(got, want) {
222 t.Errorf("Exports = %v; want %v", got, want)
226 // checkIdents checks the Index's indents map.
227 // It is only set when we're indexing documentation.
228 func checkIdents(t *testing.T, c *Corpus, ix *Index) {
229 want := map[SpotKind]map[string][]Ident{}
231 want = map[SpotKind]map[string][]Ident{
234 {"bar", "bar", "bar", "Package bar is another example to test races."},
235 {"other/bar", "bar", "bar", "Package bar is another bar package."},
237 "foo": {{"foo", "foo", "foo", "Package foo is an example."}},
238 "other": {{"other/bar", "bar", "bar", "Package bar is another bar package."}},
241 "Pi": {{"foo", "foo", "Pi", ""}},
244 "Foos": {{"foo", "foo", "Foos", ""}},
247 "Foo": {{"foo", "foo", "Foo", "Foo is stuff."}},
250 "New": {{"foo", "foo", "New", ""}},
251 "X": {{"other/bar", "bar", "X", ""}},
255 if got := ix.Idents(); !reflect.DeepEqual(got, want) {
256 t.Errorf("Idents = %v; want %v", got, want)
260 func TestIdentResultSort(t *testing.T) {
261 ic := map[string]int{
266 for _, tc := range []struct {
272 {"/a/b/pkg2", "pkg2", "MyFunc2", ""},
273 {"/b/d/pkg3", "pkg3", "MyFunc3", ""},
274 {"/a/b/pkg1", "pkg1", "MyFunc1", ""},
277 {"/b/d/pkg3", "pkg3", "MyFunc3", ""},
278 {"/a/b/pkg1", "pkg1", "MyFunc1", ""},
279 {"/a/b/pkg2", "pkg2", "MyFunc2", ""},
284 {"/a/a/pkg1", "pkg1", "MyFunc1", ""},
285 {"/a/b/pkg1", "pkg1", "MyFunc1", ""},
288 {"/a/b/pkg1", "pkg1", "MyFunc1", ""},
289 {"/a/a/pkg1", "pkg1", "MyFunc1", ""},
293 if sort.Sort(byImportCount{tc.ir, ic}); !reflect.DeepEqual(tc.ir, tc.exp) {
294 t.Errorf("got: %v, want %v", tc.ir, tc.exp)
299 func TestIdentFilter(t *testing.T) {
300 ic := map[string]int{}
301 for _, tc := range []struct {
308 {"/a/b/pkg2", "pkg2", "MyFunc2", ""},
309 {"/b/d/pkg3", "pkg3", "MyFunc3", ""},
310 {"/a/b/pkg1", "pkg1", "MyFunc1", ""},
314 {"/a/b/pkg2", "pkg2", "MyFunc2", ""},
318 res := byImportCount{tc.ir, ic}.filter(tc.pak)
319 if !reflect.DeepEqual(res, tc.exp) {
320 t.Errorf("got: %v, want %v", res, tc.exp)