Debian Codegraph @

internal/modindex/lookup_test.go

raw symbols SCIP
1 // Copyright 2024 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
5 package modindex_test
6
7 import (
8 "fmt"
9 "log"
10 "os"
11 "path/filepath"
12 "strings"
13 "testing"
14
15 . "golang.org/x/tools/internal/modindex"
16 )
17
18 type tdata struct {
19 fname string
20 pkg string
21 items []titem
22 }
23
24 type titem struct {
25 code string
26 result result
27 }
28
29 var thedata = tdata{
30 fname: "cloud.google.com/go/[email protected]/foo.go",
31 pkg: "foo",
32 items: []titem{
33 // these need to be in alphabetical order
34 {"func Foo() {}", result{"Foo", Func, false, 0, nil}},
35 {"const FooC = 23", result{"FooC", Const, false, 0, nil}},
36 {"func FooF(int, float) error {return nil}", result{"FooF", Func, false, 1,
37 []Field{{"_", "int"}, {"_", "float"}}}},
38 {"type FooT struct{}", result{"FooT", Type, false, 0, nil}},
39 {"var FooV int", result{"FooV", Var, false, 0, nil}},
40 {"func Goo() {}", result{"Goo", Func, false, 0, nil}},
41 {"/*Deprecated: too weird\n*/\n// Another Goo\nvar GooVV int", result{"GooVV", Var, true, 0, nil}},
42 {"func Ⱋoox(x int) {}", result{"Ⱋoox", Func, false, 0, []Field{{"x", "int"}}}},
43 },
44 }
45
46 type result struct {
47 name string
48 typ LexType
49 deprecated bool
50 result int
51 sig []Field
52 }
53
54 func okresult(r result, p Candidate) bool {
55 if r.name != p.Name || r.typ != p.Type || r.result != int(p.Results) {
56 return false
57 }
58 if r.deprecated != p.Deprecated {
59 return false
60 }
61 if len(r.sig) != len(p.Sig) {
62 return false
63 }
64 for i := 0; i < len(r.sig); i++ {
65 if r.sig[i] != p.Sig[i] {
66 return false
67 }
68 }
69 return true
70 }
71
72 func TestLookup(t *testing.T) {
73 dir := testModCache(t)
74 wrtData(t, dir, thedata)
75 if _, err := Create(dir); err != nil {
76 t.Fatal(err)
77 }
78 ix, err := Read(dir)
79 if err != nil {
80 t.Fatal(err)
81 }
82 if len(ix.Entries) != 1 {
83 t.Fatalf("got %d Entries, expected 1", len(ix.Entries))
84 }
85 // get all the symbols
86 p := ix.Lookup("foo", "", true)
87 if len(p) > 0 && !strings.Contains(p[0].ImportPath, "Longrunning") {
88 t.Errorf("Failed to convert escaped path: %s", p[0].ImportPath)
89 }
90 if len(p) != len(thedata.items) {
91 t.Errorf("got %d possibilities for pkg foo, expected %d", len(p), len(thedata.items))
92 }
93 for i, r := range thedata.items {
94 if !okresult(r.result, p[i]) {
95 t.Errorf("got %#v, expected %#v", p[i], r.result)
96 }
97 }
98 // look for the Foo... and check that each is a Foo...
99 p = ix.Lookup("foo", "Foo", true)
100 if len(p) != 5 {
101 t.Errorf("got %d possibilities for foo.Foo*, expected 5", len(p))
102 }
103 for _, r := range p {
104 if !strings.HasPrefix(r.Name, "Foo") {
105 t.Errorf("got %s, expected Foo...", r.Name)
106 }
107 }
108 // fail to find something
109 p = ix.Lookup("foo", "FooVal", false)
110 if len(p) != 0 {
111 t.Errorf("got %d possibilities for foo.FooVal, expected 0", len(p))
112 }
113 // find an exact match
114 p = ix.Lookup("foo", "Foo", false)
115 if len(p) != 1 {
116 t.Errorf("got %d possibilities for foo.Foo, expected 1", len(p))
117 }
118 // "Foo" is the first test datum
119 if !okresult(thedata.items[0].result, p[0]) {
120 t.Errorf("got %#v, expected %#v", p[0], thedata.items[0].result)
121 }
122 }
123
124 func wrtData(t *testing.T, dir string, data tdata) {
125 t.Helper()
126 locname := filepath.FromSlash(data.fname)
127 if err := os.MkdirAll(filepath.Join(dir, filepath.Dir(locname)), 0755); err != nil {
128 t.Fatal(err)
129 }
130 fd, err := os.Create(filepath.Join(dir, locname))
131 if err != nil {
132 t.Fatal(err)
133 }
134 defer fd.Close()
135 fmt.Fprintf(fd, "package %s\n", data.pkg)
136 for _, item := range data.items {
137 fd.WriteString(item.code + "\n")
138 }
139 }
140
141 func TestLookupAll(t *testing.T) {
142 log.SetFlags(log.Lshortfile)
143 dir := testModCache(t)
144 wrtModule := func(mod string, nms ...string) {
145 dname := filepath.Join(dir, mod)
146 if err := os.MkdirAll(dname, 0755); err != nil {
147 t.Fatal(err)
148 }
149 fname := filepath.Join(dname, "foo.go")
150 fd, err := os.Create(fname)
151 if err != nil {
152 t.Fatal(err)
153 }
154 defer fd.Close()
155 if _, err := fmt.Fprintf(fd, "package foo\n"); err != nil {
156 t.Fatal(err)
157 }
158 for _, nm := range nms {
159 fmt.Fprintf(fd, "func %s() {}\n", nm)
160 }
161 }
162 wrtModule("a.com/go/[email protected]", "A", "B", "C", "D")
163 wrtModule("b.com/go/[email protected]", "A", "B", "C")
164 wrtModule("c.com/go/[email protected]", "A", "B", "C", "D", "E")
165
166 if _, err := Create(dir); err != nil {
167 t.Fatal(err)
168 }
169 ix, err := Read(dir)
170 if err != nil {
171 t.Fatal(err)
172 }
173 cands := ix.Lookup("foo", "A", false)
174 if len(cands) != 3 {
175 t.Errorf("got %d candidates for A, expected 3", len(cands))
176 }
177 got := ix.LookupAll("foo", "A", "B", "C", "D")
178 if len(got) != 2 {
179 t.Errorf("got %d candidates for A,B,C,D, expected 2", len(got))
180 }
181 got = ix.LookupAll("foo", []string{"A", "B", "C", "D", "E"}...)
182 if len(got) != 1 {
183 t.Errorf("got %d candidates for A,B,C,D,E, expected 1", len(got))
184 }
185 }
186
187 func TestUniquify(t *testing.T) {
188 var v []string
189 for i := 1; i < 4; i++ {
190 v = append(v, "A")
191 w := Uniquify(v)
192 if len(w) != 1 {
193 t.Errorf("got %d, expected 1", len(w))
194 }
195 }
196 for i := 1; i < 3; i++ {
197 v = append(v, "B", "C")
198 w := Uniquify(v)
199 if len(w) != 3 {
200 t.Errorf("got %d, expected 3", len(w))
201 }
202 }
203 }