Skip to content

Commit 9bcda6e

Browse files
committed
Add test to cover finalizing the solution in inline rename, and file mapping
1 parent 8bac290 commit 9bcda6e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/EditorFeatures/Test2/Rename/InlineRenameTests.vb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,53 @@ class [|C|]
23792379
Await session.CommitAsync(previewChanges:=False, editorOperationContext:=Nothing)
23802380

23812381
Await VerifyTagsAreCorrect(workspace)
2382+
2383+
Await VerifyChangedSourceGeneratedDocumentFilenames(workspace)
2384+
End Using
2385+
End Function
2386+
2387+
<WpfTheory>
2388+
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
2389+
Public Async Function RenameWithRazorGeneratedFile(host As RenameTestHost) As Task
2390+
Dim generatedCode = "
2391+
public class GeneratedClass
2392+
{
2393+
public void M(MyClass c) { }
2394+
}
2395+
"
2396+
Dim razorGenerator = New Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator(Sub(c) c.AddSource("generated_file.cs", generatedCode))
2397+
2398+
Using workspace = CreateWorkspaceWithWaiter(
2399+
<Workspace>
2400+
<Project Language="C#" CommonReferences="true" LanguageVersion="preview">
2401+
<Document>
2402+
partial class [|$$MyClass|]
2403+
{
2404+
public void M1()
2405+
{
2406+
}
2407+
}
2408+
</Document>
2409+
</Project>
2410+
</Workspace>, host)
2411+
2412+
Dim project = workspace.CurrentSolution.Projects.First().AddAnalyzerReference(New TestGeneratorReference(razorGenerator))
2413+
workspace.TryApplyChanges(project.Solution)
2414+
2415+
Dim session = StartSession(workspace)
2416+
2417+
' Type a bit in the file
2418+
Dim cursorDocument = workspace.Documents.Single(Function(d) d.CursorPosition.HasValue)
2419+
Dim caretPosition = cursorDocument.CursorPosition.Value
2420+
Dim textBuffer = cursorDocument.GetTextBuffer()
2421+
2422+
textBuffer.Insert(caretPosition, "Example")
2423+
2424+
Await session.CommitAsync(previewChanges:=False, editorOperationContext:=Nothing)
2425+
2426+
Await VerifyTagsAreCorrect(workspace)
2427+
2428+
Await VerifyChangedSourceGeneratedDocumentFilenames(workspace, "generated_file.cs")
23822429
End Using
23832430
End Function
23842431

src/EditorFeatures/Test2/Rename/RenameTestHelpers.vb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
8181
Next
8282
End Function
8383

84+
Public Async Function VerifyChangedSourceGeneratedDocumentFileNames(workspace As EditorTestWorkspace, ParamArray mappedFiles As String()) As Task
85+
Await WaitForRename(workspace)
86+
87+
Assert.Equal(workspace.ChangedSourceGeneratedDocumentFileNames, mappedFiles)
88+
End Function
89+
8490
Public Sub VerifyFileName(document As Document, newIdentifierName As String)
8591
Dim expectedName = Path.ChangeExtension(newIdentifierName, Path.GetExtension(document.Name))
8692
Assert.Equal(expectedName, document.Name)

src/EditorFeatures/TestUtilities/Workspaces/EditorTestWorkspace.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
8+
using System.IO;
89
using System.Linq;
910
using System.Xml.Linq;
1011
using Microsoft.CodeAnalysis.CSharp.DecompiledSource;
@@ -15,6 +16,7 @@
1516
using Microsoft.CodeAnalysis.Editor.UnitTests;
1617
using Microsoft.CodeAnalysis.Formatting;
1718
using Microsoft.CodeAnalysis.Host;
19+
using Microsoft.CodeAnalysis.Shared.Extensions;
1820
using Microsoft.CodeAnalysis.Text;
1921
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
2022
using Microsoft.VisualStudio.Composition;
@@ -25,13 +27,16 @@
2527
using Roslyn.Test.EditorUtilities;
2628
using Roslyn.Test.Utilities;
2729
using Roslyn.Utilities;
30+
using Xunit;
2831

2932
namespace Microsoft.CodeAnalysis.Test.Utilities;
3033

3134
public sealed partial class EditorTestWorkspace : TestWorkspace<EditorTestHostDocument, EditorTestHostProject, EditorTestHostSolution>
3235
{
3336
private const string ReferencesOnDiskAttributeName = "ReferencesOnDisk";
3437

38+
public List<string> ChangedSourceGeneratedDocumentFileNames { get; } = [];
39+
3540
private readonly Dictionary<string, ITextBuffer2> _createdTextBuffers = [];
3641

3742
internal EditorTestWorkspace(
@@ -521,4 +526,14 @@ protected override (MetadataReference reference, ImmutableArray<byte> peImage) C
521526

522527
return (reference, image);
523528
}
529+
530+
internal override void ApplyMappedFileChanges(SolutionChanges solutionChanges)
531+
{
532+
foreach (var (docId, _) in solutionChanges.NewSolution.CompilationState.FrozenSourceGeneratedDocumentStates.States)
533+
{
534+
var document = solutionChanges.NewSolution.GetRequiredSourceGeneratedDocumentForAlreadyGeneratedId(docId);
535+
Assert.NotNull(document.FilePath);
536+
ChangedSourceGeneratedDocumentFileNames.Add(Path.GetFileName(document.FilePath));
537+
}
538+
}
524539
}

0 commit comments

Comments
 (0)