MapTo/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs

64 lines
3.0 KiB
C#
Raw Normal View History

using System;
2021-01-03 12:26:07 +03:00
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using MapTo.Tests.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
2021-01-03 12:26:07 +03:00
namespace MapTo.Tests.Infrastructure
{
internal static class CSharpGenerator
{
2021-04-09 09:48:23 +03:00
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
string source,
bool assertCompilation = false,
2021-06-29 09:14:00 +03:00
IDictionary<string, string>? analyzerConfigOptions = null,
2021-04-09 09:48:23 +03:00
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
LanguageVersion languageVersion = LanguageVersion.CSharp7_3) =>
GetOutputCompilation(
new[] { source },
assertCompilation,
analyzerConfigOptions,
nullableContextOptions,
languageVersion);
2021-02-07 13:46:12 +03:00
2021-04-09 09:48:23 +03:00
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
IEnumerable<string> sources,
bool assertCompilation = false,
2021-06-29 09:14:00 +03:00
IDictionary<string, string>? analyzerConfigOptions = null,
2021-04-09 09:48:23 +03:00
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
LanguageVersion languageVersion = LanguageVersion.CSharp7_3)
{
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-04-09 09:48:23 +03:00
sources.Select((source, index) => CSharpSyntaxTree.ParseText(source, path: $"Test{index:00}.g.cs", options: new CSharpParseOptions(languageVersion))),
2021-01-27 10:53:29 +03:00
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: nullableContextOptions));
2021-04-09 09:48:23 +03:00
if (assertCompilation)
{
// NB: fail tests when the injected program isn't valid _before_ running generators
compilation.GetDiagnostics().ShouldBeSuccessful();
}
2021-01-27 10:53:29 +03:00
2021-01-03 12:26:07 +03:00
var driver = CSharpGeneratorDriver.Create(
new[] { new MapToGenerator() },
2021-04-09 09:48:23 +03:00
optionsProvider: new TestAnalyzerConfigOptionsProvider(analyzerConfigOptions),
parseOptions: new CSharpParseOptions(languageVersion)
2021-01-03 12:26:07 +03:00
);
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
generateDiagnostics.ShouldBeSuccessful(ignoreDiagnosticsIds: new[] { "MT" });
2021-02-07 13:46:12 +03:00
outputCompilation.GetDiagnostics().ShouldBeSuccessful(outputCompilation);
return (outputCompilation, generateDiagnostics);
}
}
}