Skip to content

Commit 4b8e81b

Browse files
committed
Add tests for comments and groups when moving out of a namespace
1 parent 4bbe32e commit 4b8e81b

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,19 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
237237
continue;
238238
}
239239

240-
triviaToMove.Insert(m, SyntaxFactory.Whitespace(indentation));
241-
m++;
240+
if (string.IsNullOrEmpty(indentation))
241+
{
242+
if (triviaToMove[m].IsKind(SyntaxKind.WhitespaceTrivia))
243+
{
244+
triviaToMove.RemoveAt(m);
245+
m--;
246+
}
247+
}
248+
else
249+
{
250+
triviaToMove.Insert(m, SyntaxFactory.Whitespace(indentation));
251+
m++;
252+
}
242253
}
243254

244255
// strip any leading whitespace on each line (and also blank lines)

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200OutsideNamespaceUnitTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,46 @@ namespace TestNamespace
270270
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
271271
}
272272

273+
[Fact]
274+
public async Task TestFileHeaderIsProperlyPreservedWhenMovingUsingStatementsWithCommentsAsync()
275+
{
276+
var testCode = @"// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
277+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
278+
279+
namespace TestNamespace
280+
{
281+
// Separated Comment
282+
283+
using System.Collections;
284+
// Comment
285+
using System;
286+
}
287+
";
288+
var fixedTestCode = @"// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
289+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
290+
291+
// Separated Comment
292+
293+
// Comment
294+
using System;
295+
using System.Collections;
296+
297+
namespace TestNamespace
298+
{
299+
}
300+
";
301+
302+
DiagnosticResult[] expectedResults =
303+
{
304+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(8, 5),
305+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(10, 5),
306+
};
307+
308+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
309+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
310+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
311+
}
312+
273313
/// <inheritdoc/>
274314
protected override string GetSettings()
275315
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderGroupSeparationUnitTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1919
/// </summary>
2020
public class UsingCodeFixProviderGroupSeparationUnitTests : CodeFixVerifier
2121
{
22+
private bool usingsInsideNamespace = true;
2223
private bool? systemUsingDirectivesFirst;
2324

2425
/// <summary>
@@ -71,6 +72,53 @@ public class Bar
7172
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
7273
}
7374

75+
[Fact]
76+
public async Task VerifyUsingReorderingOutsideNamespaceAsync()
77+
{
78+
this.systemUsingDirectivesFirst = true;
79+
80+
var testCode = @"namespace Foo
81+
{
82+
using Microsoft.CodeAnalysis;
83+
using SystemAction = System.Action;
84+
using static System.Math;
85+
using System;
86+
using static System.String;
87+
using MyFunc = System.Func<int,bool>;
88+
using System.Collections.Generic;
89+
using System.Collections;
90+
91+
public class Bar
92+
{
93+
}
94+
}
95+
";
96+
97+
var fixedTestCode = @"using System;
98+
using System.Collections;
99+
using System.Collections.Generic;
100+
101+
using Microsoft.CodeAnalysis;
102+
103+
using static System.Math;
104+
using static System.String;
105+
106+
using MyFunc = System.Func<int, bool>;
107+
using SystemAction = System.Action;
108+
109+
namespace Foo
110+
{
111+
public class Bar
112+
{
113+
}
114+
}
115+
";
116+
117+
this.usingsInsideNamespace = false;
118+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
119+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
120+
}
121+
74122
/// <inheritdoc/>
75123
protected override string GetSettings()
76124
{
@@ -80,6 +128,7 @@ protected override string GetSettings()
80128
{{
81129
""settings"": {{
82130
""orderingRules"": {{
131+
""usingDirectivesPlacement"": ""{(this.usingsInsideNamespace ? "insideNamespace" : "outsideNamespace")}"",
83132
""systemUsingDirectivesFirst"" : {systemUsingDirectivesFirst.ToString().ToLowerInvariant()},
84133
""blankLinesBetweenUsingGroups"": ""require""
85134
}}

0 commit comments

Comments
 (0)