|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.HelperTests |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis; |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
| 10 | + using Microsoft.CodeAnalysis.Formatting; |
| 11 | + using Microsoft.CodeAnalysis.Text; |
| 12 | + using StyleCop.Analyzers.Helpers; |
| 13 | + using StyleCop.Analyzers.Test.Helpers; |
| 14 | + using Xunit; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Unit tests for the <see cref="IndentationHelper"/> class. |
| 18 | + /// </summary> |
| 19 | + public class IndentationHelperTests |
| 20 | + { |
| 21 | + public static readonly IEnumerable<object[]> IndentationTestData = new[] |
| 22 | + { |
| 23 | + // no indentation |
| 24 | + new object[] { string.Empty, 0, 4, 4 }, |
| 25 | + |
| 26 | + // 1 space |
| 27 | + new object[] { " ", 0, 4, 4 }, |
| 28 | + |
| 29 | + // 2 spaces |
| 30 | + new object[] { " ", 1, 4, 4 }, |
| 31 | + |
| 32 | + // 3 spaces |
| 33 | + new object[] { " ", 1, 4, 4 }, |
| 34 | + |
| 35 | + // 3 spaces, indentation size = 2 |
| 36 | + new object[] { " ", 1, 4, 4 }, |
| 37 | + |
| 38 | + // 4 spaces |
| 39 | + new object[] { " ", 1, 4, 4 }, |
| 40 | + |
| 41 | + // 5 spaces |
| 42 | + new object[] { " ", 1, 4, 4 }, |
| 43 | + |
| 44 | + // 6 spaces |
| 45 | + new object[] { " ", 2, 4, 4 }, |
| 46 | + |
| 47 | + // 7 spaces |
| 48 | + new object[] { " ", 2, 4, 4 }, |
| 49 | + |
| 50 | + // 8 spaces |
| 51 | + new object[] { " ", 2, 4, 4 }, |
| 52 | + |
| 53 | + // 8 spaces indentation, indentation size = 2 |
| 54 | + new object[] { " ", 4, 2, 4 }, |
| 55 | + |
| 56 | + // 9 spaces indentation, indentation size = 3 |
| 57 | + new object[] { " ", 3, 3, 4 }, |
| 58 | + |
| 59 | + // single tab |
| 60 | + new object[] { "\t", 1, 4, 4 }, |
| 61 | + |
| 62 | + // multiple tabs |
| 63 | + new object[] { "\t\t\t", 3, 4, 4 }, |
| 64 | + |
| 65 | + // multiple tabs (difference between indentation size and tab size) |
| 66 | + new object[] { "\t\t\t", 6, 3, 6 }, |
| 67 | + |
| 68 | + // spaces + tabs mix (regression for #1036) |
| 69 | + new object[] { " \t \t \t \t", 4, 4, 4 }, |
| 70 | + |
| 71 | + // 1 space followed by a tab |
| 72 | + new object[] { " \t", 1, 4, 4 }, |
| 73 | + |
| 74 | + // 2 spaces followed by a tab |
| 75 | + new object[] { " \t", 1, 4, 4 }, |
| 76 | + |
| 77 | + // 3 spaces followed by a tab |
| 78 | + new object[] { " \t", 1, 4, 4 }, |
| 79 | + |
| 80 | + // 4 spaces followed by a tab |
| 81 | + new object[] { " \t", 2, 4, 4 }, |
| 82 | + |
| 83 | + // tab followed by 1 space |
| 84 | + new object[] { "\t ", 1, 4, 4 }, |
| 85 | + |
| 86 | + // tab followed by 2 space |
| 87 | + new object[] { "\t ", 2, 4, 4 }, |
| 88 | + |
| 89 | + // tab followed by 3 spaces |
| 90 | + new object[] { "\t ", 2, 4, 4 }, |
| 91 | + |
| 92 | + // tab followed by 4 spaces |
| 93 | + new object[] { "\t ", 2, 4, 4 } |
| 94 | + }; |
| 95 | + |
| 96 | + private const string TestProjectName = "TestProject"; |
| 97 | + private const string TestFilename = "Test0.cs"; |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Verify the workings of IndentationHelper.GetIndentationSteps./> |
| 101 | + /// </summary> |
| 102 | + /// <param name="indentationString">The indentation string to use with the test.</param> |
| 103 | + /// <param name="expectedIndentationSteps">The expected number of indentation steps.</param> |
| 104 | + /// <param name="indentationSize">The indentation size to use.</param> |
| 105 | + /// <param name="tabSize">The tab size to use.</param> |
| 106 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 107 | + [Theory] |
| 108 | + [MemberData(nameof(IndentationTestData))] |
| 109 | + public async Task VerifyGetIndentationStepsAsync(string indentationString, int expectedIndentationSteps, int indentationSize, int tabSize) |
| 110 | + { |
| 111 | + var testSource = $"{indentationString}public class TestClass {{}}"; |
| 112 | + var document = CreateTestDocument(testSource, indentationSize, false, tabSize); |
| 113 | + var indentationOptions = IndentationOptions.FromDocument(document); |
| 114 | + |
| 115 | + var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); |
| 116 | + var firstToken = syntaxRoot.GetFirstToken(); |
| 117 | + |
| 118 | + Assert.Equal(expectedIndentationSteps, IndentationHelper.GetIndentationSteps(indentationOptions, firstToken)); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Verify the that IndentationHelper.GetIndentationSteps will return zero (0) for tokens that are not the first token on a line. |
| 123 | + /// </summary> |
| 124 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 125 | + [Fact] |
| 126 | + public async Task VerifyGetIndentationStepsForTokenNotAtStartOfLineAsync() |
| 127 | + { |
| 128 | + var testSource = " public class TestClass {}"; |
| 129 | + var document = CreateTestDocument(testSource); |
| 130 | + var indentationOptions = IndentationOptions.FromDocument(document); |
| 131 | + |
| 132 | + var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); |
| 133 | + var secondToken = syntaxRoot.GetFirstToken().GetNextToken(); |
| 134 | + |
| 135 | + Assert.Equal(0, IndentationHelper.GetIndentationSteps(indentationOptions, secondToken)); |
| 136 | + } |
| 137 | + |
| 138 | + private static Document CreateTestDocument(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4) |
| 139 | + { |
| 140 | + var workspace = new AdhocWorkspace(); |
| 141 | + workspace.Options = workspace.Options |
| 142 | + .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, indentationSize) |
| 143 | + .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, useTabs) |
| 144 | + .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, tabSize); |
| 145 | + |
| 146 | + var projectId = ProjectId.CreateNewId(); |
| 147 | + var documentId = DocumentId.CreateNewId(projectId); |
| 148 | + var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); |
| 149 | + |
| 150 | + var solution = workspace.CurrentSolution |
| 151 | + .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp) |
| 152 | + .WithProjectCompilationOptions(projectId, compilationOptions) |
| 153 | + .AddMetadataReference(projectId, MetadataReferences.CorlibReference) |
| 154 | + .AddMetadataReference(projectId, MetadataReferences.SystemReference) |
| 155 | + .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference) |
| 156 | + .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference) |
| 157 | + .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference) |
| 158 | + .AddDocument(documentId, TestFilename, SourceText.From(source)); |
| 159 | + |
| 160 | + return solution.GetDocument(documentId); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments