|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
| 10 | + using Microsoft.CodeAnalysis.Testing; |
| 11 | + |
| 12 | + using StyleCop.Analyzers.Lightup; |
6 | 13 | using StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules; |
7 | 14 |
|
| 15 | + using Xunit; |
| 16 | + |
| 17 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 18 | + StyleCop.Analyzers.MaintainabilityRules.SA1119StatementMustNotUseUnnecessaryParenthesis, |
| 19 | + StyleCop.Analyzers.MaintainabilityRules.SA1119CodeFixProvider>; |
| 20 | + |
8 | 21 | public class SA1119CSharp8UnitTests : SA1119CSharp7UnitTests |
9 | 22 | { |
| 23 | + /// <summary> |
| 24 | + /// Verifies that a type cast followed by a switch expression is handled correctly. |
| 25 | + /// </summary> |
| 26 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 27 | + [Fact] |
| 28 | + [WorkItem(3171, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3171")] |
| 29 | + public async Task TestTypeCastFollowedBySwitchExpressionIsHandledCorrectlyAsync() |
| 30 | + { |
| 31 | + const string testCode = @" |
| 32 | +public class Foo |
| 33 | +{ |
| 34 | + public string TestMethod(int n, object a, object b) |
| 35 | + { |
| 36 | + return (string)(n switch { 1 => a, 2 => b }); |
| 37 | + } |
| 38 | +} |
| 39 | +"; |
| 40 | + |
| 41 | + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Verifies that a swith expression with unnecessary parenthesis is handled correcly. |
| 46 | + /// </summary> |
| 47 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 48 | + [Fact] |
| 49 | + [WorkItem(3171, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3171")] |
| 50 | + public async Task TestSwitchExpressionWithUnnecessaryParenthesisAsync() |
| 51 | + { |
| 52 | + const string testCode = @" |
| 53 | +public class Foo |
| 54 | +{ |
| 55 | + public void TestMethod(int n, object a, object b) |
| 56 | + { |
| 57 | + var test = (n switch { 1 => a, 2 => b }); |
| 58 | + } |
| 59 | +} |
| 60 | +"; |
| 61 | + |
| 62 | + const string fixedCode = @" |
| 63 | +public class Foo |
| 64 | +{ |
| 65 | + public void TestMethod(int n, object a, object b) |
| 66 | + { |
| 67 | + var test = n switch { 1 => a, 2 => b }; |
| 68 | + } |
| 69 | +} |
| 70 | +"; |
| 71 | + |
| 72 | + DiagnosticResult[] expected = |
| 73 | + { |
| 74 | + Diagnostic(DiagnosticId).WithSpan(6, 20, 6, 49), |
| 75 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(6, 20), |
| 76 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(6, 48), |
| 77 | + }; |
| 78 | + |
| 79 | + await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 80 | + } |
10 | 81 | } |
11 | 82 | } |
0 commit comments