@@ -7,17 +7,36 @@ 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 const string TestSettingsNoSystemDirectivesFirst = @"
29+ {
30+ ""settings"": {
31+ ""orderingRules"": {
32+ ""systemUsingDirectivesFirst"": false
33+ }
34+ }
35+ }
36+ " ;
37+
38+ private bool useSystemUsingDirectivesFirst ;
39+
2140 /// <summary>
2241 /// Verifies that the analyzer will not produce diagnostics for correctly ordered using directives inside a namespace.
2342 /// </summary>
@@ -34,7 +53,7 @@ public async Task TestValidUsingDirectivesInNamespaceAsync()
3453}
3554" ;
3655
37- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
56+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
3857 }
3958
4059 /// <summary>
@@ -61,7 +80,7 @@ namespace Bar
6180}
6281" ;
6382
64- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
83+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
6584 }
6685
6786 /// <summary>
@@ -81,7 +100,7 @@ public class Foo
81100}
82101" ;
83102
84- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
103+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
85104 }
86105
87106 /// <summary>
@@ -131,7 +150,7 @@ namespace Bar
131150 Diagnostic ( ) . WithLocation ( 11 , 5 ) . WithArguments ( "System.Math" , "System.Array" ) ,
132151 } ;
133152
134- await VerifyCSharpFixAsync ( testCode , expectedDiagnostics , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
153+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostics , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
135154 }
136155
137156 /// <summary>
@@ -150,7 +169,7 @@ public async Task TestValidUsingDirectivesWithInlineCommentsAsync()
150169}
151170" ;
152171
153- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
172+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
154173 }
155174
156175 /// <summary>
@@ -178,9 +197,12 @@ public async Task TestInvalidUsingDirectivesWithGlobalPrefixAsync()
178197}
179198" ;
180199
181- var expectedDiagnostic = Diagnostic ( ) . WithLocation ( 5 , 5 ) . WithArguments ( "System.Math" , "global::System.Array" ) ;
200+ DiagnosticResult [ ] expectedDiagnostic =
201+ {
202+ Diagnostic ( ) . WithLocation ( 5 , 5 ) . WithArguments ( "System.Math" , "global::System.Array" ) ,
203+ } ;
182204
183- await VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
205+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
184206 }
185207
186208 /// <summary>
@@ -191,8 +213,8 @@ public async Task TestInvalidUsingDirectivesWithGlobalPrefixAsync()
191213 public async Task TestPreprocessorDirectivesAsync ( )
192214 {
193215 var testCode = @"
194- using System;
195216using Microsoft.Win32;
217+ using System;
196218using MyList = System.Collections.Generic.List<int>;
197219using static System.Tuple;
198220
@@ -205,8 +227,8 @@ public async Task TestPreprocessorDirectivesAsync()
205227#endif" ;
206228
207229 var fixedTestCode = @"
208- using System;
209230using Microsoft.Win32;
231+ using System;
210232using static System.Tuple;
211233using MyList = System.Collections.Generic.List<int>;
212234
@@ -219,9 +241,136 @@ public async Task TestPreprocessorDirectivesAsync()
219241#endif" ;
220242
221243 // else block is skipped
222- var expected = Diagnostic ( ) . WithLocation ( 8 , 1 ) . WithArguments ( "System.String" , "System.Math" ) ;
244+ DiagnosticResult [ ] expectedDiagnostic =
245+ {
246+ Diagnostic ( ) . WithLocation ( 8 , 1 ) . WithArguments ( "System.String" , "System.Math" ) ,
247+ } ;
248+
249+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
250+ }
251+
252+ /// <summary>
253+ /// Verify that the systemUsingDirectivesFirst setting is honored correctly.
254+ /// </summary>
255+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
256+ [ Fact ]
257+ [ WorkItem ( 2163 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2163" ) ]
258+ public async Task VerifySystemUsingDirectivesFirstAsync ( )
259+ {
260+ this . useSystemUsingDirectivesFirst = true ;
261+
262+ var testCode = @"
263+ using static MyNamespace.TestClass;
264+ using static System.Math;
265+
266+ namespace MyNamespace
267+ {
268+ public static class TestClass
269+ {
270+ public static void TestMethod()
271+ {
272+ }
273+ }
274+ }
275+ " ;
276+
277+ var fixedTestCode = @"
278+ using static System.Math;
279+ using static MyNamespace.TestClass;
280+
281+ namespace MyNamespace
282+ {
283+ public static class TestClass
284+ {
285+ public static void TestMethod()
286+ {
287+ }
288+ }
289+ }
290+ " ;
291+
292+ DiagnosticResult [ ] expectedDiagnostic =
293+ {
294+ Diagnostic ( ) . WithLocation ( 2 , 1 ) . WithArguments ( "MyNamespace.TestClass" , "System.Math" ) ,
295+ } ;
296+
297+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
298+ }
299+
300+ /// <summary>
301+ /// Verify that the systemUsingDirectivesFirst setting is honored correctly when using multiple static system usings.
302+ /// </summary>
303+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
304+ [ Fact ]
305+ [ WorkItem ( 2163 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2163" ) ]
306+ public async Task VerifyMultipleStaticSystemUsingDirectivesAsync ( )
307+ {
308+ this . useSystemUsingDirectivesFirst = true ;
309+
310+ var testCode = @"
311+ using static System.Math;
312+ using static System.Activator;
313+
314+ namespace MyNamespace
315+ {
316+ public static class TestClass
317+ {
318+ public static void TestMethod()
319+ {
320+ }
321+ }
322+ }
323+ " ;
324+
325+ var fixedTestCode = @"
326+ using static System.Activator;
327+ using static System.Math;
328+
329+ namespace MyNamespace
330+ {
331+ public static class TestClass
332+ {
333+ public static void TestMethod()
334+ {
335+ }
336+ }
337+ }
338+ " ;
339+
340+ DiagnosticResult [ ] expectedDiagnostic =
341+ {
342+ Diagnostic ( ) . WithLocation ( 2 , 1 ) . WithArguments ( "System.Math" , "System.Activator" ) ,
343+ } ;
344+
345+ await this . VerifyCSharpFixAsync ( testCode , expectedDiagnostic , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
346+ }
347+
348+ private static DiagnosticResult Diagnostic ( )
349+ => StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . Diagnostic ( ) ;
350+
351+ private Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
352+ {
353+ var test = new StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . CSharpTest
354+ {
355+ TestCode = source ,
356+ Settings = this . useSystemUsingDirectivesFirst ? TestSettings : TestSettingsNoSystemDirectivesFirst ,
357+ } ;
358+
359+ test . ExpectedDiagnostics . AddRange ( expected ) ;
360+ return test . RunAsync ( cancellationToken ) ;
361+ }
362+
363+ private Task VerifyCSharpFixAsync ( string source , DiagnosticResult [ ] expected , string fixedSource , CancellationToken cancellationToken )
364+ {
365+ var test = new StyleCopCodeFixVerifier < SA1217UsingStaticDirectivesMustBeOrderedAlphabetically , UsingCodeFixProvider > . CSharpTest
366+ {
367+ TestCode = source ,
368+ FixedCode = fixedSource ,
369+ Settings = this . useSystemUsingDirectivesFirst ? TestSettings : TestSettingsNoSystemDirectivesFirst ,
370+ } ;
223371
224- await VerifyCSharpFixAsync ( testCode , expected , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
372+ test . ExpectedDiagnostics . AddRange ( expected ) ;
373+ return test . RunAsync ( cancellationToken ) ;
225374 }
226375 }
227376}
0 commit comments