@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
66 using System . Collections . Generic ;
77 using System . Threading ;
88 using System . Threading . Tasks ;
9+ using Microsoft . CodeAnalysis ;
910 using Microsoft . CodeAnalysis . CodeFixes ;
1011 using Microsoft . CodeAnalysis . Diagnostics ;
1112 using StyleCop . Analyzers . DocumentationRules ;
@@ -17,12 +18,22 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1718 /// </summary>
1819 public class InheritdocCodeFixProviderUnitTests : CodeFixVerifier
1920 {
21+ private static readonly DiagnosticDescriptor SA1600 = new SA1600ElementsMustBeDocumented ( ) . SupportedDiagnostics [ 0 ] ;
22+ private static readonly DiagnosticDescriptor CS1591 =
23+ new DiagnosticDescriptor ( nameof ( CS1591 ) , "Title" , "Missing XML comment for publicly visible type or member '{0}'" , "Category" , DiagnosticSeverity . Error , AnalyzerConstants . EnabledByDefault ) ;
24+
25+ private DiagnosticDescriptor descriptor = SA1600 ;
26+
2027 [ Theory ]
21- [ InlineData ( "string TestMember { get; set; }" ) ]
22- [ InlineData ( "string TestMember() { return null; }" ) ]
23- [ InlineData ( "string this[int a] { get { return \" a\" ; } set { } }" ) ]
24- [ InlineData ( "event EventHandler TestMember { add { } remove { } }" ) ]
25- public async Task TestClassVirtualInheritedMembersAsync ( string memberData )
28+ [ InlineData ( false , null , "string TestMember { get; set; }" ) ]
29+ [ InlineData ( false , null , "string TestMember() { return null; }" ) ]
30+ [ InlineData ( false , null , "string this[int a] { get { return \" a\" ; } set { } }" ) ]
31+ [ InlineData ( false , null , "event EventHandler TestMember { add { } remove { } }" ) ]
32+ [ InlineData ( true , "ChildClass.TestMember" , "string TestMember { get; set; }" ) ]
33+ [ InlineData ( true , "ChildClass.TestMember()" , "string TestMember() { return null; }" ) ]
34+ [ InlineData ( true , "ChildClass.this[int]" , "string this[int a] { get { return \" a\" ; } set { } }" ) ]
35+ [ InlineData ( true , "ChildClass.TestMember" , "event EventHandler TestMember { add { } remove { } }" ) ]
36+ public async Task TestClassVirtualInheritedMembersAsync ( bool compilerWarning , string memberName , string memberData )
2637 {
2738 var testCode = $@ "using System;
2839public class ParentClass
@@ -55,17 +66,22 @@ public override {memberData}
5566}}
5667" ;
5768
69+ if ( compilerWarning )
70+ {
71+ this . descriptor = CS1591 ;
72+ }
73+
5874 DiagnosticResult [ ] expected =
5975 {
60- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 14 ) ,
61- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
62- this . CSharpDiagnostic ( ) . WithLocation ( 12 , 40 ) ,
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 ) ,
6379 } ;
6480
6581 DiagnosticResult [ ] expectedFixed =
6682 {
67- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 14 ) ,
68- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
83+ this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ParentClass" ) . WithLocation ( 2 , 14 ) ,
84+ this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
6985 } ;
7086
7187 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
@@ -74,11 +90,15 @@ public override {memberData}
7490 }
7591
7692 [ Theory ]
77- [ InlineData ( "string TestMember { get; set; }" , "string TestMember { get; set; }" ) ]
78- [ InlineData ( "string TestMember();" , "string TestMember() { return null; }" ) ]
79- [ InlineData ( "string this[int a] { get; set; }" , "string this[int a] { get { return \" a\" ; } set { } }" ) ]
80- [ InlineData ( "event EventHandler TestMember;" , "event EventHandler TestMember { add { } remove { } }" ) ]
81- public async Task TestInterfaceInheritedMembersAsync ( string parentData , string childData )
93+ [ InlineData ( false , null , "string TestMember { get; set; }" , "string TestMember { get; set; }" ) ]
94+ [ InlineData ( false , null , "string TestMember();" , "string TestMember() { return null; }" ) ]
95+ [ InlineData ( false , null , "string this[int a] { get; set; }" , "string this[int a] { get { return \" a\" ; } set { } }" ) ]
96+ [ InlineData ( false , null , "event EventHandler TestMember;" , "event EventHandler TestMember { add { } remove { } }" ) ]
97+ [ InlineData ( true , "ChildClass.TestMember" , "string TestMember { get; set; }" , "string TestMember { get; set; }" ) ]
98+ [ InlineData ( true , "ChildClass.TestMember()" , "string TestMember();" , "string TestMember() { return null; }" ) ]
99+ [ InlineData ( true , "ChildClass.this[int]" , "string this[int a] { get; set; }" , "string this[int a] { get { return \" a\" ; } set { } }" ) ]
100+ [ InlineData ( true , "ChildClass.TestMember" , "event EventHandler TestMember;" , "event EventHandler TestMember { add { } remove { } }" ) ]
101+ public async Task TestInterfaceInheritedMembersAsync ( bool compilerWarning , string memberName , string parentData , string childData )
82102 {
83103 var testCode = $@ "using System;
84104public interface IParent
@@ -111,17 +131,22 @@ public class ChildClass : IParent
111131}}
112132" ;
113133
134+ if ( compilerWarning )
135+ {
136+ this . descriptor = CS1591 ;
137+ }
138+
114139 DiagnosticResult [ ] expected =
115140 {
116- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 18 ) ,
117- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
118- this . CSharpDiagnostic ( ) . WithLocation ( 12 , 31 ) ,
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 ) ,
119144 } ;
120145
121146 DiagnosticResult [ ] expectedFixed =
122147 {
123- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 18 ) ,
124- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
148+ this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "IParent" ) . WithLocation ( 2 , 18 ) ,
149+ this . CSharpDiagnostic ( this . descriptor ) . WithArguments ( "ChildClass" ) . WithLocation ( 10 , 14 ) ,
125150 } ;
126151
127152 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
@@ -153,9 +178,9 @@ public class ChildClass : ParentClass
153178
154179 DiagnosticResult [ ] expected =
155180 {
156- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 14 ) ,
157- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
158- this . CSharpDiagnostic ( ) . WithLocation ( 12 , 35 ) ,
181+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 2 , 14 ) ,
182+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 10 , 14 ) ,
183+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 12 , 35 ) ,
159184 } ;
160185
161186 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
@@ -188,15 +213,45 @@ public class ChildClass : ParentClass
188213
189214 DiagnosticResult [ ] expected =
190215 {
191- this . CSharpDiagnostic ( ) . WithLocation ( 2 , 14 ) ,
192- this . CSharpDiagnostic ( ) . WithLocation ( 10 , 14 ) ,
193- this . CSharpDiagnostic ( ) . WithLocation ( 12 , 35 ) ,
216+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 2 , 14 ) ,
217+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 10 , 14 ) ,
218+ this . CSharpDiagnostic ( this . descriptor ) . WithLocation ( 12 , 35 ) ,
194219 } ;
195220
196221 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
197222 await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
198223 }
199224
225+ /// <inheritdoc/>
226+ protected override IEnumerable < string > GetDisabledDiagnostics ( )
227+ {
228+ if ( this . descriptor == CS1591 )
229+ {
230+ yield return SA1600 . Id ;
231+ }
232+ }
233+
234+ /// <inheritdoc/>
235+ protected override Project ApplyCompilationOptions ( Project project )
236+ {
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 ) ;
243+
244+ // update the project compilation options
245+ var modifiedSpecificDiagnosticOptions = project . CompilationOptions . SpecificDiagnosticOptions . SetItem ( CS1591 . Id , ReportDiagnostic . Error ) ;
246+ var modifiedCompilationOptions = project . CompilationOptions . WithSpecificDiagnosticOptions ( modifiedSpecificDiagnosticOptions ) ;
247+
248+ Solution solution = project . Solution . WithProjectCompilationOptions ( project . Id , modifiedCompilationOptions ) ;
249+ project = solution . GetProject ( project . Id ) ;
250+ }
251+
252+ return project ;
253+ }
254+
200255 /// <inheritdoc/>
201256 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
202257 {
0 commit comments