SuggestionModeCompletionProviderTests.cs 16.9 KB
Newer Older
1 2 3
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Threading;
C
Cyrus Najmabadi 已提交
4
using System.Threading.Tasks;
5
using Microsoft.CodeAnalysis.Completion;
6
using Microsoft.CodeAnalysis.CSharp.Completion.SuggestionMode;
7 8 9 10 11 12 13 14
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.CompletionProviders
{
    public class SuggestionModeCompletionProviderTests : AbstractCSharpCompletionProviderTests
    {
15 16 17 18
        public SuggestionModeCompletionProviderTests(CSharpTestWorkspaceFixture workspaceFixture) : base(workspaceFixture)
        {
        }

M
Matt Warren 已提交
19
        internal override CompletionProvider CreateCompletionProvider()
20
        {
21
            return new CSharpSuggestionModeCompletionProvider();
22 23
        }

24
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
25
        public async Task AfterFirstExplicitArgument()
26
        {
C
Cyrus Najmabadi 已提交
27
            await VerifyNotBuilderAsync(AddInsideMethod(@"Func<int, int, int> f = (int x, i $$"));
28 29
        }

30
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
31
        public async Task AfterFirstImplicitArgument()
32
        {
C
Cyrus Najmabadi 已提交
33
            await VerifyNotBuilderAsync(AddInsideMethod(@"Func<int, int, int> f = (x, i $$"));
34 35
        }

36
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
37
        public async Task AfterFirstImplicitArgumentInMethodCall()
38 39 40 41 42 43 44 45 46 47 48
        {
            var markup = @"class c
{
    private void bar(Func<int, int, bool> f) { }
    
    private void foo()
    {
        bar((x, i $$
    }
}
";
C
Cyrus Najmabadi 已提交
49
            await VerifyNotBuilderAsync(markup);
50 51
        }

52
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
53
        public async Task AfterFirstExplicitArgumentInMethodCall()
54 55 56 57 58 59 60 61 62 63 64
        {
            var markup = @"class c
{
    private void bar(Func<int, int, bool> f) { }
    
    private void foo()
    {
        bar((int x, i $$
    }
}
";
C
Cyrus Najmabadi 已提交
65
            await VerifyNotBuilderAsync(markup);
66 67
        }

68
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
69
        public async Task DelegateTypeExpected1()
70 71 72 73 74 75 76 77 78 79 80 81 82
        {
            var markup = @"using System;

class c
{
    private void bar(Func<int, int, bool> f) { }
    
    private void foo()
    {
        bar($$
    }
}
";
C
Cyrus Najmabadi 已提交
83
            await VerifyBuilderAsync(markup);
84 85
        }

86
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
87
        public async Task DelegateTypeExpected2()
88
        {
C
Cyrus Najmabadi 已提交
89
            await VerifyBuilderAsync(AddUsingDirectives("using System;", AddInsideMethod(@"Func<int, int, int> f = $$")));
90 91
        }

92
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
93
        public async Task ObjectInitializerDelegateType()
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        {
            var markup = @"using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public Func<int> myfunc { get; set; }
}

class a
{
    void foo()
    {
        var b = new Program() { myfunc = $$
    }
}";
C
Cyrus Najmabadi 已提交
111
            await VerifyBuilderAsync(markup);
112 113
        }

J
Jared Parsons 已提交
114
        [Fact, WorkItem(817145, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/817145"), Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
115
        public async Task ExplicitArrayInitializer()
116 117 118 119 120 121 122 123 124 125
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        Func<int, int>[] myfunc = new Func<int, int>[] { $$;
    }
}";
C
Cyrus Najmabadi 已提交
126
            await VerifyBuilderAsync(markup);
127 128
        }

129
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
130
        public async Task ImplicitArrayInitializerUnknownType()
131 132 133 134 135 136 137 138 139 140
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        var a = new [] { $$;
    }
}";
C
Cyrus Najmabadi 已提交
141
            await VerifyNotBuilderAsync(markup);
142 143
        }

144
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
145
        public async Task ImplicitArrayInitializerKnownDelegateType()
146 147 148 149 150 151 152 153 154 155
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        var a = new [] { x => 2 * x, $$
    }
}";
C
Cyrus Najmabadi 已提交
156
            await VerifyBuilderAsync(markup);
157 158
        }

159
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
160
        public async Task TernaryOperatorUnknownType()
161 162 163 164 165 166 167 168 169 170
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        var a = true ? $$
    }
}";
C
Cyrus Najmabadi 已提交
171
            await VerifyNotBuilderAsync(markup);
172 173
        }

174
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
175
        public async Task TernaryOperatorKnownDelegateType1()
176 177 178 179 180 181 182 183 184 185
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        var a = true ? x => x * 2 : $$
    }
}";
C
Cyrus Najmabadi 已提交
186
            await VerifyBuilderAsync(markup);
187 188
        }

189
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
190
        public async Task TernaryOperatorKnownDelegateType2()
191 192 193 194 195 196 197 198 199 200
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        Func<int, int> a = true ? $$
    }
}";
C
Cyrus Najmabadi 已提交
201
            await VerifyBuilderAsync(markup);
202 203
        }

204
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
205
        public async Task OverloadTakesADelegate1()
206 207 208 209 210 211 212 213 214 215 216 217 218
        {
            var markup = @"using System;

class a
{
    void foo(int a) { }
    void foo(Func<int, int> a) { }

    void bar()
    {
        this.foo($$
    }
}";
C
Cyrus Najmabadi 已提交
219
            await VerifyBuilderAsync(markup);
220 221
        }

222
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
223
        public async Task OverloadTakesDelegate2()
224 225 226 227 228 229 230 231 232 233 234 235 236
        {
            var markup = @"using System;

class a
{
    void foo(int i, int a) { }
    void foo(int i, Func<int, int> a) { }

    void bar()
    {
        this.foo(1, $$
    }
}";
C
Cyrus Najmabadi 已提交
237
            await VerifyBuilderAsync(markup);
238 239
        }

240
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
241
        public async Task ExplicitCastToDelegate()
242 243 244 245 246 247 248 249 250 251 252
        {
            var markup = @"using System;

class a
{

    void bar()
    {
        (Func<int, int>) ($$
    }
}";
C
Cyrus Najmabadi 已提交
253
            await VerifyBuilderAsync(markup);
254 255
        }

256
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
J
Jared Parsons 已提交
257
        [WorkItem(860580, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/860580")]
C
Cyrus Najmabadi 已提交
258
        public async Task ReturnStatement()
259 260 261 262 263 264 265 266 267 268
        {
            var markup = @"using System;

class a
{
    Func<int, int> bar()
    {
        return $$
    }
}";
C
Cyrus Najmabadi 已提交
269
            await VerifyBuilderAsync(markup);
270 271
        }

272
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
273
        public async Task BuilderInAnonymousType1()
274 275 276 277 278 279 280 281 282 283
        {
            var markup = @"using System;

class a
{
    int bar()
    {
        var q = new {$$
    }
}";
C
Cyrus Najmabadi 已提交
284
            await VerifyBuilderAsync(markup);
285 286
        }

287
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
288
        public async Task BuilderInAnonymousType2()
289 290 291 292 293 294 295 296 297 298
        {
            var markup = @"using System;

class a
{
    int bar()
    {
        var q = new {$$ 1, 2 };
    }
}";
C
Cyrus Najmabadi 已提交
299
            await VerifyBuilderAsync(markup);
300 301
        }

302
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
303
        public async Task BuilderInAnonymousType3()
304 305 306 307 308 309 310 311 312
        {
            var markup = @"using System;
class a
{
    int bar()
    {
        var q = new {Name = 1, $$ };
    }
}";
C
Cyrus Najmabadi 已提交
313
            await VerifyBuilderAsync(markup);
314 315
        }

316
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
317
        public async Task BuilderInFromClause()
318 319 320 321 322 323 324 325 326 327 328
        {
            var markup = @"using System;
using System.Linq;

class a
{
    int bar()
    {
        var q = from $$
    }
}";
C
Cyrus Najmabadi 已提交
329
            await VerifyBuilderAsync(markup.ToString());
330 331
        }

J
Jared Parsons 已提交
332
        [WorkItem(823968, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/823968")]
333
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
334
        public async Task BuilderInJoinClause()
335 336 337 338 339 340 341 342 343 344 345 346 347 348
        {
            var markup = @"using System;
using System.Linq;
using System.Collections.Generic;

class a
{
    int bar()
    {
        var list = new List<int>();
        var q = from a in list
                join $$
    }
}";
C
Cyrus Najmabadi 已提交
349
            await VerifyBuilderAsync(markup.ToString());
350 351
        }

J
Jared Parsons 已提交
352
        [WorkItem(544290, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544290")]
353
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
354
        public async Task ParenthesizedLambdaArgument()
355 356 357 358 359 360 361 362 363
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler((a$$, e) => { });
    }
}";
C
Cyrus Najmabadi 已提交
364
            await VerifyBuilderAsync(markup);
365 366
        }

J
Jared Parsons 已提交
367
        [WorkItem(544379, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544379")]
368
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
369
        public async Task IncompleteParenthesizedLambdaArgument()
370 371 372 373 374 375 376 377 378
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler((a$$
    }
}";
C
Cyrus Najmabadi 已提交
379
            await VerifyBuilderAsync(markup);
380 381
        }

J
Jared Parsons 已提交
382
        [WorkItem(544379, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544379")]
383
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
384
        public async Task IncompleteNestedParenthesizedLambdaArgument()
385 386 387 388 389 390 391 392 393
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler(((a$$
    }
}";
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            await VerifyBuilderAsync(markup);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
        public async Task ParenthesizedExpression()
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        var x = (a$$
    }
}";
            await VerifyBuilderAsync(markup);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
        public async Task TupleExpressionAfterParen()
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        var x = (a$$, b)
    }
}";
            await VerifyBuilderAsync(markup);
423 424
        }

425 426 427 428 429 430 431 432 433 434 435 436 437 438
        public async Task TupleExpressionAfterComma()
        {
            var markup = @"using System;
class Program
{
    static void Main(string[] args)
    {
        var x = (a, b$$)
    }
}";
            await VerifyBuilderAsync(markup);
        }


J
Jared Parsons 已提交
439
        [WorkItem(546363, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546363")]
440
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
441
        public async Task BuilderForLinqExpression()
442 443 444 445 446 447 448 449 450 451 452
        {
            var markup = @"using System;
using System.Linq.Expressions;
 
public class Class
{
    public void Foo(Expression<Action<int>> arg)
    {
        Foo($$
    }
}";
C
Cyrus Najmabadi 已提交
453
            await VerifyBuilderAsync(markup);
454 455
        }

J
Jared Parsons 已提交
456
        [WorkItem(546363, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546363")]
457
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
458
        public async Task NotInTypeParameter()
459 460 461 462 463 464 465 466 467 468 469
        {
            var markup = @"using System;
using System.Linq.Expressions;
 
public class Class
{
    public void Foo(Expression<Action<int>> arg)
    {
        Enumerable.Empty<$$
    }
}";
C
Cyrus Najmabadi 已提交
470
            await VerifyNotBuilderAsync(markup);
471 472
        }

J
Jared Parsons 已提交
473
        [WorkItem(611477, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/611477")]
474
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
475
        public async Task ExtensionMethodFaultTolerance()
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        {
            var markup = @"using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
namespace Outer
{
    public struct ImmutableArray<T> : IEnumerable<T>
    {
        public IEnumerator<T> GetEnumerator()
        {
            throw new NotImplementedException();
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
 
    public static class ReadOnlyArrayExtensions
    {
        public static ImmutableArray<TResult> Select<T, TResult>(this ImmutableArray<T> array, Func<T, TResult> selector)
        {
            throw new NotImplementedException();
        }
    }
 
    namespace Inner
    {
        class Program
        {
            static void Main(string[] args)
            {
                args.Select($$
            }
        }
    }
}
";
C
Cyrus Najmabadi 已提交
517
            await VerifyBuilderAsync(markup);
518 519
        }

J
Jared Parsons 已提交
520
        [WorkItem(834609, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/834609")]
521
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
522
        public async Task LambdaWithAutomaticBraceCompletion()
523 524 525 526 527 528 529 530 531 532 533
        {
            var markup = @"using System;
using System;
 
public class Class
{
    public void Foo()
    {
        EventHandler h = (s$$)
    }
}";
C
Cyrus Najmabadi 已提交
534
            await VerifyBuilderAsync(markup);
535 536
        }

J
Jared Parsons 已提交
537
        [WorkItem(858112, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/858112")]
538
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
539
        public async Task ThisConstructorInitializer()
540 541 542 543 544 545
        {
            var markup = @"using System;
class X 
{ 
    X(Func<X> x) : this($$) { } 
}";
C
Cyrus Najmabadi 已提交
546
            await VerifyBuilderAsync(markup);
547 548
        }

J
Jared Parsons 已提交
549
        [WorkItem(858112, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/858112")]
550
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
551
        public async Task BaseConstructorInitializer()
552 553 554 555 556 557 558 559 560 561 562
        {
            var markup = @"using System;
class B
{
    public B(Func<B> x) {}
}

class D : B 
{ 
    D() : base($$) { } 
}";
C
Cyrus Najmabadi 已提交
563
            await VerifyBuilderAsync(markup);
564 565
        }

J
Jared Parsons 已提交
566
        [WorkItem(887842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/887842")]
567
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
568
        public async Task PreprocessorExpression()
569 570 571 572 573
        {
            var markup = @"class C
{
#if $$
}";
C
Cyrus Najmabadi 已提交
574
            await VerifyBuilderAsync(markup);
575 576
        }

J
Jared Parsons 已提交
577
        [WorkItem(967254, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/967254")]
578
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
C
Cyrus Najmabadi 已提交
579
        public async Task ImplicitArrayInitializerAfterNew()
580 581 582 583 584 585 586 587 588 589
        {
            var markup = @"using System;

class a
{
    void foo()
    {
        int[] a = new $$;
    }
}";
C
Cyrus Najmabadi 已提交
590
            await VerifyBuilderAsync(markup);
591 592
        }

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        [WorkItem(7213, "https://github.com/dotnet/roslyn/issues/7213")]
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
        public async Task NamespaceDeclaration_Unqualified()
        {
            var markup = @"namespace $$";
            await VerifyBuilderAsync(markup);
        }

        [WorkItem(7213, "https://github.com/dotnet/roslyn/issues/7213")]
        [Fact, Trait(Traits.Feature, Traits.Features.Completion)]
        public async Task NamespaceDeclaration_Qualified()
        {
            var markup = @"namespace A.$$";
            await VerifyBuilderAsync(markup);
        }

C
Cyrus Najmabadi 已提交
609
        private async Task VerifyNotBuilderAsync(string markup)
610
        {
C
Cyrus Najmabadi 已提交
611
            await VerifyWorkerAsync(markup, isBuilder: false);
612 613
        }

C
Cyrus Najmabadi 已提交
614
        private async Task VerifyBuilderAsync(string markup)
615
        {
C
Cyrus Najmabadi 已提交
616
            await VerifyWorkerAsync(markup, isBuilder: true);
617 618
        }

C
Cyrus Najmabadi 已提交
619
        private async Task VerifyWorkerAsync(string markup, bool isBuilder)
620 621 622 623 624 625 626
        {
            string code;
            int position;
            MarkupTestFile.GetPosition(markup, out code, out position);

            using (var workspaceFixture = new CSharpTestWorkspaceFixture())
            {
C
Cyrus Najmabadi 已提交
627
                var document1 = await workspaceFixture.UpdateDocumentAsync(code, SourceCodeKind.Regular);
628
                await CheckResultsAsync(document1, position, isBuilder);
629

630
                if (await CanUseSpeculativeSemanticModelAsync(document1, position))
631
                {
C
Cyrus Najmabadi 已提交
632
                    var document2 = await workspaceFixture.UpdateDocumentAsync(code, SourceCodeKind.Regular, cleanBeforeUpdate: false);
633
                    await CheckResultsAsync(document2, position, isBuilder);
634 635 636 637
                }
            }
        }

638
        private async Task CheckResultsAsync(Document document, int position, bool isBuilder)
639
        {
M
Matt Warren 已提交
640
            var triggerInfo = CompletionTrigger.CreateInsertionTrigger('a');
641 642 643 644
            var service = GetCompletionService(document.Project.Solution.Workspace);
            var completionList = await service.GetContextAsync(
                service.ExclusiveProviders?[0], document, position, triggerInfo,
                options: null, cancellationToken: CancellationToken.None);
645 646 647

            if (isBuilder)
            {
648
                Assert.NotNull(completionList);
M
Matt Warren 已提交
649
                Assert.NotNull(completionList.SuggestionModeItem);
650 651 652
            }
            else
            {
653
                if (completionList != null)
654
                {
M
Matt Warren 已提交
655
                    Assert.True(completionList.SuggestionModeItem == null, "group.Builder == " + (completionList.SuggestionModeItem != null ? completionList.SuggestionModeItem.DisplayText : "null"));
656 657 658 659 660
                }
            }
        }
    }
}