2020-12-19 10:55:47 +03:00
|
|
|
|
using System;
|
2021-01-03 12:26:07 +03:00
|
|
|
|
using System.Collections.Generic;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Linq;
|
2021-01-21 11:19:17 +03:00
|
|
|
|
using MapTo.Tests.Extensions;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2021-01-03 12:26:07 +03:00
|
|
|
|
namespace MapTo.Tests.Infrastructure
|
2020-12-19 10:55:47 +03:00
|
|
|
|
{
|
|
|
|
|
internal static class CSharpGenerator
|
|
|
|
|
{
|
2021-02-07 13:46:12 +03:00
|
|
|
|
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(string source, bool assertCompilation = false, IDictionary<string, string> analyzerConfigOptions = null, NullableContextOptions nullableContextOptions = NullableContextOptions.Disable) =>
|
|
|
|
|
GetOutputCompilation(new[] { source }, assertCompilation, analyzerConfigOptions, nullableContextOptions);
|
|
|
|
|
|
|
|
|
|
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(IEnumerable<string> sources, bool assertCompilation = false, IDictionary<string, string> analyzerConfigOptions = null, NullableContextOptions nullableContextOptions = NullableContextOptions.Disable)
|
2020-12-19 10:55:47 +03:00
|
|
|
|
{
|
|
|
|
|
var references = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
.Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
|
|
|
|
|
.Select(a => MetadataReference.CreateFromFile(a.Location))
|
|
|
|
|
.ToList();
|
2021-02-07 13:46:12 +03:00
|
|
|
|
|
2021-01-27 10:53:29 +03:00
|
|
|
|
var compilation = CSharpCompilation.Create(
|
|
|
|
|
$"{typeof(CSharpGenerator).Assembly.GetName().Name}.Dynamic",
|
2021-02-07 13:46:12 +03:00
|
|
|
|
sources.Select((source, index) => CSharpSyntaxTree.ParseText(source, path: $"Test{index:00}.g.cs")),
|
2021-01-27 10:53:29 +03:00
|
|
|
|
references,
|
|
|
|
|
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: nullableContextOptions));
|
2020-12-19 10:55:47 +03:00
|
|
|
|
|
2020-12-21 13:20:29 +03:00
|
|
|
|
if (assertCompilation)
|
|
|
|
|
{
|
|
|
|
|
// NB: fail tests when the injected program isn't valid _before_ running generators
|
2021-01-21 11:19:17 +03:00
|
|
|
|
compilation.GetDiagnostics().ShouldBeSuccessful();
|
2020-12-21 13:20:29 +03:00
|
|
|
|
}
|
2021-01-27 10:53:29 +03:00
|
|
|
|
|
2021-01-03 12:26:07 +03:00
|
|
|
|
var driver = CSharpGeneratorDriver.Create(
|
|
|
|
|
new[] { new MapToGenerator() },
|
|
|
|
|
optionsProvider: new TestAnalyzerConfigOptionsProvider(analyzerConfigOptions)
|
|
|
|
|
);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
2021-01-03 12:26:07 +03:00
|
|
|
|
driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics);
|
2021-02-07 13:46:12 +03:00
|
|
|
|
|
2021-02-17 11:32:23 +03:00
|
|
|
|
generateDiagnostics.ShouldBeSuccessful(ignoreDiagnosticsIds: new[] { "MT" });
|
2021-02-07 13:46:12 +03:00
|
|
|
|
outputCompilation.GetDiagnostics().ShouldBeSuccessful(outputCompilation);
|
2021-01-21 11:19:17 +03:00
|
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
|
return (outputCompilation, generateDiagnostics);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|