Skip to content

Commit

Permalink
gopls/internal/regtest/marker: port remaining rename tests
Browse files Browse the repository at this point in the history
For golang/go#54845

Change-Id: I6d5490703b50dbf944c170384f7f41311e5229bc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/539665
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Nov 9, 2023
1 parent ef85672 commit 80e691c
Show file tree
Hide file tree
Showing 50 changed files with 609 additions and 1,428 deletions.
62 changes: 0 additions & 62 deletions gopls/internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -295,67 +294,6 @@ func (r *runner) InlayHints(t *testing.T, spn span.Span) {
}
}

func (r *runner) Rename(t *testing.T, spn span.Span, newText string) {
tag := fmt.Sprintf("%s-rename", newText)

uri := spn.URI()
filename := uri.Filename()
sm, err := r.data.Mapper(uri)
if err != nil {
t.Fatal(err)
}
loc, err := sm.SpanLocation(spn)
if err != nil {
t.Fatalf("failed for %v: %v", spn, err)
}

wedit, err := r.server.Rename(r.ctx, &protocol.RenameParams{
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
Position: loc.Range.Start,
NewName: newText,
})
if err != nil {
renamed := string(r.data.Golden(t, tag, filename, func() ([]byte, error) {
return []byte(err.Error()), nil
}))
if err.Error() != renamed {
t.Errorf("%s: rename failed for %s, expected:\n%v\ngot:\n%v\n", spn, newText, renamed, err)
}
return
}
res, err := applyTextDocumentEdits(r, wedit.DocumentChanges)
if err != nil {
t.Fatal(err)
}
var orderedURIs []string
for uri := range res {
orderedURIs = append(orderedURIs, string(uri))
}
sort.Strings(orderedURIs)

// Print the name and content of each modified file,
// concatenated, and compare against the golden.
var buf bytes.Buffer
for i := 0; i < len(res); i++ {
if i != 0 {
buf.WriteByte('\n')
}
uri := span.URIFromURI(orderedURIs[i])
if len(res) > 1 {
buf.WriteString(filepath.Base(uri.Filename()))
buf.WriteString(":\n")
}
buf.Write(res[uri])
}
got := buf.Bytes()
want := r.data.Golden(t, tag, filename, func() ([]byte, error) {
return got, nil
})
if diff := compare.Bytes(want, got); diff != "" {
t.Errorf("rename failed for %s:\n%s", newText, diff)
}
}

func applyTextDocumentEdits(r *runner, edits []protocol.DocumentChanges) (map[span.URI][]byte, error) {
res := make(map[span.URI][]byte)
for _, docEdits := range edits {
Expand Down
Loading

0 comments on commit 80e691c

Please sign in to comment.