@@ -6,17 +6,16 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
66 using System . Collections . Generic ;
77 using System . Threading ;
88 using System . Threading . Tasks ;
9- using Microsoft . CodeAnalysis ;
10- using Microsoft . CodeAnalysis . Diagnostics ;
119 using StyleCop . Analyzers . DocumentationRules ;
12- using StyleCop . Analyzers . Test . Helpers ;
10+ using StyleCop . Analyzers . Test . Verifiers ;
1311 using TestHelper ;
1412 using Xunit ;
13+ using static StyleCop . Analyzers . Test . Verifiers . CustomDiagnosticVerifier < StyleCop . Analyzers . DocumentationRules . GenericTypeParameterDocumentationAnalyzer > ;
1514
1615 /// <summary>
1716 /// This class contains unit tests for the SA1621 diagnostic.
1817 /// </summary>
19- public class SA1621UnitTests : DiagnosticVerifier
18+ public class SA1621UnitTests
2019 {
2120 public static IEnumerable < object [ ] > Members
2221 {
@@ -55,7 +54,7 @@ public class ClassName
5554{
5655 public ##
5756}" ;
58- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
57+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
5958 }
6059
6160 [ Theory ]
@@ -64,7 +63,7 @@ public async Task TestTypesWithNoDocumentationAsync(string p)
6463 {
6564 var testCode = @"
6665public ##" ;
67- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
66+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
6867 }
6968
7069 [ Fact ]
@@ -75,7 +74,7 @@ public async Task TestTypesWithoutTypeParametersAsync()
7574/// Foo
7675/// </summary>
7776public class Foo { }" ;
78- await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
77+ await VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
7978 }
8079
8180 [ Theory ]
@@ -95,7 +94,7 @@ public class ClassName
9594 /// <typeparam name=""Tb"">Param 2</param>
9695 public ##
9796}" ;
98- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
97+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
9998 }
10099
101100 [ Theory ]
@@ -109,7 +108,7 @@ public async Task TestTypesWithAllDocumentationAsync(string p)
109108/// <typeparam name=""Ta"">Param 1</param>
110109/// <typeparam name=""Tb"">Param 2</param>
111110public ##" ;
112- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
111+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
113112 }
114113
115114 [ Theory ]
@@ -134,13 +133,13 @@ public class ClassName
134133
135134 var expected = new [ ]
136135 {
137- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 10 , 8 ) ,
138- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 11 , 8 ) ,
139- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 12 , 25 ) ,
140- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 13 , 25 ) ,
136+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 10 , 8 ) ,
137+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 11 , 8 ) ,
138+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 12 , 25 ) ,
139+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 13 , 25 ) ,
141140 } ;
142141
143- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "$$" , declaration ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
142+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "$$" , declaration ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
144143 }
145144
146145 [ Theory ]
@@ -159,13 +158,13 @@ public async Task TestTypesWithInvalidParamsAsync(string declaration)
159158
160159 var expected = new [ ]
161160 {
162- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 4 ) ,
163- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 6 , 4 ) ,
164- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 7 , 21 ) ,
165- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 8 , 21 ) ,
161+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 4 ) ,
162+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 6 , 4 ) ,
163+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 7 , 21 ) ,
164+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 8 , 21 ) ,
166165 } ;
167166
168- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "$$" , declaration ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
167+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "$$" , declaration ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
169168 }
170169
171170 /// <summary>
@@ -182,7 +181,7 @@ public async Task TestTypeWithIncludedDocumentationAsync(string p)
182181public ##
183182" ;
184183
185- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
184+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
186185 }
187186
188187 /// <summary>
@@ -199,7 +198,7 @@ public async Task TestTypeWithoutTypeparamInIncludedDocumentationAsync(string p)
199198public ##
200199" ;
201200
202- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
201+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
203202 }
204203
205204 /// <summary>
@@ -216,7 +215,7 @@ public async Task TestTypeWithInheritDocInIncludedDocumentationAsync(string p)
216215public ##
217216" ;
218217
219- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
218+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
220219 }
221220
222221 /// <summary>
@@ -234,12 +233,12 @@ public async Task TestTypeWithInvalidTypeParamInIncludedDocumentationAsync(strin
234233" ;
235234 DiagnosticResult [ ] expected =
236235 {
237- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
238- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
239- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
236+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
237+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
238+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 2 , 5 ) ,
240239 } ;
241240
242- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
241+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
243242 }
244243
245244 /// <summary>
@@ -260,7 +259,7 @@ public class TestClass
260259}
261260" ;
262261
263- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
262+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
264263 }
265264
266265 /// <summary>
@@ -281,7 +280,7 @@ public class TestClass
281280}
282281" ;
283282
284- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
283+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
285284 }
286285
287286 /// <summary>
@@ -302,7 +301,7 @@ public class TestClass
302301}
303302" ;
304303
305- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
304+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
306305 }
307306
308307 /// <summary>
@@ -324,41 +323,44 @@ public class TestClass
324323" ;
325324 DiagnosticResult [ ] expected =
326325 {
327- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
328- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
329- this . CSharpDiagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
326+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
327+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
328+ Diagnostic ( GenericTypeParameterDocumentationAnalyzer . SA1621Descriptor ) . WithLocation ( 5 , 9 ) ,
330329 } ;
331330
332- await this . VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
331+ await VerifyCSharpDiagnosticAsync ( testCode . Replace ( "##" , p ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
333332 }
334333
335- protected override Project ApplyCompilationOptions ( Project project )
334+ private static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult expected , CancellationToken cancellationToken )
335+ => VerifyCSharpDiagnosticAsync ( source , new [ ] { expected } , cancellationToken ) ;
336+
337+ private static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
336338 {
337- var resolver = new TestXmlReferenceResolver ( ) ;
339+ var test = CreateTest ( expected ) ;
340+ test . TestCode = source ;
341+
342+ return test . RunAsync ( cancellationToken ) ;
343+ }
338344
345+ private static StyleCopDiagnosticVerifier < GenericTypeParameterDocumentationAnalyzer > . CSharpTest CreateTest ( DiagnosticResult [ ] expected )
346+ {
339347 string contentTypeWithTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
340348<Foo>
341349 <summary>Test class</summary>
342350 <typeparam name=""Ta"">Param 1</typeparam>
343351 <typeparam name=""Tb"">Param 2</typeparam>
344352</Foo>
345353" ;
346- resolver . XmlReferences . Add ( "TypeWithTypeparamsDoc.xml" , contentTypeWithTypeparamDoc ) ;
347-
348354 string contentTypeWithoutTypeparamsDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
349355<Foo>
350356 <summary>Test class</summary>
351357</Foo>
352358" ;
353- resolver . XmlReferences . Add ( "TypeWithoutTypeparamsDoc.xml" , contentTypeWithoutTypeparamsDoc ) ;
354-
355359 string contentTypeInheritdoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
356360<Foo>
357361 <inheritdoc/>
358362</Foo>
359363" ;
360- resolver . XmlReferences . Add ( "TypeWithInheritdoc.xml" , contentTypeInheritdoc ) ;
361-
362364 string contentTypeWithInvalidTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
363365<Foo>
364366 <summary>Test class</summary>
@@ -367,8 +369,6 @@ protected override Project ApplyCompilationOptions(Project project)
367369 <typeparam name="" "">Param 3</typeparam>
368370</Foo>
369371" ;
370- resolver . XmlReferences . Add ( "TypeWithInvalidTypeparamsDoc.xml" , contentTypeWithInvalidTypeparamDoc ) ;
371-
372372 string contentMethodWithTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
373373<TestClass>
374374 <Foo>
@@ -378,26 +378,20 @@ protected override Project ApplyCompilationOptions(Project project)
378378 </Foo>
379379</TestClass>
380380" ;
381- resolver . XmlReferences . Add ( "MethodWithTypeparamsDoc.xml" , contentMethodWithTypeparamDoc ) ;
382-
383381 string contentMethodWithoutTypeparamsDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
384382<TestClass>
385383 <Foo>
386384 <summary>Test class</summary>
387385 </Foo>
388386</TestClass>
389387" ;
390- resolver . XmlReferences . Add ( "MethodWithoutTypeparamsDoc.xml" , contentMethodWithoutTypeparamsDoc ) ;
391-
392388 string contentMethodInheritdoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
393389<TestClass>
394390 <Foo>
395391 <inheritdoc/>
396392 </Foo>
397393</TestClass>
398394" ;
399- resolver . XmlReferences . Add ( "MethodWithInheritdoc.xml" , contentMethodInheritdoc ) ;
400-
401395 string contentMethodWithInvalidTypeparamDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
402396<TestClass>
403397 <Foo>
@@ -408,21 +402,25 @@ protected override Project ApplyCompilationOptions(Project project)
408402 </Foo>
409403</TestClass>
410404" ;
411- resolver . XmlReferences . Add ( "MethodWithInvalidTypeparamsDoc.xml" , contentMethodWithInvalidTypeparamDoc ) ;
412405
413- project = base . ApplyCompilationOptions ( project ) ;
414- project = project . WithCompilationOptions ( project . CompilationOptions . WithXmlReferenceResolver ( resolver ) ) ;
415- return project ;
416- }
417-
418- protected override IEnumerable < string > GetDisabledDiagnostics ( )
419- {
420- yield return GenericTypeParameterDocumentationAnalyzer . SA1622Descriptor . Id ;
421- }
406+ var test = new StyleCopDiagnosticVerifier < GenericTypeParameterDocumentationAnalyzer > . CSharpTest
407+ {
408+ DisabledDiagnostics = { GenericTypeParameterDocumentationAnalyzer . SA1622Descriptor . Id } ,
409+ XmlReferences =
410+ {
411+ { "TypeWithTypeparamsDoc.xml" , contentTypeWithTypeparamDoc } ,
412+ { "TypeWithoutTypeparamsDoc.xml" , contentTypeWithoutTypeparamsDoc } ,
413+ { "TypeWithInheritdoc.xml" , contentTypeInheritdoc } ,
414+ { "TypeWithInvalidTypeparamsDoc.xml" , contentTypeWithInvalidTypeparamDoc } ,
415+ { "MethodWithTypeparamsDoc.xml" , contentMethodWithTypeparamDoc } ,
416+ { "MethodWithoutTypeparamsDoc.xml" , contentMethodWithoutTypeparamsDoc } ,
417+ { "MethodWithInheritdoc.xml" , contentMethodInheritdoc } ,
418+ { "MethodWithInvalidTypeparamsDoc.xml" , contentMethodWithInvalidTypeparamDoc } ,
419+ } ,
420+ } ;
422421
423- protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
424- {
425- yield return new GenericTypeParameterDocumentationAnalyzer ( ) ;
422+ test . ExpectedDiagnostics . AddRange ( expected ) ;
423+ return test ;
426424 }
427425 }
428426}
0 commit comments