MapTo/test/MapTo.Tests/Extensions/ShouldlyExtensions.cs

88 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
2021-01-06 11:52:50 +03:00
using System.Linq;
2021-02-07 13:46:12 +03:00
using System.Text;
2021-01-06 11:52:50 +03:00
using Microsoft.CodeAnalysis;
using Shouldly;
using Xunit;
2021-01-06 11:52:50 +03:00
namespace MapTo.Tests.Extensions
{
internal static class ShouldlyExtensions
{
2021-06-29 09:14:00 +03:00
internal static void ShouldContainSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string? customMessage = null)
2021-01-06 11:52:50 +03:00
{
var syntax = syntaxTree
.Select(s => s.ToString().Trim())
.SingleOrDefault(s => s.Contains(typeName));
syntax.ShouldNotBeNullOrWhiteSpace();
syntax.ShouldBe(expectedSource, customMessage);
}
2021-06-29 09:14:00 +03:00
internal static void ShouldContainPartialSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string? customMessage = null)
2021-04-09 09:48:23 +03:00
{
var syntax = syntaxTree
.Select(s => s.ToString().Trim())
.SingleOrDefault(s => s.Contains(typeName));
syntax.ShouldNotBeNullOrWhiteSpace();
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
}
2021-06-29 09:14:00 +03:00
internal static void ShouldContainPartialSource(this SyntaxTree syntaxTree, string expectedSource, string? customMessage = null)
2021-02-13 14:27:32 +03:00
{
var syntax = syntaxTree.ToString();
syntax.ShouldNotBeNullOrWhiteSpace();
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
}
2021-06-29 09:14:00 +03:00
internal static void ShouldBeSuccessful(this IEnumerable<Diagnostic> diagnostics, Compilation? compilation = null, IEnumerable<string>? ignoreDiagnosticsIds = null)
{
2021-02-07 13:46:12 +03:00
var actual = diagnostics
2021-07-02 11:24:46 +03:00
.Where(d => (ignoreDiagnosticsIds is null || ignoreDiagnosticsIds.All(i => !d.Id.StartsWith(i) )) && (d.Severity is DiagnosticSeverity.Warning or DiagnosticSeverity.Error))
2021-02-07 13:46:12 +03:00
.Select(c => $"{c.Severity}: {c.Location.GetLineSpan()} - {c.GetMessage()}").ToArray();
if (!actual.Any())
{
return;
}
var builder = new StringBuilder();
builder.AppendLine("Failed");
foreach (var d in actual)
{
builder.AppendFormat("- {0}", d).AppendLine();
}
if (compilation is not null)
{
builder.AppendLine("Generated Sources:");
builder.AppendLine(compilation.PrintSyntaxTree());
}
Assert.False(true, builder.ToString());
}
internal static void ShouldNotBeSuccessful(this ImmutableArray<Diagnostic> diagnostics, Diagnostic expectedError)
{
var actualDiagnostics = diagnostics.SingleOrDefault(d => d.Id == expectedError.Id);
var compilationDiagnostics = actualDiagnostics == null ? diagnostics : diagnostics.Except(new[] { actualDiagnostics });
compilationDiagnostics.ShouldBeSuccessful();
Assert.NotNull(actualDiagnostics);
2021-06-29 09:14:00 +03:00
Assert.Equal(expectedError.Id, actualDiagnostics?.Id);
Assert.Equal(expectedError.Descriptor.Id, actualDiagnostics?.Descriptor.Id);
Assert.Equal(expectedError.Descriptor.Description, actualDiagnostics?.Descriptor.Description);
Assert.Equal(expectedError.Descriptor.Title, actualDiagnostics?.Descriptor.Title);
if (expectedError.Location != Location.None)
{
2021-06-29 09:14:00 +03:00
Assert.Equal(expectedError.Location, actualDiagnostics?.Location);
}
}
2021-01-06 11:52:50 +03:00
}
}