2021-01-21 11:19:17 +03:00
|
|
|
|
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;
|
2021-01-21 11:19:17 +03:00
|
|
|
|
using Xunit;
|
2021-01-06 11:52:50 +03:00
|
|
|
|
|
|
|
|
|
namespace MapTo.Tests.Extensions
|
|
|
|
|
{
|
|
|
|
|
internal static class ShouldlyExtensions
|
|
|
|
|
{
|
|
|
|
|
internal static void ShouldContainSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string customMessage = null)
|
|
|
|
|
{
|
|
|
|
|
var syntax = syntaxTree
|
|
|
|
|
.Select(s => s.ToString().Trim())
|
|
|
|
|
.SingleOrDefault(s => s.Contains(typeName));
|
|
|
|
|
|
|
|
|
|
syntax.ShouldNotBeNullOrWhiteSpace();
|
|
|
|
|
syntax.ShouldBe(expectedSource, customMessage);
|
|
|
|
|
}
|
2021-01-21 11:19:17 +03:00
|
|
|
|
|
2021-02-07 13:46:12 +03:00
|
|
|
|
internal static void ShouldBeSuccessful(this IEnumerable<Diagnostic> diagnostics, Compilation compilation = null)
|
2021-01-21 11:19:17 +03:00
|
|
|
|
{
|
2021-02-07 13:46:12 +03:00
|
|
|
|
var actual = diagnostics
|
|
|
|
|
.Where(d => !d.Id.StartsWith("MT") && (d.Severity == DiagnosticSeverity.Warning || d.Severity == DiagnosticSeverity.Error))
|
|
|
|
|
.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());
|
2021-01-21 11:19:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ShouldBeUnsuccessful(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);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(expectedError.Location, actualDiagnostics.Location);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-06 11:52:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|