CommentStructureTests.cs 4.3 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.Linq;
4
using System.Threading.Tasks;
5
using Microsoft.CodeAnalysis.CSharp.Structure;
C
CyrusNajmabadi 已提交
6
using Microsoft.CodeAnalysis.Editor.UnitTests.Structure;
7
using Microsoft.CodeAnalysis.Structure;
8
using Roslyn.Test.Utilities;
9
using Roslyn.Utilities;
10 11
using Xunit;

C
CyrusNajmabadi 已提交
12
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure
13
{
C
CyrusNajmabadi 已提交
14
    public class CommentTests : AbstractSyntaxStructureProviderTests
15
    {
16 17
        protected override string LanguageName => LanguageNames.CSharp;

C
CyrusNajmabadi 已提交
18
        internal override async Task<BlockSpan[]> GetBlockSpansAsync(Document document, int position)
19
        {
20
            var root = await document.GetSyntaxRootAsync();
21 22 23 24 25 26
            var trivia = root.FindTrivia(position, findInsideTrivia: true);

            var token = trivia.Token;

            if (token.LeadingTrivia.Contains(trivia))
            {
27
                return CSharpStructureHelpers.CreateCommentBlockSpan(token.LeadingTrivia).ToArray();
28 29 30
            }
            else if (token.TrailingTrivia.Contains(trivia))
            {
31
                return CSharpStructureHelpers.CreateCommentBlockSpan(token.TrailingTrivia).ToArray();
32 33 34
            }
            else
            {
35
                return Contract.FailWithReturn<BlockSpan[]>();
36 37 38
            }
        }

39
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
40
        public async Task TestSimpleComment1()
41
        {
42 43 44 45 46 47 48 49
            const string code = @"
{|span:// Hello
// $$C#|}
class C
{
}
";

C
CyrusNajmabadi 已提交
50
            await VerifyBlockSpansAsync(code,
51
                Region("span", "// Hello ...", autoCollapse: true));
52 53
        }

54
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
55
        public async Task TestSimpleComment2()
56
        {
57 58 59 60 61 62 63 64 65
            const string code = @"
{|span:// Hello
//
// $$C#!|}
class C
{
}
";

C
CyrusNajmabadi 已提交
66
            await VerifyBlockSpansAsync(code,
67
                Region("span", "// Hello ...", autoCollapse: true));
68 69
        }

70
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
71
        public async Task TestSimpleComment3()
72
        {
73 74 75 76 77 78 79 80 81
            const string code = @"
{|span:// Hello

// $$C#!|}
class C
{
}
";

C
CyrusNajmabadi 已提交
82
            await VerifyBlockSpansAsync(code,
83
                Region("span", "// Hello ...", autoCollapse: true));
84 85
        }

86
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
87
        public async Task TestSingleLineCommentGroupFollowedByDocumentationComment()
88
        {
89 90 91 92 93 94 95 96 97 98
            const string code = @"
{|span:// Hello

// $$C#!|}
/// <summary></summary>
class C
{
}
";

C
CyrusNajmabadi 已提交
99
            await VerifyBlockSpansAsync(code,
100
                Region("span", "// Hello ...", autoCollapse: true));
101 102
        }

103
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
104
        public async Task TestMultilineComment1()
105
        {
106 107 108 109 110 111 112 113
            const string code = @"
{|span:/* Hello
$$C# */|}
class C
{
}
";

C
CyrusNajmabadi 已提交
114
            await VerifyBlockSpansAsync(code,
115
                Region("span", "/* Hello ...", autoCollapse: true));
116 117
        }

118
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
119
        public async Task TestMultilineCommentOnOneLine()
120
        {
121 122 123 124 125 126
            const string code = @"
{|span:/* Hello $$C# */|}
class C
{
}
";
127

C
CyrusNajmabadi 已提交
128
            await VerifyBlockSpansAsync(code,
129
                Region("span", "/* Hello C# ...", autoCollapse: true));
130
        }
131

132
        [WorkItem(791, "https://github.com/dotnet/roslyn/issues/791")]
J
Jared Parsons 已提交
133
        [WorkItem(1108049, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1108049")]
134
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
135
        public async Task TestIncompleteMultilineCommentZeroSpace()
136
        {
137 138
            const string code = @"
{|span:$$/*|}";
139

C
CyrusNajmabadi 已提交
140
            await VerifyBlockSpansAsync(code,
141
                Region("span", "/*  ...", autoCollapse: true));
142 143
        }

144
        [WorkItem(791, "https://github.com/dotnet/roslyn/issues/791")]
J
Jared Parsons 已提交
145
        [WorkItem(1108049, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1108049")]
146
        [Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
147
        public async Task TestIncompleteMultilineCommentSingleSpace()
148
        {
149 150
            const string code = @"
{|span:$$/* |}";
151

C
CyrusNajmabadi 已提交
152
            await VerifyBlockSpansAsync(code,
153
                Region("span", "/*  ...", autoCollapse: true));
154
        }
155 156
    }
}