Skip to content

Commit 8c9f2c2

Browse files
committed
Add a few more tests and fix edit validation
1 parent ae54aca commit 8c9f2c2

File tree

1 file changed

+212
-29
lines changed

1 file changed

+212
-29
lines changed

src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CohostRenameEndpointTest.cs

Lines changed: 212 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
57
using System.Linq;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -231,6 +233,195 @@ The end.
231233
expected: "",
232234
fileKind: RazorFileKind.Legacy);
233235

236+
[Fact]
237+
public Task Component_WithContent()
238+
=> VerifyRenamesAsync(
239+
input: $"""
240+
This is a Razor document.
241+
242+
<Component>Hello</Compon$$ent>
243+
<Component>
244+
Hello
245+
</Component>
246+
247+
The end.
248+
""",
249+
additionalFiles: [
250+
(FilePath("Component.razor"), "")
251+
],
252+
newName: "DifferentName",
253+
expected: """
254+
This is a Razor document.
255+
256+
<DifferentName>Hello</DifferentName>
257+
<DifferentName>
258+
Hello
259+
</DifferentName>
260+
261+
The end.
262+
""",
263+
renames: [
264+
("Component.razor", "DifferentName.razor")
265+
]);
266+
267+
[Fact]
268+
public Task Component_WithContent_FullyQualified()
269+
=> VerifyRenamesAsync(
270+
input: $"""
271+
This is a Razor document.
272+
273+
<My.Namespace.Component>Hello</My.Namespace.Compon$$ent>
274+
<My.Namespace.Component>
275+
Hello
276+
</My.Namespace.Component>
277+
278+
The end.
279+
""",
280+
additionalFiles: [
281+
(FilePath("Component.razor"), """
282+
@namespace My.Namespace
283+
""")
284+
],
285+
newName: "DifferentName",
286+
expected: """
287+
This is a Razor document.
288+
289+
<My.Namespace.DifferentName>Hello</My.Namespace.DifferentName>
290+
<My.Namespace.DifferentName>
291+
Hello
292+
</My.Namespace.DifferentName>
293+
294+
The end.
295+
""",
296+
renames: [
297+
("Component.razor", "DifferentName.razor")
298+
]);
299+
300+
[Fact]
301+
public Task Component_MultipleUsage()
302+
=> VerifyRenamesAsync(
303+
input: $"""
304+
This is a Razor document.
305+
306+
<Comp$$onent />
307+
<Component></Component>
308+
<Component>
309+
</Component>
310+
311+
The end.
312+
""",
313+
additionalFiles: [
314+
(FilePath("Component.razor"), ""),
315+
(FilePath("OtherComponent.razor"), """
316+
<Component />
317+
<Component></Component>
318+
<Component>
319+
</Component>
320+
""")
321+
],
322+
newName: "DifferentName",
323+
expected: """
324+
This is a Razor document.
325+
326+
<DifferentName />
327+
<DifferentName></DifferentName>
328+
<DifferentName>
329+
</DifferentName>
330+
331+
The end.
332+
""",
333+
renames: [
334+
("Component.razor", "DifferentName.razor")
335+
],
336+
additionalExpectedFiles: [
337+
(FileUri("OtherComponent.razor"), """
338+
<DifferentName />
339+
<DifferentName></DifferentName>
340+
<DifferentName>
341+
</DifferentName>
342+
""")
343+
]);
344+
345+
[Fact]
346+
public Task Component_FullyQualified()
347+
=> VerifyRenamesAsync(
348+
input: $"""
349+
This is a Razor document.
350+
351+
<My.Namespace.Comp$$onent />
352+
<My.Namespace.Component></My.Namespace.Component>
353+
<My.Namespace.Component>
354+
</My.Namespace.Component>
355+
356+
The end.
357+
""",
358+
additionalFiles: [
359+
(FilePath("Component.razor"), """
360+
@namespace My.Namespace
361+
""")
362+
],
363+
newName: "DifferentName",
364+
expected: """
365+
This is a Razor document.
366+
367+
<My.Namespace.DifferentName />
368+
<My.Namespace.DifferentName></My.Namespace.DifferentName>
369+
<My.Namespace.DifferentName>
370+
</My.Namespace.DifferentName>
371+
372+
The end.
373+
""",
374+
renames: [
375+
("Component.razor", "DifferentName.razor")
376+
]);
377+
378+
[Fact]
379+
public Task Component_MultipleUsage_FullyQualified()
380+
=> VerifyRenamesAsync(
381+
input: $"""
382+
This is a Razor document.
383+
384+
<My.Namespace.Comp$$onent />
385+
<My.Namespace.Component></My.Namespace.Component>
386+
<My.Namespace.Component>
387+
</My.Namespace.Component>
388+
389+
The end.
390+
""",
391+
additionalFiles: [
392+
(FilePath("Component.razor"), """
393+
@namespace My.Namespace
394+
"""),
395+
(FilePath("OtherComponent.razor"), """
396+
<My.Namespace.Component />
397+
<My.Namespace.Component></My.Namespace.Component>
398+
<My.Namespace.Component>
399+
</My.Namespace.Component>
400+
""")
401+
],
402+
newName: "DifferentName",
403+
expected: """
404+
This is a Razor document.
405+
406+
<My.Namespace.DifferentName />
407+
<My.Namespace.DifferentName></My.Namespace.DifferentName>
408+
<My.Namespace.DifferentName>
409+
</My.Namespace.DifferentName>
410+
411+
The end.
412+
""",
413+
renames: [
414+
("Component.razor", "DifferentName.razor")
415+
],
416+
additionalExpectedFiles: [
417+
(FileUri("OtherComponent.razor"), """
418+
<My.Namespace.DifferentName />
419+
<My.Namespace.DifferentName></My.Namespace.DifferentName>
420+
<My.Namespace.DifferentName>
421+
</My.Namespace.DifferentName>
422+
""")
423+
]);
424+
234425
private async Task VerifyRenamesAsync(
235426
string input,
236427
string newName,
@@ -271,53 +462,45 @@ private async Task VerifyRenamesAsync(
271462
{
272463
Assert.NotNull(renames);
273464

465+
var expectedRenames = renames.ToList();
274466
foreach (var change in changes)
275467
{
276468
if (change.TryGetThird(out var renameEdit))
277469
{
278-
Assert.Contains(renames,
470+
var found = Assert.Single(renames,
279471
r => renameEdit.OldDocumentUri.GetRequiredParsedUri().GetDocumentFilePath().EndsWith(r.oldName) &&
280472
renameEdit.NewDocumentUri.GetRequiredParsedUri().GetDocumentFilePath().EndsWith(r.newName));
473+
expectedRenames.Remove(found);
281474
}
282475
}
283-
}
284476

285-
await ProcessRazorDocumentEditsAsync(inputText, expected, document, additionalExpectedFiles, result, DisposalToken).ConfigureAwait(false);
477+
Assert.Empty(expectedRenames);
478+
}
286479

480+
var expectedChanges = (additionalExpectedFiles ?? []).Concat([(document.CreateUri(), expected)]);
481+
await VerifyWorkspaceEditAsync(result, document.Project.Solution, expectedChanges, DisposalToken);
287482
}
288483

289-
private static async Task ProcessRazorDocumentEditsAsync(SourceText inputText, string expected, TextDocument razorDocument, (Uri fileUri, string contents)[]? additionalExpectedFiles, WorkspaceEdit result, CancellationToken cancellationToken)
484+
private static async Task VerifyWorkspaceEditAsync(WorkspaceEdit workspaceEdit, Solution solution, IEnumerable<(Uri fileUri, string contents)> expectedChanges, CancellationToken cancellationToken)
290485
{
291-
var razorDocumentUri = razorDocument.CreateUri();
292-
var solution = razorDocument.Project.Solution;
293-
294-
Assert.True(result.TryGetTextDocumentEdits(out var textDocumentEdits));
486+
Assert.True(workspaceEdit.TryGetTextDocumentEdits(out var textDocumentEdits));
295487
foreach (var textDocumentEdit in textDocumentEdits)
296488
{
297-
if (textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri() == razorDocumentUri)
298-
{
299-
foreach (var edit in textDocumentEdit.Edits)
300-
{
301-
inputText = inputText.WithChanges(inputText.GetTextChange((TextEdit)edit));
302-
}
303-
}
304-
else if (additionalExpectedFiles is not null)
305-
{
306-
foreach (var (uri, contents) in additionalExpectedFiles)
307-
{
308-
var additionalDocument = solution.GetTextDocuments(uri).First();
309-
var text = await additionalDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
489+
var (uri, _) = expectedChanges.Single(e => e.fileUri == textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri());
310490

311-
foreach (var edit in textDocumentEdit.Edits)
312-
{
313-
text = text.WithChanges(text.GetTextChange((TextEdit)edit));
314-
}
491+
var document = solution.GetTextDocuments(uri).First();
492+
var text = await document.GetTextAsync(cancellationToken);
315493

316-
AssertEx.EqualOrDiff(contents, text.ToString());
317-
}
318-
}
494+
text = text.WithChanges(textDocumentEdit.Edits.Select(e => text.GetTextChange((TextEdit)e)));
495+
496+
solution = solution.WithAdditionalDocumentText(document.Id, text);
319497
}
320498

321-
AssertEx.EqualOrDiff(expected, inputText.ToString());
499+
foreach (var (uri, contents) in expectedChanges)
500+
{
501+
var document = solution.GetTextDocuments(uri).First();
502+
var text = await document.GetTextAsync(cancellationToken);
503+
AssertEx.EqualOrDiff(contents, text.ToString());
504+
}
322505
}
323506
}

0 commit comments

Comments
 (0)