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
|
package lsp
import (
"bytes"
"errors"
"strings"
"testing"
"github.com/grindlemire/go-tui/internal/lsp/gopls"
"github.com/grindlemire/go-tui/internal/lsp/provider"
)
func newAdapterServer(t *testing.T) (*Server, string) {
t.Helper()
s := NewServer(strings.NewReader(""), new(bytes.Buffer))
uri := "file:///adapter.gsx"
doc := s.docs.Open(uri, indexCoverageSrc, 1)
s.index.IndexDocument(uri, doc.AST)
return s, uri
}
func TestComponentIndexAdapter(t *testing.T) {
s, uri := newAdapterServer(t)
a := &componentIndexAdapter{index: s.index}
t.Run("Lookup", func(t *testing.T) {
info, ok := a.Lookup("Card")
if !ok || info == nil {
t.Fatal("Card not found")
}
if info.Name != "Card" || info.Location.URI != uri {
t.Errorf("info = %+v", info)
}
if _, ok := a.Lookup("Nope"); ok {
t.Error("Lookup(Nope) should not be found")
}
})
t.Run("LookupFunc", func(t *testing.T) {
info, ok := a.LookupFunc("format")
if !ok || info == nil {
t.Fatal("format not found")
}
if info.Returns != "string" {
t.Errorf("Returns = %q, want string", info.Returns)
}
if _, ok := a.LookupFunc("nope"); ok {
t.Error("LookupFunc(nope) should not be found")
}
})
t.Run("LookupParam", func(t *testing.T) {
info, ok := a.LookupParam("Card", "title")
if !ok || info == nil {
t.Fatal("Card.title not found")
}
if info.Type != "string" || info.ComponentName != "Card" {
t.Errorf("info = %+v", info)
}
if _, ok := a.LookupParam("Card", "nope"); ok {
t.Error("LookupParam(Card, nope) should not be found")
}
})
t.Run("LookupFuncParam", func(t *testing.T) {
info, ok := a.LookupFuncParam("format", "prefix")
if !ok || info == nil {
t.Fatal("format.prefix not found")
}
if info.FuncName != "format" || info.Type != "string" {
t.Errorf("info = %+v", info)
}
if info.Location.URI != uri {
t.Errorf("URI = %q, want %q", info.Location.URI, uri)
}
wantEnd := info.Location.Range.Start.Character + len("prefix")
if info.Location.Range.End.Character != wantEnd {
t.Errorf("end character = %d, want %d", info.Location.Range.End.Character, wantEnd)
}
if _, ok := a.LookupFuncParam("format", "nope"); ok {
t.Error("LookupFuncParam(format, nope) should not be found")
}
})
t.Run("All and AllFunctions", func(t *testing.T) {
if got := a.All(); len(got) != 1 || got[0] != "Card" {
t.Errorf("All = %v, want [Card]", got)
}
if got := a.AllFunctions(); len(got) != 1 || got[0] != "format" {
t.Errorf("AllFunctions = %v, want [format]", got)
}
})
}
func TestDocumentAdapter(t *testing.T) {
s, uri := newAdapterServer(t)
a := &documentAdapter{server: s}
doc := a.GetDocument(uri)
if doc == nil {
t.Fatal("GetDocument returned nil for open document")
}
if doc.URI != uri || doc.Content != indexCoverageSrc || doc.AST == nil {
t.Errorf("converted document = %+v", doc)
}
if a.GetDocument("file:///nope.gsx") != nil {
t.Error("GetDocument for unopened URI should be nil")
}
all := a.AllDocuments()
if len(all) != 1 || all[0].URI != uri {
t.Errorf("AllDocuments = %+v, want one entry for %s", all, uri)
}
}
func TestWorkspaceASTAdapter(t *testing.T) {
s, _ := newAdapterServer(t)
a := &workspaceASTAdapter{server: s}
wsURI := "file:///workspace-only.gsx"
wsDoc := parseTestDoc(routerCoverageSrc)
s.workspaceASTsMu.Lock()
s.workspaceASTs[wsURI] = wsDoc.AST
s.workspaceASTsMu.Unlock()
if got := a.GetWorkspaceAST(wsURI); got != wsDoc.AST {
t.Error("GetWorkspaceAST did not return the cached AST")
}
if a.GetWorkspaceAST("file:///nope.gsx") != nil {
t.Error("GetWorkspaceAST for unknown URI should be nil")
}
all := a.AllWorkspaceASTs()
if len(all) != 1 || all[wsURI] != wsDoc.AST {
t.Errorf("AllWorkspaceASTs = %v, want copy containing %s", all, wsURI)
}
delete(all, wsURI)
if a.GetWorkspaceAST(wsURI) == nil {
t.Error("mutating the returned map changed server state")
}
}
func TestVirtualFileAdapter(t *testing.T) {
s, uri := newAdapterServer(t)
a := &virtualFileAdapter{server: s}
if a.GetVirtualFile(uri) != nil {
t.Error("expected nil virtual file before caching")
}
s.virtualFiles.Put(uri, gopls.TuiURIToGoURI(uri), "package main", nil, 1)
cached := a.GetVirtualFile(uri)
if cached == nil {
t.Fatal("expected cached virtual file")
}
if cached.Content != "package main" {
t.Errorf("Content = %q", cached.Content)
}
}
func TestFunctionNameCheckerAdapter(t *testing.T) {
type tc struct {
name string
want bool
}
tests := map[string]tc{
"indexed function": {name: "format", want: true},
"go builtin": {name: "len", want: true},
"unknown": {name: "mystery", want: false},
}
s, _ := newAdapterServer(t)
a := &functionNameCheckerAdapter{server: s}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := a.IsFunctionName(tt.name); got != tt.want {
t.Errorf("IsFunctionName(%q) = %v, want %v", tt.name, got, tt.want)
}
})
}
}
type stubSemanticProvider struct {
result *provider.SemanticTokens
err error
}
func (s *stubSemanticProvider) SemanticTokensFull(*provider.Document) (*provider.SemanticTokens, error) {
return s.result, s.err
}
func TestSemanticTokensProviderAdapter(t *testing.T) {
doc := &Document{URI: "file:///s.gsx", Content: "package main"}
t.Run("nil result becomes empty tokens", func(t *testing.T) {
a := newSemanticTokensProviderAdapter(&stubSemanticProvider{result: nil})
got, err := a.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got == nil || got.Data == nil || len(got.Data) != 0 {
t.Errorf("got %+v, want empty token data", got)
}
})
t.Run("error is propagated", func(t *testing.T) {
a := newSemanticTokensProviderAdapter(&stubSemanticProvider{err: errors.New("sem boom")})
got, err := a.SemanticTokensFull(doc)
if err == nil || !strings.Contains(err.Error(), "sem boom") {
t.Fatalf("error = %v, want sem boom", err)
}
if got != nil {
t.Errorf("result = %+v, want nil on error", got)
}
})
}
|