Skip to content

Commit e0fa070

Browse files
committed
Convert the remaining maintainability tests to the new test helpers
1 parent 2166a2a commit e0fa070

20 files changed

Lines changed: 568 additions & 516 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1402CSharp7ForClassUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1402CSharp7ForClassUnitTests : SA1402ForClassUnitTests
8+
internal class SA1402CSharp7ForClassUnitTests : SA1402ForClassUnitTests
99
{
1010
}
1111
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1402CSharp7ForInterfaceUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1402CSharp7ForInterfaceUnitTests : SA1402ForInterfaceUnitTests
8+
internal class SA1402CSharp7ForInterfaceUnitTests : SA1402ForInterfaceUnitTests
99
{
1010
}
1111
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1402CSharp7ForStructUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1402CSharp7ForStructUnitTests : SA1402ForStructUnitTests
8+
internal class SA1402CSharp7ForStructUnitTests : SA1402ForStructUnitTests
99
{
1010
}
1111
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1403CSharp7UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1403CSharp7UnitTests : SA1403UnitTests
8+
internal class SA1403CSharp7UnitTests : SA1403UnitTests
99
{
1010
}
1111
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1405CSharp7UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1405CSharp7UnitTests : SA1405UnitTests
8+
internal class SA1405CSharp7UnitTests : SA1405UnitTests
99
{
1010
}
1111
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1406CSharp7UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
55
{
66
using StyleCop.Analyzers.Test.MaintainabilityRules;
77

8-
public class SA1406CSharp7UnitTests : SA1406UnitTests
8+
internal class SA1406CSharp7UnitTests : SA1406UnitTests
99
{
1010
}
1111
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.Helpers
5+
{
6+
using System.Collections.Immutable;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CodeFixes;
9+
using StyleCop.Analyzers.Helpers;
10+
11+
internal sealed class EmptyCodeFixProvider : CodeFixProvider
12+
{
13+
public override ImmutableArray<string> FixableDiagnosticIds
14+
=> ImmutableArray<string>.Empty;
15+
16+
public override Task RegisterCodeFixesAsync(CodeFixContext context)
17+
=> SpecializedTasks.CompletedTask;
18+
}
19+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/DebugMessagesUnitTestsBase.cs renamed to StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/DebugMessagesUnitTestsBase`1.cs

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33

44
namespace StyleCop.Analyzers.Test.MaintainabilityRules
55
{
6+
using System;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.Diagnostics;
1214
using Microsoft.CodeAnalysis.Text;
15+
using StyleCop.Analyzers.Test.Verifiers;
1316
using TestHelper;
1417
using Xunit;
1518

16-
public abstract class DebugMessagesUnitTestsBase : DiagnosticVerifier
19+
public abstract class DebugMessagesUnitTestsBase<TAnalyzer>
20+
where TAnalyzer : DiagnosticAnalyzer, new()
1721
{
18-
protected bool IncludeSystemDll { get; set; } = true;
19-
2022
protected abstract string MethodName
2123
{
2224
get;
@@ -27,6 +29,9 @@ protected abstract IEnumerable<string> InitialArguments
2729
get;
2830
}
2931

32+
private static DiagnosticResult[] EmptyDiagnosticResults
33+
=> DiagnosticVerifier<TAnalyzer>.EmptyDiagnosticResults;
34+
3035
[Fact]
3136
public async Task TestConstantMessage_Field_PassAsync()
3237
{
@@ -202,7 +207,7 @@ public void Bar()
202207
}}
203208
}}";
204209

205-
await this.VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
210+
await VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
206211
}
207212

208213
[Fact]
@@ -230,7 +235,7 @@ public static void Fail(string s)
230235
}}
231236
";
232237

233-
await this.VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
238+
await VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
234239
}
235240

236241
[Fact]
@@ -245,7 +250,7 @@ public void Bar()
245250
}
246251
}";
247252

248-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
253+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
249254
}
250255

251256
[Fact]
@@ -268,14 +273,43 @@ public void Bar()
268273

269274
DiagnosticResult[] expected =
270275
{
271-
this.CSharpDiagnostic().WithLocation(8, 9),
272-
this.CSharpDiagnostic().WithLocation(9, 9),
273-
this.CSharpDiagnostic().WithLocation(10, 9),
274-
this.CSharpDiagnostic().WithLocation(11, 9),
275-
this.CSharpDiagnostic().WithLocation(12, 9),
276+
Diagnostic().WithLocation(8, 9),
277+
Diagnostic().WithLocation(9, 9),
278+
Diagnostic().WithLocation(10, 9),
279+
Diagnostic().WithLocation(11, 9),
280+
Diagnostic().WithLocation(12, 9),
276281
};
277282

278-
await this.VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), expected, CancellationToken.None).ConfigureAwait(false);
283+
await VerifyCSharpDiagnosticAsync(this.BuildTestCode(testCode), expected, CancellationToken.None).ConfigureAwait(false);
284+
}
285+
286+
protected static DiagnosticResult Diagnostic()
287+
=> StyleCopDiagnosticVerifier<TAnalyzer>.Diagnostic();
288+
289+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
290+
=> VerifyCSharpDiagnosticAsync(source, new[] { expected }, includeSystemDll: true, cancellationToken);
291+
292+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
293+
=> VerifyCSharpDiagnosticAsync(source, expected, includeSystemDll: true, cancellationToken);
294+
295+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, bool includeSystemDll, CancellationToken cancellationToken)
296+
{
297+
var test = new StyleCopDiagnosticVerifier<TAnalyzer>.CSharpTest
298+
{
299+
TestCode = source,
300+
};
301+
302+
if (!includeSystemDll)
303+
{
304+
test.SolutionTransforms.Add((solution, projectId) =>
305+
{
306+
var references = solution.GetProject(projectId).MetadataReferences;
307+
return solution.WithProjectMetadataReferences(projectId, references.Where(x => !x.Display.Contains("System.dll")));
308+
});
309+
}
310+
311+
test.ExpectedDiagnostics.AddRange(expected);
312+
return test.RunAsync(cancellationToken);
279313
}
280314

281315
protected virtual string BuildTestCode(string format)
@@ -299,22 +333,6 @@ protected virtual string BuildTestCode(string format)
299333
return string.Format(format, this.MethodName, argumentList);
300334
}
301335

302-
protected override Solution CreateSolution(ProjectId projectId, string language)
303-
{
304-
Solution solution = base.CreateSolution(projectId, language);
305-
306-
if (this.IncludeSystemDll)
307-
{
308-
return solution;
309-
}
310-
else
311-
{
312-
IEnumerable<MetadataReference> references = solution.Projects.First().MetadataReferences;
313-
314-
return solution.WithProjectMetadataReferences(solution.ProjectIds[0], references.Where(x => !x.Display.Contains("System.dll")));
315-
}
316-
}
317-
318336
private async Task TestConstantMessage_Field_PassExecuterAsync(string argument, params DiagnosticResult[] expected)
319337
{
320338
var testCodeFormat = @"using System.Diagnostics;
@@ -327,7 +345,7 @@ public void Bar()
327345
}}}}
328346
}}}}";
329347

330-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
348+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
331349
}
332350

333351
private async Task TestConstantMessage_Local_PassExecuterAsync(string argument, params DiagnosticResult[] expected)
@@ -342,7 +360,7 @@ public void Bar()
342360
}}}}
343361
}}}}";
344362

345-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
363+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
346364
}
347365

348366
private async Task TestConstantMessage_Inline_PassExecuterAsync(string argument, params DiagnosticResult[] expected)
@@ -356,7 +374,7 @@ public void Bar()
356374
}}}}
357375
}}}}";
358376

359-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
377+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
360378
}
361379

362380
private async Task TestConstantMessage_Field_FailAsync(string argument)
@@ -371,9 +389,8 @@ public void Bar()
371389
}}}}
372390
}}}}";
373391

374-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 9);
375-
376-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
392+
DiagnosticResult expected = Diagnostic().WithLocation(7, 9);
393+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
377394
}
378395

379396
private async Task TestConstantMessage_Local_FailAsync(string argument)
@@ -388,9 +405,8 @@ public void Bar()
388405
}}}}
389406
}}}}";
390407

391-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 9);
392-
393-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
408+
DiagnosticResult expected = Diagnostic().WithLocation(7, 9);
409+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
394410
}
395411

396412
private async Task TestConstantMessage_Inline_FailAsync(string argument)
@@ -404,9 +420,8 @@ public void Bar()
404420
}}}}
405421
}}}}";
406422

407-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 9);
408-
409-
await this.VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
423+
DiagnosticResult expected = Diagnostic().WithLocation(6, 9);
424+
await VerifyCSharpDiagnosticAsync(string.Format(this.BuildTestCode(testCodeFormat), argument), expected, CancellationToken.None).ConfigureAwait(false);
410425
}
411426
}
412427
}

0 commit comments

Comments
 (0)