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

P
Paul Chen 已提交
3 4
using System;
using System.Linq;
5
using System.Threading;
C
Cyrus Najmabadi 已提交
6
using System.Threading.Tasks;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editor.CSharp.QuickInfo;
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.QuickInfo;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.UnitTests.QuickInfo;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Projection;
using Microsoft.VisualStudio.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.QuickInfo
{
    public class SyntacticQuickInfoSourceTests : AbstractQuickInfoSourceTests
    {
29
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
30
        public async Task Brackets_0()
31
        {
C
Cyrus Najmabadi 已提交
32
            await TestInMethodAndScriptAsync(@"
33 34 35 36
            switch (true)
            {
            }$$
",
P
Paul Chen 已提交
37
            "switch (true)\r\n{");
38 39
        }

40
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
41
        public async Task Brackets_1()
P
Paul Chen 已提交
42
        {
C
Cyrus Najmabadi 已提交
43
            await TestInClassAsync("int Property { get; }$$ ", "int Property {");
P
Paul Chen 已提交
44 45 46
        }

        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
47
        public async Task Brackets_2()
P
Paul Chen 已提交
48
        {
C
Cyrus Najmabadi 已提交
49
            await TestInClassAsync("void M()\r\n{ }$$ ", "void M()\r\n{");
P
Paul Chen 已提交
50 51 52
        }

        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
53
        public async Task Brackets_3()
P
Paul Chen 已提交
54
        {
C
Cyrus Najmabadi 已提交
55
            await TestInMethodAndScriptAsync("var a = new int[] { }$$ ", "new int[] {");
P
Paul Chen 已提交
56 57 58
        }

        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
59
        public async Task Brackets_4()
60
        {
C
Cyrus Najmabadi 已提交
61
            await TestInMethodAndScriptAsync(@"
62 63 64 65
            if (true)
            {
            }$$
",
P
Paul Chen 已提交
66
            "if (true)\r\n{");
67 68
        }

P
Paul Chen 已提交
69
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
70
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
71
        public async Task ScopeBrackets_0()
72
        {
C
Cyrus Najmabadi 已提交
73
            await TestInMethodAndScriptAsync(@"
74 75 76 77 78 79
            if (true)
            {
                {
                }$$
            }
",
P
Paul Chen 已提交
80
            "{");
81 82
        }

P
Paul Chen 已提交
83
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
84
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
85
        public async Task ScopeBrackets_1()
86
        {
C
Cyrus Najmabadi 已提交
87
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
88
            while (true)
89 90 91 92 93 94 95 96 97
            {
                // some
                // comment
                {
                }$$
            }
",
@"// some
// comment
P
Paul Chen 已提交
98
{");
99 100
        }

P
Paul Chen 已提交
101
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
102
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
103
        public async Task ScopeBrackets_2()
104
        {
C
Cyrus Najmabadi 已提交
105
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
106
            do
107 108 109 110 111
            {
                /* comment */
                {
                }$$
            }
P
Paul Chen 已提交
112
            while (true);
113 114
",
@"/* comment */
P
Paul Chen 已提交
115
{");
116 117
        }

P
Paul Chen 已提交
118
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
119
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
120
        public async Task ScopeBrackets_3()
121
        {
C
Cyrus Najmabadi 已提交
122
            await TestInMethodAndScriptAsync(@"
123
            if (true)
P
Paul Chen 已提交
124 125 126
            {
            }
            else
127 128 129 130 131 132 133 134 135
            {
                {
                    // some
                    // comment
                }$$
            }
",
@"{
    // some
P
Paul Chen 已提交
136
    // comment");
137 138
        }

P
Paul Chen 已提交
139
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
140
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
141
        public async Task ScopeBrackets_4()
142
        {
C
Cyrus Najmabadi 已提交
143
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
144
            using (var x = new X())
145 146 147 148 149 150 151
            {
                {
                    /* comment */
                }$$
            }
",
@"{
P
Paul Chen 已提交
152
    /* comment */");
153 154
        }

P
Paul Chen 已提交
155 156
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
157
        public async Task ScopeBrackets_5()
P
Paul Chen 已提交
158
        {
C
Cyrus Najmabadi 已提交
159
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
160 161 162 163 164 165 166 167 168
            foreach (var x in xs)
            {
                // above
                {
                    /* below */
                }$$
            }
",
@"// above
P
Paul Chen 已提交
169
{");
P
Paul Chen 已提交
170 171 172 173
        }

        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
174
        public async Task ScopeBrackets_6()
P
Paul Chen 已提交
175
        {
C
Cyrus Najmabadi 已提交
176
            await TestInMethodAndScriptAsync(@"
177
            for (;;)
P
Paul Chen 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            {
                /*************/

                // part 1

                // part 2
                {
                }$$
            }
",
@"/*************/

// part 1

// part 2
P
Paul Chen 已提交
193
{");
P
Paul Chen 已提交
194 195 196 197
        }

        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
198
        public async Task ScopeBrackets_7()
P
Paul Chen 已提交
199
        {
C
Cyrus Najmabadi 已提交
200
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
            try
            {
                /*************/

                // part 1

                // part 2
                {
                }$$
            }
            catch { throw; }
",
@"/*************/

// part 1

// part 2
P
Paul Chen 已提交
218
{");
P
Paul Chen 已提交
219 220 221 222
        }

        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
223
        public async Task ScopeBrackets_8()
P
Paul Chen 已提交
224
        {
C
Cyrus Najmabadi 已提交
225
            await TestInMethodAndScriptAsync(@"
P
Paul Chen 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
            {
                /*************/

                // part 1

                // part 2
            }$$
",
@"{
    /*************/

    // part 1

P
Paul Chen 已提交
239
    // part 2");
P
Paul Chen 已提交
240 241 242 243
        }

        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
244
        public async Task ScopeBrackets_9()
P
Paul Chen 已提交
245
        {
C
Cyrus Najmabadi 已提交
246
            await TestInClassAsync(@"
P
Paul Chen 已提交
247 248 249 250 251 252 253 254 255
            int Property
            {
                set
                {
                    {
                    }$$
                }
            }
",
P
Paul Chen 已提交
256
            "{");
P
Paul Chen 已提交
257 258
        }

259 260
        [WorkItem(325, "https://github.com/dotnet/roslyn/issues/325")]
        [WpfFact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
C
Cyrus Najmabadi 已提交
261
        public async Task ScopeBrackets_10()
262
        {
C
Cyrus Najmabadi 已提交
263
            await TestInMethodAndScriptAsync(@"
264 265 266 267 268 269 270 271 272 273 274 275 276
            switch (true)
            {
                default:
                    // comment
                    {
                    }$$
                    break;
            }
",
@"// comment
{");
        }

277 278 279 280 281 282 283 284 285 286
        private IQuickInfoProvider CreateProvider(TestWorkspace workspace)
        {
            return new SyntacticQuickInfoProvider(
                workspace.GetService<IProjectionBufferFactoryService>(),
                workspace.GetService<IEditorOptionsFactoryService>(),
                workspace.GetService<ITextEditorFactoryService>(),
                workspace.GetService<IGlyphService>(),
                workspace.GetService<ClassificationTypeMap>());
        }

C
Cyrus Najmabadi 已提交
287
        protected override async Task AssertNoContentAsync(
288 289 290 291 292
            TestWorkspace workspace,
            Document document,
            int position)
        {
            var provider = CreateProvider(workspace);
C
Cyrus Najmabadi 已提交
293
            Assert.Null(await provider.GetItemAsync(document, position, CancellationToken.None));
294 295
        }

C
Cyrus Najmabadi 已提交
296
        protected override async Task AssertContentIsAsync(
297 298 299 300 301 302 303
            TestWorkspace workspace,
            Document document,
            int position,
            string expectedContent,
            string expectedDocumentationComment = null)
        {
            var provider = CreateProvider(workspace);
C
Cyrus Najmabadi 已提交
304
            var state = await provider.GetItemAsync(document, position, cancellationToken: CancellationToken.None);
305 306 307 308 309 310 311 312 313 314 315 316 317
            Assert.NotNull(state);

            var viewHostingControl = (ViewHostingControl)((ElisionBufferDeferredContent)state.Content).Create();
            try
            {
                var actualContent = viewHostingControl.ToString();
                Assert.Equal(expectedContent, actualContent);
            }
            finally
            {
                viewHostingControl.TextView_TestOnly.Close();
            }
        }
P
Paul Chen 已提交
318

C
Cyrus Najmabadi 已提交
319
        protected override Task TestInMethodAsync(string code, string expectedContent, string expectedDocumentationComment = null)
P
Paul Chen 已提交
320
        {
C
Cyrus Najmabadi 已提交
321
            return TestInClassAsync("void M(){" + code + "}", expectedContent, expectedDocumentationComment);
P
Paul Chen 已提交
322 323
        }

C
Cyrus Najmabadi 已提交
324
        protected override Task TestInClassAsync(string code, string expectedContent, string expectedDocumentationComment = null)
P
Paul Chen 已提交
325
        {
C
Cyrus Najmabadi 已提交
326
            return TestAsync("class C {" + code + "}", expectedContent, expectedDocumentationComment);
P
Paul Chen 已提交
327 328
        }

C
Cyrus Najmabadi 已提交
329
        protected override Task TestInScriptAsync(string code, string expectedContent, string expectedDocumentationComment = null)
P
Paul Chen 已提交
330
        {
C
Cyrus Najmabadi 已提交
331
            return TestAsync(code, expectedContent, expectedContent, Options.Script);
P
Paul Chen 已提交
332 333
        }

C
Cyrus Najmabadi 已提交
334
        protected override async Task TestAsync(
P
Paul Chen 已提交
335 336 337 338 339
            string code,
            string expectedContent,
            string expectedDocumentationComment = null,
            CSharpParseOptions parseOptions = null)
        {
C
Cyrus Najmabadi 已提交
340
            using (var workspace = await TestWorkspace.CreateCSharpAsync(code, parseOptions))
P
Paul Chen 已提交
341 342 343 344 345
            {
                var testDocument = workspace.Documents.Single();
                var position = testDocument.CursorPosition.Value;
                var document = workspace.CurrentSolution.Projects.First().Documents.First();

P
Paul Chen 已提交
346
                if (string.IsNullOrEmpty(expectedContent))
P
Paul Chen 已提交
347
                {
C
Cyrus Najmabadi 已提交
348
                    await AssertNoContentAsync(workspace, document, position);
P
Paul Chen 已提交
349 350 351
                }
                else
                {
C
Cyrus Najmabadi 已提交
352
                    await AssertContentIsAsync(workspace, document, position, expectedContent, expectedDocumentationComment);
P
Paul Chen 已提交
353 354 355
                }
            }
        }
356 357
    }
}