|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | + |
| 4 | +namespace CommunityToolkit.Tooling.SampleGen.Tests.Helpers; |
| 5 | + |
| 6 | +public static partial class TestHelpers |
| 7 | +{ |
| 8 | + internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this string source, string assemblyName, string markdown = "") where TGenerator : class, IIncrementalGenerator, new() => RunSourceGenerator<TGenerator>(source.ToSyntaxTree(), assemblyName, markdown); |
| 9 | + |
| 10 | + internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this SyntaxTree syntaxTree, string assemblyName, string markdown = "") |
| 11 | + where TGenerator : class, IIncrementalGenerator, new() |
| 12 | + { |
| 13 | + var references = GetAllReferencedAssemblies(); |
| 14 | + var compilation = syntaxTree.CreateCompilation(assemblyName, references); // assembly name should always be supplied in param |
| 15 | + return RunSourceGenerator<TGenerator>(syntaxTree, compilation, markdown); |
| 16 | + } |
| 17 | + |
| 18 | + internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this SyntaxTree syntaxTree, Compilation compilation, string markdown = "") |
| 19 | + where TGenerator : class, IIncrementalGenerator, new() |
| 20 | + { |
| 21 | + // Create a driver for the source generator |
| 22 | + var driver = syntaxTree |
| 23 | + .CreateSourceGeneratorDriver(new TGenerator()) |
| 24 | + .WithMarkdown(markdown); |
| 25 | + |
| 26 | + // Update the original compilation using the source generator |
| 27 | + _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation generatorCompilation, out ImmutableArray<Diagnostic> postGeneratorCompilationDiagnostics); |
| 28 | + |
| 29 | + return new(generatorCompilation, postGeneratorCompilationDiagnostics); |
| 30 | + } |
| 31 | + |
| 32 | + internal static void AssertDiagnosticsAre(this IEnumerable<Diagnostic> diagnostics, params DiagnosticDescriptor[] expectedDiagnosticDescriptors) |
| 33 | + { |
| 34 | + var expectedIds = expectedDiagnosticDescriptors.Select(x => x.Id).ToHashSet(); |
| 35 | + var resultingIds = diagnostics.Select(diagnostic => diagnostic.Id).ToHashSet(); |
| 36 | + |
| 37 | + Assert.IsTrue(resultingIds.SetEquals(expectedIds), $"Expected [{string.Join(", ", expectedIds)}] diagnostic Ids. Got [{string.Join(", ", resultingIds)}]"); |
| 38 | + } |
| 39 | + |
| 40 | + internal static void AssertNoCompilationErrors(this Compilation outputCompilation) |
| 41 | + { |
| 42 | + var generatedCompilationDiagnostics = outputCompilation.GetDiagnostics(); |
| 43 | + Assert.IsTrue(generatedCompilationDiagnostics.All(x => x.Severity != DiagnosticSeverity.Error), $"Expected no generated compilation errors. Got: \n{string.Join("\n", generatedCompilationDiagnostics.Where(x => x.Severity == DiagnosticSeverity.Error).Select(x => $"[{x.Id}: {x.GetMessage()}]"))}"); |
| 44 | + } |
| 45 | + |
| 46 | + internal static void AssertSourceGenerated(this Compilation compilation, string filename, string expectedContents) |
| 47 | + { |
| 48 | + var generatedTree = compilation.SyntaxTrees.SingleOrDefault(tree => Path.GetFileName(tree.FilePath) == filename); |
| 49 | + |
| 50 | + Assert.IsNotNull(generatedTree, $"No file named {filename} was generated"); |
| 51 | + Assert.AreEqual(expectedContents, generatedTree.ToString(), "Unexpected code generated"); |
| 52 | + } |
| 53 | + |
| 54 | + internal static void AssertDiagnosticsAre(this SourceGeneratorRunResult result, params DiagnosticDescriptor[] expectedDiagnosticDescriptors) => AssertDiagnosticsAre(result.Diagnostics, expectedDiagnosticDescriptors); |
| 55 | + |
| 56 | + internal static void AssertNoCompilationErrors(this SourceGeneratorRunResult result) => AssertNoCompilationErrors(result.Compilation); |
| 57 | + |
| 58 | + internal static void AssertSourceGenerated(this SourceGeneratorRunResult result, string filename, string expectedContents) => AssertSourceGenerated(result.Compilation, filename, expectedContents); |
| 59 | +} |
0 commit comments