@@ -7,17 +7,26 @@ namespace StyleCop.Analyzers.Test.OrderingRules
77 using System . Threading . Tasks ;
88 using Microsoft . CodeAnalysis . Testing ;
99 using StyleCop . Analyzers . OrderingRules ;
10- using TestHelper ;
10+ using StyleCop . Analyzers . Test . Verifiers ;
1111 using Xunit ;
12- using static StyleCop . Analyzers . Test . Verifiers . StyleCopCodeFixVerifier <
13- StyleCop . Analyzers . OrderingRules . SA1217UsingStaticDirectivesMustBeOrderedAlphabetically ,
14- StyleCop . Analyzers . OrderingRules . UsingCodeFixProvider > ;
1512
1613 /// <summary>
1714 /// Unit tests for <see cref="SA1217UsingStaticDirectivesMustBeOrderedAlphabetically"/>.
1815 /// </summary>
1916 public class SA1217UnitTests
2017 {
18+ private const string TestSettings = @"
19+ {
20+ ""settings"": {
21+ ""orderingRules"": {
22+ ""systemUsingDirectivesFirst"": ""true""
23+ }
24+ }
25+ }
26+ " ;
27+
28+ private bool useSystemUsingDirectivesFirst ;
29+
2130 /// <summary>
2231 /// Verifies that the analyzer will not produce diagnostics for correctly ordered using directives inside a namespace.
2332 /// </summary>
@@ -34,7 +43,7 @@ public async Task TestValidUsingDirectivesInNamespaceAsync()
3443}
3544" ;
3645
37- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
46+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
3847 }
3948
4049 /// <summary>
@@ -61,7 +70,7 @@ namespace Bar
6170}
6271" ;
6372
64- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
73+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
6574 }
6675
6776 /// <summary>
@@ -81,7 +90,7 @@ public class Foo
8190}
8291" ;
8392
84- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
93+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
8594 }
8695
8796 /// <summary>
@@ -131,7 +140,7 @@ namespace Bar
131140 Diagnostic ( ) . WithLocation ( 11 , 5 ) . WithArguments ( "System.Math" , "System.Array" ) ,
132141 } ;
133142
134- await VerifyCSharpFixAsync ( testCode , expectedDiagnostics , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
143+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostics , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
135144 }
136145
137146 /// <summary>
@@ -150,7 +159,7 @@ public async Task TestValidUsingDirectivesWithInlineCommentsAsync()
150159}
151160" ;
152161
153- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
162+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
154163 }
155164
156165 /// <summary>
@@ -178,9 +187,12 @@ public async Task TestInvalidUsingDirectivesWithGlobalPrefixAsync()
178187}
179188" ;
180189
181- var expectedDiagnostic = Diagnostic ( ) . WithLocation ( 5 , 5 ) . WithArguments ( "System.Math" , "global::System.Array" ) ;
190+ DiagnosticResult [ ] expectedDiagnostic =
191+ {
192+ Diagnostic ( ) . WithLocation ( 5 , 5 ) . WithArguments ( "System.Math" , "global::System.Array" ) ,
193+ } ;
182194
183- await VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
195+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
184196 }
185197
186198 /// <summary>
@@ -219,9 +231,88 @@ public async Task TestPreprocessorDirectivesAsync()
219231#endif" ;
220232
221233 // else block is skipped
222- var expected = Diagnostic ( ) . WithLocation ( 8 , 1 ) . WithArguments ( "System.String" , "System.Math" ) ;
234+ DiagnosticResult [ ] expectedDiagnostic =
235+ {
236+ Diagnostic ( ) . WithLocation ( 8 , 1 ) . WithArguments ( "System.String" , "System.Math" ) ,
237+ } ;
238+
239+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
240+ }
241+
242+ /// <summary>
243+ /// Verify that the systemUsingDirectivesFirst setting is honored correctly.
244+ /// </summary>
245+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
246+ [ Fact ]
247+ [ WorkItem ( 2163 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2163" ) ]
248+ public async Task VerifySystemUsingDirectivesFirstAsync ( )
249+ {
250+ this . useSystemUsingDirectivesFirst = true ;
251+
252+ var testCode = @"
253+ using static MyNamespace.TestClass;
254+ using static System.Math;
255+
256+ namespace MyNamespace
257+ {
258+ public static class TestClass
259+ {
260+ public static void TestMethod()
261+ {
262+ }
263+ }
264+ }
265+ " ;
266+
267+ var fixedTestCode = @"
268+ using static System.Math;
269+ using static MyNamespace.TestClass;
270+
271+ namespace MyNamespace
272+ {
273+ public static class TestClass
274+ {
275+ public static void TestMethod()
276+ {
277+ }
278+ }
279+ }
280+ " ;
281+
282+ DiagnosticResult [ ] expectedDiagnostic =
283+ {
284+ Diagnostic ( ) . WithLocation ( 2 , 1 ) . WithArguments ( "MyNamespace.TestClass" , "System.Math" ) ,
285+ } ;
286+
287+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
288+ }
289+
290+ private static DiagnosticResult Diagnostic ( )
291+ => StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . Diagnostic ( ) ;
292+
293+ private Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
294+ {
295+ var test = new StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . CSharpTest
296+ {
297+ TestCode = source ,
298+ Settings = this . useSystemUsingDirectivesFirst ? TestSettings : null ,
299+ } ;
300+
301+ test . ExpectedDiagnostics . AddRange ( expected ) ;
302+ return test . RunAsync ( cancellationToken ) ;
303+ }
304+
305+ private Task VerifyCSharpFixAsync ( string source , DiagnosticResult [ ] expected , string fixedSource , CancellationToken cancellationToken )
306+ {
307+ var test = new StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . CSharpTest
308+ {
309+ TestCode = source ,
310+ FixedCode = fixedSource ,
311+ Settings = this . useSystemUsingDirectivesFirst ? TestSettings : null ,
312+ } ;
223313
224- await VerifyCSharpFixAsync ( testCode , expected , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
314+ test . ExpectedDiagnostics . AddRange ( expected ) ;
315+ return test . RunAsync ( cancellationToken ) ;
225316 }
226317 }
227318}
0 commit comments