Implement IsSuppressed diagnostic validation via reflection.

This commit is contained in:
tom-englert 2021-10-04 18:15:52 +02:00
Родитель d77fa1a4ee
Коммит 31de3764d0
2 изменённых файлов: 60 добавлений и 0 удалений

Просмотреть файл

@ -15,6 +15,7 @@ using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Testing.Extensions;
using Microsoft.CodeAnalysis.Testing.Model;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Composition;
@ -409,6 +410,19 @@ namespace Microsoft.CodeAnalysis.Testing
message);
}
if (expected.IsSuppressed.HasValue)
{
if (actual.diagnostic.TryGetIsSuppressed(out var actualValue))
{
message = FormatVerifierMessage(analyzers, actual.diagnostic, expected, $"Expected diagnostic suppression state to match");
verifier.Equal(expected.IsSuppressed.Value, actualValue, message);
}
else
{
throw new NotSupportedException("DiagnosticSuppressors are not supported on this platform.");
}
}
DiagnosticVerifier?.Invoke(actual.diagnostic, expected, verifier);
}
}
@ -803,6 +817,13 @@ namespace Microsoft.CodeAnalysis.Testing
builder.Append(")");
}
if (diagnostics[i].TryGetIsSuppressed(out var isSuppressed) && isSuppressed)
{
builder.Append($".{nameof(DiagnosticResult.WithIsSuppressed)}(");
builder.Append(isSuppressed);
builder.Append(")");
}
builder.AppendLine(",");
}
@ -886,6 +907,13 @@ namespace Microsoft.CodeAnalysis.Testing
builder.Append(")");
}
if (diagnostics[i].IsSuppressed.HasValue)
{
builder.Append($".{nameof(DiagnosticResult.WithIsSuppressed)}(");
builder.Append(diagnostics[i].IsSuppressed.GetValueOrDefault());
builder.Append(")");
}
builder.AppendLine(",");
}

Просмотреть файл

@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Microsoft.CodeAnalysis.Testing.Extensions
{
internal static class DiagnosticExtensions
{
private static readonly MethodInfo? IsSuppressedGetMethod = typeof(Diagnostic).GetProperty("IsSuppressed")?.GetMethod;
public static bool TryGetIsSuppressed(this Diagnostic diagnostic, out bool value)
{
value = false;
var rawValue = IsSuppressedGetMethod?.Invoke(diagnostic, null);
if (rawValue?.GetType() != typeof(bool))
{
return false;
}
value = (bool)rawValue;
return true;
}
}
}