AbstractSyntaxStructureProviderTests.cs 5.3 KB
Newer Older
1 2 3 4
// 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;
using System.Collections.Generic;
C
CyrusNajmabadi 已提交
5
using System.Collections.Immutable;
6
using System.Linq;
7
using System.Threading.Tasks;
8
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
9
using Microsoft.CodeAnalysis.Structure;
10
using Microsoft.CodeAnalysis.Text;
C
CyrusNajmabadi 已提交
11
using Roslyn.Utilities;
12 13
using Xunit;

C
CyrusNajmabadi 已提交
14
namespace Microsoft.CodeAnalysis.Editor.UnitTests.Structure
15
{
C
CyrusNajmabadi 已提交
16
    public abstract class AbstractSyntaxStructureProviderTests
17 18 19
    {
        protected abstract string LanguageName { get; }

20
        protected virtual string WorkspaceKind => CodeAnalysis.WorkspaceKind.Test;
21

22
        private Task<ImmutableArray<BlockSpan>> GetBlockSpansAsync(Document document, int position)
C
CyrusNajmabadi 已提交
23
        {
24
            return GetBlockSpansWorkerAsync(document, position);
C
CyrusNajmabadi 已提交
25 26 27
        }

        internal abstract Task<ImmutableArray<BlockSpan>> GetBlockSpansWorkerAsync(Document document, int position);
28

C
CyrusNajmabadi 已提交
29
        protected async Task VerifyBlockSpansAsync(string markupCode, params Tuple<string, string, string, bool, bool>[] expectedRegionData)
30
        {
C
CyrusNajmabadi 已提交
31
            using (var workspace = TestWorkspace.Create(WorkspaceKind, LanguageName, compilationOptions: null, parseOptions: null, content: markupCode))
32 33 34 35 36
            {
                var hostDocument = workspace.Documents.Single();
                Assert.True(hostDocument.CursorPosition.HasValue, "Test must specify a position.");
                var position = hostDocument.CursorPosition.Value;

C
CyrusNajmabadi 已提交
37
                var expectedRegions = expectedRegionData.Select(data => CreateBlockSpan(data, hostDocument.AnnotatedSpans)).ToArray();
38 39

                var document = workspace.CurrentSolution.GetDocument(hostDocument.Id);
C
CyrusNajmabadi 已提交
40
                var actualRegions = await GetBlockSpansAsync(document, position);
41 42 43 44 45 46 47 48 49 50

                Assert.True(expectedRegions.Length == actualRegions.Length, $"Expected {expectedRegions.Length} regions but there were {actualRegions.Length}");

                for (int i = 0; i < expectedRegions.Length; i++)
                {
                    AssertRegion(expectedRegions[i], actualRegions[i]);
                }
            }
        }

C
CyrusNajmabadi 已提交
51
        protected async Task VerifyNoBlockSpansAsync(string markupCode)
52
        {
C
CyrusNajmabadi 已提交
53
            using (var workspace = TestWorkspace.Create(WorkspaceKind, LanguageName, compilationOptions: null, parseOptions: null, content: markupCode))
54 55 56 57 58 59
            {
                var hostDocument = workspace.Documents.Single();
                Assert.True(hostDocument.CursorPosition.HasValue, "Test must specify a position.");
                var position = hostDocument.CursorPosition.Value;

                var document = workspace.CurrentSolution.GetDocument(hostDocument.Id);
C
CyrusNajmabadi 已提交
60
                var actualRegions = await GetBlockSpansAsync(document, position);
61 62 63 64 65

                Assert.True(actualRegions.Length == 0, $"Expected no regions but found {actualRegions.Length}.");
            }
        }

66
        protected Tuple<string, string, string, bool, bool> Region(string textSpanName, string hintSpanName, string bannerText, bool autoCollapse, bool isDefaultCollapsed = false)
67
        {
68
            return Tuple.Create(textSpanName, hintSpanName, bannerText, autoCollapse, isDefaultCollapsed);
69 70
        }

71
        protected Tuple<string, string, string, bool, bool> Region(string textSpanName, string bannerText, bool autoCollapse, bool isDefaultCollapsed = false)
72
        {
73
            return Tuple.Create(textSpanName, textSpanName, bannerText, autoCollapse, isDefaultCollapsed);
74 75
        }

76 77 78
        private static BlockSpan CreateBlockSpan(
            Tuple<string, string, string, bool, bool> regionData,
            IDictionary<string, ImmutableArray<TextSpan>> spans)
79
        {
80
            var textSpanName = regionData.Item1;
81 82 83 84 85
            var hintSpanName = regionData.Item2;
            var bannerText = regionData.Item3;
            var autoCollapse = regionData.Item4;
            var isDefaultCollapsed = regionData.Item5;

86 87
            Assert.True(spans.ContainsKey(textSpanName) && spans[textSpanName].Length == 1, $"Test did not specify '{textSpanName}' span.");
            Assert.True(spans.ContainsKey(hintSpanName) && spans[hintSpanName].Length == 1, $"Test did not specify '{hintSpanName}' span.");
88

89
            var textSpan = spans[textSpanName][0];
90 91
            var hintSpan = spans[hintSpanName][0];

92
            return new BlockSpan(isCollapsible: true,
93
                textSpan: textSpan, 
94
                hintSpan: hintSpan,
C
CyrusNajmabadi 已提交
95
                type: BlockTypes.Nonstructural,
96 97 98
                bannerText: bannerText,
                autoCollapse: autoCollapse, 
                isDefaultCollapsed: isDefaultCollapsed);
99 100
        }

101
        internal static void AssertRegion(BlockSpan expected, BlockSpan actual)
102 103 104 105 106 107 108 109 110 111
        {
            Assert.Equal(expected.TextSpan.Start, actual.TextSpan.Start);
            Assert.Equal(expected.TextSpan.End, actual.TextSpan.End);
            Assert.Equal(expected.HintSpan.Start, actual.HintSpan.Start);
            Assert.Equal(expected.HintSpan.End, actual.HintSpan.End);
            Assert.Equal(expected.BannerText, actual.BannerText);
            Assert.Equal(expected.AutoCollapse, actual.AutoCollapse);
            Assert.Equal(expected.IsDefaultCollapsed, actual.IsDefaultCollapsed);
        }
    }
112
}