AbstractCompletionProviderTests.cs 49.9 KB
Newer Older
S
Sam Harwell 已提交
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3

using System;
4
using System.Collections.Immutable;
5 6 7
using System.Linq;
using System.Security;
using System.Threading;
C
Cyrus Najmabadi 已提交
8
using System.Threading.Tasks;
9 10
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
11
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion;
12 13 14
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
15
using Microsoft.CodeAnalysis.Shared.Extensions;
16
using Microsoft.CodeAnalysis.Test.Utilities;
17 18 19 20 21 22 23 24
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Moq;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
25
using RoslynCompletion = Microsoft.CodeAnalysis.Completion;
26 27 28

namespace Microsoft.CodeAnalysis.Editor.UnitTests.Completion
{
29
    [UseExportProvider]
30
    public abstract class AbstractCompletionProviderTests<TWorkspaceFixture> : TestBase, IClassFixture<TWorkspaceFixture>
31 32 33
        where TWorkspaceFixture : TestWorkspaceFixture, new()
    {
        protected readonly Mock<ICompletionSession> MockCompletionSession;
34
        protected TWorkspaceFixture WorkspaceFixture;
35

36
        protected AbstractCompletionProviderTests(TWorkspaceFixture workspaceFixture)
37 38 39
        {
            MockCompletionSession = new Mock<ICompletionSession>(MockBehavior.Strict);

40
            this.WorkspaceFixture = workspaceFixture;
41 42
        }

43 44
        public override void Dispose()
        {
45
            this.WorkspaceFixture.DisposeAfterTest();
46 47 48
            base.Dispose();
        }

C
Cyrus Najmabadi 已提交
49
        protected static async Task<bool> CanUseSpeculativeSemanticModelAsync(Document document, int position)
50
        {
51
            var service = document.GetLanguageService<ISyntaxFactsService>();
C
Cyrus Najmabadi 已提交
52
            var node = (await document.GetSyntaxRootAsync()).FindToken(position).Parent;
53 54 55 56

            return !service.GetMemberBodySpanForSpeculativeBinding(node).IsEmpty;
        }

57
        internal CompletionServiceWithProviders GetCompletionService(Workspace workspace)
58
        {
C
CyrusNajmabadi 已提交
59
            return CreateCompletionService(workspace, ImmutableArray.Create(CreateCompletionProvider()));
60 61
        }

C
CyrusNajmabadi 已提交
62
        internal abstract CompletionServiceWithProviders CreateCompletionService(
63
            Workspace workspace, ImmutableArray<CompletionProvider> exclusiveProviders);
C
CyrusNajmabadi 已提交
64

65 66
        protected abstract string ItemPartiallyWritten(string expectedItemOrNull);

C
CyrusNajmabadi 已提交
67
        protected abstract TestWorkspace CreateWorkspace(string fileContents);
68

69 70 71
        protected abstract Task BaseVerifyWorkerAsync(
            string code, int position, string expectedItemOrNull, string expectedDescriptionOrNull,
            SourceCodeKind sourceCodeKind, bool usePreviousCharAsTrigger, bool checkForAbsence,
G
Gen Lu 已提交
72
            int? glyph, int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription);
73

C
CyrusNajmabadi 已提交
74
        internal static CompletionHelper GetCompletionHelper(Document document)
75
        {
C
CyrusNajmabadi 已提交
76
            return CompletionHelper.GetHelper(document);
77 78
        }

79
        internal Task<RoslynCompletion.CompletionList> GetCompletionListAsync(
C
CyrusNajmabadi 已提交
80
            CompletionService service,
81
            Document document, int position, RoslynCompletion.CompletionTrigger triggerInfo, OptionSet options = null)
82
        {
C
CyrusNajmabadi 已提交
83
            return service.GetCompletionsAsync(document, position, triggerInfo, options: options);
84 85
        }

R
Ravi Chande 已提交
86
        protected async Task CheckResultsAsync(
C
CyrusNajmabadi 已提交
87 88
            Document document, int position, string expectedItemOrNull,
            string expectedDescriptionOrNull, bool usePreviousCharAsTrigger,
D
dotnet-bot 已提交
89
            bool checkForAbsence, int? glyph, int? matchPriority,
G
Gen Lu 已提交
90
            bool? hasSuggestionModeItem, string displayTextSuffix, string inlineDescription)
91
        {
92
            var code = (await document.GetTextAsync()).ToString();
93

94
            var trigger = RoslynCompletion.CompletionTrigger.Invoke;
95 96 97

            if (usePreviousCharAsTrigger)
            {
98
                trigger = RoslynCompletion.CompletionTrigger.CreateInsertionTrigger(insertedCharacter: code.ElementAt(position - 1));
99 100
            }

101
            var completionService = GetCompletionService(document.Project.Solution.Workspace);
C
CyrusNajmabadi 已提交
102
            var completionList = await GetCompletionListAsync(completionService, document, position, trigger);
103
            var items = completionList == null ? ImmutableArray<RoslynCompletion.CompletionItem>.Empty : completionList.Items;
104

C
CyrusNajmabadi 已提交
105 106 107 108 109
            if (hasSuggestionModeItem != null)
            {
                Assert.Equal(hasSuggestionModeItem.Value, completionList.SuggestionModeItem != null);
            }

110 111
            if (checkForAbsence)
            {
112
                if (items == null)
113 114 115 116 117 118
                {
                    return;
                }

                if (expectedItemOrNull == null)
                {
119
                    Assert.Empty(items);
120 121 122 123
                }
                else
                {
                    AssertEx.None(
124
                        items,
G
Gen Lu 已提交
125 126 127 128
                        c => CompareItems(c.DisplayText, expectedItemOrNull)
                                && CompareItems(c.DisplayTextSuffix, displayTextSuffix ?? "")
                                && CompareItems(c.InlineDescription, inlineDescription ?? "")
                                && (expectedDescriptionOrNull != null ? completionService.GetDescriptionAsync(document, c).Result.Text == expectedDescriptionOrNull : true));
129 130 131 132 133 134
                }
            }
            else
            {
                if (expectedItemOrNull == null)
                {
135
                    Assert.NotEmpty(items);
136 137 138
                }
                else
                {
G
Gen Lu 已提交
139 140 141 142 143 144 145 146 147
                    Func<CompletionItem, bool> predicate = c
                        => CompareItems(c.DisplayText, expectedItemOrNull)
                              && CompareItems(c.DisplayTextSuffix, displayTextSuffix ?? "")
                              && CompareItems(c.InlineDescription, inlineDescription ?? "")
                              && (expectedDescriptionOrNull != null ? completionService.GetDescriptionAsync(document, c).Result.Text == expectedDescriptionOrNull : true)
                              && (glyph.HasValue ? c.Tags.SequenceEqual(GlyphTags.GetTags((Glyph)glyph.Value)) : true)
                              && (matchPriority.HasValue ? (int)c.Rules.MatchPriority == matchPriority.Value : true);

                    AssertEx.Any(items, predicate);
148 149 150 151
                }
            }
        }

152 153 154
        private Task VerifyAsync(
            string markup, string expectedItemOrNull, string expectedDescriptionOrNull,
            SourceCodeKind sourceCodeKind, bool usePreviousCharAsTrigger, bool checkForAbsence,
G
Gen Lu 已提交
155
            int? glyph, int? matchPriority, bool? hasSuggestionModeItem, string displayTextSuffix, string inlineDescription)
156
        {
G
Gen Lu 已提交
157
            var workspace = WorkspaceFixture.GetWorkspace(markup);
I
Ivan Basov 已提交
158 159
            var code = WorkspaceFixture.Code;
            var position = WorkspaceFixture.Position;
G
Gen Lu 已提交
160
            SetWorkspaceOptions(workspace);
161

162 163
            return VerifyWorkerAsync(
                code, position, expectedItemOrNull, expectedDescriptionOrNull,
C
CyrusNajmabadi 已提交
164
                sourceCodeKind, usePreviousCharAsTrigger, checkForAbsence, glyph,
G
Gen Lu 已提交
165
                matchPriority, hasSuggestionModeItem, displayTextSuffix, inlineDescription);
166 167
        }

C
Cyrus Najmabadi 已提交
168
        protected async Task VerifyCustomCommitProviderAsync(string markupBeforeCommit, string itemToCommit, string expectedCodeAfterCommit, SourceCodeKind? sourceCodeKind = null, char? commitChar = null)
169
        {
I
Ivan Basov 已提交
170
            using (WorkspaceFixture.GetWorkspace(markupBeforeCommit))
171
            {
I
Ivan Basov 已提交
172 173
                var code = WorkspaceFixture.Code;
                var position = WorkspaceFixture.Position;
174 175 176 177 178 179 180 181 182 183

                if (sourceCodeKind.HasValue)
                {
                    await VerifyCustomCommitProviderWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, sourceCodeKind.Value, commitChar);
                }
                else
                {
                    await VerifyCustomCommitProviderWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, SourceCodeKind.Regular, commitChar);
                    await VerifyCustomCommitProviderWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, SourceCodeKind.Script, commitChar);
                }
184 185 186
            }
        }

C
Cyrus Najmabadi 已提交
187
        protected async Task VerifyProviderCommitAsync(string markupBeforeCommit, string itemToCommit, string expectedCodeAfterCommit,
188 189
            char? commitChar, string textTypedSoFar, SourceCodeKind? sourceCodeKind = null)
        {
I
Ivan Basov 已提交
190 191 192 193
            WorkspaceFixture.GetWorkspace(markupBeforeCommit);

            var code = WorkspaceFixture.Code;
            var position = WorkspaceFixture.Position;
194 195 196 197

            expectedCodeAfterCommit = expectedCodeAfterCommit.NormalizeLineEndings();
            if (sourceCodeKind.HasValue)
            {
C
Cyrus Najmabadi 已提交
198
                await VerifyProviderCommitWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, commitChar, textTypedSoFar, sourceCodeKind.Value);
199 200 201
            }
            else
            {
C
Cyrus Najmabadi 已提交
202 203
                await VerifyProviderCommitWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, commitChar, textTypedSoFar, SourceCodeKind.Regular);
                await VerifyProviderCommitWorkerAsync(code, position, itemToCommit, expectedCodeAfterCommit, commitChar, textTypedSoFar, SourceCodeKind.Script);
204 205 206 207 208 209 210 211
            }
        }

        protected virtual bool CompareItems(string actualItem, string expectedItem)
        {
            return actualItem.Equals(expectedItem);
        }

212 213 214
        protected async Task VerifyItemExistsAsync(
            string markup, string expectedItem, string expectedDescriptionOrNull = null,
            SourceCodeKind? sourceCodeKind = null, bool usePreviousCharAsTrigger = false,
215
            int? glyph = null, int? matchPriority = null, bool? hasSuggestionModeItem = null,
G
Gen Lu 已提交
216
            string displayTextSuffix = null, string inlineDescription = null)
217 218 219
        {
            if (sourceCodeKind.HasValue)
            {
220 221
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull,
                    sourceCodeKind.Value, usePreviousCharAsTrigger, checkForAbsence: false,
D
dotnet-bot 已提交
222
                    glyph: glyph, matchPriority: matchPriority,
G
Gen Lu 已提交
223 224
                    hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix,
                    inlineDescription: inlineDescription);
225 226 227
            }
            else
            {
G
Gen Lu 已提交
228 229
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull, SourceCodeKind.Regular, usePreviousCharAsTrigger, checkForAbsence: false, glyph: glyph, matchPriority: matchPriority, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull, SourceCodeKind.Script, usePreviousCharAsTrigger, checkForAbsence: false, glyph: glyph, matchPriority: matchPriority, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
230 231 232
            }
        }

233 234
        protected async Task VerifyItemIsAbsentAsync(
            string markup, string expectedItem, string expectedDescriptionOrNull = null,
C
CyrusNajmabadi 已提交
235
            SourceCodeKind? sourceCodeKind = null, bool usePreviousCharAsTrigger = false,
G
Gen Lu 已提交
236
            bool? hasSuggestionModeItem = null, string displayTextSuffix = null, string inlineDescription = null)
237 238 239
        {
            if (sourceCodeKind.HasValue)
            {
G
Gen Lu 已提交
240
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull, sourceCodeKind.Value, usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
241 242 243
            }
            else
            {
G
Gen Lu 已提交
244 245
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull, SourceCodeKind.Regular, usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
                await VerifyAsync(markup, expectedItem, expectedDescriptionOrNull, SourceCodeKind.Script, usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
246 247 248
            }
        }

249
        protected async Task VerifyAnyItemExistsAsync(
D
dotnet-bot 已提交
250
            string markup, SourceCodeKind? sourceCodeKind = null, bool usePreviousCharAsTrigger = false,
G
Gen Lu 已提交
251
            bool? hasSuggestionModeItem = null, string displayTextSuffix = null, string inlineDescription = null)
252 253 254
        {
            if (sourceCodeKind.HasValue)
            {
G
Gen Lu 已提交
255
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: sourceCodeKind.Value, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: false, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
256 257 258
            }
            else
            {
G
Gen Lu 已提交
259 260
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: SourceCodeKind.Regular, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: false, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: SourceCodeKind.Script, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: false, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
261 262 263
            }
        }

C
CyrusNajmabadi 已提交
264
        protected async Task VerifyNoItemsExistAsync(
D
dotnet-bot 已提交
265
            string markup, SourceCodeKind? sourceCodeKind = null,
266
            bool usePreviousCharAsTrigger = false, bool? hasSuggestionModeItem = null,
G
Gen Lu 已提交
267
            string displayTextSuffix = null, string inlineDescription = null)
268 269 270
        {
            if (sourceCodeKind.HasValue)
            {
G
Gen Lu 已提交
271
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: sourceCodeKind.Value, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
272 273 274
            }
            else
            {
G
Gen Lu 已提交
275 276
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: SourceCodeKind.Regular, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
                await VerifyAsync(markup, expectedItemOrNull: null, expectedDescriptionOrNull: null, sourceCodeKind: SourceCodeKind.Script, usePreviousCharAsTrigger: usePreviousCharAsTrigger, checkForAbsence: true, glyph: null, matchPriority: null, hasSuggestionModeItem: hasSuggestionModeItem, displayTextSuffix: displayTextSuffix, inlineDescription: inlineDescription);
277 278 279
            }
        }

M
Matt Warren 已提交
280
        internal abstract CompletionProvider CreateCompletionProvider();
281 282 283 284 285 286 287 288 289

        /// <summary>
        /// Override this to change parameters or return without verifying anything, e.g. for script sources. Or to test in other code contexts.
        /// </summary>
        /// <param name="code">The source code (not markup).</param>
        /// <param name="expectedItemOrNull">The expected item. If this is null, verifies that *any* item shows up for this CompletionProvider (or no items show up if checkForAbsence is true).</param>
        /// <param name="expectedDescriptionOrNull">If this is null, the Description for the item is ignored.</param>
        /// <param name="usePreviousCharAsTrigger">Whether or not the previous character in markup should be used to trigger IntelliSense for this provider. If false, invokes it through the invoke IntelliSense command.</param>
        /// <param name="checkForAbsence">If true, checks for absence of a specific item (or that no items are returned from this CompletionProvider)</param>
290 291 292 293
        protected virtual async Task VerifyWorkerAsync(
            string code, int position,
            string expectedItemOrNull, string expectedDescriptionOrNull,
            SourceCodeKind sourceCodeKind,
294
            bool usePreviousCharAsTrigger, bool checkForAbsence,
295
            int? glyph, int? matchPriority, bool? hasSuggestionModeItem,
G
Gen Lu 已提交
296 297
            string displayTextSuffix,
            string inlineDescription)
298
        {
C
CyrusNajmabadi 已提交
299
            var document1 = WorkspaceFixture.UpdateDocument(code, sourceCodeKind);
C
CyrusNajmabadi 已提交
300
            await CheckResultsAsync(
D
dotnet-bot 已提交
301 302 303
                document1, position, expectedItemOrNull,
                expectedDescriptionOrNull, usePreviousCharAsTrigger,
                checkForAbsence, glyph, matchPriority,
G
Gen Lu 已提交
304
                hasSuggestionModeItem, displayTextSuffix, inlineDescription);
305

C
Cyrus Najmabadi 已提交
306
            if (await CanUseSpeculativeSemanticModelAsync(document1, position))
307
            {
C
CyrusNajmabadi 已提交
308
                var document2 = WorkspaceFixture.UpdateDocument(code, sourceCodeKind, cleanBeforeUpdate: false);
C
CyrusNajmabadi 已提交
309 310
                await CheckResultsAsync(
                    document2, position, expectedItemOrNull, expectedDescriptionOrNull,
311
                    usePreviousCharAsTrigger, checkForAbsence, glyph, matchPriority,
G
Gen Lu 已提交
312
                    hasSuggestionModeItem, displayTextSuffix, inlineDescription);
313 314 315 316 317 318 319 320 321 322
            }
        }

        /// <summary>
        /// Override this to change parameters or return without verifying anything, e.g. for script sources. Or to test in other code contexts.
        /// </summary>
        /// <param name="codeBeforeCommit">The source code (not markup).</param>
        /// <param name="position">Position where intellisense is invoked.</param>
        /// <param name="itemToCommit">The item to commit from the completion provider.</param>
        /// <param name="expectedCodeAfterCommit">The expected code after commit.</param>
C
Cyrus Najmabadi 已提交
323
        protected virtual async Task VerifyCustomCommitProviderWorkerAsync(string codeBeforeCommit, int position, string itemToCommit, string expectedCodeAfterCommit, SourceCodeKind sourceCodeKind, char? commitChar = null)
324
        {
C
CyrusNajmabadi 已提交
325
            var document1 = WorkspaceFixture.UpdateDocument(codeBeforeCommit, sourceCodeKind);
C
Cyrus Najmabadi 已提交
326
            await VerifyCustomCommitProviderCheckResultsAsync(document1, codeBeforeCommit, position, itemToCommit, expectedCodeAfterCommit, commitChar);
327

C
Cyrus Najmabadi 已提交
328
            if (await CanUseSpeculativeSemanticModelAsync(document1, position))
329
            {
C
CyrusNajmabadi 已提交
330
                var document2 = WorkspaceFixture.UpdateDocument(codeBeforeCommit, sourceCodeKind, cleanBeforeUpdate: false);
C
Cyrus Najmabadi 已提交
331
                await VerifyCustomCommitProviderCheckResultsAsync(document2, codeBeforeCommit, position, itemToCommit, expectedCodeAfterCommit, commitChar);
332 333 334
            }
        }

C
Cyrus Najmabadi 已提交
335
        private async Task VerifyCustomCommitProviderCheckResultsAsync(Document document, string codeBeforeCommit, int position, string itemToCommit, string expectedCodeAfterCommit, char? commitChar)
336
        {
C
CyrusNajmabadi 已提交
337
            var workspace = WorkspaceFixture.GetWorkspace();
338
            SetWorkspaceOptions(workspace);
I
Ivan Basov 已提交
339
            var textBuffer = WorkspaceFixture.CurrentDocument.TextBuffer;
340

341
            var service = GetCompletionService(workspace);
342 343
            var completionLlist = await GetCompletionListAsync(service, document, position, RoslynCompletion.CompletionTrigger.Invoke);
            var items = completionLlist.Items;
344
            var firstItem = items.First(i => CompareItems(i.DisplayText, itemToCommit));
345

346
            if (service.GetTestAccessor().ExclusiveProviders?[0] is ICustomCommitCompletionProvider customCommitCompletionProvider)
347
            {
C
CyrusNajmabadi 已提交
348
                var completionRules = GetCompletionHelper(document);
I
Ivan Basov 已提交
349
                var textView = WorkspaceFixture.CurrentDocument.GetTextView();
C
CyrusNajmabadi 已提交
350
                VerifyCustomCommitWorker(service, customCommitCompletionProvider, firstItem, completionRules, textView, textBuffer, codeBeforeCommit, expectedCodeAfterCommit, commitChar);
351 352 353
            }
            else
            {
354
                await VerifyCustomCommitWorkerAsync(service, document, firstItem, completionLlist.Span, codeBeforeCommit, expectedCodeAfterCommit, commitChar);
355 356 357
            }
        }

358 359 360 361
        protected virtual void SetWorkspaceOptions(TestWorkspace workspace)
        {
        }

C
CyrusNajmabadi 已提交
362 363
        internal async Task VerifyCustomCommitWorkerAsync(
            CompletionServiceWithProviders service,
M
Matt Warren 已提交
364
            Document document,
365
            RoslynCompletion.CompletionItem completionItem,
366
            TextSpan completionListSpan,
M
Matt Warren 已提交
367 368 369 370
            string codeBeforeCommit,
            string expectedCodeAfterCommit,
            char? commitChar = null)
        {
C
CyrusNajmabadi 已提交
371
            MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition);
M
Matt Warren 已提交
372

373
            if (commitChar.HasValue &&
374
                !CommitManager.IsCommitCharacter(service.GetRules(), completionItem, commitChar.Value, commitChar.Value.ToString()))
M
Matt Warren 已提交
375 376 377 378 379
            {
                Assert.Equal(codeBeforeCommit, actualExpectedCode);
                return;
            }

G
Gen Lu 已提交
380 381 382 383
            // textview is created lazily, so need to access it before making 
            // changes to document, so the cursor position is tracked correctly.
            var textView = WorkspaceFixture.CurrentDocument.GetTextView();

384
            var commit = await service.GetChangeAsync(document, completionItem, completionListSpan, commitChar, CancellationToken.None);
M
Matt Warren 已提交
385

C
CyrusNajmabadi 已提交
386
            var text = await document.GetTextAsync();
C
CyrusNajmabadi 已提交
387
            var newText = text.WithChanges(commit.TextChange);
M
Matt Warren 已提交
388 389 390
            var newDoc = document.WithText(newText);
            document.Project.Solution.Workspace.TryApplyChanges(newDoc.Project.Solution);

I
Ivan Basov 已提交
391
            var textBuffer = WorkspaceFixture.CurrentDocument.TextBuffer;
M
Matt Warren 已提交
392 393 394 395 396 397 398 399

            string actualCodeAfterCommit = textBuffer.CurrentSnapshot.AsText().ToString();
            var caretPosition = commit.NewPosition != null ? commit.NewPosition.Value : textView.Caret.Position.BufferPosition.Position;

            Assert.Equal(actualExpectedCode, actualCodeAfterCommit);
            Assert.Equal(expectedCaretPosition, caretPosition);
        }

400
        internal virtual void VerifyCustomCommitWorker(
C
CyrusNajmabadi 已提交
401
            CompletionService service,
402
            ICustomCommitCompletionProvider customCommitCompletionProvider,
403
            RoslynCompletion.CompletionItem completionItem,
M
Matt Warren 已提交
404
            CompletionHelper completionRules,
405 406 407 408 409 410
            ITextView textView,
            ITextBuffer textBuffer,
            string codeBeforeCommit,
            string expectedCodeAfterCommit,
            char? commitChar = null)
        {
C
CyrusNajmabadi 已提交
411
            MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition);
412

C
CyrusNajmabadi 已提交
413
            if (commitChar.HasValue &&
414
                !CommitManager.IsCommitCharacter(service.GetRules(), completionItem, commitChar.Value, commitChar.Value.ToString()))
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
            {
                Assert.Equal(codeBeforeCommit, actualExpectedCode);
                return;
            }

            customCommitCompletionProvider.Commit(completionItem, textView, textBuffer, textView.TextSnapshot, commitChar);

            string actualCodeAfterCommit = textBuffer.CurrentSnapshot.AsText().ToString();
            var caretPosition = textView.Caret.Position.BufferPosition.Position;

            Assert.Equal(actualExpectedCode, actualCodeAfterCommit);
            Assert.Equal(expectedCaretPosition, caretPosition);
        }

        /// <summary>
        /// Override this to change parameters or return without verifying anything, e.g. for script sources. Or to test in other code contexts.
        /// </summary>
        /// <param name="codeBeforeCommit">The source code (not markup).</param>
        /// <param name="position">Position where intellisense is invoked.</param>
        /// <param name="itemToCommit">The item to commit from the completion provider.</param>
        /// <param name="expectedCodeAfterCommit">The expected code after commit.</param>
C
Cyrus Najmabadi 已提交
436
        protected virtual async Task VerifyProviderCommitWorkerAsync(string codeBeforeCommit, int position, string itemToCommit, string expectedCodeAfterCommit,
437 438
            char? commitChar, string textTypedSoFar, SourceCodeKind sourceCodeKind)
        {
C
CyrusNajmabadi 已提交
439
            var document1 = WorkspaceFixture.UpdateDocument(codeBeforeCommit, sourceCodeKind);
C
Cyrus Najmabadi 已提交
440
            await VerifyProviderCommitCheckResultsAsync(document1, position, itemToCommit, expectedCodeAfterCommit, commitChar, textTypedSoFar);
441

C
Cyrus Najmabadi 已提交
442
            if (await CanUseSpeculativeSemanticModelAsync(document1, position))
443
            {
C
CyrusNajmabadi 已提交
444
                var document2 = WorkspaceFixture.UpdateDocument(codeBeforeCommit, sourceCodeKind, cleanBeforeUpdate: false);
C
Cyrus Najmabadi 已提交
445
                await VerifyProviderCommitCheckResultsAsync(document2, position, itemToCommit, expectedCodeAfterCommit, commitChar, textTypedSoFar);
446 447
            }
        }
D
dotnet-bot 已提交
448

C
CyrusNajmabadi 已提交
449 450
        private async Task VerifyProviderCommitCheckResultsAsync(
            Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char? commitCharOpt, string textTypedSoFar)
451
        {
C
CyrusNajmabadi 已提交
452
            var workspace = WorkspaceFixture.GetWorkspace();
I
Ivan Basov 已提交
453
            var textBuffer = WorkspaceFixture.CurrentDocument.TextBuffer;
454 455
            var textSnapshot = textBuffer.CurrentSnapshot.AsText();

456
            var service = GetCompletionService(workspace);
457 458
            var completionList = await GetCompletionListAsync(service, document, position, RoslynCompletion.CompletionTrigger.Invoke);
            var items = completionList.Items;
459
            var firstItem = items.First(i => CompareItems(i.DisplayText + i.DisplayTextSuffix, itemToCommit));
460

C
CyrusNajmabadi 已提交
461
            var completionRules = GetCompletionHelper(document);
462
            var commitChar = commitCharOpt ?? '\t';
463

464
            var text = await document.GetTextAsync();
465

C
CyrusNajmabadi 已提交
466
            if (commitChar == '\t' ||
467
                CommitManager.IsCommitCharacter(service.GetRules(), firstItem, commitChar, textTypedSoFar + commitChar))
468
            {
469
                var textChange = (await service.GetChangeAsync(document, firstItem, completionList.Span, commitChar, CancellationToken.None)).TextChange;
470 471 472 473 474 475 476 477 478 479 480

                // Adjust TextChange to include commit character, so long as it isn't TAB.
                if (commitChar != '\t')
                {
                    textChange = new TextChange(textChange.Span, textChange.NewText.TrimEnd(commitChar) + commitChar);
                }

                text = text.WithChanges(textChange);
            }
            else
            {
481
                // nothing was committed, but we should insert the commit character.
M
Matt Warren 已提交
482
                var textChange = new TextChange(new TextSpan(firstItem.Span.End, 0), commitChar.ToString());
483 484 485 486
                text = text.WithChanges(textChange);
            }

            Assert.Equal(expectedCodeAfterCommit, text.ToString());
487 488
        }

C
CyrusNajmabadi 已提交
489 490 491
        protected async Task VerifyItemInEditorBrowsableContextsAsync(
            string markup, string referencedCode, string item, int expectedSymbolsSameSolution, int expectedSymbolsMetadataReference,
            string sourceLanguage, string referencedLanguage, bool hideAdvancedMembers = false)
492
        {
C
Cyrus Najmabadi 已提交
493 494
            await VerifyItemWithMetadataReferenceAsync(markup, referencedCode, item, expectedSymbolsMetadataReference, sourceLanguage, referencedLanguage, hideAdvancedMembers);
            await VerifyItemWithProjectReferenceAsync(markup, referencedCode, item, expectedSymbolsSameSolution, sourceLanguage, referencedLanguage, hideAdvancedMembers);
495 496 497 498

            // If the source and referenced languages are different, then they cannot be in the same project
            if (sourceLanguage == referencedLanguage)
            {
C
Cyrus Najmabadi 已提交
499
                await VerifyItemInSameProjectAsync(markup, referencedCode, item, expectedSymbolsSameSolution, sourceLanguage, hideAdvancedMembers);
500 501 502
            }
        }

G
Gen Lu 已提交
503
        protected Task VerifyItemWithMetadataReferenceAsync(string markup, string metadataReferenceCode, string expectedItem, int expectedSymbols,
504 505
                                                           string sourceLanguage, string referencedLanguage, bool hideAdvancedMembers)
        {
G
Gen Lu 已提交
506 507 508 509 510 511 512 513
            var xmlString = CreateMarkupForProjectWithMetadataReference(markup, metadataReferenceCode, sourceLanguage, referencedLanguage);

            return VerifyItemWithReferenceWorkerAsync(xmlString, expectedItem, expectedSymbols, hideAdvancedMembers);
        }

        protected static string CreateMarkupForProjectWithMetadataReference(string markup, string metadataReferenceCode, string sourceLanguage, string referencedLanguage)
        {
            return string.Format(@"
514
<Workspace>
G
Gen Lu 已提交
515
    <Project Language=""{0}"" CommonReferences=""true"" AssemblyName=""Project1"">
G
Gen Lu 已提交
516
        <Document FilePath=""SourceDocument"">{1}</Document>
517
        <MetadataReferenceFromSource Language=""{2}"" CommonReferences=""true"" IncludeXmlDocComments=""true"" DocumentationMode=""Diagnose"">
G
Gen Lu 已提交
518
            <Document FilePath=""ReferencedDocument"">{3}</Document>
519 520 521 522 523
        </MetadataReferenceFromSource>
    </Project>
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), referencedLanguage, SecurityElement.Escape(metadataReferenceCode));
        }

C
Cyrus Najmabadi 已提交
524
        protected Task VerifyItemWithAliasedMetadataReferencesAsync(string markup, string metadataAlias, string expectedItem, int expectedSymbols,
525 526
                                                   string sourceLanguage, string referencedLanguage, bool hideAdvancedMembers)
        {
527
            var xmlString = CreateMarkupForProjectWithAliasedMetadataReference(markup, metadataAlias, "", sourceLanguage, referencedLanguage);
G
Gen Lu 已提交
528 529 530 531

            return VerifyItemWithReferenceWorkerAsync(xmlString, expectedItem, expectedSymbols, hideAdvancedMembers);
        }

532
        protected static string CreateMarkupForProjectWithAliasedMetadataReference(string markup, string metadataAlias, string referencedCode, string sourceLanguage, string referencedLanguage, bool hasGlobalAlias = true)
G
Gen Lu 已提交
533
        {
534
            var aliases = hasGlobalAlias ? $"{metadataAlias},{MetadataReferenceProperties.GlobalAlias}" : $"{metadataAlias}";
G
Gen Lu 已提交
535
            return string.Format(@"
536
<Workspace>
G
Gen Lu 已提交
537
    <Project Language=""{0}"" CommonReferences=""true"" AssemblyName=""Project1"">
G
Gen Lu 已提交
538
        <Document FilePath=""SourceDocument"">{1}</Document>
539 540
        <MetadataReferenceFromSource Language=""{2}"" CommonReferences=""true"" Aliases=""{3}"" IncludeXmlDocComments=""true"" DocumentationMode=""Diagnose"">
            <Document FilePath=""ReferencedDocument"">{4}</Document>
541 542
        </MetadataReferenceFromSource>
    </Project>
543
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), referencedLanguage, SecurityElement.Escape(aliases), SecurityElement.Escape(referencedCode));
G
Gen Lu 已提交
544 545 546 547 548
        }

        protected Task VerifyItemWithProjectReferenceAsync(string markup, string referencedCode, string expectedItem, int expectedSymbols, string sourceLanguage, string referencedLanguage, bool hideAdvancedMembers)
        {
            var xmlString = CreateMarkupForProjecWithProjectReference(markup, referencedCode, sourceLanguage, referencedLanguage);
549

C
Cyrus Najmabadi 已提交
550
            return VerifyItemWithReferenceWorkerAsync(xmlString, expectedItem, expectedSymbols, hideAdvancedMembers);
551 552
        }

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
        protected static string CreateMarkupForProjecWithAliasedProjectReference(string markup, string projectAlias, string referencedCode, string sourceLanguage, string referencedLanguage)
        {
            return string.Format(@"
<Workspace>
    <Project Language=""{0}"" CommonReferences=""true"" AssemblyName=""Project1"">
        <ProjectReference Alias=""{4}"">ReferencedProject</ProjectReference>
        <Document FilePath=""SourceDocument"">{1}</Document>
    </Project>
    <Project Language=""{2}"" CommonReferences=""true"" AssemblyName=""ReferencedProject"" IncludeXmlDocComments=""true"" DocumentationMode=""Diagnose"">
        <Document FilePath=""ReferencedDocument"">{3}</Document>
    </Project>
    
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), referencedLanguage, SecurityElement.Escape(referencedCode), SecurityElement.Escape(projectAlias));
        }

G
Gen Lu 已提交
568
        protected static string CreateMarkupForProjecWithProjectReference(string markup, string referencedCode, string sourceLanguage, string referencedLanguage)
569
        {
G
Gen Lu 已提交
570
            return string.Format(@"
571
<Workspace>
G
Gen Lu 已提交
572
    <Project Language=""{0}"" CommonReferences=""true"" AssemblyName=""Project1"">
573
        <ProjectReference>ReferencedProject</ProjectReference>
G
Gen Lu 已提交
574
        <Document FilePath=""SourceDocument"">{1}</Document>
575 576
    </Project>
    <Project Language=""{2}"" CommonReferences=""true"" AssemblyName=""ReferencedProject"" IncludeXmlDocComments=""true"" DocumentationMode=""Diagnose"">
G
Gen Lu 已提交
577
        <Document FilePath=""ReferencedDocument"">{3}</Document>
578 579 580
    </Project>
    
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), referencedLanguage, SecurityElement.Escape(referencedCode));
G
Gen Lu 已提交
581 582
        }

G
Gen Lu 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
        protected static string CreateMarkupForProjecWithVBProjectReference(string markup, string referencedCode, string sourceLanguage, string rootnamespace = "")
        {
            return string.Format(@"
<Workspace>
    <Project Language=""{0}"" CommonReferences=""true"" AssemblyName=""Project1"">
        <ProjectReference>ReferencedProject</ProjectReference>
        <Document FilePath=""SourceDocument"">{1}</Document>
    </Project>
    <Project Language=""{2}"" CommonReferences=""true"" AssemblyName=""ReferencedProject"" IncludeXmlDocComments=""true"" DocumentationMode=""Diagnose"">
        <Document FilePath=""ReferencedDocument"">{3}</Document>
        <CompilationOptions RootNamespace=""{4}""/>
    </Project>
    
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), LanguageNames.VisualBasic, SecurityElement.Escape(referencedCode), rootnamespace);
        }

G
Gen Lu 已提交
599 600 601
        private Task VerifyItemInSameProjectAsync(string markup, string referencedCode, string expectedItem, int expectedSymbols, string sourceLanguage, bool hideAdvancedMembers)
        {
            var xmlString = CreateMarkupForSingleProject(markup, referencedCode, sourceLanguage);
602

C
Cyrus Najmabadi 已提交
603
            return VerifyItemWithReferenceWorkerAsync(xmlString, expectedItem, expectedSymbols, hideAdvancedMembers);
604 605
        }

G
Gen Lu 已提交
606
        protected static string CreateMarkupForSingleProject(string markup, string referencedCode, string sourceLanguage)
607
        {
G
Gen Lu 已提交
608
            return string.Format(@"
609 610
<Workspace>
    <Project Language=""{0}"" CommonReferences=""true"">
G
Gen Lu 已提交
611 612 613
        <Document FilePath=""SourceDocument"">{1}</Document>
        <Document FilePath=""ReferencedDocument"">{2}</Document>
    </Project>    
614 615 616
</Workspace>", sourceLanguage, SecurityElement.Escape(markup), SecurityElement.Escape(referencedCode));
        }

C
CyrusNajmabadi 已提交
617 618
        private async Task VerifyItemWithReferenceWorkerAsync(
            string xmlString, string expectedItem, int expectedSymbols, bool hideAdvancedMembers)
619
        {
C
CyrusNajmabadi 已提交
620
            using (var testWorkspace = TestWorkspace.Create(xmlString))
621
            {
622
                var position = testWorkspace.Documents.Single(d => d.Name == "SourceDocument").CursorPosition.Value;
623
                var solution = testWorkspace.CurrentSolution;
624 625 626
                var documentId = testWorkspace.Documents.Single(d => d.Name == "SourceDocument").Id;
                var document = solution.GetDocument(documentId);

627
                testWorkspace.Options = testWorkspace.Options.WithChangedOption(CompletionOptions.HideAdvancedMembers, document.Project.Language, hideAdvancedMembers);
628

629
                var triggerInfo = RoslynCompletion.CompletionTrigger.Invoke;
630

631
                var completionService = GetCompletionService(testWorkspace);
C
CyrusNajmabadi 已提交
632
                var completionList = await GetCompletionListAsync(completionService, document, position, triggerInfo);
633 634 635

                if (expectedSymbols >= 1)
                {
636
                    AssertEx.Any(completionList.Items, c => CompareItems(c.DisplayText, expectedItem));
637

638
                    var item = completionList.Items.First(c => CompareItems(c.DisplayText, expectedItem));
M
Matt Warren 已提交
639
                    var description = await completionService.GetDescriptionAsync(document, item);
640 641 642

                    if (expectedSymbols == 1)
                    {
M
Matt Warren 已提交
643
                        Assert.DoesNotContain("+", description.Text, StringComparison.Ordinal);
644 645 646
                    }
                    else
                    {
M
Matt Warren 已提交
647
                        Assert.Contains(GetExpectedOverloadSubstring(expectedSymbols), description.Text, StringComparison.Ordinal);
648 649 650 651
                    }
                }
                else
                {
652
                    if (completionList != null)
653
                    {
654
                        AssertEx.None(completionList.Items, c => CompareItems(c.DisplayText, expectedItem));
655 656 657 658 659
                    }
                }
            }
        }

C
Cyrus Najmabadi 已提交
660
        protected Task VerifyItemWithMscorlib45Async(string markup, string expectedItem, string expectedDescription, string sourceLanguage)
661 662 663 664 665 666 667 668 669 670
        {
            var xmlString = string.Format(@"
<Workspace>
    <Project Language=""{0}"" CommonReferencesNet45=""true""> 
        <Document FilePath=""SourceDocument"">
{1}
        </Document>
    </Project>
</Workspace>", sourceLanguage, SecurityElement.Escape(markup));

C
Cyrus Najmabadi 已提交
671
            return VerifyItemWithMscorlib45WorkerAsync(xmlString, expectedItem, expectedDescription);
672 673
        }

C
CyrusNajmabadi 已提交
674 675
        private async Task VerifyItemWithMscorlib45WorkerAsync(
            string xmlString, string expectedItem, string expectedDescription)
676
        {
C
CyrusNajmabadi 已提交
677
            using (var testWorkspace = TestWorkspace.Create(xmlString))
678
            {
679
                var position = testWorkspace.Documents.Single(d => d.Name == "SourceDocument").CursorPosition.Value;
680
                var solution = testWorkspace.CurrentSolution;
681 682
                var documentId = testWorkspace.Documents.Single(d => d.Name == "SourceDocument").Id;
                var document = solution.GetDocument(documentId);
683

684
                var triggerInfo = RoslynCompletion.CompletionTrigger.Invoke;
685
                var completionService = GetCompletionService(testWorkspace);
C
CyrusNajmabadi 已提交
686
                var completionList = await GetCompletionListAsync(completionService, document, position, triggerInfo);
M
Matt Warren 已提交
687

688
                var item = completionList.Items.FirstOrDefault(i => i.DisplayText == expectedItem);
M
Matt Warren 已提交
689
                Assert.Equal(expectedDescription, (await completionService.GetDescriptionAsync(document, item)).Text);
690 691 692 693 694 695 696 697 698
            }
        }

        private const char NonBreakingSpace = (char)0x00A0;

        private string GetExpectedOverloadSubstring(int expectedSymbols)
        {
            if (expectedSymbols <= 1)
            {
699
                throw new ArgumentOutOfRangeException(nameof(expectedSymbols));
700 701
            }

702
            return "+" + NonBreakingSpace + (expectedSymbols - 1) + NonBreakingSpace + FeaturesResources.overload;
703 704
        }

C
Cyrus Najmabadi 已提交
705
        protected async Task VerifyItemInLinkedFilesAsync(string xmlString, string expectedItem, string expectedDescription)
706
        {
C
CyrusNajmabadi 已提交
707
            using (var testWorkspace = TestWorkspace.Create(xmlString))
708
            {
709
                var position = testWorkspace.Documents.First().CursorPosition.Value;
710 711 712
                var solution = testWorkspace.CurrentSolution;
                var textContainer = testWorkspace.Documents.First().TextBuffer.AsTextContainer();
                var currentContextDocumentId = testWorkspace.GetDocumentIdInCurrentContext(textContainer);
713
                var document = solution.GetDocument(currentContextDocumentId);
714

715
                var triggerInfo = RoslynCompletion.CompletionTrigger.Invoke;
716
                var completionService = GetCompletionService(testWorkspace);
C
CyrusNajmabadi 已提交
717
                var completionList = await GetCompletionListAsync(completionService, document, position, triggerInfo);
718

719
                var item = completionList.Items.Single(c => c.DisplayText == expectedItem);
720 721 722
                Assert.NotNull(item);
                if (expectedDescription != null)
                {
M
Matt Warren 已提交
723
                    var actualDescription = (await completionService.GetDescriptionAsync(document, item)).Text;
724 725 726 727
                    Assert.Equal(expectedDescription, actualDescription);
                }
            }
        }
728

729 730 731 732
        protected Task VerifyAtPositionAsync(
            string code, int position, string insertText, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
            SourceCodeKind sourceCodeKind, bool checkForAbsence,
733
            int? glyph, int? matchPriority, bool? hasSuggestionItem,
G
Gen Lu 已提交
734
            string displayTextSuffix, string inlineDescription = null)
735 736 737 738
        {
            code = code.Substring(0, position) + insertText + code.Substring(position);
            position += insertText.Length;

739 740 741
            return BaseVerifyWorkerAsync(code, position,
                expectedItemOrNull, expectedDescriptionOrNull,
                sourceCodeKind, usePreviousCharAsTrigger, checkForAbsence,
G
Gen Lu 已提交
742
                glyph, matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
743 744
        }

745 746 747
        protected Task VerifyAtPositionAsync(
            string code, int position, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
C
CyrusNajmabadi 已提交
748
            SourceCodeKind sourceCodeKind, bool checkForAbsence, int? glyph,
G
Gen Lu 已提交
749
            int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription = null)
750
        {
751 752 753
            return VerifyAtPositionAsync(
                code, position, string.Empty, usePreviousCharAsTrigger,
                expectedItemOrNull, expectedDescriptionOrNull, sourceCodeKind, checkForAbsence,
G
Gen Lu 已提交
754
                glyph, matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
755 756
        }

757 758 759
        protected async Task VerifyAtEndOfFileAsync(
            string code, int position, string insertText, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
D
dotnet-bot 已提交
760
            SourceCodeKind sourceCodeKind, bool checkForAbsence, int? glyph,
G
Gen Lu 已提交
761
            int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription = null)
762 763 764 765 766 767 768 769 770 771
        {
            // only do this if the placeholder was at the end of the text.
            if (code.Length != position)
            {
                return;
            }

            code = code.Substring(startIndex: 0, length: position) + insertText;
            position += insertText.Length;

772 773
            await BaseVerifyWorkerAsync(
                code, position, expectedItemOrNull, expectedDescriptionOrNull,
D
dotnet-bot 已提交
774
                sourceCodeKind, usePreviousCharAsTrigger, checkForAbsence, glyph,
G
Gen Lu 已提交
775
                matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
776 777
        }

778 779 780
        protected Task VerifyAtPosition_ItemPartiallyWrittenAsync(
            string code, int position, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
D
dotnet-bot 已提交
781
            SourceCodeKind sourceCodeKind, bool checkForAbsence, int? glyph,
G
Gen Lu 已提交
782
            int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription = null)
783
        {
784 785
            return VerifyAtPositionAsync(
                code, position, ItemPartiallyWritten(expectedItemOrNull), usePreviousCharAsTrigger,
C
CyrusNajmabadi 已提交
786
                expectedItemOrNull, expectedDescriptionOrNull, sourceCodeKind,
G
Gen Lu 已提交
787
                checkForAbsence, glyph, matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
788 789
        }

790 791 792
        protected Task VerifyAtEndOfFileAsync(
            string code, int position, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
D
dotnet-bot 已提交
793
            SourceCodeKind sourceCodeKind, bool checkForAbsence, int? glyph,
G
Gen Lu 已提交
794
            int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription = null)
795
        {
796
            return VerifyAtEndOfFileAsync(code, position, string.Empty, usePreviousCharAsTrigger,
C
CyrusNajmabadi 已提交
797
                expectedItemOrNull, expectedDescriptionOrNull, sourceCodeKind,
G
Gen Lu 已提交
798
                checkForAbsence, glyph, matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
799 800
        }

801 802 803
        protected Task VerifyAtEndOfFile_ItemPartiallyWrittenAsync(
            string code, int position, bool usePreviousCharAsTrigger,
            string expectedItemOrNull, string expectedDescriptionOrNull,
D
dotnet-bot 已提交
804
            SourceCodeKind sourceCodeKind, bool checkForAbsence, int? glyph,
G
Gen Lu 已提交
805
            int? matchPriority, bool? hasSuggestionItem, string displayTextSuffix, string inlineDescription = null)
806
        {
807 808 809
            return VerifyAtEndOfFileAsync(
                code, position, ItemPartiallyWritten(expectedItemOrNull), usePreviousCharAsTrigger,
                expectedItemOrNull, expectedDescriptionOrNull, sourceCodeKind, checkForAbsence,
G
Gen Lu 已提交
810
                glyph, matchPriority, hasSuggestionItem, displayTextSuffix, inlineDescription);
811 812
        }

C
CyrusNajmabadi 已提交
813
        protected void VerifyTextualTriggerCharacter(
814 815
            string markup, bool shouldTriggerWithTriggerOnLettersEnabled, bool shouldTriggerWithTriggerOnLettersDisabled)
        {
C
CyrusNajmabadi 已提交
816 817
            VerifyTextualTriggerCharacterWorker(markup, expectedTriggerCharacter: shouldTriggerWithTriggerOnLettersEnabled, triggerOnLetter: true);
            VerifyTextualTriggerCharacterWorker(markup, expectedTriggerCharacter: shouldTriggerWithTriggerOnLettersDisabled, triggerOnLetter: false);
818 819
        }

C
CyrusNajmabadi 已提交
820
        private void VerifyTextualTriggerCharacterWorker(
821 822
            string markup, bool expectedTriggerCharacter, bool triggerOnLetter)
        {
C
CyrusNajmabadi 已提交
823
            using (var workspace = CreateWorkspace(markup))
824 825 826 827 828 829
            {
                var document = workspace.Documents.Single();
                var position = document.CursorPosition.Value;
                var text = document.TextBuffer.CurrentSnapshot.AsText();
                var options = workspace.Options.WithChangedOption(
                    CompletionOptions.TriggerOnTypingLetters, document.Project.Language, triggerOnLetter);
830
                var trigger = RoslynCompletion.CompletionTrigger.CreateInsertionTrigger(text[position]);
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

                var service = GetCompletionService(workspace);
                var isTextualTriggerCharacterResult = service.ShouldTriggerCompletion(text, position + 1, trigger, options: options);

                if (expectedTriggerCharacter)
                {
                    var assertText = "'" + text.ToString(new TextSpan(position, 1)) + "' expected to be textual trigger character";
                    Assert.True(isTextualTriggerCharacterResult, assertText);
                }
                else
                {
                    var assertText = "'" + text.ToString(new TextSpan(position, 1)) + "' expected to NOT be textual trigger character";
                    Assert.False(isTextualTriggerCharacterResult, assertText);
                }
            }
        }

        protected async Task VerifyCommonCommitCharactersAsync(string initialMarkup, string textTypedSoFar)
        {
            var commitCharacters = new[]
            {
                ' ', '{', '}', '[', ']', '(', ')', '.', ',', ':',
                ';', '+', '-', '*', '/', '%', '&', '|', '^', '!',
                '~', '=', '<', '>', '?', '@', '#', '\'', '\"', '\\'
            };

            await VerifyCommitCharactersAsync(initialMarkup, textTypedSoFar, commitCharacters);
        }

        protected async Task VerifyCommitCharactersAsync(string initialMarkup, string textTypedSoFar, char[] validChars, char[] invalidChars = null)
        {
            Assert.NotNull(validChars);
            invalidChars = invalidChars ?? new[] { 'x' };

C
CyrusNajmabadi 已提交
865
            using (var workspace = CreateWorkspace(initialMarkup))
866 867 868 869 870 871 872
            {
                var hostDocument = workspace.DocumentWithCursor;
                var documentId = workspace.GetDocumentId(hostDocument);
                var document = workspace.CurrentSolution.GetDocument(documentId);
                var position = hostDocument.CursorPosition.Value;

                var service = GetCompletionService(workspace);
873
                var completionList = await GetCompletionListAsync(service, document, position, RoslynCompletion.CompletionTrigger.Invoke);
874 875 876 877
                var item = completionList.Items.First(i => i.DisplayText.StartsWith(textTypedSoFar));

                foreach (var ch in validChars)
                {
878
                    Assert.True(CommitManager.IsCommitCharacter(
C
CyrusNajmabadi 已提交
879
                        service.GetRules(), item, ch, textTypedSoFar + ch), $"Expected '{ch}' to be a commit character");
880 881 882 883
                }

                foreach (var ch in invalidChars)
                {
884
                    Assert.False(CommitManager.IsCommitCharacter(
C
CyrusNajmabadi 已提交
885
                        service.GetRules(), item, ch, textTypedSoFar + ch), $"Expected '{ch}' NOT to be a commit character");
886 887 888
                }
            }
        }
889 890 891 892

        protected async Task<ImmutableArray<CompletionItem>> GetCompletionItemsAsync(
            string markup, SourceCodeKind sourceCodeKind, bool usePreviousCharAsTrigger = false)
        {
I
Ivan Basov 已提交
893 894 895
            WorkspaceFixture.GetWorkspace(markup);
            var code = WorkspaceFixture.Code;
            var position = WorkspaceFixture.Position;
896 897 898 899 900 901 902 903 904 905 906
            var document = WorkspaceFixture.UpdateDocument(code, sourceCodeKind);

            var trigger = usePreviousCharAsTrigger
                ? CompletionTrigger.CreateInsertionTrigger(insertedCharacter: code.ElementAt(position - 1))
                : CompletionTrigger.Invoke;

            var completionService = GetCompletionService(document.Project.Solution.Workspace);
            var completionList = await GetCompletionListAsync(completionService, document, position, trigger);

            return completionList == null ? ImmutableArray<CompletionItem>.Empty : completionList.Items;
        }
907
    }
S
Sam Harwell 已提交
908
}