Skip to content

Commit

Permalink
Add DiagnosticOptions.IgnoreSeverity
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 17, 2020
1 parent e982889 commit ac7ee94
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,11 @@ private void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResults, Immu
message = FormatVerifierMessage(analyzers, actual, expected, $"Expected diagnostic id to be \"{expected.Id}\" was \"{actual.Id}\"");
verifier.Equal(expected.Id, actual.Id, message);

message = FormatVerifierMessage(analyzers, actual, expected, $"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"");
verifier.Equal(expected.Severity, actual.Severity, message);
if (!expected.Options.HasFlag(DiagnosticOptions.IgnoreSeverity))
{
message = FormatVerifierMessage(analyzers, actual, expected, $"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"");
verifier.Equal(expected.Severity, actual.Severity, message);
}

if (expected.Message != null)
{
Expand Down Expand Up @@ -509,7 +512,7 @@ static MatchQuality GetMatchValue(Diagnostic diagnostic, string diagnosticId, Fi
var isIdMatch = diagnosticId == diagnosticResult.Id;
if (isLocationMatch
&& isIdMatch
&& diagnostic.Severity == diagnosticResult.Severity
&& IsSeverityMatch(diagnostic, diagnosticResult)
&& IsMessageMatch(diagnostic, actualArguments, diagnosticResult, expectedArguments))
{
return MatchQuality.Full;
Expand Down Expand Up @@ -584,6 +587,16 @@ static bool IsLocationMatch2(Location actual, FileLinePositionSpan actualSpan, D
return true;
}

static bool IsSeverityMatch(Diagnostic actual, DiagnosticResult expected)
{
if (expected.Options.HasFlag(DiagnosticOptions.IgnoreSeverity))
{
return true;
}

return actual.Severity == expected.Severity;
}

static bool IsMessageMatch(Diagnostic actual, ImmutableArray<string> actualArguments, DiagnosticResult expected, ImmutableArray<string> expectedArguments)
{
if (expected.Message is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public enum DiagnosticOptions
/// validation of additional locations reported for the corresponding diagnostics.
/// </summary>
IgnoreAdditionalLocations = 1,

/// <summary>
/// Ignore the diagnostic severity when verifying this diagnostic result.
/// </summary>
IgnoreSeverity = 2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions.IgnoreLength = 1 -> Mic
Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions.None = 0 -> Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions
Microsoft.CodeAnalysis.Testing.DiagnosticOptions
Microsoft.CodeAnalysis.Testing.DiagnosticOptions.IgnoreAdditionalLocations = 1 -> Microsoft.CodeAnalysis.Testing.DiagnosticOptions
Microsoft.CodeAnalysis.Testing.DiagnosticOptions.IgnoreSeverity = 2 -> Microsoft.CodeAnalysis.Testing.DiagnosticOptions
Microsoft.CodeAnalysis.Testing.DiagnosticOptions.None = 0 -> Microsoft.CodeAnalysis.Testing.DiagnosticOptions
Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.DiagnosticResult(Microsoft.CodeAnalysis.DiagnosticDescriptor descriptor) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public SolutionState WithProcessedMarkup(MarkupOptions markupOptions, Diagnostic
}
}

return diagnosticResult.WithMessage(null).WithOptions(DiagnosticOptions.IgnoreAdditionalLocations);
return diagnosticResult.WithMessage(null).WithOptions(DiagnosticOptions.IgnoreAdditionalLocations | DiagnosticOptions.IgnoreSeverity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private async Task<Document> CreateChangedDocument(
}

/// <summary>
/// Verifies that a test case with automatically include compiler diagnostics which are part of the the provided codefix
/// Verifies that a test case will automatically include compiler diagnostics which are part of the the provided
/// codefix.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
Expand Down Expand Up @@ -95,5 +96,41 @@ public void SomeMethod(){}
var diagnostic = DiagnosticResult.CompilerWarning("CS0169").WithSpan(8, 21, 8, 30).WithArguments("ConsoleApp1.TestClass.someField");
await Verify<SomeCodeFix>.VerifyCodeFixAsync(before, diagnostic, after);
}

/// <summary>
/// Verifies that a test case will automatically include compiler diagnostics which are part of the the provided
/// codefix, and the markup will ignore the severity of the diagnostic.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(419, "https://github.com/dotnet/roslyn-sdk/issues/419")]
public async Task VerifySimpleMarkupSyntaxWorks()
{
var before = @"
using System;
namespace ConsoleApp1
{
public class TestClass
{
private int {|CS0169:someField|};
public void SomeMethod(){}
}
}";

var after = @"
using System;
namespace ConsoleApp1
{
public class TestClass
{
public void SomeMethod(){}
}
}";

await Verify<SomeCodeFix>.VerifyCodeFixAsync(before, after);
}
}
}

0 comments on commit ac7ee94

Please sign in to comment.