|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#nullable disable |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Composition; |
| 10 | +using System.Linq; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Microsoft.CodeAnalysis.Host.Mef; |
| 14 | +using Microsoft.CodeAnalysis.LanguageServer.Handler; |
| 15 | +using Microsoft.CodeAnalysis.Test.Utilities; |
| 16 | +using Roslyn.LanguageServer.Protocol; |
| 17 | +using Roslyn.Test.Utilities; |
| 18 | +using Xunit; |
| 19 | +using Xunit.Abstractions; |
| 20 | +using LSP = Roslyn.LanguageServer.Protocol; |
| 21 | + |
| 22 | +namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Rename; |
| 23 | + |
| 24 | +public sealed class WillRenameTests(ITestOutputHelper testOutputHelper) : AbstractLanguageServerProtocolTests(testOutputHelper) |
| 25 | +{ |
| 26 | + protected override TestComposition Composition => base.Composition |
| 27 | + .AddParts(typeof(TestWillRenameListener1)) |
| 28 | + .AddParts(typeof(TestWillRenameListener2)); |
| 29 | + |
| 30 | + [Theory, CombinatorialData] |
| 31 | + public async Task SetsCapabilities(bool mutatingLspWorkspace) |
| 32 | + { |
| 33 | + var clientCapabilities = new ClientCapabilities |
| 34 | + { |
| 35 | + Workspace = new WorkspaceClientCapabilities |
| 36 | + { |
| 37 | + FileOperations = new FileOperationsWorkspaceClientCapabilities |
| 38 | + { |
| 39 | + WillRename = true |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + await using var testLspServer = await CreateTestLspServerAsync("", mutatingLspWorkspace, clientCapabilities); |
| 45 | + |
| 46 | + var capabilities = testLspServer.GetServerCapabilities(); |
| 47 | + |
| 48 | + Assert.Collection(capabilities.Workspace.FileOperations.WillRename.Filters, |
| 49 | + filter => Assert.Equal("*.cs", filter.Pattern.Glob), |
| 50 | + filter => Assert.Equal("*.vb", filter.Pattern.Glob)); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory, CombinatorialData] |
| 54 | + public async Task CallsListener(bool mutatingLspWorkspace) |
| 55 | + { |
| 56 | + await using var testLspServer = await CreateTestLspServerAsync("", mutatingLspWorkspace); |
| 57 | + |
| 58 | + var listeners = ((IMefHostExportProvider)testLspServer.TestWorkspace.Services.HostServices).GetExportedValues<ILspWillRenameListener>().OfType<TestWillRenameListener1>().ToArray(); |
| 59 | + |
| 60 | + // Need at least one change, or the result will be dropped |
| 61 | + listeners[0].Result = new WorkspaceEdit() { DocumentChanges = new TextDocumentEdit[] { new() { } } }; |
| 62 | + |
| 63 | + var edit = await RunWillRenameAsync(testLspServer); |
| 64 | + Assert.NotNull(edit); |
| 65 | + } |
| 66 | + |
| 67 | + [Theory, CombinatorialData] |
| 68 | + public async Task CombinesDocumentChanges(bool mutatingLspWorkspace) |
| 69 | + { |
| 70 | + await using var testLspServer = await CreateTestLspServerAsync("", mutatingLspWorkspace); |
| 71 | + |
| 72 | + var listeners = ((IMefHostExportProvider)testLspServer.TestWorkspace.Services.HostServices).GetExportedValues<ILspWillRenameListener>().OfType<TestWillRenameListener1>().ToArray(); |
| 73 | + |
| 74 | + var expected = new WorkspaceEdit() |
| 75 | + { |
| 76 | + DocumentChanges = new TextDocumentEdit[] { |
| 77 | + new() { TextDocument = new() { DocumentUri = new("file://file1.cs") } }, |
| 78 | + new() { TextDocument = new() { DocumentUri = new("file://file2.cs") } } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + listeners[0].Result = new WorkspaceEdit() { DocumentChanges = new TextDocumentEdit[] { new() { TextDocument = new() { DocumentUri = new("file://file1.cs") } } } }; |
| 83 | + listeners[1].Result = new WorkspaceEdit() { DocumentChanges = new TextDocumentEdit[] { new() { TextDocument = new() { DocumentUri = new("file://file2.cs") } } } }; |
| 84 | + |
| 85 | + var edit = await RunWillRenameAsync(testLspServer); |
| 86 | + Assert.NotNull(edit); |
| 87 | + |
| 88 | + AssertJsonEquals(expected, edit); |
| 89 | + } |
| 90 | + |
| 91 | + [Theory, CombinatorialData] |
| 92 | + public async Task CombinesDocumentChanges_SumType(bool mutatingLspWorkspace) |
| 93 | + { |
| 94 | + await using var testLspServer = await CreateTestLspServerAsync("", mutatingLspWorkspace); |
| 95 | + |
| 96 | + var listeners = ((IMefHostExportProvider)testLspServer.TestWorkspace.Services.HostServices).GetExportedValues<ILspWillRenameListener>().OfType<TestWillRenameListener1>().ToArray(); |
| 97 | + |
| 98 | + var expected = new WorkspaceEdit() |
| 99 | + { |
| 100 | + DocumentChanges = new SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[] { |
| 101 | + new TextDocumentEdit() { TextDocument = new() { DocumentUri = new("file://file1.cs") } }, |
| 102 | + new RenameFile() { OldDocumentUri = new("file://file2.cs") } |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + listeners[0].Result = new WorkspaceEdit() { DocumentChanges = new TextDocumentEdit[] { new() { TextDocument = new() { DocumentUri = new("file://file1.cs") } } } }; |
| 107 | + listeners[1].Result = new WorkspaceEdit() { DocumentChanges = new SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[] { new RenameFile() { OldDocumentUri = new("file://file2.cs") } } }; |
| 108 | + |
| 109 | + var edit = await RunWillRenameAsync(testLspServer); |
| 110 | + Assert.NotNull(edit); |
| 111 | + |
| 112 | + AssertJsonEquals(expected, edit); |
| 113 | + } |
| 114 | + |
| 115 | + [Theory, CombinatorialData] |
| 116 | + public async Task CombinesDocumentChanges_Changes(bool mutatingLspWorkspace) |
| 117 | + { |
| 118 | + await using var testLspServer = await CreateTestLspServerAsync("", mutatingLspWorkspace); |
| 119 | + |
| 120 | + var listeners = ((IMefHostExportProvider)testLspServer.TestWorkspace.Services.HostServices).GetExportedValues<ILspWillRenameListener>().OfType<TestWillRenameListener1>().ToArray(); |
| 121 | + |
| 122 | + var expected = new WorkspaceEdit() |
| 123 | + { |
| 124 | + Changes = new Dictionary<string, TextEdit[]> |
| 125 | + { |
| 126 | + { "file://file1.cs", []}, |
| 127 | + { "file://file2.cs", [] } |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + listeners[0].Result = new WorkspaceEdit() { Changes = new Dictionary<string, TextEdit[]> { { "file://file1.cs", [] } } }; |
| 132 | + listeners[1].Result = new WorkspaceEdit() { Changes = new Dictionary<string, TextEdit[]> { { "file://file2.cs", [] } } }; |
| 133 | + |
| 134 | + var edit = await RunWillRenameAsync(testLspServer); |
| 135 | + Assert.NotNull(edit); |
| 136 | + |
| 137 | + AssertJsonEquals(expected, edit); |
| 138 | + } |
| 139 | + |
| 140 | + private static async Task<WorkspaceEdit> RunWillRenameAsync(TestLspServer testLspServer) |
| 141 | + { |
| 142 | + var renameParams = new RenameFilesParams(); |
| 143 | + return await testLspServer.ExecuteRequestAsync<LSP.RenameFilesParams, LSP.WorkspaceEdit>(LSP.Methods.WorkspaceWillRenameFilesName, renameParams, CancellationToken.None); |
| 144 | + } |
| 145 | + |
| 146 | + [ExportLspWillRenameListener("*.cs")] |
| 147 | + [Shared] |
| 148 | + [PartNotDiscoverable] |
| 149 | + [method: ImportingConstructor] |
| 150 | + [method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 151 | + private class TestWillRenameListener1() : ILspWillRenameListener |
| 152 | + { |
| 153 | + public WorkspaceEdit Result { get; set; } |
| 154 | + |
| 155 | + public Task<WorkspaceEdit> HandleWillRenameAsync(RenameFilesParams renameParams, RequestContext context, CancellationToken cancellationToken) |
| 156 | + { |
| 157 | + return Task.FromResult(Result); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [ExportLspWillRenameListener("*.vb")] |
| 162 | + [Shared] |
| 163 | + [PartNotDiscoverable] |
| 164 | + [method: ImportingConstructor] |
| 165 | + [method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 166 | + private class TestWillRenameListener2() : TestWillRenameListener1 |
| 167 | + { |
| 168 | + } |
| 169 | +} |
0 commit comments