Skip to content

Commit 505b920

Browse files
committed
Add test for block statements and fix bugs
* Fix handling of consecutive labels * Fix analysis of nodes which are not indented * Fix logic error in code fix
1 parent 0cdf3ec commit 505b920

4 files changed

Lines changed: 127 additions & 17 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/IndentationCodeFixProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ private static bool TryGetTextChange(Diagnostic diagnostic, SyntaxNode syntaxRoo
7070
TextSpan originalSpan;
7171
if (trivia == default(SyntaxTrivia))
7272
{
73-
originalSpan = trivia.Span;
73+
// The warning was reported on a token because the line is not indented
74+
originalSpan = new TextSpan(diagnostic.Location.SourceSpan.Start, 0);
7475
}
7576
else
7677
{
77-
originalSpan = new TextSpan(diagnostic.Location.SourceSpan.Start, 0);
78+
originalSpan = trivia.Span;
7879
}
7980

8081
textChange = new TextChange(originalSpan, replacement);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.ReadabilityRules
5+
{
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.CodeFixes;
10+
using Microsoft.CodeAnalysis.Diagnostics;
11+
using StyleCop.Analyzers.ReadabilityRules;
12+
using TestHelper;
13+
using Xunit;
14+
15+
/// <summary>
16+
/// This class contains unit tests for <see cref="SA1137ElementsShouldHaveTheSameIndentation"/>.
17+
/// </summary>
18+
public class SA1137UnitTests : CodeFixVerifier
19+
{
20+
[Fact]
21+
public async Task TestBlockAsync()
22+
{
23+
string testCode = @"
24+
class ClassName
25+
{
26+
void MethodName()
27+
{
28+
label1:
29+
if (true)
30+
{
31+
}
32+
33+
label2:
34+
while (true)
35+
{
36+
}
37+
38+
label3:
39+
while (true)
40+
{
41+
label4a:
42+
label4b:
43+
int x;
44+
45+
label5a:
46+
label5b:
47+
int y;
48+
}
49+
}
50+
}
51+
";
52+
string fixedCode = @"
53+
class ClassName
54+
{
55+
void MethodName()
56+
{
57+
label1:
58+
if (true)
59+
{
60+
}
61+
62+
label2:
63+
while (true)
64+
{
65+
}
66+
67+
label3:
68+
while (true)
69+
{
70+
label4a:
71+
label4b:
72+
int x;
73+
74+
label5a:
75+
label5b:
76+
int y;
77+
}
78+
}
79+
}
80+
";
81+
82+
DiagnosticResult[] expected =
83+
{
84+
this.CSharpDiagnostic().WithLocation(11, 1),
85+
this.CSharpDiagnostic().WithLocation(12, 1),
86+
this.CSharpDiagnostic().WithLocation(16, 1),
87+
this.CSharpDiagnostic().WithLocation(17, 1),
88+
this.CSharpDiagnostic().WithLocation(20, 1),
89+
this.CSharpDiagnostic().WithLocation(23, 1),
90+
this.CSharpDiagnostic().WithLocation(25, 1),
91+
};
92+
93+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
94+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
95+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
96+
}
97+
98+
/// <inheritdoc/>
99+
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
100+
{
101+
yield return new SA1137ElementsShouldHaveTheSameIndentation();
102+
}
103+
104+
/// <inheritdoc/>
105+
protected override CodeFixProvider GetCSharpCodeFixProvider()
106+
{
107+
return new IndentationCodeFixProvider();
108+
}
109+
}
110+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
<Compile Include="ReadabilityRules\SA1132UnitTests.cs" />
327327
<Compile Include="ReadabilityRules\SA1133UnitTests.cs" />
328328
<Compile Include="ReadabilityRules\SA1134UnitTests.cs" />
329+
<Compile Include="ReadabilityRules\SA1137UnitTests.cs" />
329330
<Compile Include="ReadabilityRules\SX1101UnitTests.cs" />
330331
<Compile Include="Settings\SettingsFileCodeFixProviderUnitTests.cs" />
331332
<Compile Include="Settings\SettingsUnitTests.cs" />

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,14 @@ private static void HandleBlock(SyntaxNodeAnalysisContext context)
250250

251251
foreach (var statement in block.Statements)
252252
{
253-
if (statement.IsKind(SyntaxKind.LabeledStatement))
253+
StatementSyntax statementToAlign = statement;
254+
while (statementToAlign.IsKind(SyntaxKind.LabeledStatement))
254255
{
255-
labeledStatements.Add(statement);
256-
statements.Add(((LabeledStatementSyntax)statement).Statement);
257-
}
258-
else
259-
{
260-
statements.Add(statement);
256+
labeledStatements.Add(statementToAlign);
257+
statementToAlign = ((LabeledStatementSyntax)statementToAlign).Statement;
261258
}
259+
260+
statements.Add(statementToAlign);
262261
}
263262

264263
CheckElements(context, statements.ToImmutable());
@@ -277,15 +276,14 @@ private static void HandleSwitchStatement(SyntaxNodeAnalysisContext context)
277276
labels.AddRange(switchSection.Labels);
278277
foreach (var statement in switchSection.Statements)
279278
{
280-
if (statement.IsKind(SyntaxKind.LabeledStatement))
279+
StatementSyntax statementToAlign = statement;
280+
while (statementToAlign.IsKind(SyntaxKind.LabeledStatement))
281281
{
282-
labeledStatements.Add(statement);
283-
statements.Add(((LabeledStatementSyntax)statement).Statement);
284-
}
285-
else
286-
{
287-
statements.Add(statement);
282+
labeledStatements.Add(statementToAlign);
283+
statementToAlign = ((LabeledStatementSyntax)statementToAlign).Statement;
288284
}
285+
286+
statements.Add(statementToAlign);
289287
}
290288
}
291289

@@ -365,7 +363,7 @@ private static void CheckElements<T>(SyntaxNodeAnalysisContext context, Immutabl
365363
foreach (T element in elements)
366364
{
367365
SyntaxTrivia indentationTrivia = element.GetFirstToken().LeadingTrivia.LastOrDefault();
368-
string indentation = indentationTrivia == default(SyntaxTrivia) ? string.Empty : indentationTrivia.ToString();
366+
string indentation = indentationTrivia.IsKind(SyntaxKind.WhitespaceTrivia) ? indentationTrivia.ToString() : string.Empty;
369367

370368
if (first)
371369
{

0 commit comments

Comments
 (0)