Skip to content

Commit

Permalink
Merge pull request #462 from sharwell/diagnostic-options
Browse files Browse the repository at this point in the history
Implement diagnostic options
  • Loading branch information
sharwell authored Feb 18, 2020
2 parents 358cca4 + ac7ee94 commit ba227e6
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResults, Immu
else
{
VerifyDiagnosticLocation(analyzers, actual, expected, actual.Location, expected.Spans[0], verifier);
if (!expected.Spans[0].Options.HasFlag(DiagnosticLocationOptions.IgnoreAdditionalLocations))
if (!expected.Options.HasFlag(DiagnosticOptions.IgnoreAdditionalLocations))
{
var additionalLocations = actual.AdditionalLocations.ToArray();

Expand All @@ -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 @@ -539,7 +542,7 @@ static bool IsLocationMatch(Diagnostic diagnostic, FileLinePositionSpan lineSpan
return false;
}

if (diagnosticResult.Spans[0].Options.HasFlag(DiagnosticLocationOptions.IgnoreAdditionalLocations))
if (diagnosticResult.Options.HasFlag(DiagnosticOptions.IgnoreAdditionalLocations))
{
return true;
}
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 Expand Up @@ -776,7 +789,7 @@ private static string FormatDiagnostics(ImmutableArray<DiagnosticAnalyzer> analy
foreach (var span in diagnostics[i].Spans)
{
AppendLocation(span);
if (span.Options.HasFlag(DiagnosticLocationOptions.IgnoreAdditionalLocations))
if (diagnostics[i].Options.HasFlag(DiagnosticOptions.IgnoreAdditionalLocations))
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.CodeAnalysis.Testing
{
/// <summary>
/// Defines options for interpreting <see cref="DiagnosticLocation"/>.
/// </summary>
[Flags]
public enum DiagnosticLocationOptions
{
/// <summary>
Expand All @@ -17,11 +20,5 @@ public enum DiagnosticLocationOptions
/// should be ignored when comparing results.
/// </summary>
IgnoreLength = 1,

/// <summary>
/// The primary diagnostic location is defined, but additional locations have not been provided. Disables
/// validation of additional locations reported for the corresponding diagnostics.
/// </summary>
IgnoreAdditionalLocations = 2,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.CodeAnalysis.Testing
{
/// <summary>
/// Defines options for interpreting <see cref="DiagnosticResult"/>.
/// </summary>
[Flags]
public enum DiagnosticOptions
{
/// <summary>
/// The result should be interpreted using the default settings.
/// </summary>
None = 0,

/// <summary>
/// The primary diagnostic location is defined, but additional locations have not been provided. Disables
/// 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 @@ -2,7 +2,6 @@

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text;
using Microsoft.CodeAnalysis.Text;

Expand Down Expand Up @@ -41,6 +40,7 @@ private DiagnosticResult(
bool suppressMessage,
string? message,
DiagnosticSeverity severity,
DiagnosticOptions options,
string id,
LocalizableString? messageFormat,
object?[]? messageArguments)
Expand All @@ -49,6 +49,7 @@ private DiagnosticResult(
_suppressMessage = suppressMessage;
_message = message;
Severity = severity;
Options = options;
Id = id;
MessageFormat = messageFormat;
MessageArguments = messageArguments;
Expand All @@ -58,6 +59,12 @@ private DiagnosticResult(

public DiagnosticSeverity Severity { get; }

/// <summary>
/// Gets the options to consider during validation of the expected diagnostic. The default value is
/// <see cref="DiagnosticOptions.None"/>.
/// </summary>
public DiagnosticOptions Options { get; }

public string Id { get; }

public string? Message
Expand Down Expand Up @@ -109,6 +116,26 @@ public DiagnosticResult WithSeverity(DiagnosticSeverity severity)
suppressMessage: _suppressMessage,
message: _message,
severity: severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
}

/// <summary>
/// Transforms the current <see cref="DiagnosticResult"/> to have the specified <see cref="Options"/>.
/// </summary>
/// <param name="options">The options to consider during validation of the expected diagnostic.</param>
/// <returns>A new <see cref="DiagnosticResult"/> copied from the current instance with the specified
/// <paramref name="options"/> applied.</returns>
public DiagnosticResult WithOptions(DiagnosticOptions options)
{
return new DiagnosticResult(
spans: _spans,
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand All @@ -121,6 +148,7 @@ public DiagnosticResult WithArguments(params object[] arguments)
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: arguments);
Expand All @@ -133,6 +161,7 @@ public DiagnosticResult WithMessage(string? message)
suppressMessage: message is null,
message: message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand All @@ -145,6 +174,7 @@ public DiagnosticResult WithMessageFormat(LocalizableString messageFormat)
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: messageFormat,
messageArguments: MessageArguments);
Expand All @@ -157,6 +187,7 @@ public DiagnosticResult WithNoLocation()
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand Down Expand Up @@ -210,6 +241,7 @@ public DiagnosticResult WithDefaultPath(string path)
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand Down Expand Up @@ -237,6 +269,7 @@ public DiagnosticResult WithLineOffset(int offset)
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand All @@ -249,6 +282,7 @@ private DiagnosticResult AppendSpan(FileLinePositionSpan span, DiagnosticLocatio
suppressMessage: _suppressMessage,
message: _message,
severity: Severity,
options: Options,
id: Id,
messageFormat: MessageFormat,
messageArguments: MessageArguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ Microsoft.CodeAnalysis.Testing.DiagnosticLocation.DiagnosticLocation(Microsoft.C
Microsoft.CodeAnalysis.Testing.DiagnosticLocation.Options.get -> Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions
Microsoft.CodeAnalysis.Testing.DiagnosticLocation.Span.get -> Microsoft.CodeAnalysis.FileLinePositionSpan
Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions
Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions.IgnoreAdditionalLocations = 2 -> Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions
Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions.IgnoreLength = 1 -> Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions
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
Microsoft.CodeAnalysis.Testing.DiagnosticResult.DiagnosticResult(string id, Microsoft.CodeAnalysis.DiagnosticSeverity severity) -> void
Expand All @@ -47,6 +50,7 @@ Microsoft.CodeAnalysis.Testing.DiagnosticResult.Id.get -> string
Microsoft.CodeAnalysis.Testing.DiagnosticResult.Message.get -> string
Microsoft.CodeAnalysis.Testing.DiagnosticResult.MessageArguments.get -> object[]
Microsoft.CodeAnalysis.Testing.DiagnosticResult.MessageFormat.get -> Microsoft.CodeAnalysis.LocalizableString
Microsoft.CodeAnalysis.Testing.DiagnosticResult.Options.get -> Microsoft.CodeAnalysis.Testing.DiagnosticOptions
Microsoft.CodeAnalysis.Testing.DiagnosticResult.Severity.get -> Microsoft.CodeAnalysis.DiagnosticSeverity
Microsoft.CodeAnalysis.Testing.DiagnosticResult.Spans.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Testing.DiagnosticLocation>
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithArguments(params object[] arguments) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Expand All @@ -60,6 +64,7 @@ Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithLocation(string path, int li
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithMessage(string message) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithMessageFormat(Microsoft.CodeAnalysis.LocalizableString messageFormat) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithNoLocation() -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithOptions(Microsoft.CodeAnalysis.Testing.DiagnosticOptions options) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithSeverity(Microsoft.CodeAnalysis.DiagnosticSeverity severity) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithSpan(Microsoft.CodeAnalysis.FileLinePositionSpan span) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Microsoft.CodeAnalysis.Testing.DiagnosticResult.WithSpan(Microsoft.CodeAnalysis.FileLinePositionSpan span, Microsoft.CodeAnalysis.Testing.DiagnosticLocationOptions options) -> Microsoft.CodeAnalysis.Testing.DiagnosticResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public SolutionState WithProcessedMarkup(MarkupOptions markupOptions, Diagnostic
}

var linePosition = content.Lines.GetLinePosition(position);
return diagnosticResult.Value.WithLocation(filename, linePosition, DiagnosticLocationOptions.IgnoreAdditionalLocations);
return diagnosticResult.Value.WithLocation(filename, linePosition);
}

private DiagnosticResult? CreateDiagnosticForSpan(
Expand All @@ -383,7 +383,7 @@ public SolutionState WithProcessedMarkup(MarkupOptions markupOptions, Diagnostic
}

var linePositionSpan = content.Lines.GetLinePositionSpan(span);
return diagnosticResult.Value.WithSpan(new FileLinePositionSpan(filename, linePositionSpan), DiagnosticLocationOptions.IgnoreAdditionalLocations);
return diagnosticResult.Value.WithSpan(new FileLinePositionSpan(filename, linePositionSpan));
}

private DiagnosticResult? CreateDiagnostic(
Expand Down Expand Up @@ -439,7 +439,7 @@ public SolutionState WithProcessedMarkup(MarkupOptions markupOptions, Diagnostic
}
}

return diagnosticResult.WithMessage(null);
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 ba227e6

Please sign in to comment.