From 27be49f8109a114a50abaf4033da7f1e3be9a9d3 Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Carle <29762210+jscarle@users.noreply.github.com> Date: Sat, 20 Jun 2026 14:02:12 -0500 Subject: [PATCH 1/2] Updated dependencies. (#25) --- .github/workflows/{main.yml => develop.yml} | 14 ++++++---- .github/workflows/publish.yml | 13 +++++++-- README.md | 2 +- .../SourceGeneratorTestHelpers.MSTest.csproj | 28 +++++++++++-------- .../SourceGeneratorTestHelpers.NUnit.csproj | 28 +++++++++++-------- .../SourceGeneratorTestHelpers.XUnit.csproj | 28 +++++++++++-------- .../SourceGeneratorTestHelpers.csproj | 24 +++++++++------- 7 files changed, 81 insertions(+), 56 deletions(-) rename .github/workflows/{main.yml => develop.yml} (75%) diff --git a/.github/workflows/main.yml b/.github/workflows/develop.yml similarity index 75% rename from .github/workflows/main.yml rename to .github/workflows/develop.yml index 70c9371..fdfed68 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/develop.yml @@ -2,9 +2,11 @@ name: Release on: push: - branches: [ "main" ] + branches: + - develop pull_request: - branches: [ "main" ] + branches: + - develop jobs: build: @@ -17,15 +19,15 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 - name: Initialize CodeQL - uses: github/codeql-action/init@v3 + uses: github/codeql-action/init@v4 with: languages: 'csharp' - name: Setup .NET 10.0 - uses: actions/setup-dotnet@v4 + uses: actions/setup-dotnet@v5 with: dotnet-version: 10.x @@ -39,4 +41,4 @@ jobs: run: dotnet test --configuration Release --no-build --verbosity normal --framework net8.0 - name: Perform CodeQL analysis - uses: github/codeql-action/analyze@v3 + uses: github/codeql-action/analyze@v4 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index fd18239..c03cfca 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -13,13 +13,14 @@ jobs: actions: read contents: read security-events: write + id-token: write steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 - name: Setup .NET 10.0 - uses: actions/setup-dotnet@v4 + uses: actions/setup-dotnet@v5 with: dotnet-version: 10.x @@ -32,5 +33,11 @@ jobs: - name: Pack run: dotnet pack --configuration Release --no-build --output . + - name: NuGet Login + uses: NuGet/login@v1 + id: nuget-login + with: + user: ${{secrets.NUGET_USER}} + - name: Push to NuGet - run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json --skip-duplicate + run: dotnet nuget push "*.nupkg" --api-key ${{steps.nuget-login.outputs.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json --skip-duplicate diff --git a/README.md b/README.md index 5224077..59742a5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Test helpers and extension methods to simplify testing of .NET source generators. -[![main](https://img.shields.io/github/actions/workflow/status/jscarle/SourceGeneratorTestHelpers/main.yml?logo=github)](https://github.com/jscarle/SourceGeneratorTestHelpers) +[![develop](https://img.shields.io/github/actions/workflow/status/jscarle/SourceGeneratorTestHelpers/develop.yml?logo=github)](https://github.com/jscarle/SourceGeneratorTestHelpers) [![nuget](https://img.shields.io/nuget/v/SourceGeneratorTestHelpers)](https://www.nuget.org/packages/SourceGeneratorTestHelpers) [![downloads](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers)](https://www.nuget.org/packages/SourceGeneratorTestHelpers) diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj index b9ad3f1..9e180c3 100644 --- a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj +++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj @@ -1,16 +1,16 @@ - + - netstandard2.0;net6.0;net7.0;net8.0 + netstandard2.0;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.MSTest latest - 10.0.0 + 10.0.1 SourceGeneratorTestHelpers.MSTest Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. - Copyright © Jean-Sebastien Carle 2025 + Copyright © Jean-Sebastien Carle 2025-2026 SourceGeneratorTestHelpers.MSTest https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators mstest - 10.0.0.0 - 10.0.0.0 + 10.0.1.0 + 10.0.1.0 en-US true snupkg @@ -28,17 +28,18 @@ true snupkg true + true - - - - + + + + - + True \ @@ -50,5 +51,8 @@ False - + + true + true + diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj index a376d8b..fd4c1c6 100644 --- a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj +++ b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj @@ -1,16 +1,16 @@ - + - netstandard2.0;net6.0;net7.0;net8.0 + netstandard2.0;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.NUnit latest - 10.0.0 + 10.0.1 SourceGeneratorTestHelpers.NUnit Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. - Copyright © Jean-Sebastien Carle 2025 + Copyright © Jean-Sebastien Carle 2025-2026 SourceGeneratorTestHelpers.NUnit https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators nunit - 10.0.0.0 - 10.0.0.0 + 10.0.1.0 + 10.0.1.0 en-US true snupkg @@ -28,17 +28,18 @@ true snupkg true + true - - - - + + + + - + True \ @@ -50,5 +51,8 @@ False - + + true + true + diff --git a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj index 4bc1738..c1cf0dc 100644 --- a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj +++ b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj @@ -1,16 +1,16 @@ - + - netstandard2.0;net6.0;net7.0;net8.0 + netstandard2.0;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.XUnit latest - 10.0.0 + 10.0.1 SourceGeneratorTestHelpers.XUnit Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. - Copyright © Jean-Sebastien Carle 2025 + Copyright © Jean-Sebastien Carle 2025-2026 SourceGeneratorTestHelpers.XUnit https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators xunit - 10.0.0.0 - 10.0.0.0 + 10.0.1.0 + 10.0.1.0 en-US true snupkg @@ -29,17 +29,18 @@ snupkg true $(NoWarn);NU1701 + true - - - - + + + + - + True \ @@ -51,5 +52,8 @@ False - + + true + true + diff --git a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj index ce518bf..dbed528 100644 --- a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj +++ b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj @@ -1,4 +1,4 @@ - + netstandard2.0;net6.0;net7.0;net8.0;net9.0;net10.0 @@ -6,11 +6,11 @@ enable SourceGeneratorTestHelpers latest - 10.0.0 + 10.0.1 SourceGeneratorTestHelpers Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. - Copyright © Jean-Sebastien Carle 2025 + Copyright © Jean-Sebastien Carle 2025-2026 SourceGeneratorTestHelpers https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators - 10.0.0.0 - 10.0.0.0 + 10.0.1.0 + 10.0.1.0 en-US true snupkg @@ -28,6 +28,7 @@ true snupkg true + true @@ -37,13 +38,13 @@ - - - + + + - + True \ @@ -55,5 +56,8 @@ False - + + true + true + From 3bf3aba9acba3a628418a42f6dc1d4f44b5c159f Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Carle <29762210+jscarle@users.noreply.github.com> Date: Sat, 20 Jun 2026 16:14:12 -0400 Subject: [PATCH 2/2] Added support for enumerable sources. --- README.md | 40 ++++++++-- .../GeneratorDriverTestBase.cs | 64 +++------------ .../SourceGeneratorTestHelpers.MSTest.csproj | 16 ++-- .../GeneratorDriverRunResultExtensions.cs | 65 +++------------- .../SourceGeneratorTestHelpers.NUnit.csproj | 18 +++-- .../GeneratorDriverRunResultExtensions.cs | 50 ++---------- .../SourceGeneratorTestHelpers.XUnit.csproj | 16 ++-- src/SourceGeneratorTestHelpers/Common/Diff.cs | 24 +----- .../Common/Helpers.cs | 78 +++++++++++-------- src/SourceGeneratorTestHelpers/Extensions.cs | 10 --- .../GeneratedSource.cs | 6 +- .../IncrementalGenerator.cs | 62 +++++++-------- .../SourceGenerator.cs | 56 ++++++------- .../SourceGeneratorTestHelpers.csproj | 14 ++-- 14 files changed, 198 insertions(+), 321 deletions(-) diff --git a/README.md b/README.md index 59742a5..024e101 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,37 @@ Test helpers and extension methods to simplify testing of .NET source generators ## Testing a source generator ```csharp -var result = SourceGenerator.Run("your source"); +var (generator, result) = SourceGenerator.Run("your source"); +``` + +You can also run a generator against multiple source files by passing any `IEnumerable`. + +```csharp +var sources = new[] +{ + "namespace Tests; public partial class First;", + "namespace Tests; public partial class Second;" +}; + +var (generator, result) = SourceGenerator.Run(sources); ``` ## Testing an incremental source generator ```csharp -var result = IncrementalGenerator.Run("your source"); +var (generator, result) = IncrementalGenerator.Run("your source"); +``` + +The same multiple-source overload is available for incremental generators. + +```csharp +var sources = new[] +{ + "namespace Tests; public partial class First;", + "namespace Tests; public partial class Second;" +}; + +var (generator, result) = IncrementalGenerator.Run(sources); ``` ## Obtaining the generated source @@ -50,7 +74,7 @@ Using one of the testing framework packages below, you can also assert the diffe [![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.MSTest?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest) ```csharp -var result = IncrementalGenerator.Run("your source"); +var (_, result) = IncrementalGenerator.Run("your source"); result.ShouldProduce("TestId.g.cs", "expected source"); ``` @@ -58,7 +82,7 @@ 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"); +var (_, result) = IncrementalGenerator.Run("your source"); result.ShouldProduce("TestId.g.cs", "expected source", false); ``` @@ -75,7 +99,7 @@ public class SourceGeneratorTests [Fact] public Task ShouldProductTestId() { - var result = IncrementalGenerator.Run("your source"); + var (_, result) = IncrementalGenerator.Run("your source"); return result.VerifyAsync("TestId.g.cs"); } } @@ -90,7 +114,7 @@ public class SourceGeneratorTests [Test] public Task ShouldProductTestId() { - var result = IncrementalGenerator.Run("your source"); + var (_, result) = IncrementalGenerator.Run("your source"); return result.VerifyAsync("TestId.g.cs"); } } @@ -106,8 +130,8 @@ public class SourceGeneratorTests : [TestMethod] public Task ShouldProductTestId() { - var result = IncrementalGenerator.Run("your source"); - return VerifyAsync("TestId.g.cs"); + var (_, result) = IncrementalGenerator.Run("your source"); + return VerifyAsync(result, "TestId.g.cs"); } } ``` diff --git a/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs index b8ba9a5..57c4d41 100644 --- a/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs +++ b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs @@ -5,25 +5,16 @@ namespace SourceGeneratorTestHelpers.MSTest; -internal abstract class GeneratorDriverTestBase : VerifyBase +public abstract class GeneratorDriverTestBase : VerifyBase { /// 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. /// If is null. - public static void ShouldProduce( - GeneratorDriverRunResult result, - string filePathEndsWith, - string expectedSource - ) + public static void ShouldProduce(GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif result.InternalShouldProduce(filePathEndsWith, expectedSource, false, false, _ => { }); } @@ -43,39 +34,21 @@ public static void ShouldProduce( bool assertOnWarnings = false ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertFailedException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertFailedException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message), "There were errors in the output generated by the source generator." ); - result.Result.InternalShouldProduce( - filePathEndsWith, - expectedSource, - assertOnErrors, - assertOnWarnings, + result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message) ); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// The verify settings. @@ -88,12 +61,7 @@ public SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif var generatedSource = result.InternalGetSource(filePathEndsWith); var source = generatedSource.HasValue ? generatedSource.Value.Source : ""; @@ -102,10 +70,7 @@ public SettingsTask VerifyAsync( return Verify(source, "txt", verifySettings, sourceFile); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// to assert on reported errors, othwerwise. Defaults to . @@ -122,23 +87,12 @@ public SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertFailedException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertFailedException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message), "There were errors in the output generated by the source generator." ); diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj index 9e180c3..782b27e 100644 --- a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj +++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj @@ -1,12 +1,12 @@ - netstandard2.0;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.MSTest latest - 10.0.1 + 10.1.0 SourceGeneratorTestHelpers.MSTest Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators mstest - 10.0.1.0 - 10.0.1.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg @@ -33,13 +33,13 @@ - + - + + - True \ @@ -51,8 +51,10 @@ False + true true + diff --git a/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs b/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs index 26099f9..85b9f3c 100644 --- a/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs +++ b/src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs @@ -13,18 +13,9 @@ public static class GeneratorDriverRunResultExtensions /// The string that the generated source's file path should end with. /// The expected source that the generated source should match. /// If is null. - public static void ShouldProduce( - this GeneratorDriverRunResult result, - string filePathEndsWith, - string expectedSource - ) + public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif result.InternalShouldProduce(filePathEndsWith, expectedSource, false, false, _ => { }); } @@ -44,39 +35,20 @@ public static void ShouldProduce( bool assertOnWarnings = false ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertionException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertionException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertionException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertionException(message), "There were errors in the output generated by the source generator." ); - result.Result.InternalShouldProduce( - filePathEndsWith, - expectedSource, - assertOnErrors, - assertOnWarnings, - message => throw new AssertionException(message) + result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, assertOnWarnings, message => throw new AssertionException(message) ); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// The verify settings. @@ -89,24 +61,16 @@ public static SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif var generatedSource = result.InternalGetSource(filePathEndsWith); var source = generatedSource.HasValue ? generatedSource.Value.Source : ""; - + // ReSharper disable once ExplicitCallerInfoArgument return Verify(source, "txt", verifySettings, sourceFile); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// to assert on reported errors, othwerwise. Defaults to . @@ -123,23 +87,12 @@ public static SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertionException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertionException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new AssertionException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertionException(message), "There were errors in the output generated by the source generator." ); diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj index fd4c1c6..520f22e 100644 --- a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj +++ b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj @@ -1,12 +1,12 @@ - netstandard2.0;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.NUnit latest - 10.0.1 + 10.1.0 SourceGeneratorTestHelpers.NUnit Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators nunit - 10.0.1.0 - 10.0.1.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg @@ -33,13 +33,13 @@ - - - + + + + - True \ @@ -51,8 +51,10 @@ False + true true + diff --git a/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs b/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs index b0fdf08..1b97639 100644 --- a/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs +++ b/src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs @@ -16,12 +16,7 @@ public static class GeneratorDriverRunResultExtensions /// If is null. public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif result.InternalShouldProduce(filePathEndsWith, expectedSource, false, false, _ => { }); } @@ -41,33 +36,19 @@ public static void ShouldProduce( bool assertOnWarnings = false ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new XunitException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new XunitException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new XunitException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new XunitException(message), "There were errors in the output generated by the source generator." ); result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, assertOnWarnings, message => throw new XunitException(message)); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// The verify settings. @@ -80,12 +61,7 @@ public static SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif var generatedSource = result.InternalGetSource(filePathEndsWith); var source = generatedSource.HasValue ? generatedSource.Value.Source : ""; @@ -94,10 +70,7 @@ public static SettingsTask VerifyAsync( return Verify(source, "txt", verifySettings, sourceFile); } - /// - /// Verifies that the generated source from a with a specific file path using - /// . - /// + /// Verifies that the generated source from a with a specific file path using . /// The to get the source from. /// The string that the generated source's file path should end with. /// to assert on reported errors, othwerwise. Defaults to . @@ -114,23 +87,12 @@ public static SettingsTask VerifyAsync( [CallerFilePath] string sourceFile = "" ) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result.Result); -#else - if (result.Result is null) - throw new ArgumentNullException(nameof(result)); -#endif - result.CompilationDiagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new XunitException(message), + result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new XunitException(message), "There were errors in the compilation." ); - result.Result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - message => throw new XunitException(message), + result.Result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new XunitException(message), "There were errors in the output generated by the source generator." ); diff --git a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj index c1cf0dc..01df502 100644 --- a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj +++ b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj @@ -1,12 +1,12 @@ - netstandard2.0;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.XUnit latest - 10.0.1 + 10.1.0 SourceGeneratorTestHelpers.XUnit Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators xunit - 10.0.1.0 - 10.0.1.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg @@ -34,13 +34,13 @@ - - + + + - True \ @@ -52,8 +52,10 @@ False + true true + diff --git a/src/SourceGeneratorTestHelpers/Common/Diff.cs b/src/SourceGeneratorTestHelpers/Common/Diff.cs index 7b6698e..2820be7 100644 --- a/src/SourceGeneratorTestHelpers/Common/Diff.cs +++ b/src/SourceGeneratorTestHelpers/Common/Diff.cs @@ -39,21 +39,11 @@ public static (bool HasDifferences, string Differences) Compare( switch (line.Type) { case ChangeType.Inserted: - diff.AppendLine( -#if NET6_0_OR_GREATER - CultureInfo.InvariantCulture, -#endif - $"{prefix,leftMargin}+ {lineText}" - ); + diff.AppendLine(CultureInfo.InvariantCulture, $"{prefix,leftMargin}+ {lineText}"); break; case ChangeType.Deleted: - diff.AppendLine( -#if NET6_0_OR_GREATER - CultureInfo.InvariantCulture, -#endif - $"{prefix,leftMargin}- {lineText}" - ); + diff.AppendLine(CultureInfo.InvariantCulture, $"{prefix,leftMargin}- {lineText}"); break; } @@ -64,14 +54,6 @@ public static (bool HasDifferences, string Differences) Compare( private static string? NormalizeLineEndings(string? str, bool normalize) { - return normalize - ? str?.Replace( - "\r\n", - "\n" -#if NET6_0_OR_GREATER - , StringComparison.InvariantCulture -#endif - ) - : str; + return normalize ? str?.Replace("\r\n", "\n", StringComparison.InvariantCulture) : str; } } diff --git a/src/SourceGeneratorTestHelpers/Common/Helpers.cs b/src/SourceGeneratorTestHelpers/Common/Helpers.cs index 6c7fb55..ecd6a54 100644 --- a/src/SourceGeneratorTestHelpers/Common/Helpers.cs +++ b/src/SourceGeneratorTestHelpers/Common/Helpers.cs @@ -22,9 +22,10 @@ internal static (ImmutableArray CompilationDiagnostics, GeneratorDri CSharpCompilationOptions? cSharpCompilationOptions ) { - var sources = new[] { source }; + cSharpParseOptions ??= DefaultCSharpParseOptions; + var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(source, cSharpParseOptions, encoding: Encoding.UTF8) }; - return InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return InternalRunGenerator(generator, syntaxTrees, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); } internal static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator( @@ -35,23 +36,11 @@ internal static (ImmutableArray CompilationDiagnostics, GeneratorDri CSharpCompilationOptions? cSharpCompilationOptions ) { - var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, cSharpParseOptions, encoding: Encoding.UTF8)).ToArray(); cSharpParseOptions ??= DefaultCSharpParseOptions; - metadataReferences ??= DefaultMetadataReferences; - cSharpCompilationOptions ??= DefaultCSharpCompilationOptions; - - var compilation = CSharpCompilation.Create(nameof(SourceGeneratorTestHelpers), syntaxTrees, metadataReferences, cSharpCompilationOptions); - var generators = new[] { generator }; - var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions); - var updatedDriver = driver.RunGeneratorsAndUpdateCompilation( - compilation, - out var updatedCompilation, - out var generatorDiagnostics); - var compilationDiagnostics = updatedCompilation.GetDiagnostics(); - var allDiagnostics = compilationDiagnostics.AddRange(generatorDiagnostics); - var runResult = updatedDriver.GetRunResult(); + var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, cSharpParseOptions, encoding: Encoding.UTF8)) + .ToArray(); - return (allDiagnostics, runResult); + return InternalRunGenerator(generator, syntaxTrees, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); } internal static void InternalAssertOnDiagnostics( @@ -62,11 +51,13 @@ internal static void InternalAssertOnDiagnostics( string reason ) { - var errors = diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToList(); + var errors = diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error) + .ToList(); if (assertOnErrors && errors.Count > 0) assertAction(errors.ToMessage(reason)); - var warnings = diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Warning).ToList(); + var warnings = diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Warning) + .ToList(); if (assertOnWarnings && warnings.Count > 0) assertAction(warnings.ToMessage(reason)); } @@ -80,14 +71,11 @@ internal static void InternalShouldProduce( Action assertAction ) { - result.Diagnostics.InternalAssertOnDiagnostics( - assertOnErrors, - assertOnWarnings, - assertAction, + result.Diagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, assertAction, "There were errors in the output generated by the source generator." ); - var generatedSource = InternalGetSource(result, filePathEndsWith); + var generatedSource = result.InternalGetSource(filePathEndsWith); // ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract var (hasDifferences, differences) = Diff.Compare(expectedSource ?? "", generatedSource?.Source ?? ""); @@ -97,16 +85,43 @@ Action assertAction internal static GeneratedSource? InternalGetSource(this GeneratorDriverRunResult result, string filePathEndsWith) { - var sources = InternalGetSources(result); - - var matchingSources = sources.Where(x => x.FilePath.EndsWith(filePathEndsWith, StringComparison.InvariantCultureIgnoreCase)).ToList(); - - return matchingSources.Count == 0 ? null : matchingSources[0]; + foreach (var generatedTree in result.GeneratedTrees) + { + var source = GetGeneratedSource(generatedTree); + if (source.FilePath.EndsWith(filePathEndsWith, StringComparison.InvariantCultureIgnoreCase)) + return source; + } + + return null; } internal static ImmutableList InternalGetSources(this GeneratorDriverRunResult result) { - return result.GeneratedTrees.Select(GetGeneratedSource).ToImmutableList(); + return result.GeneratedTrees + .Select(GetGeneratedSource) + .ToImmutableList(); + } + + private static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator( + ISourceGenerator generator, + SyntaxTree[] syntaxTrees, + CSharpParseOptions cSharpParseOptions, + IEnumerable? metadataReferences, + CSharpCompilationOptions? cSharpCompilationOptions + ) + { + metadataReferences ??= DefaultMetadataReferences; + cSharpCompilationOptions ??= DefaultCSharpCompilationOptions; + + var compilation = CSharpCompilation.Create(nameof(SourceGeneratorTestHelpers), syntaxTrees, metadataReferences, cSharpCompilationOptions); + var generators = new[] { generator }; + var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions); + var updatedDriver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation, out var generatorDiagnostics); + var compilationDiagnostics = updatedCompilation.GetDiagnostics(); + var allDiagnostics = compilationDiagnostics.AddRange(generatorDiagnostics); + var runResult = updatedDriver.GetRunResult(); + + return (allDiagnostics, runResult); } private static string ToMessage(this IEnumerable diagnostics, string reason) @@ -121,7 +136,8 @@ private static string ToMessage(this IEnumerable diagnostics, string private static GeneratedSource GetGeneratedSource(SyntaxTree tree) { var filePath = tree.FilePath; - var source = tree.GetText().ToString(); + var source = tree.GetText() + .ToString(); return new GeneratedSource(filePath, source); } diff --git a/src/SourceGeneratorTestHelpers/Extensions.cs b/src/SourceGeneratorTestHelpers/Extensions.cs index a163863..ede7395 100644 --- a/src/SourceGeneratorTestHelpers/Extensions.cs +++ b/src/SourceGeneratorTestHelpers/Extensions.cs @@ -13,12 +13,7 @@ public static class Extensions /// If is null. public static GeneratedSource? GetSource(this GeneratorDriverRunResult result, string filePathEndsWith) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif return result.InternalGetSource(filePathEndsWith); } @@ -29,12 +24,7 @@ public static class Extensions /// If is null. public static IReadOnlyCollection GetSources(this GeneratorDriverRunResult result) { -#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(result); -#else - if (result is null) - throw new ArgumentNullException(nameof(result)); -#endif return result.InternalGetSources(); } diff --git a/src/SourceGeneratorTestHelpers/GeneratedSource.cs b/src/SourceGeneratorTestHelpers/GeneratedSource.cs index 4c650f7..e10864b 100644 --- a/src/SourceGeneratorTestHelpers/GeneratedSource.cs +++ b/src/SourceGeneratorTestHelpers/GeneratedSource.cs @@ -30,11 +30,7 @@ public override bool Equals(object? obj) /// public override int GetHashCode() { - return FilePath.GetHashCode( - #if NET6_0_OR_GREATER - StringComparison.Ordinal -#endif - ); + return FilePath.GetHashCode(StringComparison.Ordinal); } /// Determines if two instances are equal. diff --git a/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs b/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs index 67bf779..c9cb1ae 100644 --- a/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs +++ b/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs @@ -13,12 +13,9 @@ public static class IncrementalGenerator /// The source to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The results of the execution. - public static GeneratorDriverRunResult Run( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run and the result of the execution. + public static (T Generator, GeneratorDriverRunResult Result) Run( string source, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -26,9 +23,12 @@ public static GeneratorDriverRunResult Run( ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + var generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); + var result = Helpers.InternalRunGenerator(sourceGenerator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions) + .Result; - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return (generator, result); } /// Gather compilation diagnostics and executes a specified against the provided sources within a testing environment. @@ -36,12 +36,9 @@ public static GeneratorDriverRunResult Run( /// The source to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The compilation diagnostics and the result of the execution. - public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run, the result of the execution, and the compilation diagnostics. + public static (T Generator, GeneratorDriverRunResult Result, ImmutableArray Diagnostics) RunWithDiagnostics( string source, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -49,9 +46,11 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + var generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); + var result = Helpers.InternalRunGenerator(sourceGenerator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return (generator, result.Result, result.CompilationDiagnostics); } /// Executes a specified against the provided sources within a testing environment. @@ -59,12 +58,9 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult /// The sources to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The result of the execution. - public static GeneratorDriverRunResult Run( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run and the result of the execution. + public static (T Generator, GeneratorDriverRunResult Result) Run( IEnumerable sources, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -72,9 +68,12 @@ public static GeneratorDriverRunResult Run( ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + var generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); + var result = Helpers.InternalRunGenerator(sourceGenerator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions) + .Result; - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return (generator, result); } /// Gather compilation diagnostics and executes a specified against the provided sources within a testing environment. @@ -82,12 +81,9 @@ public static GeneratorDriverRunResult Run( /// The sources to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The compilation diagnostics and the result of the execution. - public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run, the result of the execution, and the compilation diagnostics. + public static (T Generator, GeneratorDriverRunResult Result, ImmutableArray Diagnostics) RunWithDiagnostics( IEnumerable sources, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -95,8 +91,10 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + var generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); + var result = Helpers.InternalRunGenerator(sourceGenerator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return (generator, result.Result, result.CompilationDiagnostics); } } diff --git a/src/SourceGeneratorTestHelpers/SourceGenerator.cs b/src/SourceGeneratorTestHelpers/SourceGenerator.cs index 66f622d..e149682 100644 --- a/src/SourceGeneratorTestHelpers/SourceGenerator.cs +++ b/src/SourceGeneratorTestHelpers/SourceGenerator.cs @@ -1,4 +1,4 @@ -using System.Collections.Immutable; +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using SourceGeneratorTestHelpers.Common; @@ -13,12 +13,9 @@ public static class SourceGenerator /// The source to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The results of the execution. - public static GeneratorDriverRunResult Run( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run and the result of the execution. + public static (T Generator, GeneratorDriverRunResult Result) Run( string source, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -27,8 +24,10 @@ public static GeneratorDriverRunResult Run( where T : ISourceGenerator, new() { var generator = new T(); + var result = Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions) + .Result; - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return (generator, result); } /// Gather compilation diagnostics and executes a specified against the provided source within a testing environment. @@ -36,12 +35,9 @@ public static GeneratorDriverRunResult Run( /// The source to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The compilation diagnostics and the result of the execution. - public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run, the result of the execution, and the compilation diagnostics. + public static (T Generator, GeneratorDriverRunResult Result, ImmutableArray Diagnostics) RunWithDiagnostics( string source, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -50,21 +46,19 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult where T : ISourceGenerator, new() { var generator = new T(); + var result = Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return (generator, result.Result, result.CompilationDiagnostics); } - /// Executes a specified against the provided source within a testing environment. + /// Executes a specified against the provided sources within a testing environment. /// The type of to execute. /// The sources to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The result of the execution. - public static GeneratorDriverRunResult Run( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run and the result of the execution. + public static (T Generator, GeneratorDriverRunResult Result) Run( IEnumerable sources, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -73,21 +67,20 @@ public static GeneratorDriverRunResult Run( where T : ISourceGenerator, new() { var generator = new T(); + var result = Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions) + .Result; - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return (generator, result); } - /// Gather compilation diagnostics and executes a specified against the provided source within a testing environment. + /// Gather compilation diagnostics and executes a specified against the provided sources within a testing environment. /// The type of to execute. /// The sources to be analyzed and processed by the . /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. - /// - /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to - /// enable. - /// - /// The compilation diagnostics and the result of the execution. - public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( + /// The C# compilation options to compile with. By default, Output will be set to library, and Nullable will be set to enable. + /// The generator instance used for the run, the result of the execution, and the compilation diagnostics. + public static (T Generator, GeneratorDriverRunResult Result, ImmutableArray Diagnostics) RunWithDiagnostics( IEnumerable sources, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, @@ -96,7 +89,8 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult where T : ISourceGenerator, new() { var generator = new T(); + var result = Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return (generator, result.Result, result.CompilationDiagnostics); } } diff --git a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj index dbed528..4261bcc 100644 --- a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj +++ b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj @@ -1,12 +1,12 @@ - netstandard2.0;net6.0;net7.0;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 enable enable SourceGeneratorTestHelpers latest - 10.0.1 + 10.1.0 SourceGeneratorTestHelpers Jean-Sebastien Carle Test helpers and extension methods to simplify testing of .NET source generators. @@ -18,8 +18,8 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators - 10.0.1.0 - 10.0.1.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg @@ -38,13 +38,13 @@ - + + - True \ @@ -56,8 +56,10 @@ False + true true +