Skip to content

Commit ea6cd55

Browse files
authored
Merge pull request #4035 from sharwell/pattern-matching
Update rules for pattern matching in C# 8
2 parents 5dd2127 + d791fe0 commit ea6cd55

31 files changed

+1429
-9
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1500CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.LayoutRules;
10+
using StyleCop.Analyzers.Test.Helpers;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.LayoutRules.SA1500BracesForMultiLineStatementsMustNotShareLine,
14+
StyleCop.Analyzers.LayoutRules.SA1500CodeFixProvider>;
715

816
public partial class SA1500CSharp8UnitTests : SA1500CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
20+
public async Task TestSwitchExpressionBracesMustOwnLineAsync()
21+
{
22+
var testCode = @"
23+
public class TestClass
24+
{
25+
public int Test(Item value)
26+
{
27+
return value switch {|#0:{|}
28+
{ Prop: 1 } => 1,
29+
_ => 0 {|#1:}|};
30+
}
31+
}
32+
33+
public class Item
34+
{
35+
public int Prop { get; set; }
36+
}
37+
";
38+
39+
var fixedCode = @"
40+
public class TestClass
41+
{
42+
public int Test(Item value)
43+
{
44+
return value switch
45+
{
46+
{ Prop: 1 } => 1,
47+
_ => 0
48+
};
49+
}
50+
}
51+
52+
public class Item
53+
{
54+
public int Prop { get; set; }
55+
}
56+
";
57+
58+
DiagnosticResult[] expectedDiagnostics =
59+
{
60+
Diagnostic().WithLocation(0),
61+
Diagnostic().WithLocation(1),
62+
};
63+
64+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
65+
}
66+
67+
[Fact]
68+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
69+
public async Task TestPropertyPatternBracesMustOwnLineAsync()
70+
{
71+
var testCode = @"
72+
public class TestClass
73+
{
74+
public bool Test(object value)
75+
{
76+
return value is TestType {|#0:{|} A: 1,
77+
B: 2 {|#1:}|};
78+
}
79+
}
80+
81+
public class TestType
82+
{
83+
public int A { get; set; }
84+
85+
public int B { get; set; }
86+
}
87+
";
88+
89+
var fixedCode = @"
90+
public class TestClass
91+
{
92+
public bool Test(object value)
93+
{
94+
return value is TestType
95+
{
96+
A: 1,
97+
B: 2
98+
};
99+
}
100+
}
101+
102+
public class TestType
103+
{
104+
public int A { get; set; }
105+
106+
public int B { get; set; }
107+
}
108+
";
109+
110+
DiagnosticResult[] expectedDiagnostics =
111+
{
112+
Diagnostic().WithLocation(0),
113+
Diagnostic().WithLocation(1),
114+
};
115+
116+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
117+
}
10118
}
11119
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1505CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.LayoutRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.LayoutRules.SA1505OpeningBracesMustNotBeFollowedByBlankLine,
13+
StyleCop.Analyzers.LayoutRules.SA1505CodeFixProvider>;
714

815
public partial class SA1505CSharp8UnitTests : SA1505CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
19+
public async Task TestSwitchExpressionOpeningBraceFollowedByBlankLineAsync()
20+
{
21+
var testCode = @"
22+
public class TestClass
23+
{
24+
public int Test(Wrapper value)
25+
{
26+
return value switch
27+
{|#0:{|}
28+
29+
{ X: 1 } => 1,
30+
_ => 0,
31+
};
32+
}
33+
}
34+
35+
public class Wrapper
36+
{
37+
public int X { get; set; }
38+
}
39+
";
40+
var fixedCode = @"
41+
public class TestClass
42+
{
43+
public int Test(Wrapper value)
44+
{
45+
return value switch
46+
{
47+
{ X: 1 } => 1,
48+
_ => 0,
49+
};
50+
}
51+
}
52+
53+
public class Wrapper
54+
{
55+
public int X { get; set; }
56+
}
57+
";
58+
59+
var expected = Diagnostic().WithLocation(0);
60+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
61+
}
62+
63+
[Fact]
64+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
65+
public async Task TestPropertyPatternOpeningBraceFollowedByBlankLineAsync()
66+
{
67+
var testCode = @"
68+
public class TestClass
69+
{
70+
public bool Test(Wrapper value)
71+
{
72+
return value is Wrapper
73+
{|#0:{|}
74+
75+
X: 1,
76+
Y: 2,
77+
};
78+
}
79+
}
80+
81+
public class Wrapper
82+
{
83+
public int X { get; set; }
84+
85+
public int Y { get; set; }
86+
}
87+
";
88+
var fixedCode = @"
89+
public class TestClass
90+
{
91+
public bool Test(Wrapper value)
92+
{
93+
return value is Wrapper
94+
{
95+
X: 1,
96+
Y: 2,
97+
};
98+
}
99+
}
100+
101+
public class Wrapper
102+
{
103+
public int X { get; set; }
104+
105+
public int Y { get; set; }
106+
}
107+
";
108+
109+
var expected = Diagnostic().WithLocation(0);
110+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
111+
}
10112
}
11113
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1506CSharp8UnitTests.cs

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

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

814
public partial class SA1506CSharp8UnitTests : SA1506CSharp7UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
18+
public async Task TestDocumentationHeaderBeforeSwitchExpressionAsync()
19+
{
20+
var testCode = @"
21+
public class TestClass
22+
{
23+
/// <summary>
24+
/// Test method.
25+
/// </summary>
26+
{|#0:
27+
|} public int Test(int value) => value switch { 0 => 0, _ => 1 };
28+
}
29+
";
30+
var fixedCode = @"
31+
public class TestClass
32+
{
33+
/// <summary>
34+
/// Test method.
35+
/// </summary>
36+
public int Test(int value) => value switch { 0 => 0, _ => 1 };
37+
}
38+
";
39+
40+
var expected = Diagnostic().WithLocation(0);
41+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
42+
}
1043
}
1144
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1508CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp7.LayoutRules;
9+
using Xunit;
10+
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine,
13+
StyleCop.Analyzers.LayoutRules.SA1508CodeFixProvider>;
714

815
public partial class SA1508CSharp8UnitTests : SA1508CSharp7UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
19+
public async Task TestSwitchExpressionClosingBracePrecededByBlankLineAsync()
20+
{
21+
var testCode = @"
22+
public class TestClass
23+
{
24+
public int Test(Wrapper value)
25+
{
26+
return value switch
27+
{
28+
{ X: 1 } => 1,
29+
_ => 0,
30+
31+
{|#0:}|};
32+
}
33+
}
34+
35+
public class Wrapper
36+
{
37+
public int X { get; set; }
38+
}
39+
";
40+
var fixedCode = @"
41+
public class TestClass
42+
{
43+
public int Test(Wrapper value)
44+
{
45+
return value switch
46+
{
47+
{ X: 1 } => 1,
48+
_ => 0,
49+
};
50+
}
51+
}
52+
53+
public class Wrapper
54+
{
55+
public int X { get; set; }
56+
}
57+
";
58+
59+
var expected = Diagnostic().WithLocation(0);
60+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
61+
}
62+
63+
[Fact]
64+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
65+
public async Task TestPropertyPatternClosingBracePrecededByBlankLineAsync()
66+
{
67+
var testCode = @"
68+
public class TestClass
69+
{
70+
public bool Test(Wrapper value)
71+
{
72+
return value is Wrapper
73+
{
74+
X: 1,
75+
76+
{|#0:}|};
77+
}
78+
}
79+
80+
public class Wrapper
81+
{
82+
public int X { get; set; }
83+
}
84+
";
85+
var fixedCode = @"
86+
public class TestClass
87+
{
88+
public bool Test(Wrapper value)
89+
{
90+
return value is Wrapper
91+
{
92+
X: 1,
93+
};
94+
}
95+
}
96+
97+
public class Wrapper
98+
{
99+
public int X { get; set; }
100+
}
101+
";
102+
103+
var expected = Diagnostic().WithLocation(0);
104+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
105+
}
10106
}
11107
}

0 commit comments

Comments
 (0)