From 0432679b4c2ea37986d8189da9ae111551742dff Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Carle <29762210+jscarle@users.noreply.github.com> Date: Fri, 23 Feb 2024 11:43:11 -0500 Subject: [PATCH] Add asserts for different testing frameworks. --- README.md | 30 +++++++++++ SourceGeneratorTestHelpers.sln | 18 +++++++ .../GeneratorDriverRunResultExtensions.cs | 25 +++++++++ .../SourceGeneratorTestHelpers.MSTest.csproj | 53 +++++++++++++++++++ .../GeneratorDriverRunResultExtensions.cs | 25 +++++++++ .../SourceGeneratorTestHelpers.NUnit.csproj | 53 +++++++++++++++++++ .../GeneratorDriverRunResultExtensions.cs | 25 +++++++++ .../SourceGeneratorTestHelpers.XUnit.csproj | 53 +++++++++++++++++++ 8 files changed, 282 insertions(+) create mode 100644 src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs create mode 100644 src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj create mode 100644 src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs create mode 100644 src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj create mode 100644 src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs create mode 100644 src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj diff --git a/README.md b/README.md index 5a4bf4e..73e09a1 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,33 @@ var generatedSources = result.GetSources(); ```csharp var generatedSource = result.GetSource("TestId.g.cs"); ``` + +### Compare the generated source with the expected source + +You can produce a diff between the generated source and the expected source. The result will contain a boolean `hasDifferences` and a line by line diff in `differences`. + +```csharp +var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource); +``` + +#### Assert the difference + +Using one of the testing framework packages below, you can also assert the difference between the generated source and the expected source. + +[![XUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=XUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.XUnit) +[![NUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=NUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit) +[![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest) + +```csharp +var result = IncrementalGenerator.Run("your source"); + +result.ShouldProduce("TestId.g.cs", "expected source"); +``` + +_Note: If you do not wish to assert on errors produced during diagnostics of the source generator run, you can simply disable them as such._ + +```csharp +var result = IncrementalGenerator.Run("your source"); + +result.ShouldProduce("TestId.g.cs", "expected source", false); +``` diff --git a/SourceGeneratorTestHelpers.sln b/SourceGeneratorTestHelpers.sln index 9ca70f9..70360c5 100644 --- a/SourceGeneratorTestHelpers.sln +++ b/SourceGeneratorTestHelpers.sln @@ -5,6 +5,12 @@ VisualStudioVersion = 17.9.34616.47 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorTestHelpers", "src\SourceGeneratorTestHelpers\SourceGeneratorTestHelpers.csproj", "{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.MSTest", "src\SourceGeneratorTestHelpers.MSTest\SourceGeneratorTestHelpers.MSTest.csproj", "{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.NUnit", "src\SourceGeneratorTestHelpers.NUnit\SourceGeneratorTestHelpers.NUnit.csproj", "{CCB277CE-4190-478A-B6BF-D75721A045E1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.XUnit", "src\SourceGeneratorTestHelpers.XUnit\SourceGeneratorTestHelpers.XUnit.csproj", "{E9304144-727F-4500-9EE6-6DA60A88DD8D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +21,18 @@ Global {C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Debug|Any CPU.Build.0 = Debug|Any CPU {C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.ActiveCfg = Release|Any CPU {C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.Build.0 = Release|Any CPU + {3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.Build.0 = Release|Any CPU + {CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.Build.0 = Release|Any CPU + {E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs new file mode 100644 index 0000000..7102622 --- /dev/null +++ b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs @@ -0,0 +1,25 @@ +using Microsoft.CodeAnalysis; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace SourceGeneratorTestHelpers.MSTest; + +/// Provides extension methods for the class. +public static class GeneratorDriverRunResultExtensions +{ + /// Verifies that the generated source from a with a specific file path ending matches the expected source. + /// The to get the source from. + /// The string that the generated source's file path should end with. + /// The expected source that the generated source should match. + /// to assert on reported errors by the source generator, othwerwise. Defaults to . + /// If is null. + public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(result); +#else + if (result is null) + throw new ArgumentNullException(nameof(result)); +#endif + result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message)); + } +} \ No newline at end of file diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj new file mode 100644 index 0000000..2a089f2 --- /dev/null +++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj @@ -0,0 +1,53 @@ + + + + netstandard2.0;net6.0;net7.0;net8.0 + true + enable + SourceGeneratorTestHelpers.MSTest + latest + 8.0.5 + SourceGeneratorTestHelpers.MSTest + Jean-Sebastien Carle + Test helpers and extension methods to simplify testing of .NET source generators. + Copyright © Jean-Sebastien Carle 2024 + SourceGeneratorTestHelpers.MSTest + https://github.com/jscarle/SourceGeneratorTestHelpers + LICENSE.md + README.md + https://github.com/jscarle/SourceGeneratorTestHelpers + git + testing source-generators + 8.0.5.0 + 8.0.5.0 + en-US + true + snupkg + latest-All + true + true + snupkg + true + + + + + + + + + + + + True + \ + False + + + True + \ + False + + + + diff --git a/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs b/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs new file mode 100644 index 0000000..ada6cd7 --- /dev/null +++ b/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs @@ -0,0 +1,25 @@ +using Microsoft.CodeAnalysis; +using NUnit.Framework; + +namespace SourceGeneratorTestHelpers.NUnit; + +/// Provides extension methods for the class. +public static class GeneratorDriverRunResultExtensions +{ + /// Verifies that the generated source from a with a specific file path ending matches the expected source. + /// The to get the source from. + /// The string that the generated source's file path should end with. + /// The expected source that the generated source should match. + /// to assert on reported errors by the source generator, othwerwise. Defaults to . + /// If is null. + public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(result); +#else + if (result is null) + throw new ArgumentNullException(nameof(result)); +#endif + result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message)); + } +} \ No newline at end of file diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj new file mode 100644 index 0000000..0bcb51b --- /dev/null +++ b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj @@ -0,0 +1,53 @@ + + + + netstandard2.0;net6.0;net7.0;net8.0 + true + enable + SourceGeneratorTestHelpers.NUnit + latest + 8.0.5 + SourceGeneratorTestHelpers.NUnit + Jean-Sebastien Carle + Test helpers and extension methods to simplify testing of .NET source generators. + Copyright © Jean-Sebastien Carle 2024 + SourceGeneratorTestHelpers.NUnit + https://github.com/jscarle/SourceGeneratorTestHelpers + LICENSE.md + README.md + https://github.com/jscarle/SourceGeneratorTestHelpers + git + testing source-generators + 8.0.5.0 + 8.0.5.0 + en-US + true + snupkg + latest-All + true + true + snupkg + true + + + + + + + + + + + + True + \ + False + + + True + \ + False + + + + diff --git a/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs b/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs new file mode 100644 index 0000000..c275890 --- /dev/null +++ b/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs @@ -0,0 +1,25 @@ +using Microsoft.CodeAnalysis; +using Xunit.Sdk; + +namespace SourceGeneratorTestHelpers.XUnit; + +/// Provides extension methods for the class. +public static class GeneratorDriverRunResultExtensions +{ + /// Verifies that the generated source from a with a specific file path ending matches the expected source. + /// The to get the source from. + /// The string that the generated source's file path should end with. + /// The expected source that the generated source should match. + /// to assert on reported errors by the source generator, othwerwise. Defaults to . + /// If is null. + public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(result); +#else + if (result is null) + throw new ArgumentNullException(nameof(result)); +#endif + result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new XunitException(message)); + } +} \ No newline at end of file diff --git a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj new file mode 100644 index 0000000..cc5102f --- /dev/null +++ b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj @@ -0,0 +1,53 @@ + + + + netstandard2.0;net6.0;net7.0;net8.0 + true + enable + SourceGeneratorTestHelpers.XUnit + latest + 8.0.5 + SourceGeneratorTestHelpers.XUnit + Jean-Sebastien Carle + Test helpers and extension methods to simplify testing of .NET source generators. + Copyright © Jean-Sebastien Carle 2024 + SourceGeneratorTestHelpers.XUnit + https://github.com/jscarle/SourceGeneratorTestHelpers + LICENSE.md + README.md + https://github.com/jscarle/SourceGeneratorTestHelpers + git + testing source-generators + 8.0.5.0 + 8.0.5.0 + en-US + true + snupkg + latest-All + true + true + snupkg + true + + + + + + + + + + + + True + \ + False + + + True + \ + False + + + +