Skip to content

Commit f74a693

Browse files
committed
Convert AnalyzerExtensionsTests and ExclusionTests to the new test helpers
1 parent 02205f6 commit f74a693

7 files changed

Lines changed: 151 additions & 148 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/AnalyzerExtensionsTests.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace StyleCop.Analyzers.Test
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
1313
using Microsoft.CodeAnalysis.Diagnostics;
1414
using StyleCop.Analyzers.SpecialRules;
15-
using TestHelper;
15+
using StyleCop.Analyzers.Test.Verifiers;
1616
using Xunit;
1717

18-
public class AnalyzerExtensionsTests : DiagnosticVerifier
18+
public class AnalyzerExtensionsTests
1919
{
2020
private bool invokedBlockCallback;
2121
private bool invokedMethodDeclarationCallback;
@@ -32,16 +32,15 @@ void MethodName()
3232
}
3333
";
3434

35-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
35+
await new CSharpTest(this)
36+
{
37+
TestCode = testCode,
38+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
39+
3640
Assert.True(this.invokedBlockCallback);
3741
Assert.True(this.invokedMethodDeclarationCallback);
3842
}
3943

40-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
41-
{
42-
yield return new CompilationStartDiagnosticAnalyzer { AnalyzerExtensionsTests = this };
43-
}
44-
4544
/// <summary>
4645
/// Note that I wanted to just extend <see cref="SA0002InvalidSettingsFile"/>, but errors in the meta analyzers
4746
/// were resulting in AD0001 during the build.
@@ -92,5 +91,20 @@ public override void Initialize(AnalysisContext context)
9291
});
9392
}
9493
}
94+
95+
private class CSharpTest : StyleCopDiagnosticVerifier<CompilationStartDiagnosticAnalyzer>.CSharpTest
96+
{
97+
private readonly AnalyzerExtensionsTests analyzerExtensionsTests;
98+
99+
public CSharpTest(AnalyzerExtensionsTests analyzerExtensionsTests)
100+
{
101+
this.analyzerExtensionsTests = analyzerExtensionsTests;
102+
}
103+
104+
protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
105+
{
106+
yield return new CompilationStartDiagnosticAnalyzer { AnalyzerExtensionsTests = this.analyzerExtensionsTests };
107+
}
108+
}
95109
}
96110
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ExclusionTests.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ namespace StyleCop.Analyzers.Test
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis.Diagnostics;
10-
using TestHelper;
119
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<TestHelper.ExclusionTestAnalyzer>;
1211

1312
/// <summary>
1413
/// Unit tests for testing exclusion of auto generated files.
1514
/// </summary>
16-
public class ExclusionTests : DiagnosticVerifier
15+
public class ExclusionTests
1716
{
1817
/// <summary>
1918
/// Gets the statements that will be used in the theory test cases.
@@ -62,7 +61,13 @@ public static IEnumerable<object[]> ShouldNotBeExcluded
6261
[MemberData(nameof(ShouldBeExcluded))]
6362
public async Task TestIsExcludedAsync(string filename, string testCode)
6463
{
65-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, filename: filename).ConfigureAwait(false);
64+
await new CSharpTest
65+
{
66+
TestSources =
67+
{
68+
(filename, testCode),
69+
},
70+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
6671
}
6772

6873
/// <summary>
@@ -75,15 +80,19 @@ public async Task TestIsExcludedAsync(string filename, string testCode)
7580
[MemberData(nameof(ShouldNotBeExcluded))]
7681
public async Task TestIsNotExcludedAsync(string filename, string testCode)
7782
{
78-
var result = this.CSharpDiagnostic().WithLocation(filename, 1, 1);
79-
80-
await this.VerifyCSharpDiagnosticAsync(testCode, result, CancellationToken.None, filename: filename).ConfigureAwait(false);
81-
}
83+
var result = Diagnostic().WithLocation(filename, 1, 1);
8284

83-
/// <inheritdoc/>
84-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
85-
{
86-
yield return new ExclusionTestAnalyzer();
85+
await new CSharpTest
86+
{
87+
TestSources =
88+
{
89+
(filename, testCode),
90+
},
91+
ExpectedDiagnostics =
92+
{
93+
result,
94+
},
95+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
8796
}
8897
}
8998
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1503UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public void Bar(int i)
522522
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
523523

524524
// Test again with the fixedCode as the test code
525-
test.TestSources[0] = fixedCode;
525+
test.TestSources[0] = (test.TestSources[0].filename, fixedCode);
526526
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
527527
}
528528
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.CodeAnalysis" Version="1.2.1" />
1919
<PackageReference Include="Microsoft.VisualStudio.Composition" Version="15.6.36" />
20+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
2021
<PackageReference Include="xunit" Version="2.3.1" />
2122
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" PrivateAssets="all" />
2223
</ItemGroup>

0 commit comments

Comments
 (0)