Skip to content

Commit 4a45cc5

Browse files
committed
Share assert method, add CreateFile support, and re-use
1 parent 1fbfbb4 commit 4a45cc5

File tree

3 files changed

+105
-137
lines changed

3 files changed

+105
-137
lines changed

src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/AssertExtensions.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
12+
using Microsoft.CodeAnalysis.Razor;
13+
using Roslyn.Test.Utilities;
414
using Roslyn.Text.Adornments;
515
using Xunit;
616

@@ -18,4 +28,83 @@ internal static void AssertExpectedClassification(
1828
Assert.Equal(expectedClassificationType, run.ClassificationTypeName);
1929
Assert.Equal(expectedClassificationStyle, run.Style);
2030
}
31+
32+
public static async Task AssertWorkspaceEditAsync(this WorkspaceEdit workspaceEdit, Solution solution, IEnumerable<(Uri fileUri, string contents)> expectedChanges, CancellationToken cancellationToken)
33+
{
34+
var changes = Assert.NotNull(workspaceEdit.DocumentChanges);
35+
36+
foreach (var change in Flatten(changes))
37+
{
38+
if (change.TryGetFirst(out var textDocumentEdit))
39+
{
40+
var uri = textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri();
41+
var documentId = solution.GetDocumentIdsWithFilePath(RazorUri.GetDocumentFilePathFromUri(uri)).Single();
42+
var document = solution.GetDocument(documentId) ?? solution.GetAdditionalDocument(documentId);
43+
Assert.NotNull(document);
44+
var text = await document.GetTextAsync(cancellationToken);
45+
46+
text = text.WithChanges(textDocumentEdit.Edits.Select(e => text.GetTextChange((TextEdit)e)));
47+
48+
solution = document is Document
49+
? solution.WithDocumentText(document.Id, text)
50+
: solution.WithAdditionalDocumentText(document.Id, text);
51+
}
52+
else if (change.TryGetSecond(out var createFile))
53+
{
54+
var uri = createFile.DocumentUri.GetRequiredParsedUri();
55+
var documentId = DocumentId.CreateNewId(solution.ProjectIds.Single());
56+
var filePath = createFile.DocumentUri.GetRequiredParsedUri().GetDocumentFilePath();
57+
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath);
58+
solution = solution.AddDocument(documentInfo);
59+
}
60+
else if (change.TryGetThird(out var renameFile))
61+
{
62+
var (oldUri, newUri) = (renameFile.OldDocumentUri.GetRequiredParsedUri(), renameFile.NewDocumentUri.GetRequiredParsedUri());
63+
var documentId = solution.GetDocumentIdsWithFilePath(RazorUri.GetDocumentFilePathFromUri(oldUri)).Single();
64+
var document = solution.GetDocument(documentId) ?? solution.GetAdditionalDocument(documentId);
65+
Assert.NotNull(document);
66+
if (document is Document)
67+
{
68+
solution = solution.WithDocumentFilePath(document.Id, newUri.GetDocumentFilePath());
69+
}
70+
else
71+
{
72+
var filePath = newUri.GetDocumentFilePath();
73+
var text = await document.GetTextAsync(cancellationToken);
74+
solution = document.Project
75+
.RemoveAdditionalDocument(document.Id)
76+
.AddAdditionalDocument(Path.GetFileName(filePath), text, filePath: filePath).Project.Solution;
77+
}
78+
}
79+
else
80+
{
81+
Assert.Fail($"Don't know how to process a {change.Value?.GetType().Name}.");
82+
}
83+
}
84+
85+
foreach (var (uri, contents) in expectedChanges)
86+
{
87+
var document = solution.GetTextDocuments(uri).First();
88+
var text = await document.GetTextAsync(cancellationToken);
89+
AssertEx.EqualOrDiff(contents, text.ToString());
90+
}
91+
92+
static IEnumerable<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>> Flatten(SumType<TextDocumentEdit[], SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[]> documentChanges)
93+
{
94+
if (documentChanges.TryGetFirst(out var textDocumentEdits))
95+
{
96+
foreach (var edit in textDocumentEdits)
97+
{
98+
yield return edit;
99+
}
100+
}
101+
else if (documentChanges.TryGetSecond(out var changes))
102+
{
103+
foreach (var change in changes)
104+
{
105+
yield return change;
106+
}
107+
}
108+
}
109+
}
21110
}

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

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Microsoft.AspNetCore.Razor.Test.Common;
1616
using Microsoft.CodeAnalysis;
1717
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
18-
using Microsoft.CodeAnalysis.Razor;
1918
using Microsoft.CodeAnalysis.Razor.CodeActions.Models;
2019
using Microsoft.CodeAnalysis.Razor.Protocol;
2120
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
@@ -52,11 +51,24 @@ private protected async Task VerifyCodeActionAsync(
5251
return;
5352
}
5453

54+
Assert.NotNull(expected);
55+
5556
var workspaceEdit = codeAction.Data is null
5657
? codeAction.Edit.AssumeNotNull()
5758
: await ResolveCodeActionAsync(document, codeAction);
5859

59-
await VerifyCodeActionResultAsync(document, workspaceEdit, expected, additionalExpectedFiles);
60+
var expectedChanges = new List<(Uri, string)>();
61+
if (expected is not null)
62+
{
63+
expectedChanges.Add((document.CreateUri(), expected));
64+
}
65+
66+
if (additionalExpectedFiles is not null)
67+
{
68+
expectedChanges.AddRange(additionalExpectedFiles);
69+
}
70+
71+
await workspaceEdit.AssertWorkspaceEditAsync(document.Project.Solution, expectedChanges, DisposalToken);
6072
}
6173

6274
private protected TextDocument CreateRazorDocument(TestCode input, RazorFileKind? fileKind = null, string? documentFilePath = null, (string filePath, string contents)[]? additionalFiles = null, bool addDefaultImports = true)
@@ -164,62 +176,6 @@ Could not find code action with name '{codeActionName}'.
164176
return await endpoint.GetTestAccessor().HandleRequestAsync(document, request, DisposalToken);
165177
}
166178

167-
private async Task VerifyCodeActionResultAsync(TextDocument document, WorkspaceEdit workspaceEdit, string? expected, (Uri fileUri, string contents)[]? additionalExpectedFiles = null)
168-
{
169-
var solution = document.Project.Solution;
170-
var validated = false;
171-
172-
if (workspaceEdit.DocumentChanges?.Value is SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[] sumTypeArray)
173-
{
174-
using var builder = new PooledArrayBuilder<TextDocumentEdit>();
175-
foreach (var sumType in sumTypeArray)
176-
{
177-
if (sumType.Value is CreateFile createFile)
178-
{
179-
validated = true;
180-
Assert.Single(additionalExpectedFiles.AssumeNotNull(), f => f.fileUri == createFile.DocumentUri.GetRequiredParsedUri());
181-
var documentId = DocumentId.CreateNewId(document.Project.Id);
182-
var filePath = createFile.DocumentUri.GetRequiredParsedUri().GetDocumentFilePath();
183-
var documentInfo = DocumentInfo.Create(documentId, filePath, filePath: filePath);
184-
solution = solution.AddDocument(documentInfo);
185-
}
186-
}
187-
}
188-
189-
if (workspaceEdit.TryGetTextDocumentEdits(out var documentEdits))
190-
{
191-
foreach (var edit in documentEdits)
192-
{
193-
var textDocument = solution.GetTextDocuments(edit.TextDocument.DocumentUri.GetRequiredParsedUri()).First();
194-
var text = await textDocument.GetTextAsync(DisposalToken).ConfigureAwait(false);
195-
if (textDocument is Document)
196-
{
197-
solution = solution.WithDocumentText(textDocument.Id, text.WithChanges(edit.Edits.Select(e => text.GetTextChange((TextEdit)e))));
198-
}
199-
else
200-
{
201-
solution = solution.WithAdditionalDocumentText(textDocument.Id, text.WithChanges(edit.Edits.Select(e => text.GetTextChange((TextEdit)e))));
202-
}
203-
}
204-
205-
if (additionalExpectedFiles is not null)
206-
{
207-
foreach (var (uri, contents) in additionalExpectedFiles)
208-
{
209-
var additionalDocument = solution.GetTextDocuments(uri).First();
210-
var text = await additionalDocument.GetTextAsync(DisposalToken).ConfigureAwait(false);
211-
AssertEx.EqualOrDiff(contents, text.ToString());
212-
}
213-
}
214-
215-
validated = true;
216-
var actual = await solution.GetAdditionalDocument(document.Id).AssumeNotNull().GetTextAsync(DisposalToken).ConfigureAwait(false);
217-
AssertEx.EqualOrDiff(expected, actual.ToString());
218-
}
219-
220-
Assert.True(validated, "Test did not validate anything. Code action response type is presumably not supported.");
221-
}
222-
223179
private async Task<WorkspaceEdit> ResolveCodeActionAsync(CodeAnalysis.TextDocument document, CodeAction codeAction)
224180
{
225181
var requestInvoker = new TestHtmlRequestInvoker();

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

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

44
using System;
5-
using System.Collections.Generic;
6-
using System.IO;
75
using System.Linq;
8-
using System.Threading;
96
using System.Threading.Tasks;
107
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.AspNetCore.Razor.Test.Common;
119
using Microsoft.CodeAnalysis;
1210
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
13-
using Microsoft.CodeAnalysis.Razor;
1411
using Microsoft.CodeAnalysis.Testing;
1512
using Microsoft.CodeAnalysis.Text;
16-
using Roslyn.Test.Utilities;
1713
using Xunit;
1814
using Xunit.Abstractions;
1915

@@ -871,79 +867,6 @@ private async Task VerifyRenamesAsync(
871867

872868
var documentUri = newFileUri ?? document.CreateUri();
873869
var expectedChanges = (additionalExpectedFiles ?? []).Concat([(documentUri, expected)]);
874-
await VerifyWorkspaceEditAsync(result, document.Project.Solution, expectedChanges, DisposalToken);
875-
}
876-
877-
private static async Task VerifyWorkspaceEditAsync(WorkspaceEdit workspaceEdit, Solution solution, IEnumerable<(Uri fileUri, string contents)> expectedChanges, CancellationToken cancellationToken)
878-
{
879-
var unseenDocs = expectedChanges.Select(e => e.fileUri).ToHashSet();
880-
var changes = Assert.NotNull(workspaceEdit.DocumentChanges);
881-
foreach (var change in Flatten(changes))
882-
{
883-
if (change.TryGetThird(out var renameFile))
884-
{
885-
var (oldUri, newUri) = (renameFile.OldDocumentUri.GetRequiredParsedUri(), renameFile.NewDocumentUri.GetRequiredParsedUri());
886-
unseenDocs.Remove(newUri);
887-
888-
var document = solution.GetTextDocuments(oldUri).First();
889-
if (document is Document)
890-
{
891-
solution = solution.WithDocumentFilePath(document.Id, newUri.GetDocumentFilePath());
892-
}
893-
else
894-
{
895-
var filePath = newUri.GetDocumentFilePath();
896-
var text = await document.GetTextAsync(cancellationToken);
897-
solution = document.Project
898-
.RemoveAdditionalDocument(document.Id)
899-
.AddAdditionalDocument(Path.GetFileName(filePath), text, filePath: filePath).Project.Solution;
900-
}
901-
}
902-
else if (change.TryGetFirst(out var textDocumentEdit))
903-
{
904-
var (uri, _) = expectedChanges.Single(e => e.fileUri == textDocumentEdit.TextDocument.DocumentUri.GetRequiredParsedUri());
905-
unseenDocs.Remove(uri);
906-
907-
var document = solution.GetTextDocuments(uri).First();
908-
var text = await document.GetTextAsync(cancellationToken);
909-
910-
text = text.WithChanges(textDocumentEdit.Edits.Select(e => text.GetTextChange((TextEdit)e)));
911-
912-
solution = document is Document
913-
? solution.WithDocumentText(document.Id, text)
914-
: solution.WithAdditionalDocumentText(document.Id, text);
915-
}
916-
else
917-
{
918-
Assert.Fail($"Don't know how to process a {change.Value?.GetType().Name}.");
919-
}
920-
}
921-
922-
Assert.Empty(unseenDocs);
923-
924-
foreach (var (uri, contents) in expectedChanges)
925-
{
926-
var document = solution.GetTextDocuments(uri).First();
927-
var text = await document.GetTextAsync(cancellationToken);
928-
AssertEx.EqualOrDiff(contents, text.ToString());
929-
}
930-
931-
static IEnumerable<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>> Flatten(SumType<TextDocumentEdit[], SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[]> documentChanges)
932-
{
933-
if (documentChanges.TryGetFirst(out var textDocumentEdits))
934-
{
935-
foreach (var edit in textDocumentEdits)
936-
{
937-
yield return edit;
938-
}
939-
}
940-
else if (documentChanges.TryGetSecond(out var changes))
941-
{
942-
foreach (var change in changes)
943-
{
944-
yield return change;
945-
}
946-
}
947-
}
870+
await result.AssertWorkspaceEditAsync(document.Project.Solution, expectedChanges, DisposalToken);
948871
}
949872
}

0 commit comments

Comments
 (0)