GoPLS Viewer

Home|gopls/godoc/index_test.go
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.
4
5package godoc
6
7import (
8    "bytes"
9    "reflect"
10    "sort"
11    "strings"
12    "testing"
13
14    "golang.org/x/tools/godoc/vfs/mapfs"
15)
16
17func newCorpus(t *testing.T) *Corpus {
18    c := NewCorpus(mapfs.New(map[string]string{
19        "src/foo/foo.go"`// Package foo is an example.
20package foo
21
22import "bar"
23
24const Pi = 3.1415
25
26var Foos []Foo
27
28// Foo is stuff.
29type Foo struct{}
30
31func New() *Foo {
32   return new(Foo)
33}
34`,
35        "src/bar/bar.go"`// Package bar is another example to test races.
36package bar
37`,
38        "src/other/bar/bar.go"`// Package bar is another bar package.
39package bar
40func X() {}
41`,
42        "src/skip/skip.go"`// Package skip should be skipped.
43package skip
44func Skip() {}
45`,
46        "src/bar/readme.txt"`Whitelisted text file.
47`,
48        "src/bar/baz.zzz"`Text file not whitelisted.
49`,
50    }))
51    c.IndexEnabled = true
52    c.IndexDirectory = func(dir stringbool {
53        return !strings.Contains(dir"skip")
54    }
55
56    if err := c.Init(); err != nil {
57        t.Fatal(err)
58    }
59    return c
60}
61
62func TestIndex(t *testing.T) {
63    for _docs := range []bool{truefalse} {
64        for _goCode := range []bool{truefalse} {
65            for _fullText := range []bool{truefalse} {
66                c := newCorpus(t)
67                c.IndexDocs = docs
68                c.IndexGoCode = goCode
69                c.IndexFullText = fullText
70                c.UpdateIndex()
71                ix_ := c.CurrentIndex()
72                if ix == nil {
73                    t.Fatal("no index")
74                }
75                t.Logf("docs, goCode, fullText = %v,%v,%v"docsgoCodefullText)
76                testIndex(tcix)
77            }
78        }
79    }
80}
81
82func TestIndexWriteRead(t *testing.T) {
83    type key struct {
84        docsgoCodefullText bool
85    }
86    type val struct {
87        buf *bytes.Buffer
88        c   *Corpus
89    }
90    m := map[key]val{}
91
92    for _docs := range []bool{truefalse} {
93        for _goCode := range []bool{truefalse} {
94            for _fullText := range []bool{truefalse} {
95                k := key{docsgoCodefullText}
96                c := newCorpus(t)
97                c.IndexDocs = docs
98                c.IndexGoCode = goCode
99                c.IndexFullText = fullText
100                c.UpdateIndex()
101                ix_ := c.CurrentIndex()
102                if ix == nil {
103                    t.Fatal("no index")
104                }
105                var buf bytes.Buffer
106                nwerr := ix.WriteTo(&buf)
107                if err != nil {
108                    t.Fatalf("Index.WriteTo: %v"err)
109                }
110                m[k] = val{bytes.NewBuffer(buf.Bytes()), c}
111                ix2 := new(Index)
112                nrerr := ix2.ReadFrom(&buf)
113                if err != nil {
114                    t.Fatalf("Index.ReadFrom: %v"err)
115                }
116                if nr != nw {
117                    t.Errorf("Wrote %d bytes to index but read %d"nwnr)
118                }
119                testIndex(tcix)
120            }
121        }
122    }
123    // Test CompatibleWith
124    for k1v1 := range m {
125        ix := new(Index)
126        if _err := ix.ReadFrom(v1.buf); err != nil {
127            t.Fatalf("Index.ReadFrom: %v"err)
128        }
129        for k2v2 := range m {
130            if gotwant := ix.CompatibleWith(v2.c), k1 == k2got != want {
131                t.Errorf("CompatibleWith = %v; want %v for %v, %v"gotwantk1k2)
132            }
133        }
134    }
135}
136
137func testIndex(t *testing.Tc *Corpusix *Index) {
138    if _ok := ix.words["Skip"]; ok {
139        t.Errorf("the word Skip was found; expected it to be skipped")
140    }
141    checkStats(tcix)
142    checkImportCount(tcix)
143    checkPackagePath(tcix)
144    checkExports(tcix)
145    checkIdents(tcix)
146}
147
148// checkStats checks the Index's statistics.
149// Some statistics are only set when we're indexing Go code.
150func checkStats(t *testing.Tc *Corpusix *Index) {
151    want := Statistics{}
152    if c.IndexFullText {
153        want.Bytes = 314
154        want.Files = 4
155        want.Lines = 21
156    } else if c.IndexDocs || c.IndexGoCode {
157        want.Bytes = 291
158        want.Files = 3
159        want.Lines = 20
160    }
161    if c.IndexGoCode {
162        want.Words = 8
163        want.Spots = 12
164    }
165    if got := ix.Stats(); !reflect.DeepEqual(gotwant) {
166        t.Errorf("Stats = %#v; want %#v"gotwant)
167    }
168}
169
170// checkImportCount checks the Index's import count map.
171// It is only set when we're indexing Go code.
172func checkImportCount(t *testing.Tc *Corpusix *Index) {
173    want := map[string]int{}
174    if c.IndexGoCode {
175        want = map[string]int{
176            "bar"1,
177        }
178    }
179    if got := ix.ImportCount(); !reflect.DeepEqual(gotwant) {
180        t.Errorf("ImportCount = %v; want %v"gotwant)
181    }
182}
183
184// checkPackagePath checks the Index's package path map.
185// It is set if at least one of the indexing options is enabled.
186func checkPackagePath(t *testing.Tc *Corpusix *Index) {
187    want := map[string]map[string]bool{}
188    if c.IndexDocs || c.IndexGoCode || c.IndexFullText {
189        want = map[string]map[string]bool{
190            "foo": {
191                "foo"true,
192            },
193            "bar": {
194                "bar":       true,
195                "other/bar"true,
196            },
197        }
198    }
199    if got := ix.PackagePath(); !reflect.DeepEqual(gotwant) {
200        t.Errorf("PackagePath = %v; want %v"gotwant)
201    }
202}
203
204// checkExports checks the Index's exports map.
205// It is only set when we're indexing Go code.
206func checkExports(t *testing.Tc *Corpusix *Index) {
207    want := map[string]map[string]SpotKind{}
208    if c.IndexGoCode {
209        want = map[string]map[string]SpotKind{
210            "foo": {
211                "Pi":   ConstDecl,
212                "Foos"VarDecl,
213                "Foo":  TypeDecl,
214                "New":  FuncDecl,
215            },
216            "other/bar": {
217                "X"FuncDecl,
218            },
219        }
220    }
221    if got := ix.Exports(); !reflect.DeepEqual(gotwant) {
222        t.Errorf("Exports = %v; want %v"gotwant)
223    }
224}
225
226// checkIdents checks the Index's indents map.
227// It is only set when we're indexing documentation.
228func checkIdents(t *testing.Tc *Corpusix *Index) {
229    want := map[SpotKind]map[string][]Ident{}
230    if c.IndexDocs {
231        want = map[SpotKind]map[string][]Ident{
232            PackageClause: {
233                "bar": {
234                    {"bar""bar""bar""Package bar is another example to test races."},
235                    {"other/bar""bar""bar""Package bar is another bar package."},
236                },
237                "foo":   {{"foo""foo""foo""Package foo is an example."}},
238                "other": {{"other/bar""bar""bar""Package bar is another bar package."}},
239            },
240            ConstDecl: {
241                "Pi": {{"foo""foo""Pi"""}},
242            },
243            VarDecl: {
244                "Foos": {{"foo""foo""Foos"""}},
245            },
246            TypeDecl: {
247                "Foo": {{"foo""foo""Foo""Foo is stuff."}},
248            },
249            FuncDecl: {
250                "New": {{"foo""foo""New"""}},
251                "X":   {{"other/bar""bar""X"""}},
252            },
253        }
254    }
255    if got := ix.Idents(); !reflect.DeepEqual(gotwant) {
256        t.Errorf("Idents = %v; want %v"gotwant)
257    }
258}
259
260func TestIdentResultSort(t *testing.T) {
261    ic := map[string]int{
262        "/a/b/pkg1"10,
263        "/a/b/pkg2"2,
264        "/b/d/pkg3"20,
265    }
266    for _tc := range []struct {
267        ir  []Ident
268        exp []Ident
269    }{
270        {
271            ir: []Ident{
272                {"/a/b/pkg2""pkg2""MyFunc2"""},
273                {"/b/d/pkg3""pkg3""MyFunc3"""},
274                {"/a/b/pkg1""pkg1""MyFunc1"""},
275            },
276            exp: []Ident{
277                {"/b/d/pkg3""pkg3""MyFunc3"""},
278                {"/a/b/pkg1""pkg1""MyFunc1"""},
279                {"/a/b/pkg2""pkg2""MyFunc2"""},
280            },
281        },
282        {
283            ir: []Ident{
284                {"/a/a/pkg1""pkg1""MyFunc1"""},
285                {"/a/b/pkg1""pkg1""MyFunc1"""},
286            },
287            exp: []Ident{
288                {"/a/b/pkg1""pkg1""MyFunc1"""},
289                {"/a/a/pkg1""pkg1""MyFunc1"""},
290            },
291        },
292    } {
293        if sort.Sort(byImportCount{tc.iric}); !reflect.DeepEqual(tc.irtc.exp) {
294            t.Errorf("got: %v, want %v"tc.irtc.exp)
295        }
296    }
297}
298
299func TestIdentFilter(t *testing.T) {
300    ic := map[string]int{}
301    for _tc := range []struct {
302        ir  []Ident
303        pak string
304        exp []Ident
305    }{
306        {
307            ir: []Ident{
308                {"/a/b/pkg2""pkg2""MyFunc2"""},
309                {"/b/d/pkg3""pkg3""MyFunc3"""},
310                {"/a/b/pkg1""pkg1""MyFunc1"""},
311            },
312            pak"pkg2",
313            exp: []Ident{
314                {"/a/b/pkg2""pkg2""MyFunc2"""},
315            },
316        },
317    } {
318        res := byImportCount{tc.iric}.filter(tc.pak)
319        if !reflect.DeepEqual(restc.exp) {
320            t.Errorf("got: %v, want %v"restc.exp)
321        }
322    }
323}
324
MembersX
TestIdentResultSort.ic
TestIndex.t
TestIndexWriteRead.val.buf
checkExports.t
checkImportCount.ix
checkExports.c
TestIdentResultSort.RangeStmt_6279.tc
TestIndexWriteRead.key.docs
TestIndexWriteRead.key.goCode
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt._
TestIdentFilter.ic
TestIndexWriteRead.RangeStmt_2579.BlockStmt.RangeStmt_2716.k2
testIndex.t
checkImportCount.want
checkStats.want
TestIdentFilter.t
newCorpus.c
TestIndex.RangeStmt_1135.BlockStmt.RangeStmt_1180.BlockStmt.RangeStmt_1228.BlockStmt.ix
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.c
TestIndexWriteRead.RangeStmt_2579.BlockStmt._
checkStats.got
checkImportCount.got
newCorpus.t
TestIndexWriteRead.t
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.k
checkPackagePath.ix
TestIdentResultSort.t
TestIndexWriteRead
checkPackagePath.want
checkStats
checkExports.got
checkIdents.c
TestIdentResultSort
TestIdentFilter.RangeStmt_7071.BlockStmt.res
newCorpus.err
TestIndex.RangeStmt_1135.docs
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.err
TestIndexWriteRead.m
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.ix2
checkPackagePath.got
checkExports.ix
checkIdents
reflect
newCorpus
TestIndex
checkIdents.got
checkStats.c
checkExports.want
checkIdents.t
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.fullText
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.ix
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.nr
TestIndexWriteRead.RangeStmt_2579.v1
TestIndexWriteRead.RangeStmt_2579.BlockStmt.RangeStmt_2716.BlockStmt.got
TestIndex.RangeStmt_1135.BlockStmt.RangeStmt_1180.goCode
TestIndex.RangeStmt_1135.BlockStmt.RangeStmt_1180.BlockStmt.RangeStmt_1228.BlockStmt.c
TestIndexWriteRead.val
checkImportCount.t
TestIdentFilter
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.goCode
testIndex
checkPackagePath
checkExports
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.nw
checkStats.ix
checkImportCount
checkImportCount.c
checkPackagePath.c
TestIndex.RangeStmt_1135.BlockStmt.RangeStmt_1180.BlockStmt.RangeStmt_1228.fullText
TestIndexWriteRead.RangeStmt_2579.k1
testIndex.c
TestIndexWriteRead.key.fullText
TestIndexWriteRead.val.c
TestIndexWriteRead.RangeStmt_1759.docs
TestIndexWriteRead.RangeStmt_2579.BlockStmt.ix
TestIndexWriteRead.RangeStmt_2579.BlockStmt.err
mapfs
TestIndex.RangeStmt_1135.BlockStmt.RangeStmt_1180.BlockStmt.RangeStmt_1228.BlockStmt._
TestIndexWriteRead.key
checkIdents.want
TestIndexWriteRead.RangeStmt_2579.BlockStmt.RangeStmt_2716.v2
testIndex.ix
checkStats.t
TestIdentFilter.RangeStmt_7071.tc
TestIndexWriteRead.RangeStmt_1759.BlockStmt.RangeStmt_1804.BlockStmt.RangeStmt_1852.BlockStmt.buf
checkPackagePath.t
checkIdents.ix
Members
X