Skip to content

Commit 42bf8da

Browse files
committed
Improve the ability of GenericAnalyzerTest to report misconfigured tests
1 parent 2b565d4 commit 42bf8da

5 files changed

Lines changed: 36 additions & 11 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1000UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ namespace Namespace
964964
var test = new CSharpTest
965965
{
966966
TestCode = testCode,
967-
FixedCode = fixedTest != testCode ? fixedTest : null,
967+
FixedCode = fixedTest,
968968
};
969969

970970
test.ExpectedDiagnostics.AddRange(expected);
@@ -997,7 +997,7 @@ class ClassName
997997
var test = new CSharpTest
998998
{
999999
TestCode = testCode,
1000-
FixedCode = fixedTest != testCode ? fixedTest : null,
1000+
FixedCode = fixedTest,
10011001
};
10021002

10031003
test.ExpectedDiagnostics.AddRange(expected);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void f(int x, int y) {{ }}
374374
var test = new CSharpTest
375375
{
376376
TestCode = originalCode,
377-
FixedCode = fixedCode != originalCode ? fixedCode : null,
377+
FixedCode = fixedCode,
378378
};
379379

380380
test.ExpectedDiagnostics.AddRange(expected);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1009UnitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public async Task TestNotReportedWhenFollowedByUnaryPlusOrMinusAsync(string oper
236236
var ignoredStatement = $"var i = (int) {operatorToken}2;";
237237
var correctStatement = $"var i = (int){operatorToken}2;";
238238

239-
await this.TestWhitespaceInStatementOrDeclAsync(ignoredStatement, string.Empty, EmptyDiagnosticResults).ConfigureAwait(false);
240-
await this.TestWhitespaceInStatementOrDeclAsync(correctStatement, string.Empty, EmptyDiagnosticResults).ConfigureAwait(false);
239+
await this.TestWhitespaceInStatementOrDeclAsync(ignoredStatement, null, EmptyDiagnosticResults).ConfigureAwait(false);
240+
await this.TestWhitespaceInStatementOrDeclAsync(correctStatement, null, EmptyDiagnosticResults).ConfigureAwait(false);
241241
}
242242

243243
[Fact]
@@ -1019,12 +1019,12 @@ public object Test
10191019
}}
10201020
";
10211021
string originalCode = string.Format(template, originalStatement);
1022-
string fixedCode = string.Format(template, fixedStatement);
1022+
string fixedCode = string.Format(template, fixedStatement ?? originalStatement);
10231023

10241024
var test = new CSharpTest
10251025
{
10261026
TestCode = originalCode,
1027-
FixedCode = expected.Length > 0 ? fixedCode : null,
1027+
FixedCode = fixedCode,
10281028
};
10291029

10301030
test.ExpectedDiagnostics.AddRange(expected);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1011UnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public async Task TestClosingBracketFollowedByNoSpaceAndIncrementOrDecrementOper
261261
@"var i = new int[1];
262262
i[0]{0};", operatorText);
263263

264-
await this.TestWhitespaceInStatementOrDeclAsync(validStatament, string.Empty, EmptyDiagnosticResults).ConfigureAwait(false);
264+
await this.TestWhitespaceInStatementOrDeclAsync(validStatament, null, EmptyDiagnosticResults).ConfigureAwait(false);
265265
}
266266

267267
[Theory]
@@ -375,12 +375,12 @@ void DoIt()
375375
}}
376376
";
377377
string originalCode = string.Format(template, originalStatement);
378-
string fixedCode = string.Format(template, fixedStatement);
378+
string fixedCode = string.Format(template, fixedStatement ?? originalStatement);
379379

380380
var test = new CSharpTest
381381
{
382382
TestCode = originalCode,
383-
FixedCode = expected.Length > 0 ? fixedCode : null,
383+
FixedCode = fixedCode,
384384
};
385385

386386
test.ExpectedDiagnostics.AddRange(expected);

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/GenericAnalyzerTest.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
102102

103103
var expected = this.ExpectedDiagnostics.ToArray();
104104
await this.VerifyDiagnosticsAsync(new[] { this.TestCode }, expected, filenames: null, cancellationToken).ConfigureAwait(false);
105-
if (this.FixedCode != null)
105+
if (this.HasFixableDiagnostics())
106106
{
107107
await this.VerifyDiagnosticsAsync(new[] { this.FixedCode }, this.RemainingDiagnostics.ToArray(), filenames: null, cancellationToken).ConfigureAwait(false);
108108
await this.VerifyFixAsync(numberOfIncrementalIterations: this.NumberOfIncrementalIterations, numberOfFixAllIterations: this.NumberOfFixAllIterations, cancellationToken: cancellationToken).ConfigureAwait(false);
@@ -123,6 +123,31 @@ public async Task RunAsync(CancellationToken cancellationToken)
123123
/// <returns>The <see cref="CodeFixProvider"/> to be used for C# code.</returns>
124124
protected abstract IEnumerable<CodeFixProvider> GetCodeFixProviders();
125125

126+
private bool HasFixableDiagnostics()
127+
{
128+
var fixers = this.GetCodeFixProviders().ToArray();
129+
if (HasFixableDiagnosticsCore())
130+
{
131+
if (this.FixedCode != null)
132+
{
133+
return true;
134+
}
135+
136+
Assert.Empty(this.RemainingDiagnostics);
137+
return false;
138+
}
139+
140+
Assert.True(string.IsNullOrEmpty(this.FixedCode) || this.FixedCode == this.TestCode);
141+
Assert.Empty(this.RemainingDiagnostics);
142+
return false;
143+
144+
// Local function
145+
bool HasFixableDiagnosticsCore()
146+
{
147+
return this.ExpectedDiagnostics.Any(diagnostic => fixers.Any(fixer => fixer.FixableDiagnosticIds.Contains(diagnostic.Id)));
148+
}
149+
}
150+
126151
private static bool IsSubjectToExclusion(DiagnosticResult result)
127152
{
128153
if (result.Id.StartsWith("CS", StringComparison.Ordinal))

0 commit comments

Comments
 (0)