Skip to content

Commit cb96274

Browse files
committed
Fix test for diagnostic without a location
1 parent a4888af commit cb96274

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public async Task TestInvalidSettingsAsync()
5252
}
5353
";
5454

55-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(null, 0, 0);
55+
// This diagnostic is reported without a location
56+
DiagnosticResult expected = this.CSharpDiagnostic();
5657

5758
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
5859
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,26 @@ private static string FormatDiagnostics(ImmutableArray<DiagnosticAnalyzer> analy
326326
return builder.ToString();
327327
}
328328

329+
private static bool IsSubjectToExclusion(DiagnosticResult result)
330+
{
331+
if (result.Id.StartsWith("CS", StringComparison.Ordinal))
332+
{
333+
return false;
334+
}
335+
336+
if (result.Id.StartsWith("AD", StringComparison.Ordinal))
337+
{
338+
return false;
339+
}
340+
341+
if (result.Locations.Length == 0)
342+
{
343+
return false;
344+
}
345+
346+
return true;
347+
}
348+
329349
/// <summary>
330350
/// General method that gets a collection of actual <see cref="Diagnostic"/>s found in the source after the
331351
/// analyzer is run, then verifies each of them.
@@ -346,12 +366,13 @@ private async Task VerifyDiagnosticsAsync(string[] sources, string language, Imm
346366
if (filenames == null)
347367
{
348368
// Also check if the analyzer honors exclusions
349-
if (expected.Any(x => x.Id.StartsWith("SA", StringComparison.Ordinal) || x.Id.StartsWith("SX", StringComparison.Ordinal)))
369+
if (expected.Any(IsSubjectToExclusion))
350370
{
351-
// We want to look at non-stylecop diagnostics only. We also insert a new line at the beginning
352-
// so we have to move all diagnostic location down by one line
371+
// Diagnostics reported by the compiler and analyzer diagnostics which don't have a location will
372+
// still be reported. We also insert a new line at the beginning so we have to move all diagnostic
373+
// locations which have a specific position down by one line.
353374
var expectedResults = expected
354-
.Where(x => !x.Id.StartsWith("SA", StringComparison.Ordinal) && !x.Id.StartsWith("SX", StringComparison.Ordinal))
375+
.Where(x => !IsSubjectToExclusion(x))
355376
.Select(x => x.WithLineOffset(1))
356377
.ToArray();
357378

0 commit comments

Comments
 (0)