RenameTests.cs 1.9 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
D
tests.  
David Barbet 已提交
4 5

using System.Linq;
A
Allison Chou 已提交
6
using System.Threading;
D
tests.  
David Barbet 已提交
7
using System.Threading.Tasks;
A
Allison Chou 已提交
8
using Microsoft.VisualStudio.LanguageServer.Protocol;
D
tests.  
David Barbet 已提交
9 10 11
using Roslyn.Test.Utilities;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

A
Allison Chou 已提交
12
namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Definitions
D
tests.  
David Barbet 已提交
13
{
A
Allison Chou 已提交
14
    public class RenameTests : AbstractLanguageServerProtocolTests
D
tests.  
David Barbet 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    {
        [WpfFact]
        public async Task TestRenameAsync()
        {
            var markup =
@"class A
{
    void {|caret:|}{|renamed:M|}()
    {
    }
    void M2()
    {
        {|renamed:M|}()
    }
}";
30 31
            using var workspace = CreateTestWorkspace(markup, out var locations);
            var renameLocation = locations["caret"].First();
D
tests.  
David Barbet 已提交
32
            var renameValue = "RENAME";
33
            var expectedEdits = locations["renamed"].Select(location => new LSP.TextEdit() { NewText = renameValue, Range = location.Range });
D
tests.  
David Barbet 已提交
34

35
            var results = await RunRenameAsync(workspace.CurrentSolution, renameLocation, renameValue);
36
            AssertJsonEquals(expectedEdits, results.DocumentChanges.First().Edits);
D
tests.  
David Barbet 已提交
37 38 39 40 41 42 43 44 45
        }

        private static LSP.RenameParams CreateRenameParams(LSP.Location location, string newName)
            => new LSP.RenameParams()
            {
                NewName = newName,
                Position = location.Range.Start,
                TextDocument = CreateTextDocumentIdentifier(location.Uri)
            };
A
Allison Chou 已提交
46 47 48

        private static async Task<WorkspaceEdit> RunRenameAsync(Solution solution, LSP.Location renameLocation, string renamevalue)
           => await GetLanguageServer(solution).RenameAsync(solution, CreateRenameParams(renameLocation, renamevalue), new LSP.ClientCapabilities(), CancellationToken.None);
D
tests.  
David Barbet 已提交
49 50
    }
}