|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp9.MaintainabilityRules
|
5 | 5 | {
|
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.CSharp; |
| 9 | + using Microsoft.CodeAnalysis.Testing; |
6 | 10 | using StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules;
|
| 11 | + using Xunit; |
| 12 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 13 | + StyleCop.Analyzers.MaintainabilityRules.SA1119StatementMustNotUseUnnecessaryParenthesis, |
| 14 | + StyleCop.Analyzers.MaintainabilityRules.SA1119CodeFixProvider>; |
7 | 15 |
|
8 | 16 | public class SA1119CSharp9UnitTests : SA1119CSharp8UnitTests
|
9 | 17 | {
|
| 18 | + /// <summary> |
| 19 | + /// Verifies that a type cast followed by a <c>with</c> expression is handled correctly. |
| 20 | + /// </summary> |
| 21 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 22 | + [Fact] |
| 23 | + [WorkItem(3239, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3239")] |
| 24 | + public async Task TestTypeCastFollowedByWithExpressionIsHandledCorrectlyAsync() |
| 25 | + { |
| 26 | + const string testCode = @" |
| 27 | +record Foo(int Value) |
| 28 | +{ |
| 29 | + public object TestMethod(Foo n, int a) |
| 30 | + { |
| 31 | + return (object)(n with { Value = a }); |
| 32 | + } |
| 33 | +} |
| 34 | +"; |
| 35 | + |
| 36 | + await new CSharpTest(LanguageVersion.CSharp9) |
| 37 | + { |
| 38 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, |
| 39 | + TestCode = testCode, |
| 40 | + }.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Verifies that a type cast followed by a <c>with</c> expression with unnecessary parentheses is handled |
| 45 | + /// correctly. |
| 46 | + /// </summary> |
| 47 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 48 | + [Fact] |
| 49 | + [WorkItem(3239, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3239")] |
| 50 | + public async Task TestTypeCastFollowedByWithExpressionWithUnnecessaryParenthesesIsHandledCorrectlyAsync() |
| 51 | + { |
| 52 | + const string testCode = @" |
| 53 | +record Foo(int Value) |
| 54 | +{ |
| 55 | + public object TestMethod(Foo n, int a) |
| 56 | + { |
| 57 | + return (object){|#0:{|#1:(|}(n with { Value = a }){|#2:)|}|}; |
| 58 | + } |
| 59 | +} |
| 60 | +"; |
| 61 | + |
| 62 | + const string fixedCode = @" |
| 63 | +record Foo(int Value) |
| 64 | +{ |
| 65 | + public object TestMethod(Foo n, int a) |
| 66 | + { |
| 67 | + return (object)(n with { Value = a }); |
| 68 | + } |
| 69 | +} |
| 70 | +"; |
| 71 | + |
| 72 | + await new CSharpTest(LanguageVersion.CSharp9) |
| 73 | + { |
| 74 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, |
| 75 | + TestCode = testCode, |
| 76 | + ExpectedDiagnostics = |
| 77 | + { |
| 78 | + Diagnostic(DiagnosticId).WithLocation(0), |
| 79 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(1), |
| 80 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(2), |
| 81 | + }, |
| 82 | + FixedCode = fixedCode, |
| 83 | + }.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Verifies that a <c>with</c> expression with unnecessary parentheses is handled correcly. |
| 88 | + /// </summary> |
| 89 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 90 | + [Fact] |
| 91 | + [WorkItem(3239, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3239")] |
| 92 | + public async Task TestWithExpressionWithUnnecessaryParenthesesAsync() |
| 93 | + { |
| 94 | + const string testCode = @" |
| 95 | +record Foo(int Value) |
| 96 | +{ |
| 97 | + public void TestMethod(Foo n, int a) |
| 98 | + { |
| 99 | + var test = {|#0:{|#1:(|}n with { Value = a }{|#2:)|}|}; |
| 100 | + } |
| 101 | +} |
| 102 | +"; |
| 103 | + |
| 104 | + const string fixedCode = @" |
| 105 | +record Foo(int Value) |
| 106 | +{ |
| 107 | + public void TestMethod(Foo n, int a) |
| 108 | + { |
| 109 | + var test = n with { Value = a }; |
| 110 | + } |
| 111 | +} |
| 112 | +"; |
| 113 | + |
| 114 | + await new CSharpTest(LanguageVersion.CSharp9) |
| 115 | + { |
| 116 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, |
| 117 | + TestCode = testCode, |
| 118 | + ExpectedDiagnostics = |
| 119 | + { |
| 120 | + Diagnostic(DiagnosticId).WithLocation(0), |
| 121 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(1), |
| 122 | + Diagnostic(ParenthesesDiagnosticId).WithLocation(2), |
| 123 | + }, |
| 124 | + FixedCode = fixedCode, |
| 125 | + }.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 126 | + } |
| 127 | + |
| 128 | + [Theory] |
| 129 | + [InlineData(".ToString()")] |
| 130 | + [InlineData("?.ToString()")] |
| 131 | + [InlineData("[0]")] |
| 132 | + [InlineData("?[0]")] |
| 133 | + [WorkItem(3239, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3239")] |
| 134 | + public async Task TestWithExpressionFollowedByDereferenceAsync(string operation) |
| 135 | + { |
| 136 | + string testCode = $@" |
| 137 | +record Foo(int Value) |
| 138 | +{{ |
| 139 | + public object this[int index] => null; |
| 140 | +
|
| 141 | + public object TestMethod(Foo n, int a) |
| 142 | + {{ |
| 143 | + return (n with {{ Value = a }}){operation}; |
| 144 | + }} |
| 145 | +}} |
| 146 | +"; |
| 147 | + |
| 148 | + await new CSharpTest(LanguageVersion.CSharp9) |
| 149 | + { |
| 150 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net50, |
| 151 | + TestCode = testCode, |
| 152 | + }.RunAsync(CancellationToken.None).ConfigureAwait(false); |
| 153 | + } |
10 | 154 | }
|
11 | 155 | }
|
0 commit comments