|
| 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.ReadabilityRules |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Microsoft.CodeAnalysis; |
| 10 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 11 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 12 | + using StyleCop.Analyzers.ReadabilityRules; |
| 13 | + using TestHelper; |
| 14 | + using Xunit; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Unit tests for the <see cref="SA1136EnumValuesShouldBeOnSeparateLines"/> analyzer. |
| 18 | + /// </summary> |
| 19 | + public class SA1136UnitTests : CodeFixVerifier |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// Verifies that an enum declaration with all values on the same line will return the expected diagnostics. |
| 23 | + /// This also verifies that an enum declaration with all values on separate lines will not produce diagnostics. |
| 24 | + /// </summary> |
| 25 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 26 | + [Fact] |
| 27 | + public async Task TestSingleLineEnumDeclarationAsync() |
| 28 | + { |
| 29 | + var testCode = @" |
| 30 | +public enum TestEnum |
| 31 | +{ |
| 32 | + FirstValue, SecondValue, ThirdValue |
| 33 | +} |
| 34 | +"; |
| 35 | + |
| 36 | + var fixedTestCode = @" |
| 37 | +public enum TestEnum |
| 38 | +{ |
| 39 | + FirstValue, |
| 40 | + SecondValue, |
| 41 | + ThirdValue |
| 42 | +} |
| 43 | +"; |
| 44 | + |
| 45 | + DiagnosticResult[] expected = |
| 46 | + { |
| 47 | + this.CSharpDiagnostic().WithLocation(4, 17), |
| 48 | + this.CSharpDiagnostic().WithLocation(4, 30), |
| 49 | + }; |
| 50 | + |
| 51 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 52 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 53 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Verifies that comments after a enum value declaration will not produce diagnostics. |
| 58 | + /// </summary> |
| 59 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 60 | + [Fact] |
| 61 | + public async Task TestEndOfLineCommentsAsync() |
| 62 | + { |
| 63 | + var testCode = @" |
| 64 | +public enum TestEnum |
| 65 | +{ |
| 66 | + FirstValue, /* comment 1 */ |
| 67 | + SecondValue, // comment 2 |
| 68 | +} |
| 69 | +"; |
| 70 | + |
| 71 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Verifies that inline comments between enum value declarations are handled properly. |
| 76 | + /// </summary> |
| 77 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 78 | + [Fact] |
| 79 | + public async Task TestInlineCommentsAsync() |
| 80 | + { |
| 81 | + var testCode = @" |
| 82 | +public enum TestEnum |
| 83 | +{ |
| 84 | + FirstValue, /* comment */ SecondValue, |
| 85 | +} |
| 86 | +"; |
| 87 | + |
| 88 | + var fixedTestCode = @" |
| 89 | +public enum TestEnum |
| 90 | +{ |
| 91 | + FirstValue, /* comment */ |
| 92 | + SecondValue, |
| 93 | +} |
| 94 | +"; |
| 95 | + |
| 96 | + DiagnosticResult[] expected = |
| 97 | + { |
| 98 | + this.CSharpDiagnostic().WithLocation(4, 31), |
| 99 | + }; |
| 100 | + |
| 101 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 102 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 103 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Verifies that values on the same line within a directive trivia are handled correctly. |
| 108 | + /// </summary> |
| 109 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 110 | + [Fact] |
| 111 | + public async Task TestDirectiveTriviaAreHandledProperlyAsync() |
| 112 | + { |
| 113 | + var testCode = @" |
| 114 | +public enum TestEnum |
| 115 | +{ |
| 116 | + FirstValue, |
| 117 | +#if !TEST |
| 118 | + SecondValue, ThirdValue, |
| 119 | +#endif |
| 120 | + FourthValue |
| 121 | +} |
| 122 | +"; |
| 123 | + |
| 124 | + var fixedTestCode = @" |
| 125 | +public enum TestEnum |
| 126 | +{ |
| 127 | + FirstValue, |
| 128 | +#if !TEST |
| 129 | + SecondValue, |
| 130 | + ThirdValue, |
| 131 | +#endif |
| 132 | + FourthValue |
| 133 | +} |
| 134 | +"; |
| 135 | + |
| 136 | + DiagnosticResult[] expected = |
| 137 | + { |
| 138 | + this.CSharpDiagnostic().WithLocation(6, 18), |
| 139 | + }; |
| 140 | + |
| 141 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 142 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 143 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Verifies that an enum declaration without a block is handled correctly. |
| 148 | + /// </summary> |
| 149 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 150 | + [Fact] |
| 151 | + public async Task TestEnumWithoutBlockAsync() |
| 152 | + { |
| 153 | + var testCode = @" |
| 154 | +public enum TestEnum |
| 155 | +"; |
| 156 | + |
| 157 | + DiagnosticResult[] expected = |
| 158 | + { |
| 159 | + this.CSharpCompilerError("CS1513").WithMessage("} expected").WithLocation(2, 21), |
| 160 | + this.CSharpCompilerError("CS1514").WithMessage("{ expected").WithLocation(2, 21), |
| 161 | + }; |
| 162 | + |
| 163 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 164 | + } |
| 165 | + |
| 166 | + /// <inheritdoc/> |
| 167 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 168 | + { |
| 169 | + return new SA1136CodeFixProvider(); |
| 170 | + } |
| 171 | + |
| 172 | + /// <inheritdoc/> |
| 173 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 174 | + { |
| 175 | + yield return new SA1136EnumValuesShouldBeOnSeparateLines(); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments