Skip to content

Commit fa031e1

Browse files
committed
Add SA1119 and SA1408 tests for pattern matching
1 parent 689c979 commit fa031e1

3 files changed

Lines changed: 103 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,83 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.MaintainabilityRules;
9+
using TestHelper;
10+
using Xunit;
711

812
public class SA1119CSharp7UnitTests : SA1119UnitTests
913
{
14+
/// <summary>
15+
/// Verifies that extra parentheses in pattern matching are reported.
16+
/// </summary>
17+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
18+
/// <seealso cref="SA1408CSharp7UnitTests.TestPatternMatchingAsync"/>
19+
[Fact]
20+
public async Task TestPatternMatchingAsync()
21+
{
22+
var testCode = @"public class Foo
23+
{
24+
public void Bar()
25+
{
26+
if ((new object() is bool b) && b)
27+
{
28+
return;
29+
}
30+
}
31+
}";
32+
var fixedCode = @"public class Foo
33+
{
34+
public void Bar()
35+
{
36+
if ( new object() is bool b && b)
37+
{
38+
return;
39+
}
40+
}
41+
}";
42+
43+
DiagnosticResult[] expected =
44+
{
45+
this.CSharpDiagnostic(DiagnosticId).WithLocation(5, 13),
46+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(5, 13),
47+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(5, 36),
48+
};
49+
50+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
51+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
52+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
53+
}
54+
55+
[Fact]
56+
public async Task TestTupleDeconstructionAsync()
57+
{
58+
var testCode = @"public class Foo
59+
{
60+
public void Bar()
61+
{
62+
var (a, (b, c), d) = (1, (2, (3)), 4);
63+
}
64+
}";
65+
var fixedCode = @"public class Foo
66+
{
67+
public void Bar()
68+
{
69+
var (a, (b, c), d) = (1, (2, 3), 4);
70+
}
71+
}";
72+
73+
DiagnosticResult[] expected =
74+
{
75+
this.CSharpDiagnostic(DiagnosticId).WithLocation(5, 38),
76+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(5, 38),
77+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(5, 40),
78+
};
79+
80+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
81+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
82+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
83+
}
1084
}
1185
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1408CSharp7UnitTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,33 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.MaintainabilityRules;
9+
using Xunit;
710

811
public class SA1408CSharp7UnitTests : SA1408UnitTests
912
{
13+
/// <summary>
14+
/// Verifies that a code fix for SA1119 in a pattern matching expression does not trigger SA1408.
15+
/// </summary>
16+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
17+
/// <seealso cref="SA1119CSharp7UnitTests.TestPatternMatchingAsync"/>
18+
[Fact]
19+
public async Task TestPatternMatchingAsync()
20+
{
21+
var testCode = @"public class Foo
22+
{
23+
public void Bar()
24+
{
25+
if (new object() is bool b && b)
26+
{
27+
return;
28+
}
29+
}
30+
}";
31+
32+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
33+
}
1034
}
1135
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1119UnitTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
1616

1717
public class SA1119UnitTests : CodeFixVerifier
1818
{
19-
private const string DiagnosticId = SA1119StatementMustNotUseUnnecessaryParenthesis.DiagnosticId;
20-
private const string ParenthesesDiagnosticId = SA1119StatementMustNotUseUnnecessaryParenthesis.ParenthesesDiagnosticId;
19+
protected const string DiagnosticId = SA1119StatementMustNotUseUnnecessaryParenthesis.DiagnosticId;
20+
protected const string ParenthesesDiagnosticId = SA1119StatementMustNotUseUnnecessaryParenthesis.ParenthesesDiagnosticId;
2121

22-
private bool mainDiagnosticShouldBeDisabled = false;
22+
protected bool MainDiagnosticShouldBeDisabled { get; set; }
2323

2424
[Fact]
2525
public async Task TestLiteralAsync()
@@ -1137,7 +1137,7 @@ public string Bar()
11371137
[Fact]
11381138
public async Task TestParenthesisDiagnosticIsNotTriggeredIfParentDiagnosticIsDisabledAsync()
11391139
{
1140-
this.mainDiagnosticShouldBeDisabled = true;
1140+
this.MainDiagnosticShouldBeDisabled = true;
11411141

11421142
string testCode = @"class Foo
11431143
{
@@ -1470,7 +1470,7 @@ protected override CodeFixProvider GetCSharpCodeFixProvider()
14701470
/// <inheritdoc/>
14711471
protected override IEnumerable<string> GetDisabledDiagnostics()
14721472
{
1473-
if (this.mainDiagnosticShouldBeDisabled)
1473+
if (this.MainDiagnosticShouldBeDisabled)
14741474
{
14751475
yield return DiagnosticId;
14761476
}

0 commit comments

Comments
 (0)