Skip to content

Commit f5f7d2d

Browse files
committed
Add tests for constraint clauses in type declarations
1 parent 0b976ba commit f5f7d2d

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,79 @@ class MyAttribute : Attribute {{ }}
101101
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
102102
}
103103

104+
[Theory]
105+
[InlineData("class")]
106+
[InlineData("struct")]
107+
[InlineData("interface")]
108+
public async Task TestTypeDeclarationConstraintClausesAsync(string typeKind)
109+
{
110+
string testCode = $@"
111+
{typeKind} NonGenericType
112+
{{
113+
}}
114+
115+
{typeKind} TypeWithoutConstraints<T>
116+
{{
117+
}}
118+
119+
{typeKind} TypeWithOneConstraint<T>
120+
where T : new()
121+
{{
122+
}}
123+
124+
{typeKind} TypeWithMultipleConstraints1<T1, T2, T3> where T1 : new()
125+
where T2 : new()
126+
where T3 : new()
127+
{{
128+
}}
129+
130+
{typeKind} TypeWithMultipleConstraints2<T1, T2, T3>
131+
where T1 : new()
132+
where T2 : new()
133+
where T3 : new()
134+
{{
135+
}}
136+
";
137+
string fixedCode = $@"
138+
{typeKind} NonGenericType
139+
{{
140+
}}
141+
142+
{typeKind} TypeWithoutConstraints<T>
143+
{{
144+
}}
145+
146+
{typeKind} TypeWithOneConstraint<T>
147+
where T : new()
148+
{{
149+
}}
150+
151+
{typeKind} TypeWithMultipleConstraints1<T1, T2, T3> where T1 : new()
152+
where T2 : new()
153+
where T3 : new()
154+
{{
155+
}}
156+
157+
{typeKind} TypeWithMultipleConstraints2<T1, T2, T3>
158+
where T1 : new()
159+
where T2 : new()
160+
where T3 : new()
161+
{{
162+
}}
163+
";
164+
165+
DiagnosticResult[] expected =
166+
{
167+
this.CSharpDiagnostic().WithLocation(17, 1),
168+
this.CSharpDiagnostic().WithLocation(23, 1),
169+
this.CSharpDiagnostic().WithLocation(24, 1),
170+
};
171+
172+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
173+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
174+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
175+
}
176+
104177
[Fact]
105178
public async Task TestBlockAsync()
106179
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
128128
{
129129
var typeDeclaration = (TypeDeclarationSyntax)context.Node;
130130

131+
CheckElements(context, typeDeclaration.ConstraintClauses);
131132
CheckElements(context, typeDeclaration.Members);
132133
}
133134

0 commit comments

Comments
 (0)