Skip to content

Commit e6720f1

Browse files
committed
Add tests for records in types using SyntaxKinds.BaseTypeDeclaration
1 parent 64e8d46 commit e6720f1

File tree

17 files changed

+244
-529
lines changed

17 files changed

+244
-529
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private Document CreateCodeFix(Document document, IndentationSettings indentatio
6969
case SyntaxKind.ClassDeclaration:
7070
case SyntaxKind.InterfaceDeclaration:
7171
case SyntaxKind.StructDeclaration:
72+
case SyntaxKindEx.RecordDeclaration:
7273
case SyntaxKind.EnumDeclaration:
7374
newSyntaxRoot = this.RegisterBaseTypeDeclarationCodeFix(syntaxRoot, (BaseTypeDeclarationSyntax)node, indentationSettings);
7475
break;

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1400CodeFixProvider.cs

+23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
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 a code fix for <see cref="SA1400AccessModifierMustBeDeclared"/>.
@@ -82,6 +83,10 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
8283
updatedDeclarationNode = HandleStructDeclaration((StructDeclarationSyntax)declarationNode);
8384
break;
8485

86+
case SyntaxKindEx.RecordDeclaration:
87+
updatedDeclarationNode = HandleRecordDeclaration((RecordDeclarationSyntaxWrapper)declarationNode);
88+
break;
89+
8590
case SyntaxKind.DelegateDeclaration:
8691
updatedDeclarationNode = HandleDelegateDeclaration((DelegateDeclarationSyntax)declarationNode);
8792
break;
@@ -194,6 +199,23 @@ private static SyntaxNode HandleStructDeclaration(StructDeclarationSyntax node)
194199
.WithoutFormatting();
195200
}
196201

202+
private static SyntaxNode HandleRecordDeclaration(RecordDeclarationSyntaxWrapper node)
203+
{
204+
SyntaxToken triviaToken = node.Keyword;
205+
if (triviaToken.IsMissing)
206+
{
207+
return null;
208+
}
209+
210+
SyntaxKind defaultVisibility = IsNestedType(node) ? SyntaxKind.PrivateKeyword : SyntaxKind.InternalKeyword;
211+
SyntaxTokenList modifiers = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref triviaToken, defaultVisibility);
212+
return node
213+
.WithKeyword(triviaToken)
214+
.WithModifiers(modifiers)
215+
.SyntaxNode
216+
.WithoutFormatting();
217+
}
218+
197219
private static SyntaxNode HandleDelegateDeclaration(DelegateDeclarationSyntax node)
198220
{
199221
SyntaxToken triviaToken = node.DelegateKeyword;
@@ -355,6 +377,7 @@ private static SyntaxNode FindParentDeclarationNode(SyntaxNode node)
355377
case SyntaxKind.InterfaceDeclaration:
356378
case SyntaxKind.EnumDeclaration:
357379
case SyntaxKind.StructDeclaration:
380+
case SyntaxKindEx.RecordDeclaration:
358381
case SyntaxKind.DelegateDeclaration:
359382
case SyntaxKind.EventDeclaration:
360383
case SyntaxKind.EventFieldDeclaration:

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1604UnitTests.cs

+7-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.Testing;
99
using StyleCop.Analyzers.DocumentationRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1011
using StyleCop.Analyzers.Test.Verifiers;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1604ElementDocumentationMustHaveSummary>;
@@ -17,10 +18,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1718
public class SA1604UnitTests
1819
{
1920
[Theory]
20-
[InlineData("enum")]
21-
[InlineData("class")]
22-
[InlineData("struct")]
23-
[InlineData("interface")]
21+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
2422
public async Task TestTypeNoDocumentationAsync(string typeName)
2523
{
2624
var testCode = @"
@@ -31,10 +29,7 @@ public async Task TestTypeNoDocumentationAsync(string typeName)
3129
}
3230

3331
[Theory]
34-
[InlineData("enum")]
35-
[InlineData("class")]
36-
[InlineData("struct")]
37-
[InlineData("interface")]
32+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
3833
public async Task TestTypeWithDocumentationAsync(string typeName)
3934
{
4035
var testCode = @"
@@ -48,10 +43,7 @@ public async Task TestTypeWithDocumentationAsync(string typeName)
4843
}
4944

5045
[Theory]
51-
[InlineData("enum")]
52-
[InlineData("class")]
53-
[InlineData("struct")]
54-
[InlineData("interface")]
46+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
5547
public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
5648
{
5749
var testCode = @"
@@ -63,10 +55,7 @@ public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
6355
}
6456

6557
[Theory]
66-
[InlineData("enum")]
67-
[InlineData("class")]
68-
[InlineData("struct")]
69-
[InlineData("interface")]
58+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
7059
public async Task TestTypeWithoutDocumentationAsync(string typeName)
7160
{
7261
var testCode = @"
@@ -82,14 +71,12 @@ public async Task TestTypeWithoutDocumentationAsync(string typeName)
8271
}
8372

8473
[Theory]
85-
[InlineData("partial class")]
86-
[InlineData("partial struct")]
87-
[InlineData("partial interface")]
74+
[MemberData(nameof(CommonMemberData.TypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
8875
public async Task TestPartialTypeWithoutDocumentationAsync(string typeName)
8976
{
9077
var testCode = @"
9178
///
92-
{0}
79+
partial {0}
9380
TypeName
9481
{{
9582
}}";

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1606UnitTests.cs

+7-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.Testing;
99
using StyleCop.Analyzers.DocumentationRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1011
using StyleCop.Analyzers.Test.Verifiers;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1606ElementDocumentationMustHaveSummaryText>;
@@ -17,10 +18,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1718
public class SA1606UnitTests
1819
{
1920
[Theory]
20-
[InlineData("enum")]
21-
[InlineData("class")]
22-
[InlineData("struct")]
23-
[InlineData("interface")]
21+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
2422
public async Task TestTypeNoDocumentationAsync(string typeName)
2523
{
2624
var testCode = @"
@@ -31,10 +29,7 @@ public async Task TestTypeNoDocumentationAsync(string typeName)
3129
}
3230

3331
[Theory]
34-
[InlineData("enum")]
35-
[InlineData("class")]
36-
[InlineData("struct")]
37-
[InlineData("interface")]
32+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
3833
public async Task TestTypeWithDocumentationAsync(string typeName)
3934
{
4035
var testCode = @"
@@ -48,10 +43,7 @@ public async Task TestTypeWithDocumentationAsync(string typeName)
4843
}
4944

5045
[Theory]
51-
[InlineData("enum")]
52-
[InlineData("class")]
53-
[InlineData("struct")]
54-
[InlineData("interface")]
46+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
5547
public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
5648
{
5749
var testCode = @"
@@ -63,10 +55,7 @@ public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
6355
}
6456

6557
[Theory]
66-
[InlineData("enum")]
67-
[InlineData("class")]
68-
[InlineData("struct")]
69-
[InlineData("interface")]
58+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
7059
public async Task TestTypeWithoutDocumentationAsync(string typeName)
7160
{
7261
var testCode = @"
@@ -84,16 +73,14 @@ public async Task TestTypeWithoutDocumentationAsync(string typeName)
8473
}
8574

8675
[Theory]
87-
[InlineData("partial class")]
88-
[InlineData("partial struct")]
89-
[InlineData("partial interface")]
76+
[MemberData(nameof(CommonMemberData.TypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
9077
public async Task TestPartialTypeWithoutDocumentationAsync(string typeName)
9178
{
9279
var testCode = @"
9380
/// <summary>
9481
///
9582
/// </summary>
96-
{0}
83+
partial {0}
9784
TypeName
9885
{{
9986
}}";

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1608UnitTests.cs

+8-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.Testing;
99
using StyleCop.Analyzers.DocumentationRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1011
using StyleCop.Analyzers.Test.Verifiers;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.DocumentationRules.SA1608ElementDocumentationMustNotHaveDefaultSummary>;
@@ -17,9 +18,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1718
public class SA1608UnitTests
1819
{
1920
[Theory]
20-
[InlineData("class")]
21-
[InlineData("struct")]
22-
[InlineData("interface")]
21+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
2322
public async Task TestTypeNoDocumentationAsync(string typeName)
2423
{
2524
var testCode = @"
@@ -30,9 +29,7 @@ public async Task TestTypeNoDocumentationAsync(string typeName)
3029
}
3130

3231
[Theory]
33-
[InlineData("class")]
34-
[InlineData("struct")]
35-
[InlineData("interface")]
32+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
3633
public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
3734
{
3835
var testCode = @"
@@ -46,9 +43,7 @@ public async Task TestTypeWithSummaryDocumentationAsync(string typeName)
4643
}
4744

4845
[Theory]
49-
[InlineData("class")]
50-
[InlineData("struct")]
51-
[InlineData("interface")]
46+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
5247
public async Task TestTypeWithContentDocumentationAsync(string typeName)
5348
{
5449
var testCode = @"
@@ -62,9 +57,7 @@ public async Task TestTypeWithContentDocumentationAsync(string typeName)
6257
}
6358

6459
[Theory]
65-
[InlineData("class")]
66-
[InlineData("struct")]
67-
[InlineData("interface")]
60+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
6861
public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
6962
{
7063
var testCode = @"
@@ -76,9 +69,7 @@ public async Task TestTypeWithInheritedDocumentationAsync(string typeName)
7669
}
7770

7871
[Theory]
79-
[InlineData("class")]
80-
[InlineData("struct")]
81-
[InlineData("interface")]
72+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
8273
public async Task TestTypeWithoutSummaryDocumentationAsync(string typeName)
8374
{
8475
var testCode = @"
@@ -93,9 +84,7 @@ public async Task TestTypeWithoutSummaryDocumentationAsync(string typeName)
9384
}
9485

9586
[Theory]
96-
[InlineData("class")]
97-
[InlineData("struct")]
98-
[InlineData("interface")]
87+
[MemberData(nameof(CommonMemberData.TypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
9988
public async Task TestTypeWithoutContentDocumentationAsync(string typeName)
10089
{
10190
var testCode = @"
@@ -110,9 +99,7 @@ public async Task TestTypeWithoutContentDocumentationAsync(string typeName)
11099
}
111100

112101
[Theory]
113-
[InlineData("class")]
114-
[InlineData("struct")]
115-
[InlineData("interface")]
102+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
116103
public async Task TestTypeWithDefaultDocumentationAsync(string typeName)
117104
{
118105
var testCode = $@"

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1625UnitTests.cs

+5
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.SA1625ElementDocumentationMustNotBeCopiedAndPasted>;
@@ -28,6 +29,10 @@ public static IEnumerable<object[]> Members
2829
yield return new[] { "public struct Test { }" };
2930
yield return new[] { "public enum Test { }" };
3031
yield return new[] { "public delegate void Test();" };
32+
if (LightupHelpers.SupportsCSharp9)
33+
{
34+
yield return new[] { "public record Test { }" };
35+
}
3136
}
3237
}
3338

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/CommonMemberData.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@
44
namespace StyleCop.Analyzers.Test.Helpers
55
{
66
using System.Collections.Generic;
7+
using System.Linq;
78
using StyleCop.Analyzers.Lightup;
89

910
public static class CommonMemberData
1011
{
11-
public static IEnumerable<object[]> TypeDeclarationKeywords
12+
public static IEnumerable<object[]> DataTypeDeclarationKeywords
1213
{
1314
get
1415
{
1516
yield return new[] { "class" };
1617
yield return new[] { "struct" };
17-
yield return new[] { "interface" };
1818
if (LightupHelpers.SupportsCSharp9)
1919
{
2020
yield return new[] { "record" };
2121
}
2222
}
2323
}
2424

25-
public static IEnumerable<object[]> BaseTypeDeclarationKeywords
25+
public static IEnumerable<object[]> TypeDeclarationKeywords
2626
{
2727
get
2828
{
29-
foreach (var keyword in TypeDeclarationKeywords)
30-
{
31-
yield return keyword;
32-
}
29+
return DataTypeDeclarationKeywords
30+
.Concat(new[] { new[] { "interface" } });
31+
}
32+
}
3333

34-
yield return new[] { "enum" };
34+
public static IEnumerable<object[]> BaseTypeDeclarationKeywords
35+
{
36+
get
37+
{
38+
return TypeDeclarationKeywords
39+
.Concat(new[] { new[] { "enum" } });
3540
}
3641
}
3742
}

0 commit comments

Comments
 (0)