33
44namespace StyleCop . Analyzers . Test . OrderingRules
55{
6- using System . Collections . Generic ;
76 using System . Threading ;
87 using System . Threading . Tasks ;
9- using Microsoft . CodeAnalysis . CodeFixes ;
10- using Microsoft . CodeAnalysis . Diagnostics ;
8+ using Microsoft . CodeAnalysis ;
119 using StyleCop . Analyzers . OrderingRules ;
1210 using StyleCop . Analyzers . Settings . ObjectModel ;
11+ using StyleCop . Analyzers . Test . Verifiers ;
1312 using TestHelper ;
1413 using Xunit ;
1514
1615 /// <summary>
1716 /// Unit tests for the <see cref="SA1200UsingDirectivesMustBePlacedCorrectly"/> when configured to use
1817 /// <see cref="UsingDirectivesPlacement.OutsideNamespace"/>.
1918 /// </summary>
20- public class SA1200OutsideNamespaceUnitTests : CodeFixVerifier
19+ public class SA1200OutsideNamespaceUnitTests
2120 {
2221 private const string TestSettings = @"
2322{
@@ -51,6 +50,9 @@ public class SA1200OutsideNamespaceUnitTests : CodeFixVerifier
5150
5251 private const string DelegateDefinition = @"public delegate void TestDelegate();" ;
5352
53+ internal static DiagnosticResult [ ] EmptyDiagnosticResults
54+ => StyleCopCodeFixVerifier < SA1200UsingDirectivesMustBePlacedCorrectly , UsingCodeFixProvider > . EmptyDiagnosticResults ;
55+
5456 /// <summary>
5557 /// Verifies that using statements in a namespace produces the expected diagnostics.
5658 /// </summary>
@@ -74,13 +76,11 @@ namespace TestNamespace
7476
7577 DiagnosticResult [ ] expectedResults =
7678 {
77- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
78- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
79+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
80+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
7981 } ;
8082
81- await this . VerifyCSharpDiagnosticAsync ( testCode , expectedResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
82- await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
83- await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
83+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
8484 }
8585
8686 /// <summary>
@@ -108,14 +108,12 @@ namespace System
108108
109109 DiagnosticResult [ ] expectedResults =
110110 {
111- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
112- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
113- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 5 , 5 ) ,
111+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
112+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
113+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 5 , 5 ) ,
114114 } ;
115115
116- await this . VerifyCSharpDiagnosticAsync ( testCode , expectedResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
117- await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
118- await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
116+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
119117 }
120118
121119 /// <summary>
@@ -145,15 +143,13 @@ namespace System.MyExtension
145143
146144 DiagnosticResult [ ] expectedResults =
147145 {
148- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
149- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
150- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 5 , 5 ) ,
151- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 6 , 5 ) ,
146+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 3 , 5 ) ,
147+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 4 , 5 ) ,
148+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 5 , 5 ) ,
149+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 6 , 5 ) ,
152150 } ;
153151
154- await this . VerifyCSharpDiagnosticAsync ( testCode , expectedResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
155- await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
156- await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
152+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
157153 }
158154
159155 /// <summary>
@@ -174,7 +170,7 @@ public async Task TestValidUsingStatementsInCompilationUnitWithTypeDefinitionAsy
174170{ typeDefinition }
175171" ;
176172
177- await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
173+ await VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
178174 }
179175
180176 /// <summary>
@@ -207,13 +203,11 @@ namespace TestNamespace
207203
208204 DiagnosticResult [ ] expectedResults =
209205 {
210- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 7 , 5 ) ,
211- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 8 , 5 ) ,
206+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 7 , 5 ) ,
207+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 8 , 5 ) ,
212208 } ;
213209
214- await this . VerifyCSharpDiagnosticAsync ( testCode , expectedResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
215- await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
216- await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
210+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
217211 }
218212
219213 /// <summary>
@@ -231,7 +225,7 @@ namespace TestNamespace
231225}
232226" ;
233227
234- await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
228+ await VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
235229 }
236230
237231 /// <summary>
@@ -262,12 +256,10 @@ namespace TestNamespace
262256
263257 DiagnosticResult [ ] expectedResults =
264258 {
265- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 6 , 5 ) ,
259+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 6 , 5 ) ,
266260 } ;
267261
268- await this . VerifyCSharpDiagnosticAsync ( testCode , expectedResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
269- await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
270- await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
262+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
271263 }
272264
273265 [ Fact ]
@@ -301,31 +293,39 @@ namespace TestNamespace
301293
302294 DiagnosticResult [ ] expectedResults =
303295 {
304- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 8 , 5 ) ,
305- this . CSharpDiagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 10 , 5 ) ,
296+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 8 , 5 ) ,
297+ Diagnostic ( SA1200UsingDirectivesMustBePlacedCorrectly . DescriptorOutside ) . WithLocation ( 10 , 5 ) ,
306298 } ;
307299
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 ) ;
300+ await VerifyCSharpFixAsync ( testCode , expectedResults , fixedTestCode , CancellationToken . None ) . ConfigureAwait ( false ) ;
311301 }
312302
313- /// <inheritdoc/>
314- protected override string GetSettings ( )
315- {
316- return TestSettings ;
317- }
303+ private static DiagnosticResult Diagnostic ( DiagnosticDescriptor descriptor )
304+ => StyleCopCodeFixVerifier < SA1200UsingDirectivesMustBePlacedCorrectly , UsingCodeFixProvider > . Diagnostic ( descriptor ) ;
318305
319- /// <inheritdoc/>
320- protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
306+ private static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
321307 {
322- yield return new SA1200UsingDirectivesMustBePlacedCorrectly ( ) ;
308+ var test = new StyleCopCodeFixVerifier < SA1200UsingDirectivesMustBePlacedCorrectly , UsingCodeFixProvider > . CSharpTest
309+ {
310+ TestCode = source ,
311+ Settings = TestSettings ,
312+ } ;
313+
314+ test . ExpectedDiagnostics . AddRange ( expected ) ;
315+ return test . RunAsync ( cancellationToken ) ;
323316 }
324317
325- /// <inheritdoc/>
326- protected override CodeFixProvider GetCSharpCodeFixProvider ( )
318+ private static Task VerifyCSharpFixAsync ( string source , DiagnosticResult [ ] expected , string fixedSource , CancellationToken cancellationToken )
327319 {
328- return new UsingCodeFixProvider ( ) ;
320+ var test = new StyleCopCodeFixVerifier < SA1200UsingDirectivesMustBePlacedCorrectly , UsingCodeFixProvider > . CSharpTest
321+ {
322+ TestCode = source ,
323+ FixedCode = fixedSource ,
324+ Settings = TestSettings ,
325+ } ;
326+
327+ test . ExpectedDiagnostics . AddRange ( expected ) ;
328+ return test . RunAsync ( cancellationToken ) ;
329329 }
330330 }
331331}
0 commit comments