|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
6 | 8 | using StyleCop.Analyzers.Test.LayoutRules; |
| 9 | + using Xunit; |
7 | 10 |
|
8 | 11 | public class SA1502CSharp7UnitTests : SA1502UnitTests |
9 | 12 | { |
| 13 | + /// <summary> |
| 14 | + /// Verifies that a valid local function will pass without diagnostic. |
| 15 | + /// </summary> |
| 16 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 17 | + [Fact] |
| 18 | + public async Task TestValidEmptyLocalFunctionAsync() |
| 19 | + { |
| 20 | + var testCode = @"public class TypeName |
| 21 | +{ |
| 22 | + public void Method() |
| 23 | + { |
| 24 | + void Bar() |
| 25 | + { |
| 26 | + } |
| 27 | + } |
| 28 | +}"; |
| 29 | + |
| 30 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Verifies that an empty local function with its block on the same line will trigger a diagnostic. |
| 35 | + /// </summary> |
| 36 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 37 | + [Fact] |
| 38 | + public async Task TestEmptyLocalFunctionOnSingleLineAsync() |
| 39 | + { |
| 40 | + var testCode = @"public class TypeName |
| 41 | +{ |
| 42 | + public void Method() |
| 43 | + { |
| 44 | + void Bar() { } |
| 45 | + } |
| 46 | +}"; |
| 47 | + |
| 48 | + var expected = this.CSharpDiagnostic().WithLocation(5, 20); |
| 49 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Verifies that a local function with its block on the same line will trigger a diagnostic. |
| 54 | + /// </summary> |
| 55 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 56 | + [Fact] |
| 57 | + public async Task TestLocalFunctionOnSingleLineAsync() |
| 58 | + { |
| 59 | + var testCode = @"public class TypeName |
| 60 | +{ |
| 61 | + public void Method() |
| 62 | + { |
| 63 | + int Bar() { return 0; } |
| 64 | + } |
| 65 | +}"; |
| 66 | + |
| 67 | + var expected = this.CSharpDiagnostic().WithLocation(5, 19); |
| 68 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Verifies that a local function with its block on a single line will trigger a diagnostic. |
| 73 | + /// </summary> |
| 74 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 75 | + [Fact] |
| 76 | + public async Task TestLocalFunctionWithBlockOnSingleLineAsync() |
| 77 | + { |
| 78 | + var testCode = @"public class TypeName |
| 79 | +{ |
| 80 | + public void Method() |
| 81 | + { |
| 82 | + int Bar() |
| 83 | + { return 0; } |
| 84 | + } |
| 85 | +}"; |
| 86 | + |
| 87 | + var expected = this.CSharpDiagnostic().WithLocation(6, 9); |
| 88 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Verifies that a local function with its block on multiple lines will pass without diagnostic. |
| 93 | + /// </summary> |
| 94 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 95 | + [Fact] |
| 96 | + public async Task TestLocalFunctionWithBlockStartOnSameLineAsync() |
| 97 | + { |
| 98 | + var testCode = @"public class TypeName |
| 99 | +{ |
| 100 | + public void Method() |
| 101 | + { |
| 102 | + int Bar() { |
| 103 | + return 0; } |
| 104 | + } |
| 105 | +}"; |
| 106 | + |
| 107 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Verifies that a local function with an expression body will pass without diagnostic. |
| 112 | + /// </summary> |
| 113 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 114 | + [Fact] |
| 115 | + public async Task TestLocalFunctionWithExpressionBodyAsync() |
| 116 | + { |
| 117 | + var testCode = @"public class TypeName |
| 118 | +{ |
| 119 | + public void Method() |
| 120 | + { |
| 121 | + int Bar(int x, int y) => x + y; |
| 122 | + } |
| 123 | +}"; |
| 124 | + |
| 125 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Verifies that the code fix for an empty local function with its block on the same line will work properly. |
| 130 | + /// </summary> |
| 131 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 132 | + [Fact] |
| 133 | + public async Task TestEmptyLocalFunctionOnSingleLineCodeFixAsync() |
| 134 | + { |
| 135 | + var testCode = @"public class TypeName |
| 136 | +{ |
| 137 | + public void Method() |
| 138 | + { |
| 139 | + void Bar() { } |
| 140 | + } |
| 141 | +}"; |
| 142 | + var fixedTestCode = @"public class TypeName |
| 143 | +{ |
| 144 | + public void Method() |
| 145 | + { |
| 146 | + void Bar() |
| 147 | + { |
| 148 | + } |
| 149 | + } |
| 150 | +}"; |
| 151 | + |
| 152 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Verifies that the code fix for a local function with its block on the same line will work properly. |
| 157 | + /// </summary> |
| 158 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 159 | + [Fact] |
| 160 | + public async Task TestLocalFunctionOnSingleLineCodeFixAsync() |
| 161 | + { |
| 162 | + var testCode = @"public class TypeName |
| 163 | +{ |
| 164 | + public void Method() |
| 165 | + { |
| 166 | + int Bar() { return 0; } |
| 167 | + } |
| 168 | +}"; |
| 169 | + var fixedTestCode = @"public class TypeName |
| 170 | +{ |
| 171 | + public void Method() |
| 172 | + { |
| 173 | + int Bar() |
| 174 | + { |
| 175 | + return 0; |
| 176 | + } |
| 177 | + } |
| 178 | +}"; |
| 179 | + |
| 180 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Verifies that the code fix for a local function with its block on a single line will work properly. |
| 185 | + /// </summary> |
| 186 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 187 | + [Fact] |
| 188 | + public async Task TestLocalFunctionWithBlockOnSingleLineCodeFixAsync() |
| 189 | + { |
| 190 | + var testCode = @"public class TypeName |
| 191 | +{ |
| 192 | + public void Method() |
| 193 | + { |
| 194 | + int Bar() |
| 195 | + { return 0; } |
| 196 | + } |
| 197 | +}"; |
| 198 | + var fixedTestCode = @"public class TypeName |
| 199 | +{ |
| 200 | + public void Method() |
| 201 | + { |
| 202 | + int Bar() |
| 203 | + { |
| 204 | + return 0; |
| 205 | + } |
| 206 | + } |
| 207 | +}"; |
| 208 | + |
| 209 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Verifies that the code fix for a local function with lots of trivia is working properly. |
| 214 | + /// </summary> |
| 215 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 216 | + [Fact] |
| 217 | + public async Task TestLocalFunctionWithLotsOfTriviaCodeFixAsync() |
| 218 | + { |
| 219 | + var testCode = @"public class TypeName |
| 220 | +{ |
| 221 | + public void Method() |
| 222 | + { |
| 223 | + int Bar() /* TR1 */ { /* TR2 */ return 0; /* TR3 */ } /* TR4 */ |
| 224 | + } |
| 225 | +}"; |
| 226 | + var fixedTestCode = @"public class TypeName |
| 227 | +{ |
| 228 | + public void Method() |
| 229 | + { |
| 230 | + int Bar() /* TR1 */ |
| 231 | + { /* TR2 */ |
| 232 | + return 0; /* TR3 */ |
| 233 | + } /* TR4 */ |
| 234 | + } |
| 235 | +}"; |
| 236 | + |
| 237 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 238 | + } |
10 | 239 | } |
11 | 240 | } |
0 commit comments