Skip to content

Commit 513f08b

Browse files
committed
Merge pull request #1540 from sharwell/fix-1539
Do not separate System using directives when SA1208 is disabled
2 parents 0ddb78f + b4f4d27 commit 513f08b

5 files changed

Lines changed: 690 additions & 22 deletions

File tree

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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.OrderingRules
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.OrderingRules;
12+
using TestHelper;
13+
using Xunit;
14+
15+
/// <summary>
16+
/// Unit tests for <see cref="SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace"/> for the special case
17+
/// where <see cref="SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives"/> is disabled.
18+
/// </summary>
19+
public class SA1210CombinedSystemDirectivesUnitTests : CodeFixVerifier
20+
{
21+
[Fact]
22+
public async Task TestProperOrderedUsingDirectivesInNamespaceDeclarationAsync()
23+
{
24+
var namespaceDeclaration = @"namespace Food
25+
{
26+
using System;
27+
using System.Threading;
28+
}
29+
30+
namespace Bar
31+
{
32+
using Food;
33+
using System;
34+
}
35+
";
36+
37+
await this.VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
}
39+
40+
[Fact]
41+
public async Task TestInvalidOrderedUsingDirectivesInNamespaceDeclarationAsync()
42+
{
43+
var testCode = @"namespace Food
44+
{
45+
using System.Threading;
46+
using System;
47+
}
48+
49+
namespace Bar
50+
{
51+
using Food;
52+
using Bar;
53+
using System.Threading;
54+
using System;
55+
}";
56+
57+
var fixedTestCode = @"namespace Food
58+
{
59+
using System;
60+
using System.Threading;
61+
}
62+
63+
namespace Bar
64+
{
65+
using Bar;
66+
using Food;
67+
using System;
68+
using System.Threading;
69+
}";
70+
71+
DiagnosticResult[] expected =
72+
{
73+
this.CSharpDiagnostic().WithLocation(3, 5),
74+
this.CSharpDiagnostic().WithLocation(9, 5),
75+
this.CSharpDiagnostic().WithLocation(11, 5)
76+
};
77+
78+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
79+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
80+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
81+
}
82+
83+
[Fact]
84+
public async Task TestInvalidOrderedUsingDirectivesWithGlobalKeywordAsync()
85+
{
86+
var testCode = @"using System.Threading;
87+
using global::System.IO;
88+
using global::System.Linq;
89+
using global::System;
90+
using XYZ = System.IO;
91+
92+
namespace Food
93+
{
94+
using global::Food;
95+
using System;
96+
}";
97+
98+
var fixedTestCode = @"namespace Food
99+
{
100+
using global::Food;
101+
using global::System;
102+
using global::System.IO;
103+
using global::System.Linq;
104+
using System.Threading;
105+
using XYZ = System.IO;
106+
}";
107+
108+
DiagnosticResult[] expected =
109+
{
110+
this.CSharpDiagnostic().WithLocation(1, 1),
111+
this.CSharpDiagnostic().WithLocation(3, 1),
112+
};
113+
114+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
115+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
116+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
117+
}
118+
119+
[Fact]
120+
public async Task TestInvalidOrderedUsingDirectivesWithNamespaceAliasQualifierAsync()
121+
{
122+
var testCode = @"using System.Threading;
123+
using global::System.IO;
124+
using global::System.Linq;
125+
using global::System;
126+
using global::Food;
127+
using Food;
128+
129+
namespace Food
130+
{
131+
using global::Food;
132+
using System;
133+
}";
134+
135+
var fixedTestCode = @"namespace Food
136+
{
137+
using global::Food;
138+
using global::System;
139+
using global::System.IO;
140+
using global::System.Linq;
141+
using System.Threading;
142+
}";
143+
144+
DiagnosticResult[] expected =
145+
{
146+
this.CSharpDiagnostic().WithLocation(1, 1),
147+
this.CSharpDiagnostic().WithLocation(3, 1),
148+
this.CSharpDiagnostic().WithLocation(4, 1),
149+
this.CSharpDiagnostic().WithLocation(5, 1)
150+
};
151+
152+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
153+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
154+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
155+
}
156+
157+
[Fact]
158+
public async Task TestValidOrderedUsingDirectivesWithStaticUsingDirectivesAsync()
159+
{
160+
var namespaceDeclaration = @"namespace Food
161+
{
162+
using Food;
163+
using System;
164+
using static System.Uri;
165+
using static System.Math;
166+
}";
167+
168+
await this.VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
169+
}
170+
171+
[Fact]
172+
public async Task TestPreprocessorDirectivesAsync()
173+
{
174+
var testCode = @"
175+
using System;
176+
using Microsoft.VisualStudio;
177+
using MyList = System.Collections.Generic.List<int>;
178+
179+
#if true
180+
using Microsoft.CodeAnalysis.Diagnostics;
181+
using Microsoft.CodeAnalysis;
182+
#else
183+
using Microsoft.CodeAnalysis.Diagnostics;
184+
using Microsoft.CodeAnalysis;
185+
#endif";
186+
187+
var fixedTestCode = @"using Microsoft.VisualStudio;
188+
using System;
189+
using MyList = System.Collections.Generic.List<int>;
190+
191+
#if true
192+
using Microsoft.CodeAnalysis;
193+
using Microsoft.CodeAnalysis.Diagnostics;
194+
#else
195+
using Microsoft.CodeAnalysis.Diagnostics;
196+
using Microsoft.CodeAnalysis;
197+
#endif";
198+
199+
// else block is skipped
200+
DiagnosticResult[] expected =
201+
{
202+
this.CSharpDiagnostic().WithLocation(2, 1),
203+
this.CSharpDiagnostic().WithLocation(7, 1),
204+
};
205+
206+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
207+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
208+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
209+
}
210+
211+
/// <inheritdoc/>
212+
protected override IEnumerable<string> GetDisabledDiagnostics()
213+
{
214+
yield return SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives.DiagnosticId;
215+
}
216+
217+
/// <inheritdoc/>
218+
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
219+
{
220+
yield return new SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace();
221+
}
222+
223+
/// <inheritdoc/>
224+
protected override CodeFixProvider GetCSharpCodeFixProvider()
225+
{
226+
return new UsingCodeFixProvider();
227+
}
228+
}
229+
}

0 commit comments

Comments
 (0)