Skip to content

Commit 28b7f55

Browse files
committed
Update SA1119 tests for relational and logical patterns
1 parent 4c07be7 commit 28b7f55

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1119CSharp9UnitTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,68 @@ public object TestMethod(Foo n, int a)
152152
TestCode = testCode,
153153
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
154154
}
155+
156+
[Fact]
157+
[WorkItem(3968, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3968")]
158+
public async Task TestOuterParenthesesAroundParenthesizedPatternAreRemovedAsync()
159+
{
160+
const string testCode = @"
161+
class C
162+
{
163+
void M(int value)
164+
{
165+
if ({|#0:{|#1:(|}(value is (> 0 and < 5)){|#2:)|}|})
166+
{
167+
}
168+
}
169+
}";
170+
171+
const string fixedCode = @"
172+
class C
173+
{
174+
void M(int value)
175+
{
176+
if (value is (> 0 and < 5))
177+
{
178+
}
179+
}
180+
}";
181+
182+
await new CSharpTest()
183+
{
184+
NumberOfIncrementalIterations = 2,
185+
NumberOfFixAllIterations = 2,
186+
TestCode = testCode,
187+
ExpectedDiagnostics =
188+
{
189+
Diagnostic(DiagnosticId).WithLocation(0),
190+
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
191+
Diagnostic(ParenthesesDiagnosticId).WithLocation(2),
192+
},
193+
FixedCode = fixedCode,
194+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
195+
}
196+
197+
/// <summary>
198+
/// Verifies that parentheses required to clarify precedence within patterns are not removed.
199+
/// </summary>
200+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
201+
[Fact]
202+
[WorkItem(3968, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3968")]
203+
public async Task TestClarifyingPatternParenthesesAreNotRemovedAsync()
204+
{
205+
const string testCode = @"
206+
class C
207+
{
208+
void M(int value)
209+
{
210+
if (value is (> 0 and < 5) or 10)
211+
{
212+
}
213+
}
214+
}";
215+
216+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
217+
}
155218
}
156219
}

0 commit comments

Comments
 (0)