Skip to content

Commit

Permalink
Fix test for diagnostic without a location
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jan 8, 2016
1 parent a4888af commit cb96274
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public async Task TestInvalidSettingsAsync()
}
";

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

await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ private static string FormatDiagnostics(ImmutableArray<DiagnosticAnalyzer> analy
return builder.ToString();
}

private static bool IsSubjectToExclusion(DiagnosticResult result)
{
if (result.Id.StartsWith("CS", StringComparison.Ordinal))
{
return false;
}

if (result.Id.StartsWith("AD", StringComparison.Ordinal))
{
return false;
}

if (result.Locations.Length == 0)
{
return false;
}

return true;
}

/// <summary>
/// General method that gets a collection of actual <see cref="Diagnostic"/>s found in the source after the
/// analyzer is run, then verifies each of them.
Expand All @@ -346,12 +366,13 @@ private async Task VerifyDiagnosticsAsync(string[] sources, string language, Imm
if (filenames == null)
{
// Also check if the analyzer honors exclusions
if (expected.Any(x => x.Id.StartsWith("SA", StringComparison.Ordinal) || x.Id.StartsWith("SX", StringComparison.Ordinal)))
if (expected.Any(IsSubjectToExclusion))
{
// We want to look at non-stylecop diagnostics only. We also insert a new line at the beginning
// so we have to move all diagnostic location down by one line
// Diagnostics reported by the compiler and analyzer diagnostics which don't have a location will
// still be reported. We also insert a new line at the beginning so we have to move all diagnostic
// locations which have a specific position down by one line.
var expectedResults = expected
.Where(x => !x.Id.StartsWith("SA", StringComparison.Ordinal) && !x.Id.StartsWith("SX", StringComparison.Ordinal))
.Where(x => !IsSubjectToExclusion(x))
.Select(x => x.WithLineOffset(1))
.ToArray();

Expand Down

0 comments on commit cb96274

Please sign in to comment.