33
44namespace StyleCop . Analyzers . Test . DocumentationRules
55{
6- using System . Collections . Generic ;
76 using System . Threading ;
87 using System . Threading . Tasks ;
98 using Microsoft . CodeAnalysis ;
10- using Microsoft . CodeAnalysis . CodeFixes ;
11- using Microsoft . CodeAnalysis . Diagnostics ;
129 using StyleCop . Analyzers . DocumentationRules ;
13- using TestHelper ;
1410 using Xunit ;
11+ using static StyleCop . Analyzers . Test . Verifiers . StyleCopCodeFixVerifier <
12+ StyleCop . Analyzers . DocumentationRules . SA1600ElementsMustBeDocumented ,
13+ StyleCop . Analyzers . DocumentationRules . InheritdocCodeFixProvider > ;
1514
1615 /// <summary>
1716 /// This class contains unit tests for <see cref="InheritdocCodeFixProvider"/>.
1817 /// </summary>
19- public class InheritdocCodeFixProviderUnitTests : CodeFixVerifier
18+ public class InheritdocCodeFixProviderUnitTests
2019 {
2120 private static readonly DiagnosticDescriptor SA1600 = new SA1600ElementsMustBeDocumented ( ) . SupportedDiagnostics [ 0 ] ;
2221 private static readonly DiagnosticDescriptor CS1591 =
2322 new DiagnosticDescriptor ( nameof ( CS1591 ) , "Title" , "Missing XML comment for publicly visible type or member '{0}'" , "Category" , DiagnosticSeverity . Error , AnalyzerConstants . EnabledByDefault ) ;
2423
25- private DiagnosticDescriptor descriptor = SA1600 ;
26-
2724 [ Theory ]
2825 [ InlineData ( false , null , "string TestMember { get; set; }" ) ]
2926 [ InlineData ( false , null , "string TestMember() { return null; }" ) ]
@@ -66,27 +63,32 @@ public override {memberData}
6663}}
6764" ;
6865
69- if ( compilerWarning )
70- {
71- this . descriptor = CS1591 ;
72- }
66+ var descriptor = compilerWarning ? CS1591 : SA1600 ;
7367
74- DiagnosticResult [ ] expected =
68+ var test = new CSharpTest
7569 {
76- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ParentClass" ) . WithLocation ( 2 , 14 ) ,
77- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
78- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( memberName ) . WithLocation ( 12 , 40 ) ,
70+ TestCode = testCode ,
71+ ExpectedDiagnostics =
72+ {
73+ Diagnostic ( descriptor ) . WithArguments ( "ParentClass" ) . WithLocation ( 2 , 14 ) ,
74+ Diagnostic ( descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
75+ Diagnostic ( descriptor ) . WithArguments ( memberName ) . WithLocation ( 12 , 40 ) ,
76+ } ,
77+ FixedCode = fixedCode ,
78+ RemainingDiagnostics =
79+ {
80+ Diagnostic ( descriptor ) . WithArguments ( "ParentClass" ) . WithLocation ( 2 , 14 ) ,
81+ Diagnostic ( descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
82+ } ,
7983 } ;
8084
81- DiagnosticResult [ ] expectedFixed =
85+ if ( compilerWarning )
8286 {
83- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ParentClass" ) . WithLocation ( 2 , 14 ) ,
84- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
85- } ;
87+ test . DisabledDiagnostics . Add ( SA1600 . Id ) ;
88+ test . SolutionTransforms . Add ( SetCompilerDocumentationWarningToError ) ;
89+ }
8690
87- await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
88- await this . VerifyCSharpDiagnosticAsync ( fixedCode , expectedFixed , CancellationToken . None ) . ConfigureAwait ( false ) ;
89- await this . VerifyCSharpFixAsync ( testCode , fixedCode , numberOfFixAllIterations : 2 , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
91+ await test . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
9092 }
9193
9294 [ Theory ]
@@ -131,27 +133,32 @@ public class ChildClass : IParent
131133}}
132134" ;
133135
134- if ( compilerWarning )
135- {
136- this . descriptor = CS1591 ;
137- }
136+ var descriptor = compilerWarning ? CS1591 : SA1600 ;
138137
139- DiagnosticResult [ ] expected =
138+ var test = new CSharpTest
140139 {
141- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "IParent" ) . WithLocation ( 2 , 18 ) ,
142- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
143- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( memberName ) . WithLocation ( 12 , 31 ) ,
140+ TestCode = testCode ,
141+ ExpectedDiagnostics =
142+ {
143+ Diagnostic ( descriptor ) . WithArguments ( "IParent" ) . WithLocation ( 2 , 18 ) ,
144+ Diagnostic ( descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
145+ Diagnostic ( descriptor ) . WithArguments ( memberName ) . WithLocation ( 12 , 31 ) ,
146+ } ,
147+ FixedCode = fixedCode ,
148+ RemainingDiagnostics =
149+ {
150+ Diagnostic ( descriptor ) . WithArguments ( "IParent" ) . WithLocation ( 2 , 18 ) ,
151+ Diagnostic ( descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
152+ } ,
144153 } ;
145154
146- DiagnosticResult [ ] expectedFixed =
155+ if ( compilerWarning )
147156 {
148- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "IParent" ) . WithLocation ( 2 , 18 ) ,
149- this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
150- } ;
157+ test . DisabledDiagnostics . Add ( SA1600 . Id ) ;
158+ test . SolutionTransforms . Add ( SetCompilerDocumentationWarningToError ) ;
159+ }
151160
152- await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
153- await this . VerifyCSharpDiagnosticAsync ( fixedCode , expectedFixed , CancellationToken . None ) . ConfigureAwait ( false ) ;
154- await this . VerifyCSharpFixAsync ( testCode , fixedCode , numberOfFixAllIterations : 2 , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
161+ await test . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
155162 }
156163
157164 [ Theory ]
@@ -176,15 +183,19 @@ public class ChildClass : ParentClass
176183}}
177184" ;
178185
179- DiagnosticResult [ ] expected =
186+ await new CSharpTest
180187 {
181- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 2 , 14 ) ,
182- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 10 , 14 ) ,
183- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 12 , 35 ) ,
184- } ;
185-
186- await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
187- await this . VerifyCSharpFixAsync ( testCode , testCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
188+ TestCode = testCode ,
189+ ExpectedDiagnostics =
190+ {
191+ Diagnostic ( SA1600 ) . WithLocation ( 2 , 14 ) ,
192+ Diagnostic ( SA1600 ) . WithLocation ( 10 , 14 ) ,
193+ Diagnostic ( SA1600 ) . WithLocation ( 12 , 35 ) ,
194+ } ,
195+ FixedCode = testCode ,
196+ NumberOfIncrementalIterations = 1 ,
197+ NumberOfFixAllIterations = 1 ,
198+ } . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
188199 }
189200
190201 [ Theory ]
@@ -209,59 +220,30 @@ public class ChildClass : ParentClass
209220}}
210221" ;
211222
212- var fixedCode = testCode ;
213-
214- DiagnosticResult [ ] expected =
223+ await new CSharpTest
215224 {
216- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 2 , 14 ) ,
217- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 10 , 14 ) ,
218- this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 12 , 35 ) ,
219- } ;
220-
221- await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
222- await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
223- }
224-
225- /// <inheritdoc/>
226- protected override IEnumerable < string > GetDisabledDiagnostics ( )
227- {
228- if ( this . descriptor == CS1591 )
229- {
230- yield return SA1600 . Id ;
231- }
225+ TestCode = testCode ,
226+ ExpectedDiagnostics =
227+ {
228+ Diagnostic ( SA1600 ) . WithLocation ( 2 , 14 ) ,
229+ Diagnostic ( SA1600 ) . WithLocation ( 10 , 14 ) ,
230+ Diagnostic ( SA1600 ) . WithLocation ( 12 , 35 ) ,
231+ } ,
232+ FixedCode = testCode ,
233+ NumberOfIncrementalIterations = 1 ,
234+ NumberOfFixAllIterations = 1 ,
235+ } . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
232236 }
233237
234- /// <inheritdoc/>
235- protected override Project ApplyCompilationOptions ( Project project )
238+ private static Solution SetCompilerDocumentationWarningToError ( Solution solution , ProjectId projectId )
236239 {
237- project = base . ApplyCompilationOptions ( project ) ;
238-
239- if ( this . descriptor == CS1591 )
240- {
241- var supportedDiagnosticsSpecificOptions = new Dictionary < string , ReportDiagnostic > ( ) ;
242- supportedDiagnosticsSpecificOptions . Add ( CS1591 . Id , ReportDiagnostic . Error ) ;
240+ var project = solution . GetProject ( projectId ) ;
243241
244- // update the project compilation options
245- var modifiedSpecificDiagnosticOptions = project . CompilationOptions . SpecificDiagnosticOptions . SetItem ( CS1591 . Id , ReportDiagnostic . Error ) ;
246- var modifiedCompilationOptions = project . CompilationOptions . WithSpecificDiagnosticOptions ( modifiedSpecificDiagnosticOptions ) ;
242+ // update the project compilation options
243+ var modifiedSpecificDiagnosticOptions = project . CompilationOptions . SpecificDiagnosticOptions . SetItem ( CS1591 . Id , ReportDiagnostic . Error ) ;
244+ var modifiedCompilationOptions = project . CompilationOptions . WithSpecificDiagnosticOptions ( modifiedSpecificDiagnosticOptions ) ;
247245
248- Solution solution = project . Solution . WithProjectCompilationOptions ( project . Id , modifiedCompilationOptions ) ;
249- project = solution . GetProject ( project . Id ) ;
250- }
251-
252- return project ;
253- }
254-
255- /// <inheritdoc/>
256- protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
257- {
258- yield return new SA1600ElementsMustBeDocumented ( ) ;
259- }
260-
261- /// <inheritdoc/>
262- protected override CodeFixProvider GetCSharpCodeFixProvider ( )
263- {
264- return new InheritdocCodeFixProvider ( ) ;
246+ return solution . WithProjectCompilationOptions ( projectId , modifiedCompilationOptions ) ;
265247 }
266248 }
267249}
0 commit comments