.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / internal / lsp / completion_test.go
1 // Copyright 2019 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 lsp
6
7 import (
8         "strings"
9         "testing"
10
11         "golang.org/x/tools/internal/lsp/protocol"
12         "golang.org/x/tools/internal/lsp/source"
13         "golang.org/x/tools/internal/lsp/tests"
14         "golang.org/x/tools/internal/span"
15 )
16
17 func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
18         got := r.callCompletion(t, src, func(opts *source.Options) {
19                 opts.DeepCompletion = false
20                 opts.Matcher = source.CaseInsensitive
21                 opts.CompleteUnimported = false
22                 opts.InsertTextFormat = protocol.SnippetTextFormat
23                 if !strings.Contains(string(src.URI()), "literal") {
24                         opts.LiteralCompletions = false
25                 }
26         })
27         got = tests.FilterBuiltins(src, got)
28         want := expected(t, test, items)
29         if diff := tests.DiffCompletionItems(want, got); diff != "" {
30                 t.Errorf("%s", diff)
31         }
32 }
33
34 func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.CompletionSnippet, placeholders bool, items tests.CompletionItems) {
35         list := r.callCompletion(t, src, func(opts *source.Options) {
36                 opts.UsePlaceholders = placeholders
37                 opts.DeepCompletion = true
38                 opts.Matcher = source.Fuzzy
39                 opts.CompleteUnimported = false
40         })
41         got := tests.FindItem(list, *items[expected.CompletionItem])
42         want := expected.PlainSnippet
43         if placeholders {
44                 want = expected.PlaceholderSnippet
45         }
46         if diff := tests.DiffSnippets(want, got); diff != "" {
47                 t.Errorf("%s", diff)
48         }
49 }
50
51 func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
52         got := r.callCompletion(t, src, func(opts *source.Options) {})
53         got = tests.FilterBuiltins(src, got)
54         want := expected(t, test, items)
55         if diff := tests.CheckCompletionOrder(want, got, false); diff != "" {
56                 t.Errorf("%s", diff)
57         }
58 }
59
60 func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
61         got := r.callCompletion(t, src, func(opts *source.Options) {
62                 opts.DeepCompletion = true
63                 opts.Matcher = source.CaseInsensitive
64                 opts.CompleteUnimported = false
65         })
66         got = tests.FilterBuiltins(src, got)
67         want := expected(t, test, items)
68         if msg := tests.DiffCompletionItems(want, got); msg != "" {
69                 t.Errorf("%s", msg)
70         }
71 }
72
73 func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
74         got := r.callCompletion(t, src, func(opts *source.Options) {
75                 opts.DeepCompletion = true
76                 opts.Matcher = source.Fuzzy
77                 opts.CompleteUnimported = false
78         })
79         got = tests.FilterBuiltins(src, got)
80         want := expected(t, test, items)
81         if msg := tests.DiffCompletionItems(want, got); msg != "" {
82                 t.Errorf("%s", msg)
83         }
84 }
85
86 func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
87         got := r.callCompletion(t, src, func(opts *source.Options) {
88                 opts.Matcher = source.CaseSensitive
89                 opts.CompleteUnimported = false
90         })
91         got = tests.FilterBuiltins(src, got)
92         want := expected(t, test, items)
93         if msg := tests.DiffCompletionItems(want, got); msg != "" {
94                 t.Errorf("%s", msg)
95         }
96 }
97
98 func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
99         got := r.callCompletion(t, src, func(opts *source.Options) {
100                 opts.DeepCompletion = true
101                 opts.Matcher = source.Fuzzy
102                 opts.CompleteUnimported = false
103                 opts.LiteralCompletions = true
104         })
105         want := expected(t, test, items)
106         if msg := tests.CheckCompletionOrder(want, got, true); msg != "" {
107                 t.Errorf("%s", msg)
108         }
109 }
110
111 func expected(t *testing.T, test tests.Completion, items tests.CompletionItems) []protocol.CompletionItem {
112         t.Helper()
113
114         var want []protocol.CompletionItem
115         for _, pos := range test.CompletionItems {
116                 item := items[pos]
117                 want = append(want, tests.ToProtocolCompletionItem(*item))
118         }
119         return want
120 }
121
122 func (r *runner) callCompletion(t *testing.T, src span.Span, options func(*source.Options)) []protocol.CompletionItem {
123         t.Helper()
124
125         view, err := r.server.session.ViewOf(src.URI())
126         if err != nil {
127                 t.Fatal(err)
128         }
129         original := view.Options()
130         modified := view.Options().Clone()
131         options(modified)
132         view, err = view.SetOptions(r.ctx, modified)
133         if err != nil {
134                 t.Error(err)
135                 return nil
136         }
137         defer view.SetOptions(r.ctx, original)
138
139         list, err := r.server.Completion(r.ctx, &protocol.CompletionParams{
140                 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
141                         TextDocument: protocol.TextDocumentIdentifier{
142                                 URI: protocol.URIFromSpanURI(src.URI()),
143                         },
144                         Position: protocol.Position{
145                                 Line:      uint32(src.Start().Line() - 1),
146                                 Character: uint32(src.Start().Column() - 1),
147                         },
148                 },
149         })
150         if err != nil {
151                 t.Fatal(err)
152         }
153         return list.Items
154 }