Skip to content

Commit 28dd671

Browse files
committed
Rename tests
1 parent b7dc8db commit 28dd671

File tree

7 files changed

+609
-42
lines changed

7 files changed

+609
-42
lines changed

internal/fourslash/baselineutil.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ func (f *FourslashTest) getBaselineForLocationsWithFileContents(spans []lsproto.
122122
return lsptestutil.GetBaselineForLocationsWithFileContents(f.FS, spans, options)
123123
}
124124

125-
func (f *FourslashTest) getBaselineForGroupedLocationsWithFileContents(groupedRanges *collections.MultiMap[lsproto.DocumentUri, lsproto.Range], options lsptestutil.BaselineLocationsOptions) string {
126-
return lsptestutil.GetBaselineForGroupedLocationsWithFileContents(f.FS, groupedRanges, options)
127-
}
128-
129125
type markerAndItem[T any] struct {
130126
Marker *Marker `json:"marker"`
131127
Item T `json:"item"`

internal/fourslash/fourslash.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,19 +1861,6 @@ func (f *FourslashTest) verifyBaselineRename(
18611861
t.Fatalf(prefix+"Unexpected rename response type: %T", resMsg.AsResponse().Result)
18621862
}
18631863

1864-
var changes map[lsproto.DocumentUri][]*lsproto.TextEdit
1865-
if result.WorkspaceEdit != nil && result.WorkspaceEdit.Changes != nil {
1866-
changes = *result.WorkspaceEdit.Changes
1867-
}
1868-
locationToText := map[lsproto.Location]string{}
1869-
fileToRange := collections.MultiMap[lsproto.DocumentUri, lsproto.Range]{}
1870-
for uri, edits := range changes {
1871-
for _, edit := range edits {
1872-
fileToRange.Add(uri, edit.Range)
1873-
locationToText[lsproto.Location{Uri: uri, Range: edit.Range}] = edit.NewText
1874-
}
1875-
}
1876-
18771864
var renameOptions strings.Builder
18781865
if preferences != nil {
18791866
if preferences.UseAliasesForRename != core.TSUnknown {
@@ -1884,29 +1871,10 @@ func (f *FourslashTest) verifyBaselineRename(
18841871
}
18851872
}
18861873

1887-
baselineFileContent := f.getBaselineForGroupedLocationsWithFileContents(
1888-
&fileToRange,
1889-
lsptestutil.BaselineLocationsOptions{
1890-
Marker: markerOrRange,
1891-
MarkerName: "/*RENAME*/",
1892-
EndMarker: "RENAME|]",
1893-
StartMarkerPrefix: func(span lsproto.Location) *string {
1894-
text := locationToText[span]
1895-
prefixAndSuffix := strings.Split(text, "?")
1896-
if prefixAndSuffix[0] != "" {
1897-
return ptrTo("/*START PREFIX*/" + prefixAndSuffix[0])
1898-
}
1899-
return nil
1900-
},
1901-
EndMarkerSuffix: func(span lsproto.Location) *string {
1902-
text := locationToText[span]
1903-
prefixAndSuffix := strings.Split(text, "?")
1904-
if prefixAndSuffix[1] != "" {
1905-
return ptrTo(prefixAndSuffix[1] + "/*END SUFFIX*/")
1906-
}
1907-
return nil
1908-
},
1909-
},
1874+
baselineFileContent := lsptestutil.GetBaselineForRename(
1875+
f.FS,
1876+
result,
1877+
lsptestutil.BaselineLocationsOptions{Marker: markerOrRange},
19101878
)
19111879

19121880
var baselineResult string
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package lspservertests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/bundled"
7+
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
8+
"github.com/microsoft/typescript-go/internal/testutil/lsptestutil"
9+
"github.com/microsoft/typescript-go/internal/testutil/stringtestutil"
10+
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
11+
)
12+
13+
func TestRename(t *testing.T) {
14+
t.Parallel()
15+
16+
if !bundled.Embedded {
17+
t.Skip("bundled files are not embedded")
18+
}
19+
20+
testCases := []*lspServerTest{
21+
{
22+
subScenario: "ancestor and project ref management",
23+
files: func() map[string]any {
24+
return map[string]any{
25+
"/user/username/projects/temp/temp.ts": "let x = 10",
26+
"/user/username/projects/temp/tsconfig.json": "{}",
27+
"/user/username/projects/container/lib/tsconfig.json": stringtestutil.Dedent(`
28+
{
29+
"compilerOptions": {
30+
"composite": true,
31+
},
32+
references: [],
33+
files: [
34+
"index.ts",
35+
],
36+
}`),
37+
"/user/username/projects/container/lib/index.ts": stringtestutil.Dedent(`
38+
export const myConst = 30;`),
39+
"/user/username/projects/container/exec/tsconfig.json": stringtestutil.Dedent(`
40+
{
41+
"files": ["./index.ts"],
42+
"references": [
43+
{ "path": "../lib" },
44+
],
45+
}`),
46+
"/user/username/projects/container/exec/index.ts": stringtestutil.Dedent(`
47+
import { myConst } from "../lib";
48+
export function getMyConst() {
49+
return myConst;
50+
}`),
51+
"/user/username/projects/container/compositeExec/tsconfig.json": stringtestutil.Dedent(`
52+
{
53+
"compilerOptions": {
54+
"composite": true,
55+
},
56+
"files": ["./index.ts"],
57+
"references": [
58+
{ "path": "../lib" },
59+
],
60+
}`),
61+
"/user/username/projects/container/compositeExec/index.ts": stringtestutil.Dedent(`
62+
import { myConst } from "../lib";
63+
export function getMyConst() {
64+
return myConst;
65+
}`),
66+
"/user/username/projects/container/tsconfig.json": stringtestutil.Dedent(`
67+
{
68+
"files": [],
69+
"include": [],
70+
"references": [
71+
{ "path": "./exec" },
72+
{ "path": "./compositeExec" },
73+
],
74+
}`),
75+
}
76+
},
77+
test: func(server *testServer) {
78+
file := "/user/username/projects/container/compositeExec/index.ts"
79+
temp := "/user/username/projects/temp/temp.ts"
80+
server.openFile(file, lsproto.LanguageKindTypeScript)
81+
// Open temp file and verify all projects alive
82+
server.openFile(temp, lsproto.LanguageKindTypeScript)
83+
84+
// Ref projects are loaded after as part of this command
85+
server.baselineRename(file, lsptestutil.PositionToLineAndCharacter(file, server.content(file), "myConst", 0))
86+
87+
// Open temp file and verify all projects alive
88+
server.closeFile(temp)
89+
server.openFile(temp, lsproto.LanguageKindTypeScript)
90+
91+
// Close all files and open temp file, only inferred project should be alive
92+
server.closeFile(file)
93+
server.closeFile(temp)
94+
server.openFile(temp, lsproto.LanguageKindTypeScript)
95+
},
96+
},
97+
{
98+
subScenario: "rename in common file renames all project",
99+
files: func() map[string]any {
100+
return map[string]any{
101+
"/users/username/projects/a/a.ts": stringtestutil.Dedent(`
102+
import {C} from "./c/fc";
103+
console.log(C)
104+
`),
105+
"/users/username/projects/a/tsconfig.json": "{}",
106+
"/users/username/projects/a/c": vfstest.Symlink("/users/username/projects/c"),
107+
"/users/username/projects/b/b.ts": stringtestutil.Dedent(`
108+
import {C} from "../c/fc";
109+
console.log(C)
110+
`),
111+
"/users/username/projects/b/tsconfig.json": "{}",
112+
"/users/username/projects/b/c": vfstest.Symlink("/users/username/projects/c"),
113+
"/users/username/projects/c/fc.ts": stringtestutil.Dedent(`
114+
export const C = 42;
115+
`),
116+
}
117+
},
118+
test: func(server *testServer) {
119+
aTs := "/users/username/projects/a/a.ts"
120+
bTs := "/users/username/projects/b/b.ts"
121+
aFc := "/users/username/projects/a/c/fc.ts"
122+
bFc := "/users/username/projects/b/c/fc.ts"
123+
server.openFile(aTs, lsproto.LanguageKindTypeScript)
124+
server.openFile(bTs, lsproto.LanguageKindTypeScript)
125+
cContent := server.content("/users/username/projects/c/fc.ts")
126+
server.openFileWithContent(aFc, cContent, lsproto.LanguageKindTypeScript)
127+
server.openFileWithContent(bFc, cContent, lsproto.LanguageKindTypeScript)
128+
server.baselineRename(aFc, lsptestutil.PositionToLineAndCharacter(aFc, cContent, "C", 0))
129+
},
130+
},
131+
}
132+
133+
for _, test := range testCases {
134+
test.run(t, "rename")
135+
}
136+
}

internal/lsp/lspservertests/testserver.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ func (s *testServer) baselineReferences(fileName string, position lsproto.Positi
210210
}) + "\n")
211211
}
212212

213+
func (s *testServer) baselineRename(fileName string, position lsproto.Position) {
214+
s.t.Helper()
215+
result := sendRequest(s.t, s, lsproto.TextDocumentRenameInfo, &lsproto.RenameParams{
216+
TextDocument: lsproto.TextDocumentIdentifier{
217+
Uri: lsproto.DocumentUri("file://" + fileName),
218+
},
219+
Position: position,
220+
NewName: "?",
221+
})
222+
s.baseline.WriteString(lsptestutil.GetBaselineForRename(s.server.FS, result, lsptestutil.BaselineLocationsOptions{
223+
Marker: &marker{fileName, position},
224+
OpenFiles: s.openFiles,
225+
}) + "\n")
226+
}
227+
213228
type marker struct {
214229
fileName string
215230
position lsproto.Position

internal/testutil/lsptestutil/baseline.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,52 @@ type BaselineLocationsOptions struct {
3636
OpenFiles map[string]string
3737
}
3838

39+
func GetBaselineForRename(
40+
fs vfs.FS,
41+
result lsproto.WorkspaceEditOrNull,
42+
options BaselineLocationsOptions,
43+
) string {
44+
var changes map[lsproto.DocumentUri][]*lsproto.TextEdit
45+
if result.WorkspaceEdit != nil && result.WorkspaceEdit.Changes != nil {
46+
changes = *result.WorkspaceEdit.Changes
47+
}
48+
locationToText := map[lsproto.Location]string{}
49+
fileToRange := collections.MultiMap[lsproto.DocumentUri, lsproto.Range]{}
50+
for uri, edits := range changes {
51+
for _, edit := range edits {
52+
fileToRange.Add(uri, edit.Range)
53+
locationToText[lsproto.Location{Uri: uri, Range: edit.Range}] = edit.NewText
54+
}
55+
}
56+
57+
return getBaselineForGroupedLocationsWithFileContents(
58+
fs,
59+
&fileToRange,
60+
BaselineLocationsOptions{
61+
Marker: options.Marker,
62+
MarkerName: "/*RENAME*/",
63+
EndMarker: "RENAME|]",
64+
StartMarkerPrefix: func(span lsproto.Location) *string {
65+
text := locationToText[span]
66+
prefixAndSuffix := strings.Split(text, "?")
67+
if prefixAndSuffix[0] != "" {
68+
return ptrTo("/*START PREFIX*/" + prefixAndSuffix[0])
69+
}
70+
return nil
71+
},
72+
EndMarkerSuffix: func(span lsproto.Location) *string {
73+
text := locationToText[span]
74+
prefixAndSuffix := strings.Split(text, "?")
75+
if prefixAndSuffix[1] != "" {
76+
return ptrTo(prefixAndSuffix[1] + "/*END SUFFIX*/")
77+
}
78+
return nil
79+
},
80+
OpenFiles: options.OpenFiles,
81+
},
82+
)
83+
}
84+
3985
func GetBaselineForLocationsWithFileContents(
4086
f vfs.FS,
4187
spans []lsproto.Location,
@@ -48,14 +94,14 @@ func GetBaselineForLocationsWithFileContents(
4894
rangesByFile.Add(file, loc.Range)
4995
}
5096
}
51-
return GetBaselineForGroupedLocationsWithFileContents(
97+
return getBaselineForGroupedLocationsWithFileContents(
5298
f,
5399
&rangesByFile,
54100
options,
55101
)
56102
}
57103

58-
func GetBaselineForGroupedLocationsWithFileContents(
104+
func getBaselineForGroupedLocationsWithFileContents(
59105
f vfs.FS,
60106
groupedRanges *collections.MultiMap[lsproto.DocumentUri, lsproto.Range],
61107
options BaselineLocationsOptions,

0 commit comments

Comments
 (0)