FormatDocumentTests.cs 2.0 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.
4 5 6 7 8

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
D
David Barbet 已提交
9
using Roslyn.Test.Utilities;
10 11 12 13 14
using Xunit;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Formatting
{
D
tests.  
David Barbet 已提交
15
    public class FormatDocumentTests : AbstractLanguageServerProtocolTests
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    {
        [Fact]
        public async Task TestFormatDocumentAsync()
        {
            var markup =
@"class A
{
void M()
{
            int i = 1;{|caret:|}
    }
}";
            var expected =
@"class A
{
    void M()
    {
        int i = 1;
    }
}";
36
            using var workspace = CreateTestWorkspace(markup, out var locations);
D
David Barbet 已提交
37
            var documentURI = locations["caret"].Single().Uri;
38
            var documentText = await workspace.CurrentSolution.GetDocuments(documentURI).Single().GetTextAsync();
39

40
            var results = await RunFormatDocumentAsync(workspace.CurrentSolution, documentURI);
41 42 43 44 45
            var actualText = ApplyTextEdits(results, documentText);
            Assert.Equal(expected, actualText);
        }

        private static async Task<LSP.TextEdit[]> RunFormatDocumentAsync(Solution solution, Uri uri)
D
David Barbet 已提交
46
            => await GetLanguageServer(solution).ExecuteRequestAsync<LSP.DocumentFormattingParams, LSP.TextEdit[]>(LSP.Methods.TextDocumentFormattingName,
47
                CreateDocumentFormattingParams(uri), new LSP.ClientCapabilities(), null, CancellationToken.None);
48 49 50 51 52 53 54 55 56 57 58 59

        private static LSP.DocumentFormattingParams CreateDocumentFormattingParams(Uri uri)
            => new LSP.DocumentFormattingParams()
            {
                TextDocument = CreateTextDocumentIdentifier(uri),
                Options = new LSP.FormattingOptions()
                {
                    // TODO - Format should respect formatting options.
                }
            };
    }
}