Skip to content

Commit

Permalink
gopls/internal/regtest/marker: port remaining completion tests
Browse files Browse the repository at this point in the history
The following changes were made to test data:

- anon.txt, complit.txt: added additional literal completion results.
- errors.txt: copied in part of the 'types' package.

As described in CL 538797, porting completion tests is a blocker for
zero config gopls.

For golang/go#57979
For golang/go#54845

Change-Id: I02a0c15a14ea36bea4ecc55664f95a2d635aaa53
Reviewed-on: https://go-review.googlesource.com/c/tools/+/538800
Reviewed-by: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
findleyr committed Nov 2, 2023
1 parent 5e2efda commit 26349af
Show file tree
Hide file tree
Showing 24 changed files with 253 additions and 130 deletions.
17 changes: 0 additions & 17 deletions gopls/internal/lsp/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package lsp

import (
"fmt"
"strings"
"testing"

"golang.org/x/tools/gopls/internal/lsp/protocol"
Expand All @@ -16,22 +15,6 @@ import (
"golang.org/x/tools/gopls/internal/span"
)

func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
got := r.callCompletion(t, src, func(opts *source.Options) {
opts.DeepCompletion = false
opts.Matcher = source.CaseInsensitive
opts.CompleteUnimported = false
opts.InsertTextFormat = protocol.SnippetTextFormat
opts.LiteralCompletions = strings.Contains(string(src.URI()), "literal")
opts.ExperimentalPostfixCompletions = strings.Contains(string(src.URI()), "postfix")
})
got = tests.FilterBuiltins(src, got)
want := expected(t, test, items)
if diff := tests.DiffCompletionItems(want, got); diff != "" {
t.Errorf("mismatching completion items (-want +got):\n%s", diff)
}
}

func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.CompletionSnippet, placeholders bool, items tests.CompletionItems) {
list := r.callCompletion(t, src, func(opts *source.Options) {
opts.UsePlaceholders = placeholders
Expand Down
23 changes: 0 additions & 23 deletions gopls/internal/lsp/testdata/anon/anon.go.in

This file was deleted.

10 changes: 0 additions & 10 deletions gopls/internal/lsp/testdata/errors/errors.go

This file was deleted.

8 changes: 0 additions & 8 deletions gopls/internal/lsp/testdata/issues/issue56505.go

This file was deleted.

5 changes: 2 additions & 3 deletions gopls/internal/lsp/testdata/summary.txt.golden
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
-- summary --
CallHierarchyCount = 2
CompletionsCount = 72
CompletionSnippetCount = 18
RankedCompletionsCount = 56
CompletionSnippetCount = 9
RankedCompletionsCount = 45
SemanticTokenCount = 3
SuggestedFixCount = 80
MethodExtractionCount = 8
Expand Down
6 changes: 0 additions & 6 deletions gopls/internal/lsp/testdata/unresolved/unresolved.go.in

This file was deleted.

21 changes: 0 additions & 21 deletions gopls/internal/lsp/testdata/variadic/variadic_intf.go

This file was deleted.

17 changes: 2 additions & 15 deletions gopls/internal/lsp/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var UpdateGolden = flag.Bool("golden", false, "Update golden files")
// type in the field name and the make() expression.
type CallHierarchy = map[span.Span]*CallHierarchyResult
type CompletionItems = map[token.Pos]*completion.CompletionItem
type Completions = map[span.Span][]Completion
type CompletionSnippets = map[span.Span][]CompletionSnippet
type RankCompletions = map[span.Span][]Completion
type SemanticTokens = []span.Span
Expand All @@ -69,7 +68,6 @@ type Data struct {
Exported *packagestest.Exported
CallHierarchy CallHierarchy
CompletionItems CompletionItems
Completions Completions
CompletionSnippets CompletionSnippets
RankCompletions RankCompletions
SemanticTokens SemanticTokens
Expand Down Expand Up @@ -99,7 +97,6 @@ type Data struct {
// we can abolish the interface now.
type Tests interface {
CallHierarchy(*testing.T, span.Span, *CallHierarchyResult)
Completion(*testing.T, span.Span, Completion, CompletionItems)
CompletionSnippet(*testing.T, span.Span, CompletionSnippet, bool, CompletionItems)
RankCompletion(*testing.T, span.Span, Completion, CompletionItems)
SemanticTokens(*testing.T, span.Span)
Expand All @@ -115,7 +112,7 @@ type CompletionTestType int

const (
// Default runs the standard completion tests.
CompletionDefault = CompletionTestType(iota)
_ = CompletionTestType(iota)

// CompletionRank candidates in test must be valid and in the right relative order.
CompletionRank
Expand Down Expand Up @@ -206,7 +203,6 @@ func load(t testing.TB, mode string, dir string) *Data {
datum := &Data{
CallHierarchy: make(CallHierarchy),
CompletionItems: make(CompletionItems),
Completions: make(Completions),
CompletionSnippets: make(CompletionSnippets),
RankCompletions: make(RankCompletions),
Renames: make(Renames),
Expand Down Expand Up @@ -346,7 +342,6 @@ func load(t testing.TB, mode string, dir string) *Data {
// Collect any data that needs to be used by subsequent tests.
if err := datum.Exported.Expect(map[string]interface{}{
"item": datum.collectCompletionItems,
"complete": datum.collectCompletions(CompletionDefault),
"rank": datum.collectCompletions(CompletionRank),
"snippet": datum.collectCompletionSnippets,
"semantic": datum.collectSemanticTokens,
Expand Down Expand Up @@ -439,11 +434,6 @@ func Run(t *testing.T, tests Tests, data *Data) {
}
})

t.Run("Completion", func(t *testing.T) {
t.Helper()
eachCompletion(t, data.Completions, tests.Completion)
})

t.Run("CompletionSnippets", func(t *testing.T) {
t.Helper()
for _, placeholders := range []bool{true, false} {
Expand Down Expand Up @@ -575,7 +565,6 @@ func checkData(t *testing.T, data *Data) {
}

fmt.Fprintf(buf, "CallHierarchyCount = %v\n", len(data.CallHierarchy))
fmt.Fprintf(buf, "CompletionsCount = %v\n", countCompletions(data.Completions))
fmt.Fprintf(buf, "CompletionSnippetCount = %v\n", snippetCount)
fmt.Fprintf(buf, "RankedCompletionsCount = %v\n", countCompletions(data.RankCompletions))
fmt.Fprintf(buf, "SemanticTokenCount = %v\n", len(data.SemanticTokens))
Expand Down Expand Up @@ -676,9 +665,7 @@ func (data *Data) collectCompletions(typ CompletionTestType) func(span.Span, []t
result(data.RankCompletions, src, expected)
}
default:
return func(src span.Span, expected []token.Pos) {
result(data.Completions, src, expected)
}
panic("unsupported")
}
}

Expand Down
37 changes: 37 additions & 0 deletions gopls/internal/regtest/marker/testdata/completion/anon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
This test checks completion related to anonymous structs.

-- flags --
-ignore_extra_diags

-- settings.json --
{
"deepCompletion": false
}

-- anon.go --
package anon

// Literal completion results.
/* int() */ //@item(int, "int()", "int", "var")

func _() {
for _, _ := range []struct {
i, j int //@item(anonI, "i", "int", "field"),item(anonJ, "j", "int", "field")
}{
{
i: 1,
//@complete("", anonJ)
},
{
//@complete("", anonI, anonJ, int)
},
} {
continue
}

s := struct{ f int }{ } //@item(anonF, "f", "int", "field"),item(structS, "s", "struct{...}", "var"),complete(" }", anonF, int)

_ = map[struct{ x int }]int{ //@item(anonX, "x", "int", "field")
struct{ x int }{ }: 1, //@complete(" }", anonX, int, structS)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
This test checks completion related to basic literals.

-- flags --
-ignore_extra_diags

-- basiclit.go --
package basiclit

func _() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
This test checks completion related to casts.

-- flags --
-ignore_extra_diags

-- cast.go --
package cast

func _() {
Expand All @@ -8,4 +14,4 @@ func _() {
func _() {
foo := struct{x int}{x: 1}
_ = float64(foo. //@complete(" /", x_field)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
This test checks completion related to channels.

-- flags --
-ignore_extra_diags

-- settings.json --
{
"completeUnimported": false
}

-- channel.go --
package channel

func _() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
This test checks completion related to composite literals.

-- flags --
-ignore_extra_diags

-- settings.json --
{
"completeUnimported": false
}

-- complit.go --
package complit

// Literal completion results.
/* int() */ //@item(int, "int()", "int", "var")

// general completions

type position struct { //@item(structPosition, "position", "struct{...}", "struct")
Expand All @@ -8,7 +22,7 @@ type position struct { //@item(structPosition, "position", "struct{...}", "struc

func _() {
_ = position{
//@complete("", fieldX, fieldY, structPosition)
//@complete("", fieldX, fieldY, int, structPosition)
}
_ = position{
X: 1,
Expand All @@ -20,7 +34,7 @@ func _() {
}
_ = []*position{
{
//@complete("", fieldX, fieldY, structPosition)
//@complete("", fieldX, fieldY, int, structPosition)
},
}
}
Expand All @@ -36,15 +50,15 @@ func _() {
}

_ = map[int]int{
//@complete("", abVar, aaVar, structPosition)
//@complete("", abVar, int, aaVar, structPosition)
}

_ = []string{a: ""} //@complete(":", abVar, aaVar)
_ = [1]string{a: ""} //@complete(":", abVar, aaVar)

_ = position{X: a} //@complete("}", abVar, aaVar)
_ = position{a} //@complete("}", abVar, aaVar)
_ = position{a, } //@complete("}", abVar, aaVar, structPosition)
_ = position{a, } //@complete("}", abVar, int, aaVar, structPosition)

_ = []int{a} //@complete("}", abVar, aaVar)
_ = [1]int{a} //@complete("}", abVar, aaVar)
Expand Down Expand Up @@ -73,18 +87,18 @@ func _() {
func _() {
type foo struct{} //@item(complitFoo, "foo", "struct{...}", "struct")

var _ *foo = &fo{} //@snippet("{", complitFoo, "foo", "foo")
var _ *foo = fo{} //@snippet("{", complitFoo, "&foo", "&foo")
var _ *foo = &fo{} //@snippet("{", complitFoo, "foo")
var _ *foo = fo{} //@snippet("{", complitFoo, "&foo")

struct { a, b *foo }{
a: &fo{}, //@rank("{", complitFoo)
b: fo{}, //@snippet("{", complitFoo, "&foo", "&foo")
b: fo{}, //@snippet("{", complitFoo, "&foo")
}
}

func _() {
_ := position{
X: 1, //@complete("X", fieldX),complete(" 1", structPosition)
Y: , //@complete(":", fieldY),complete(" ,", structPosition)
X: 1, //@complete("X", fieldX),complete(" 1", int, structPosition)
Y: , //@complete(":", fieldY),complete(" ,", int, structPosition)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
This test checks completion related to constants.

-- flags --
-ignore_extra_diags

-- constant.go --
package constant

const x = 1 //@item(constX, "x", "int", "const")
Expand Down
33 changes: 33 additions & 0 deletions gopls/internal/regtest/marker/testdata/completion/errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
This test checks completion related to errors.

-- flags --
-ignore_extra_diags

-- settings.json --
{
"deepCompletion": false
}

-- go.mod --
module golang.org/lsptests

go 1.18

-- errors.go --
package errors

import (
"golang.org/lsptests/types"
)

func _() {
bob.Bob() //@complete(".")
types.b //@complete(" //", Bob_interface)
}

-- types/types.go --
package types

type Bob interface { //@item(Bob_interface, "Bob", "interface{...}", "interface")
Bobby()
}
Loading

0 comments on commit 26349af

Please sign in to comment.