Skip to content

Commit 2adb2e6

Browse files
committed
Update SA1000/SA1001/SA1024 for pattern matching
1 parent 0fc50cc commit 2adb2e6

File tree

3 files changed

+157
-6
lines changed

3 files changed

+157
-6
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1000CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
12+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
713

814
public partial class SA1000CSharp8UnitTests : SA1000CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestSwitchExpressionKeywordSpacingAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
public int Test(int value) => value {|#0:switch|}{ 1 => 0, _ => 1 };
24+
}
25+
";
26+
var fixedCode = @"
27+
public class TestClass
28+
{
29+
public int Test(int value) => value switch { 1 => 0, _ => 1 };
30+
}
31+
";
32+
33+
var expected = Diagnostic().WithLocation(0).WithArguments("switch", string.Empty);
34+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
35+
}
36+
37+
[Fact]
38+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
39+
public async Task TestIsKeywordSpacingInPropertyPatternAsync()
40+
{
41+
var testCode = @"
42+
public class TestClass
43+
{
44+
public bool Test(SomeType value) => value {|#0:is|}{ Length: 1 };
45+
}
46+
47+
public class SomeType
48+
{
49+
public int Length { get; set; }
50+
}
51+
";
52+
var fixedCode = @"
53+
public class TestClass
54+
{
55+
public bool Test(SomeType value) => value is { Length: 1 };
56+
}
57+
58+
public class SomeType
59+
{
60+
public int Length { get; set; }
61+
}
62+
";
63+
64+
var expected = Diagnostic().WithLocation(0).WithArguments("is", string.Empty);
65+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
66+
}
1067
}
1168
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1001CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.SpacingRules.SA1001CommasMustBeSpacedCorrectly,
12+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
713

814
public partial class SA1001CSharp8UnitTests : SA1001CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestTuplePatternCommaSpacingAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
public int Test(object value) => value switch
24+
{
25+
(1{|#0:,|}2) => 0,
26+
_ => 1,
27+
};
28+
}
29+
";
30+
var fixedCode = @"
31+
public class TestClass
32+
{
33+
public int Test(object value) => value switch
34+
{
35+
(1, 2) => 0,
36+
_ => 1,
37+
};
38+
}
39+
";
40+
41+
var expected = Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed");
42+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
43+
}
44+
45+
[Fact]
46+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
47+
public async Task TestSwitchExpressionArmSeparatorSpacingAsync()
48+
{
49+
var testCode = @"
50+
public class TestClass
51+
{
52+
public string Test(int value) => value switch { 1 => ""one""{|#0:,|}2 => ""two"", _ => ""other"" };
53+
}
54+
";
55+
var fixedCode = @"
56+
public class TestClass
57+
{
58+
public string Test(int value) => value switch { 1 => ""one"", 2 => ""two"", _ => ""other"" };
59+
}
60+
";
61+
62+
var expected = Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed");
63+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
64+
}
1065
}
1166
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1024CSharp8UnitTests.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
109
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
1110
using Xunit;
1211
using static StyleCop.Analyzers.SpacingRules.SA1024ColonsMustBeSpacedCorrectly;
@@ -25,20 +24,20 @@ public partial class SA1024CSharp8UnitTests : SA1024CSharp7UnitTests
2524
[WorkItem(3053, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3053")]
2625
public async Task TestColonAfterClosingBraceInPatternAsync()
2726
{
28-
const string testCode = @"using System;
27+
string testCode = @"using System;
2928
3029
public class Foo
3130
{
3231
public void TestMethod(object value)
3332
{
3433
switch (value)
3534
{
36-
case Exception { Message: { } message } :
35+
case Exception { Message: { } message } {|#0::|}
3736
break;
3837
}
3938
}
4039
}";
41-
const string fixedCode = @"using System;
40+
string fixedCode = @"using System;
4241
4342
public class Foo
4443
{
@@ -52,7 +51,47 @@ public void TestMethod(object value)
5251
}
5352
}";
5453

55-
var expected = Diagnostic(DescriptorNotPreceded).WithSpan(9, 49, 9, 50);
54+
var expected = Diagnostic(DescriptorNotPreceded).WithLocation(0);
55+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
56+
}
57+
58+
[Fact]
59+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
60+
public async Task TestPropertyPatternColonSpacingAsync()
61+
{
62+
string testCode = @"
63+
public class TestClass
64+
{
65+
public bool Test(SomeType value) => value is { X {|#0::|}1, Y{|#1::|}2 };
66+
}
67+
68+
public class SomeType
69+
{
70+
public int X { get; set; }
71+
public int Y { get; set; }
72+
}
73+
";
74+
75+
string fixedCode = @"
76+
public class TestClass
77+
{
78+
public bool Test(SomeType value) => value is { X: 1, Y: 2 };
79+
}
80+
81+
public class SomeType
82+
{
83+
public int X { get; set; }
84+
public int Y { get; set; }
85+
}
86+
";
87+
88+
DiagnosticResult[] expected =
89+
{
90+
Diagnostic(DescriptorNotPreceded).WithLocation(0),
91+
Diagnostic(DescriptorFollowed).WithLocation(0),
92+
Diagnostic(DescriptorFollowed).WithLocation(1),
93+
};
94+
5695
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5796
}
5897
}

0 commit comments

Comments
 (0)