From 18e8f03cb1a63be434a3432128ab61732ae4950b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Dec 2023 17:32:34 +0100 Subject: [PATCH] Use CSharpGeneratorTest in output tests --- .../Test_D2DPixelShaderSourceGenerator.cs | 48 +------------ .../CSharpGeneratorTest{TGenerator}.cs | 68 ++++++++++++++----- .../Test_ComputeShaderDescriptorGenerator.cs | 48 +------------ 3 files changed, 54 insertions(+), 110 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index 9fd032a4..d72bf52e 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -1,17 +1,6 @@ -extern alias Core; -extern alias D2D1; - -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.IO; -using System.Linq; using System.Threading.Tasks; -using Basic.Reference.Assemblies; using ComputeSharp.D2D1.SourceGenerators; using ComputeSharp.Tests.SourceGenerators.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests.SourceGenerators; @@ -1775,7 +1764,6 @@ public class Test_D2DPixelShaderSourceGenerator /// The task for the operation. private static async Task VerifyGeneratedDiagnosticsAsync(string source, (string Filename, string Source) result) { - // Also validate all analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); @@ -1791,40 +1779,6 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - // Get all assembly references for the .NET TFM and ComputeSharp - IEnumerable metadataReferences = - [ - .. Net80.References.All, - MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location), - MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location) - ]; - - // Parse the source text (C# 12) - SyntaxTree sourceTree = CSharpSyntaxTree.ParseText( - source, - CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12)); - - // Create the original compilation - CSharpCompilation compilation = CSharpCompilation.Create( - "original", - [sourceTree], - metadataReferences, - new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); - - // Create the generator driver with the D2D shader generator - GeneratorDriver driver = CSharpGeneratorDriver.Create(new D2DPixelShaderDescriptorGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options); - - // Run all source generators on the input source code - _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); - - // Ensure that no diagnostics were generated - CollectionAssert.AreEquivalent(Array.Empty(), diagnostics); - - // Update the assembly version using the version from the assembly of the input generators. - // This allows the tests to not need updates whenever the version of the MVVM Toolkit changes. - string expectedText = result.Source.Replace("", $"\"{typeof(D2DPixelShaderDescriptorGenerator).Assembly.GetName().Version}\""); - string actualText = outputCompilation.SyntaxTrees.Single(tree => Path.GetFileName(tree.FilePath) == result.Filename).ToString(); - - Assert.AreEqual(expectedText, actualText); + CSharpGeneratorTest.VerifySources(source, result); } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs index 068cbe79..7198457e 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Helpers/CSharpGeneratorTest{TGenerator}.cs @@ -6,8 +6,10 @@ extern alias D2D1; extern alias D3D12; #endif +using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.IO; using System.Linq; using Basic.Reference.Assemblies; using Microsoft.CodeAnalysis.CSharp; @@ -29,6 +31,54 @@ internal static class CSharpGeneratorTest /// The input source to process. /// The expected diagnostics ids to be generated. public static void VerifyDiagnostics(string source, params string[] diagnosticsIds) + { + RunGenerator(source, out Compilation compilation, out ImmutableArray diagnostics); + + Dictionary diagnosticMap = diagnostics.DistinctBy(diagnostic => diagnostic.Id).ToDictionary(diagnostic => diagnostic.Id); + + // Check that the diagnostics match + Assert.IsTrue(diagnosticMap.Keys.ToHashSet().SetEquals(diagnosticsIds), $"Diagnostics didn't match. {string.Join(", ", diagnosticMap.Values)}"); + + // If the compilation was supposed to succeed, ensure that no further errors were generated + if (diagnosticsIds.Length == 0) + { + // Compute diagnostics for the final compiled output (just include errors) + List outputCompilationDiagnostics = compilation.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToList(); + + Assert.IsTrue(outputCompilationDiagnostics.Count == 0, $"resultingIds: {string.Join(", ", outputCompilationDiagnostics)}"); + } + } + + /// + /// Verifies the resulting sources produced by a source generator. + /// + /// The input source to process. + /// The expected source to be generated. + public static void VerifySources(string source, (string Filename, string Source) result) + { + RunGenerator(source, out Compilation compilation, out ImmutableArray diagnostics); + + // Ensure that no diagnostics were generated + CollectionAssert.AreEquivalent(Array.Empty(), diagnostics); + + // Update the assembly version using the version from the assembly of the input generators. + // This allows the tests to not need updates whenever the version of the MVVM Toolkit changes. + string expectedText = result.Source.Replace("", $"\"{typeof(TGenerator).Assembly.GetName().Version}\""); + string actualText = compilation.SyntaxTrees.Single(tree => Path.GetFileName(tree.FilePath) == result.Filename).ToString(); + + Assert.AreEqual(expectedText, actualText); + } + + /// + /// Runs a generator and gathers the output results. + /// + /// The input source to process. + /// + /// + private static void RunGenerator( + string source, + out Compilation compilation, + out ImmutableArray diagnostics) { // Get all assembly references for the .NET TFM and ComputeSharp IEnumerable metadataReferences = @@ -48,7 +98,7 @@ internal static class CSharpGeneratorTest CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12)); // Create the original compilation - CSharpCompilation compilation = CSharpCompilation.Create( + CSharpCompilation originalCompilation = CSharpCompilation.Create( "original", [sourceTree], metadataReferences, @@ -58,20 +108,6 @@ internal static class CSharpGeneratorTest GeneratorDriver driver = CSharpGeneratorDriver.Create(new TGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options); // Run all source generators on the input source code - _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); - - Dictionary diagnosticMap = diagnostics.DistinctBy(diagnostic => diagnostic.Id).ToDictionary(diagnostic => diagnostic.Id); - - // Check that the diagnostics match - Assert.IsTrue(diagnosticMap.Keys.ToHashSet().SetEquals(diagnosticsIds), $"Diagnostics didn't match. {string.Join(", ", diagnosticMap.Values)}"); - - // If the compilation was supposed to succeed, ensure that no further errors were generated - if (diagnosticsIds.Length == 0) - { - // Compute diagnostics for the final compiled output (just include errors) - List outputCompilationDiagnostics = outputCompilation.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToList(); - - Assert.IsTrue(outputCompilationDiagnostics.Count == 0, $"resultingIds: {string.Join(", ", outputCompilationDiagnostics)}"); - } + _ = driver.RunGeneratorsAndUpdateCompilation(originalCompilation, out compilation, out diagnostics); } } diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs index e9bfd73f..447f4cee 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs @@ -1,17 +1,6 @@ -extern alias Core; -extern alias D3D12; - -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.IO; -using System.Linq; using System.Threading.Tasks; -using Basic.Reference.Assemblies; using ComputeSharp.SourceGenerators; using ComputeSharp.Tests.SourceGenerators.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests.SourceGenerators; @@ -874,7 +863,6 @@ public class Test_ComputeShaderDescriptorGenerator /// The task for the operation. private static async Task VerifyGeneratedDiagnosticsAsync(string source, (string Filename, string Source) result) { - // Also validate all analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); @@ -886,40 +874,6 @@ public class Test_ComputeShaderDescriptorGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - // Get all assembly references for the .NET TFM and ComputeSharp - IEnumerable metadataReferences = - [ - .. Net80.References.All, - MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location), - MetadataReference.CreateFromFile(typeof(D3D12::ComputeSharp.IComputeShader).Assembly.Location) - ]; - - // Parse the source text (C# 12) - SyntaxTree sourceTree = CSharpSyntaxTree.ParseText( - source, - CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12)); - - // Create the original compilation - CSharpCompilation compilation = CSharpCompilation.Create( - "original", - [sourceTree], - metadataReferences, - new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); - - // Create the generator driver with the shader generator - GeneratorDriver driver = CSharpGeneratorDriver.Create(new ComputeShaderDescriptorGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options); - - // Run all source generators on the input source code - _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); - - // Ensure that no diagnostics were generated - CollectionAssert.AreEquivalent(Array.Empty(), diagnostics); - - // Update the assembly version using the version from the assembly of the input generators. - // This allows the tests to not need updates whenever the version of the MVVM Toolkit changes. - string expectedText = result.Source.Replace("", $"\"{typeof(ComputeShaderDescriptorGenerator).Assembly.GetName().Version}\""); - string actualText = outputCompilation.SyntaxTrees.Single(tree => Path.GetFileName(tree.FilePath) == result.Filename).ToString(); - - Assert.AreEqual(expectedText, actualText); + CSharpGeneratorTest.VerifySources(source, result); } } \ No newline at end of file