CompletionTests.cs 3.6 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

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
8
using Microsoft.CodeAnalysis.Completion;
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.Completion
{
D
tests.  
David Barbet 已提交
15
    public class CompletionTests : AbstractLanguageServerProtocolTests
16 17 18 19 20 21 22 23 24 25 26 27
    {
        [Fact]
        public async Task TestGetCompletionsAsync()
        {
            var markup =
@"class A
{
    void M()
    {
        {|caret:|}
    }
}";
28
            using var workspace = CreateTestWorkspace(markup, out var locations);
D
David Barbet 已提交
29
            var expected = CreateCompletionItem("A", LSP.CompletionItemKind.Class, new string[] { "Class", "Internal" }, CreateCompletionParams(locations["caret"].Single()));
30
            var clientCapabilities = new LSP.VSClientCapabilities { SupportsVisualStudioExtensions = true };
31

32
            var results = await RunGetCompletionsAsync(workspace.CurrentSolution, locations["caret"].Single(), clientCapabilities).ConfigureAwait(false);
D
David Barbet 已提交
33
            AssertJsonEquals(expected, results.First());
34 35
        }

36 37 38 39 40 41 42 43 44 45 46
        [Fact]
        public async Task TestGetCompletionsDoesNotIncludeUnimportedTypesAsync()
        {
            var markup =
@"class A
{
    void M()
    {
        {|caret:|}
    }
}";
47 48
            using var workspace = CreateTestWorkspace(markup, out var locations);
            var solution = workspace.CurrentSolution;
49 50

            // Make sure the unimported types option is on by default.
51 52 53
            solution = solution.WithOptions(solution.Options
                .WithChangedOption(CompletionOptions.ShowItemsFromUnimportedNamespaces, LanguageNames.CSharp, true)
                .WithChangedOption(CompletionServiceOptions.IsExpandedCompletion, true));
54 55 56 57

            var expected = CreateCompletionItem("A", LSP.CompletionItemKind.Class, new string[] { "Class", "Internal" }, CreateCompletionParams(locations["caret"].Single()));
            var clientCapabilities = new LSP.VSClientCapabilities { SupportsVisualStudioExtensions = true };

D
David Barbet 已提交
58
            var results = await RunGetCompletionsAsync(solution, locations["caret"].Single(), clientCapabilities);
59 60 61 62

            Assert.False(results.Any(item => "Console" == item.Label));
        }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        [Fact]
        public async Task TestGetCompletionsDoesNotIncludeSnippetsAsync()
        {
            var markup =
@"class A
{
    {|caret:|}
}";
            using var workspace = CreateTestWorkspace(markup, out var locations);
            var solution = workspace.CurrentSolution;
            solution = solution.WithOptions(solution.Options
                .WithChangedOption(CompletionOptions.SnippetsBehavior, LanguageNames.CSharp, SnippetsRule.AlwaysInclude));

            var clientCapabilities = new LSP.VSClientCapabilities { SupportsVisualStudioExtensions = true };

            var results = await RunGetCompletionsAsync(solution, locations["caret"].Single(), clientCapabilities);

            Assert.False(results.Any(item => "ctor" == item.Label));
        }

D
David Barbet 已提交
83 84
        private static async Task<LSP.CompletionItem[]> RunGetCompletionsAsync(Solution solution, LSP.Location caret, LSP.ClientCapabilities clientCapabilities = null)
            => await GetLanguageServer(solution).ExecuteRequestAsync<LSP.CompletionParams, LSP.CompletionItem[]>(LSP.Methods.TextDocumentCompletionName,
85
                CreateCompletionParams(caret), clientCapabilities, null, CancellationToken.None);
86 87
    }
}