Skip to content

Commit 139c3c7

Browse files
committed
Add tests for records in types using SyntaxKinds.TypeDeclaration
1 parent 0bd482e commit 139c3c7

File tree

8 files changed

+159
-58
lines changed

8 files changed

+159
-58
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.OrderingRules
1313
using Microsoft.CodeAnalysis.CSharp;
1414
using Microsoft.CodeAnalysis.CSharp.Syntax;
1515
using StyleCop.Analyzers.Helpers;
16+
using StyleCop.Analyzers.Lightup;
1617

1718
/// <summary>
1819
/// Implements code fixes for <see cref="SA1205PartialElementsMustDeclareAccess"/>.
@@ -108,6 +109,8 @@ private static TypeDeclarationSyntax ReplaceModifiers(TypeDeclarationSyntax node
108109
return ((InterfaceDeclarationSyntax)node).WithModifiers(modifiers);
109110
case SyntaxKind.StructDeclaration:
110111
return ((StructDeclarationSyntax)node).WithModifiers(modifiers);
112+
case SyntaxKindEx.RecordDeclaration:
113+
return ((RecordDeclarationSyntaxWrapper)node).WithModifiers(modifiers);
111114
}
112115

113116
return node;
@@ -125,6 +128,8 @@ private static TypeDeclarationSyntax ReplaceKeyword(TypeDeclarationSyntax node,
125128
return ((InterfaceDeclarationSyntax)node).WithKeyword(keyword);
126129
case SyntaxKind.StructDeclaration:
127130
return ((StructDeclarationSyntax)node).WithKeyword(keyword);
131+
case SyntaxKindEx.RecordDeclaration:
132+
return ((RecordDeclarationSyntaxWrapper)node).WithKeyword(keyword);
128133
}
129134

130135
return node;

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1605UnitTests.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
namespace StyleCop.Analyzers.Test.DocumentationRules
55
{
6+
using System.Collections.Generic;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis.Testing;
910
using StyleCop.Analyzers.DocumentationRules;
11+
using StyleCop.Analyzers.Lightup;
1012
using StyleCop.Analyzers.Test.Verifiers;
1113
using Xunit;
1214
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1605PartialElementDocumentationMustHaveSummary>;
@@ -26,10 +28,35 @@ public class SA1605UnitTests
2628
}
2729
";
2830

31+
public static IEnumerable<object[]> TypeDeclarationKeywords
32+
{
33+
get
34+
{
35+
yield return new[] { "class" };
36+
yield return new[] { "struct" };
37+
yield return new[] { "interface" };
38+
if (LightupHelpers.SupportsCSharp9)
39+
{
40+
yield return new[] { "record" };
41+
}
42+
}
43+
}
44+
45+
public static IEnumerable<object[]> BaseTypeDeclarationKeywords
46+
{
47+
get
48+
{
49+
foreach (var keyword in TypeDeclarationKeywords)
50+
{
51+
yield return keyword;
52+
}
53+
54+
yield return new[] { "enum" };
55+
}
56+
}
57+
2958
[Theory]
30-
[InlineData("class")]
31-
[InlineData("struct")]
32-
[InlineData("interface")]
59+
[MemberData(nameof(TypeDeclarationKeywords))]
3360
public async Task TestTypeNoDocumentationAsync(string typeName)
3461
{
3562
var testCode = @"
@@ -40,9 +67,7 @@ public async Task TestTypeNoDocumentationAsync(string typeName)
4067
}
4168

4269
[Theory]
43-
[InlineData("class")]
44-
[InlineData("struct")]
45-
[InlineData("interface")]
70+
[MemberData(nameof(TypeDeclarationKeywords))]
4671
public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
4772
{
4873
var testCode = @"
@@ -56,9 +81,7 @@ public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
5681
}
5782

5883
[Theory]
59-
[InlineData("class")]
60-
[InlineData("struct")]
61-
[InlineData("interface")]
84+
[MemberData(nameof(TypeDeclarationKeywords))]
6285
public async Task TestTypeWithContentDocumentationAsync(string typeName)
6386
{
6487
var testCode = @"
@@ -72,9 +95,7 @@ public async Task TestTypeWithContentDocumentationAsync(string typeName)
7295
}
7396

7497
[Theory]
75-
[InlineData("class")]
76-
[InlineData("struct")]
77-
[InlineData("interface")]
98+
[MemberData(nameof(TypeDeclarationKeywords))]
7899
public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
79100
{
80101
var testCode = @"
@@ -86,9 +107,7 @@ public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
86107
}
87108

88109
[Theory]
89-
[InlineData("class")]
90-
[InlineData("struct")]
91-
[InlineData("interface")]
110+
[MemberData(nameof(TypeDeclarationKeywords))]
92111
public async Task TestTypeWithoutDocumentationAsync(string typeName)
93112
{
94113
var testCode = @"
@@ -104,10 +123,7 @@ public async Task TestTypeWithoutDocumentationAsync(string typeName)
104123
}
105124

106125
[Theory]
107-
[InlineData("enum")]
108-
[InlineData("class")]
109-
[InlineData("struct")]
110-
[InlineData("interface")]
126+
[MemberData(nameof(BaseTypeDeclarationKeywords))]
111127
public async Task TestNonPartialTypeWithoutDocumentationAsync(string typeName)
112128
{
113129
var testCode = @"

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1607UnitTests.cs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
namespace StyleCop.Analyzers.Test.DocumentationRules
55
{
6+
using System.Collections.Generic;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis.Testing;
910
using StyleCop.Analyzers.DocumentationRules;
11+
using StyleCop.Analyzers.Lightup;
1012
using StyleCop.Analyzers.Test.Verifiers;
1113
using Xunit;
1214
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1607PartialElementDocumentationMustHaveSummaryText>;
@@ -16,10 +18,35 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1618
/// </summary>
1719
public class SA1607UnitTests
1820
{
21+
public static IEnumerable<object[]> TypeDeclarationKeywords
22+
{
23+
get
24+
{
25+
yield return new[] { "class" };
26+
yield return new[] { "struct" };
27+
yield return new[] { "interface" };
28+
if (LightupHelpers.SupportsCSharp9)
29+
{
30+
yield return new[] { "record" };
31+
}
32+
}
33+
}
34+
35+
public static IEnumerable<object[]> BaseTypeDeclarationKeywords
36+
{
37+
get
38+
{
39+
foreach (var keyword in TypeDeclarationKeywords)
40+
{
41+
yield return keyword;
42+
}
43+
44+
yield return new[] { "enum" };
45+
}
46+
}
47+
1948
[Theory]
20-
[InlineData("class")]
21-
[InlineData("struct")]
22-
[InlineData("interface")]
49+
[MemberData(nameof(TypeDeclarationKeywords))]
2350
public async Task TestTypeNoDocumentationAsync(string typeName)
2451
{
2552
var testCode = @"
@@ -30,9 +57,7 @@ public async Task TestTypeNoDocumentationAsync(string typeName)
3057
}
3158

3259
[Theory]
33-
[InlineData("class")]
34-
[InlineData("struct")]
35-
[InlineData("interface")]
60+
[MemberData(nameof(TypeDeclarationKeywords))]
3661
public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
3762
{
3863
var testCode = @"
@@ -46,9 +71,7 @@ public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
4671
}
4772

4873
[Theory]
49-
[InlineData("class")]
50-
[InlineData("struct")]
51-
[InlineData("interface")]
74+
[MemberData(nameof(TypeDeclarationKeywords))]
5275
public async Task TestTypeWithContentDocumentationAsync(string typeName)
5376
{
5477
var testCode = @"
@@ -62,9 +85,7 @@ public async Task TestTypeWithContentDocumentationAsync(string typeName)
6285
}
6386

6487
[Theory]
65-
[InlineData("class")]
66-
[InlineData("struct")]
67-
[InlineData("interface")]
88+
[MemberData(nameof(TypeDeclarationKeywords))]
6889
public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
6990
{
7091
var testCode = @"
@@ -76,9 +97,7 @@ public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
7697
}
7798

7899
[Theory]
79-
[InlineData("class")]
80-
[InlineData("struct")]
81-
[InlineData("interface")]
100+
[MemberData(nameof(TypeDeclarationKeywords))]
82101
public async Task TestTypeWithoutSummaryDocumentationAsync(string typeName)
83102
{
84103
var testCode = @"
@@ -96,10 +115,7 @@ public async Task TestTypeWithoutSummaryDocumentationAsync(string typeName)
96115
}
97116

98117
[Theory]
99-
[InlineData("enum")]
100-
[InlineData("class")]
101-
[InlineData("struct")]
102-
[InlineData("interface")]
118+
[MemberData(nameof(BaseTypeDeclarationKeywords))]
103119
public async Task TestNonPartialTypeWithoutSummaryDocumentationAsync(string typeName)
104120
{
105121
var testCode = @"
@@ -114,9 +130,7 @@ public async Task TestNonPartialTypeWithoutSummaryDocumentationAsync(string type
114130
}
115131

116132
[Theory]
117-
[InlineData("class")]
118-
[InlineData("struct")]
119-
[InlineData("interface")]
133+
[MemberData(nameof(TypeDeclarationKeywords))]
120134
public async Task TestTypeWithoutContentDocumentationAsync(string typeName)
121135
{
122136
var testCode = @"
@@ -134,10 +148,7 @@ public async Task TestTypeWithoutContentDocumentationAsync(string typeName)
134148
}
135149

136150
[Theory]
137-
[InlineData("enum")]
138-
[InlineData("class")]
139-
[InlineData("struct")]
140-
[InlineData("interface")]
151+
[MemberData(nameof(BaseTypeDeclarationKeywords))]
141152
public async Task TestNonPartialTypeWithoutContentDocumentationAsync(string typeName)
142153
{
143154
var testCode = @"
@@ -388,10 +399,10 @@ public partial class ClassName
388399
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
389400
}
390401

391-
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
402+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
392403
=> VerifyCSharpDiagnosticAsync(source, new[] { expected }, cancellationToken);
393404

394-
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
405+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
395406
{
396407
string contentWithoutSummaryOrContent = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
397408
<ClassName>

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1618UnitTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis.Testing;
1010
using StyleCop.Analyzers.DocumentationRules;
11+
using StyleCop.Analyzers.Lightup;
1112
using StyleCop.Analyzers.Test.Verifiers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1618GenericTypeParametersMustBeDocumented>;
@@ -39,6 +40,11 @@ public static IEnumerable<object[]> Types
3940
yield return new object[] { "class Foo<Ta, T\\u0062> { }" };
4041
yield return new object[] { "struct Foo<Ta, T\\u0062> { }" };
4142
yield return new object[] { "interface Foo<Ta, T\\u0062> { }" };
43+
if (LightupHelpers.SupportsCSharp9)
44+
{
45+
yield return new object[] { "record Foo<Ta, Tb> { }" };
46+
yield return new object[] { "record Foo<Ta, T\\u0062> { }" };
47+
}
4248
}
4349
}
4450

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1619UnitTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis.Testing;
1010
using StyleCop.Analyzers.DocumentationRules;
11+
using StyleCop.Analyzers.Lightup;
1112
using StyleCop.Analyzers.Test.Verifiers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1619GenericTypeParametersMustBeDocumentedPartialClass>;
@@ -27,6 +28,11 @@ public static IEnumerable<object[]> Types
2728
yield return new object[] { "class Foo<Ta, T\\u0062> { }" };
2829
yield return new object[] { "struct Foo<Ta, T\\u0062> { }" };
2930
yield return new object[] { "interface Foo<Ta, T\\u0062> { }" };
31+
if (LightupHelpers.SupportsCSharp9)
32+
{
33+
yield return new object[] { "record Foo<Ta, Tb> { }" };
34+
yield return new object[] { "record Foo<Ta, T\\u0062> { }" };
35+
}
3036
}
3137
}
3238

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1205UnitTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis.Testing;
10+
using StyleCop.Analyzers.Lightup;
1011
using StyleCop.Analyzers.OrderingRules;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -45,6 +46,14 @@ public static IEnumerable<object[]> ValidDeclarations
4546
yield return new object[] { "class" };
4647
yield return new object[] { "struct" };
4748
yield return new object[] { "interface" };
49+
if (LightupHelpers.SupportsCSharp9)
50+
{
51+
yield return new object[] { "public partial record" };
52+
yield return new object[] { "internal partial record" };
53+
yield return new object[] { "public sealed partial record" };
54+
yield return new object[] { "internal sealed partial record" };
55+
yield return new object[] { "record" };
56+
}
4857
}
4958
}
5059

@@ -57,6 +66,11 @@ public static IEnumerable<object[]> InvalidDeclarations
5766
yield return new object[] { "static partial class" };
5867
yield return new object[] { "partial struct" };
5968
yield return new object[] { "partial interface" };
69+
if (LightupHelpers.SupportsCSharp9)
70+
{
71+
yield return new object[] { "partial record" };
72+
yield return new object[] { "sealed partial record" };
73+
}
6074
}
6175
}
6276

@@ -81,6 +95,15 @@ public static IEnumerable<object[]> ValidNestedDeclarations
8195
yield return new object[] { "internal", "interface" };
8296
yield return new object[] { "protected internal", "interface" };
8397
yield return new object[] { "private", "interface" };
98+
99+
if (LightupHelpers.SupportsCSharp9)
100+
{
101+
yield return new object[] { "public", "record" };
102+
yield return new object[] { "protected", "record" };
103+
yield return new object[] { "internal", "record" };
104+
yield return new object[] { "protected internal", "record" };
105+
yield return new object[] { "private", "record" };
106+
}
84107
}
85108
}
86109

0 commit comments

Comments
 (0)