33
44namespace StyleCop . Analyzers . Test . MaintainabilityRules
55{
6- using System ;
76 using System . Collections . Generic ;
87 using System . Linq ;
98 using System . Text ;
@@ -14,11 +13,9 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
1413 using Microsoft . CodeAnalysis . Testing ;
1514 using Microsoft . CodeAnalysis . Text ;
1615 using StyleCop . Analyzers . Test . Verifiers ;
17- using TestHelper ;
1816 using Xunit ;
1917
20- public abstract class DebugMessagesUnitTestsBase < TAnalyzer >
21- where TAnalyzer : DiagnosticAnalyzer , new ( )
18+ public abstract class DebugMessagesUnitTestsBase
2219 {
2320 protected abstract string MethodName
2421 {
@@ -30,6 +27,8 @@ protected abstract IEnumerable<string> InitialArguments
3027 get ;
3128 }
3229
30+ protected abstract DiagnosticAnalyzer Analyzer { get ; }
31+
3332 [ Fact ]
3433 public async Task TestConstantMessage_Field_PassAsync ( )
3534 {
@@ -45,7 +44,7 @@ public async Task TestConstantMessage_Field_PassExpressionAsync()
4544 [ Fact ]
4645 public async Task TestConstantMessage_Field_PassWrongTypeAsync ( )
4746 {
48- LinePosition linePosition = new LinePosition ( 4 , 28 ) ;
47+ LinePosition linePosition = new LinePosition ( 3 , 27 ) ;
4948 DiagnosticResult [ ] expected =
5049 {
5150 DiagnosticResult . CompilerError ( "CS0029" ) . WithSpan ( new FileLinePositionSpan ( "Test0.cs" , linePosition , linePosition ) ) . WithMessage ( "Cannot implicitly convert type 'int' to 'string'" ) ,
@@ -69,7 +68,7 @@ public async Task TestConstantMessage_Local_PassExpressionAsync()
6968 [ Fact ]
7069 public async Task TestConstantMessage_Local_PassWrongTypeAsync ( )
7170 {
72- LinePosition linePosition = new LinePosition ( 6 , 32 ) ;
71+ LinePosition linePosition = new LinePosition ( 5 , 31 ) ;
7372 DiagnosticResult [ ] expected =
7473 {
7574 DiagnosticResult . CompilerError ( "CS0029" ) . WithSpan ( new FileLinePositionSpan ( "Test0.cs" , linePosition , linePosition ) ) . WithMessage ( "Cannot implicitly convert type 'int' to 'string'" ) ,
@@ -93,10 +92,10 @@ public async Task TestConstantMessage_Inline_PassExpressionAsync()
9392 [ Fact ]
9493 public async Task TestConstantMessage_Inline_PassWrongTypeAsync ( )
9594 {
96- LinePosition linePosition = new LinePosition ( 6 , 16 + this . MethodName . Length + this . InitialArguments . Sum ( i => i . Length + ", " . Length ) ) ;
95+ LinePosition linePosition = new LinePosition ( 5 , 15 + this . MethodName . Length + this . InitialArguments . Sum ( i => i . Length + ", " . Length ) ) ;
9796 DiagnosticResult [ ] expected =
9897 {
99- DiagnosticResult . CompilerError ( "CS1503" ) . WithSpan ( new FileLinePositionSpan ( "Test0.cs" , linePosition , linePosition ) ) . WithMessage ( $ "Argument { 1 + this . InitialArguments . Count ( ) } : cannot implicitly convert type 'int' to 'string'") ,
98+ DiagnosticResult . CompilerError ( "CS1503" ) . WithSpan ( new FileLinePositionSpan ( "Test0.cs" , linePosition , linePosition ) ) . WithMessage ( $ "Argument { 1 + this . InitialArguments . Count ( ) } : cannot convert from 'int' to 'string'") ,
10099 } ;
101100
102101 await this . TestConstantMessage_Inline_PassExecuterAsync ( "3" , expected ) . ConfigureAwait ( false ) ;
@@ -187,7 +186,7 @@ public void Bar()
187186 }}
188187}}" ;
189188
190- await VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
189+ await this . VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
191190 }
192191
193192 [ Fact ]
@@ -215,7 +214,7 @@ public static void Fail(string s)
215214}}
216215" ;
217216
218- await VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
217+ await this . VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
219218 }
220219
221220 [ Fact ]
@@ -230,7 +229,7 @@ public void Bar()
230229 }
231230}" ;
232231
233- await VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
232+ await this . VerifyCSharpDiagnosticAsync ( testCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
234233 }
235234
236235 [ Fact ]
@@ -253,28 +252,28 @@ public void Bar()
253252
254253 DiagnosticResult [ ] expected =
255254 {
256- Diagnostic ( ) . WithLocation ( 8 , 9 ) ,
257- Diagnostic ( ) . WithLocation ( 9 , 9 ) ,
258- Diagnostic ( ) . WithLocation ( 10 , 9 ) ,
259- Diagnostic ( ) . WithLocation ( 11 , 9 ) ,
260- Diagnostic ( ) . WithLocation ( 12 , 9 ) ,
255+ this . Diagnostic ( ) . WithLocation ( 8 , 9 ) ,
256+ this . Diagnostic ( ) . WithLocation ( 9 , 9 ) ,
257+ this . Diagnostic ( ) . WithLocation ( 10 , 9 ) ,
258+ this . Diagnostic ( ) . WithLocation ( 11 , 9 ) ,
259+ this . Diagnostic ( ) . WithLocation ( 12 , 9 ) ,
261260 } ;
262261
263- await VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
262+ await this . VerifyCSharpDiagnosticAsync ( this . BuildTestCode ( testCode ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
264263 }
265264
266- protected static DiagnosticResult Diagnostic ( )
267- => StyleCopDiagnosticVerifier < TAnalyzer > . Diagnostic ( ) ;
265+ protected DiagnosticResult Diagnostic ( )
266+ => new DiagnosticResult ( this . Analyzer . SupportedDiagnostics . Single ( ) ) ;
268267
269- protected static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult expected , CancellationToken cancellationToken )
270- => VerifyCSharpDiagnosticAsync ( source , new [ ] { expected } , includeSystemDll : true , cancellationToken ) ;
268+ protected Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult expected , CancellationToken cancellationToken )
269+ => this . VerifyCSharpDiagnosticAsync ( source , new [ ] { expected } , includeSystemDll : true , cancellationToken ) ;
271270
272- protected static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
273- => VerifyCSharpDiagnosticAsync ( source , expected , includeSystemDll : true , cancellationToken ) ;
271+ protected Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , CancellationToken cancellationToken )
272+ => this . VerifyCSharpDiagnosticAsync ( source , expected , includeSystemDll : true , cancellationToken ) ;
274273
275- protected static Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , bool includeSystemDll , CancellationToken cancellationToken )
274+ protected Task VerifyCSharpDiagnosticAsync ( string source , DiagnosticResult [ ] expected , bool includeSystemDll , CancellationToken cancellationToken )
276275 {
277- var test = new StyleCopDiagnosticVerifier < TAnalyzer > . CSharpTest
276+ var test = new CSharpTest ( this )
278277 {
279278 TestCode = source ,
280279 } ;
@@ -325,7 +324,7 @@ public void Bar()
325324 }}}}
326325}}}}" ;
327326
328- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
327+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
329328 }
330329
331330 private async Task TestConstantMessage_Local_PassExecuterAsync ( string argument , params DiagnosticResult [ ] expected )
@@ -340,7 +339,7 @@ public void Bar()
340339 }}}}
341340}}}}" ;
342341
343- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
342+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
344343 }
345344
346345 private async Task TestConstantMessage_Inline_PassExecuterAsync ( string argument , params DiagnosticResult [ ] expected )
@@ -354,7 +353,7 @@ public void Bar()
354353 }}}}
355354}}}}" ;
356355
357- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
356+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
358357 }
359358
360359 private async Task TestConstantMessage_Field_FailAsync ( string argument )
@@ -369,8 +368,8 @@ public void Bar()
369368 }}}}
370369}}}}" ;
371370
372- DiagnosticResult expected = Diagnostic ( ) . WithLocation ( 7 , 9 ) ;
373- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
371+ DiagnosticResult expected = this . Diagnostic ( ) . WithLocation ( 7 , 9 ) ;
372+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
374373 }
375374
376375 private async Task TestConstantMessage_Local_FailAsync ( string argument )
@@ -385,8 +384,8 @@ public void Bar()
385384 }}}}
386385}}}}" ;
387386
388- DiagnosticResult expected = Diagnostic ( ) . WithLocation ( 7 , 9 ) ;
389- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
387+ DiagnosticResult expected = this . Diagnostic ( ) . WithLocation ( 7 , 9 ) ;
388+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
390389 }
391390
392391 private async Task TestConstantMessage_Inline_FailAsync ( string argument )
@@ -400,8 +399,23 @@ public void Bar()
400399 }}}}
401400}}}}" ;
402401
403- DiagnosticResult expected = Diagnostic ( ) . WithLocation ( 6 , 9 ) ;
404- await VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
402+ DiagnosticResult expected = this . Diagnostic ( ) . WithLocation ( 6 , 9 ) ;
403+ await this . VerifyCSharpDiagnosticAsync ( string . Format ( this . BuildTestCode ( testCodeFormat ) , argument ) , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
404+ }
405+
406+ private class CSharpTest : StyleCopDiagnosticVerifier < EmptyAnalyzer > . CSharpTest
407+ {
408+ private readonly DebugMessagesUnitTestsBase testFixture ;
409+
410+ public CSharpTest ( DebugMessagesUnitTestsBase testFixture )
411+ {
412+ this . testFixture = testFixture ;
413+ }
414+
415+ protected override IEnumerable < DiagnosticAnalyzer > GetDiagnosticAnalyzers ( )
416+ {
417+ yield return this . testFixture . Analyzer ;
418+ }
405419 }
406420 }
407421}
0 commit comments