From 3716a2a6443f4835103caabd6a3ef69d5b1b1218 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Nov 2020 19:06:30 +0100 Subject: [PATCH] Added unit tests for the source generator --- .../ComputeSharp.SourceGenerators.csproj | 8 +- .../ComputeSharp.Tests.Internals.csproj | 2 + .../SourceGeneratorTests.cs | 96 +++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index f9c83fae..92f227f9 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -2,13 +2,13 @@ netstandard2.0 - 9.0 - enable + 9.0 + enable - - + + diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 72b5e1aa..9674f666 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -9,9 +9,11 @@ + + diff --git a/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs b/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs new file mode 100644 index 00000000..266dc6e6 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGenerators; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Internals +{ + [TestClass] + [TestCategory("SourceGenerator")] + public class SourceGeneratorTests + { + [TestMethod] + public void SequentialEnum() + { + string source = @" + using MyLibrary; + using System.Numerics; + + namespace MyLibrary + { + public class Buffer + { + } + } + + namespace MyFancyApp + { + [AutoConstructor] + public readonly partial struct MyShader + { + private readonly Buffer A; + private readonly float B; + } + }"; + + string expected = @" + namespace MyFancyApp + { + public readonly partial struct MyShader + { + public MyShader(MyLibrary.Buffer A, float B) + { + this.A = A; + this.B = B; + } + } + }"; + + VerifyGeneratedMethodLines(source, 1, expected); + } + + /// + /// Verifies the output of a source generator. + /// + /// The generator type to use. + /// The input source to process. + /// The target index to check in the resulting output. + /// The expected body to compare with the generated code. + private void VerifyGeneratedMethodLines(string source, int index, string expectedBody) + where TGenerator : class, ISourceGenerator, new() + { + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); + + IEnumerable references = + from assembly in AppDomain.CurrentDomain.GetAssemblies() + where !assembly.IsDynamic + let reference = MetadataReference.CreateFromFile(assembly.Location) + select reference; + + CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + + ISourceGenerator generator = new TGenerator(); + + CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(generator); + + driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); + + string outputBody = outputCompilation.SyntaxTrees.Skip(index + 1).First().ToString(); + + string[] + outputLines = outputBody.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), + expectedLines = expectedBody.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + Assert.AreEqual(expectedLines.Length, outputLines.Length); + + for (int i = 0; i < outputLines.Length; i++) + { + Assert.AreEqual(expectedLines[i], outputLines[i]); + } + } + } +} \ No newline at end of file