diff --git a/README.md b/README.md index 5224077..44e587f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ Test helpers and extension methods to simplify testing of .NET source generators [![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) +# More languages +[Chinese](./doc/zh-Hans_README.md) + ## Testing a source generator ```csharp diff --git a/doc/zh-Hans_README.md b/doc/zh-Hans_README.md new file mode 100644 index 0000000..14bb69d --- /dev/null +++ b/doc/zh-Hans_README.md @@ -0,0 +1,137 @@ +# Source Generator Test Helpers(源生成器测试辅助工具) + +用于简化 .NET 源生成器测试的测试辅助方法和扩展方法。 + +[![main](https://img.shields.io/github/actions/workflow/status/jscarle/SourceGeneratorTestHelpers/main.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) + +## 测试一个源生成器 + +```csharp +var result = SourceGenerator.Run("要测试的代码"); +``` + +## 测试一个增量源生成器 + +```csharp +var result = IncrementalGenerator.Run("要测试的代码"); +``` + +## 添加.NET环境和依赖 + +```csharp + var references = new List(); + + // 添加 .NET 10 所有程序集(ReferenceAssemblies.Net.Net100) + ImmutableArray defaultReferences = ReferenceAssemblies.Net.Net100.ResolveAsync(null, default)。Result; + references.AddRange(defaultReferences); + + //添加依赖 + references.Add(MetadataReference.CreateFromFile( + Path.Combine("D:","C#", "Lib", "Debug", "net10.0", "XXX.dll"))); + + var result = IncrementalGenerator.Run("要测试的代码", null, references); +``` + +## 获取创建的源生成器/增量源生成器 +```csharp + var result = IncrementalGenerator.Run(sourceCode, out YourSourceGenerator generator, null, references); +``` +```csharp + var result = SourceGenerator.Run(sourceCode, out YourSourceGenerator generator, null, references); +``` +## 获取生成的源代码 + +### 获取所有生成的源代码 + +```csharp +var generatedSources = result.GetSources(); +``` + +### 获取特定文件的源代码 + +```csharp +var generatedSource = result.GetSource("TestId.g.cs"); +``` + +### 对比生成的源代码与期望的源代码 + +你可以对比生成的源代码与期望的源代码之间的差异。结果包含一个bool值 hasDifferences 以及按行区分的差异列表 differences。 + +```csharp +var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource); +``` + +## 进行验证 + +### 使用断言 + +借助下面其中一个测试框架包,你还可以断言生成的源代码与期望源的代码之间的差异。 + +[![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.NUnit?label=NUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit) +[![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.MSTest?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest) + +```csharp +var result = IncrementalGenerator.Run("要测试的代码"); + +result.ShouldProduce("TestId.g.cs", "预期的代码"); +``` + +_注意:如果你不想断言源生成器运行期间的诊断错误,可以像下面这样直接禁用。_ + +```csharp +var result = IncrementalGenerator.Run("要测试的代码"); + +result.ShouldProduce("TestId.g.cs", "预期的代码", false); +``` + +### 使用Verify + +内置了对 [Verify](https://github.com/VerifyTests/Verify) 的支持,通过 VerifyAsync 方法实现。 + +#### XUnit + +```cs +public class SourceGeneratorTests +{ + [Fact] + public Task ShouldProductTestId() + { + var result = IncrementalGenerator.Run("要测试的代码"); + return result.VerifyAsync("TestId.g.cs"); + } +} +``` + +#### NUnit + +```cs +[TestFixture] +public class SourceGeneratorTests +{ + [Test] + public Task ShouldProductTestId() + { + var result = IncrementalGenerator.Run("要测试的代码"); + return result.VerifyAsync("TestId.g.cs"); + } +} +``` + +#### MSTest + +```cs +[TestClass] +public class SourceGeneratorTests : + GeneratorDriverTestBase +{ + [TestMethod] + public Task ShouldProductTestId() + { + var result = IncrementalGenerator.Run("要测试的代码"); + return VerifyAsync("TestId.g.cs"); + } +} +``` diff --git a/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs index b8ba9a5..006f8e1 100644 --- a/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs +++ b/src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs @@ -1,11 +1,11 @@ -using System.Collections.Immutable; -using System.Runtime.CompilerServices; using Microsoft.CodeAnalysis; using SourceGeneratorTestHelpers.Common; +using System.Collections.Immutable; +using System.Runtime.CompilerServices; 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. diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj index b9ad3f1..f7dadb0 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;netstandard2.1;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.MSTest latest - 10.0.0 + 10.1.0 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 2026 SourceGeneratorTestHelpers.MSTest https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,27 +18,25 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators mstest - 10.0.0.0 - 10.0.0.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg latest-All true - true - snupkg true - + - + True \ diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj index a376d8b..2dc6687 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;netstandard2.1;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.NUnit latest - 10.0.0 + 10.1.0 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 2026 SourceGeneratorTestHelpers.NUnit https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,15 +18,13 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators nunit - 10.0.0.0 - 10.0.0.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg latest-All true - true - snupkg true @@ -38,7 +36,7 @@ - + True \ diff --git a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj index 4bc1738..89fb9f2 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;netstandard2.1;net8.0;net9.0;net10.0 true enable SourceGeneratorTestHelpers.XUnit latest - 10.0.0 + 10.1.0 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 2026 SourceGeneratorTestHelpers.XUnit https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,15 +18,13 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators xunit - 10.0.0.0 - 10.0.0.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg latest-All true - true - snupkg true $(NoWarn);NU1701 diff --git a/src/SourceGeneratorTestHelpers/Common/Diff.cs b/src/SourceGeneratorTestHelpers/Common/Diff.cs index 7b6698e..7d993ca 100644 --- a/src/SourceGeneratorTestHelpers/Common/Diff.cs +++ b/src/SourceGeneratorTestHelpers/Common/Diff.cs @@ -1,8 +1,8 @@ -using System.Globalization; -using System.Text; using DiffPlex; using DiffPlex.DiffBuilder; using DiffPlex.DiffBuilder.Model; +using System.Globalization; +using System.Text; namespace SourceGeneratorTestHelpers.Common; @@ -65,10 +65,11 @@ public static (bool HasDifferences, string Differences) Compare( private static string? NormalizeLineEndings(string? str, bool normalize) { return normalize - ? str?.Replace( + ? + str?.Replace( "\r\n", "\n" -#if NET6_0_OR_GREATER +#if !NETSTANDARD2_0 , StringComparison.InvariantCulture #endif ) diff --git a/src/SourceGeneratorTestHelpers/Common/Helpers.cs b/src/SourceGeneratorTestHelpers/Common/Helpers.cs index 6c7fb55..8d588b7 100644 --- a/src/SourceGeneratorTestHelpers/Common/Helpers.cs +++ b/src/SourceGeneratorTestHelpers/Common/Helpers.cs @@ -1,8 +1,8 @@ -using System.Collections.Immutable; -using System.Text; using Basic.Reference.Assemblies; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; +using System.Collections.Immutable; +using System.Text; namespace SourceGeneratorTestHelpers.Common; @@ -14,19 +14,6 @@ internal static class Helpers internal static CSharpCompilationOptions DefaultCSharpCompilationOptions { get; } = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithNullableContextOptions(NullableContextOptions.Enable); - internal static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator( - ISourceGenerator generator, - string source, - CSharpParseOptions? cSharpParseOptions, - IEnumerable? metadataReferences, - CSharpCompilationOptions? cSharpCompilationOptions - ) - { - var sources = new[] { source }; - - return InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - } - internal static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator( ISourceGenerator generator, IEnumerable sources, @@ -105,9 +92,7 @@ Action assertAction } internal static ImmutableList InternalGetSources(this GeneratorDriverRunResult result) - { - return result.GeneratedTrees.Select(GetGeneratedSource).ToImmutableList(); - } + => [.. result.GeneratedTrees.Select(GetGeneratedSource)]; private static string ToMessage(this IEnumerable diagnostics, string reason) { diff --git a/src/SourceGeneratorTestHelpers/GeneratedSource.cs b/src/SourceGeneratorTestHelpers/GeneratedSource.cs index 4c650f7..4c1b34b 100644 --- a/src/SourceGeneratorTestHelpers/GeneratedSource.cs +++ b/src/SourceGeneratorTestHelpers/GeneratedSource.cs @@ -1,4 +1,4 @@ -namespace SourceGeneratorTestHelpers; +namespace SourceGeneratorTestHelpers; /// Represents a generated source file. public readonly struct GeneratedSource : IEquatable @@ -31,7 +31,7 @@ public override bool Equals(object? obj) public override int GetHashCode() { return FilePath.GetHashCode( - #if NET6_0_OR_GREATER +#if !NETSTANDARD2_0 StringComparison.Ordinal #endif ); diff --git a/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs b/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs index 67bf779..600fb47 100644 --- a/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs +++ b/src/SourceGeneratorTestHelpers/IncrementalGenerator.cs @@ -1,7 +1,7 @@ -using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using SourceGeneratorTestHelpers.Common; +using System.Collections.Immutable; namespace SourceGeneratorTestHelpers; @@ -25,11 +25,7 @@ public static GeneratorDriverRunResult Run( CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : IIncrementalGenerator, new() - { - var generator = new T().AsSourceGenerator(); - - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; - } + => Run(source, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); /// Gather compilation diagnostics and executes a specified against the provided sources within a testing environment. /// The type of to execute. @@ -48,15 +44,91 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : IIncrementalGenerator, new() - { - var generator = new T().AsSourceGenerator(); + => RunWithDiagnostics(source, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + /// 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( + IEnumerable sources, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : IIncrementalGenerator, new() + => Run(sources, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); - } + /// 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( + IEnumerable sources, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : IIncrementalGenerator, new() + => RunWithDiagnostics(sources, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + /// Executes a specified against the provided source within a testing environment. + /// The type of to execute. + /// The source to be analyzed and processed by the . + /// The generated instance. + /// 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( + string source, + out T generator, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : IIncrementalGenerator, new() + => Run([source], out generator, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + /// Gather compilation diagnostics and executes a specified against the provided sources within a testing environment. + /// The type of to execute. + /// The source to be analyzed and processed by the . + /// The generated instance. + /// 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( + string source, + out T generator, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : IIncrementalGenerator, new() + => RunWithDiagnostics([source], out generator, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); /// 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 generated instance. /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. /// @@ -66,20 +138,23 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult /// The result of the execution. public static GeneratorDriverRunResult Run( IEnumerable sources, + out T generator, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return Helpers.InternalRunGenerator(sourceGenerator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; } /// 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 generated instance. /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. /// @@ -89,14 +164,16 @@ public static GeneratorDriverRunResult Run( /// The compilation diagnostics and the result of the execution. public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( IEnumerable sources, + out T generator, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : IIncrementalGenerator, new() { - var generator = new T().AsSourceGenerator(); + generator = new T(); + var sourceGenerator = generator.AsSourceGenerator(); - return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return Helpers.InternalRunGenerator(sourceGenerator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); } } diff --git a/src/SourceGeneratorTestHelpers/SourceGenerator.cs b/src/SourceGeneratorTestHelpers/SourceGenerator.cs index 66f622d..f592299 100644 --- a/src/SourceGeneratorTestHelpers/SourceGenerator.cs +++ b/src/SourceGeneratorTestHelpers/SourceGenerator.cs @@ -1,7 +1,7 @@ -using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using SourceGeneratorTestHelpers.Common; +using System.Collections.Immutable; namespace SourceGeneratorTestHelpers; @@ -25,15 +25,96 @@ public static GeneratorDriverRunResult Run( CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : ISourceGenerator, new() + => Run([source], out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + + /// Gather compilation diagnostics and executes a specified against the provided source within a testing environment. + /// The type of to execute. + /// 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( + string source, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : ISourceGenerator, new() + => RunWithDiagnostics(source, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + /// Executes a specified against the provided source 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( + IEnumerable sources, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : ISourceGenerator, new() + => Run(sources, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + /// Gather compilation diagnostics and executes a specified against the provided source 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( + IEnumerable sources, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : ISourceGenerator, new() + => RunWithDiagnostics(sources, out _, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + + + /// Executes a specified against the provided source within a testing environment. + /// The type of to execute. + /// The source to be analyzed and processed by the . + /// The generated instance. + /// 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( + string source, + out T generator, + CSharpParseOptions? cSharpParseOptions = null, + IEnumerable? metadataReferences = null, + CSharpCompilationOptions? cSharpCompilationOptions = null + ) + where T : ISourceGenerator, new() { - var generator = new T(); + generator = new T(); - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; + return Helpers.InternalRunGenerator(generator, [source], cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; } /// Gather compilation diagnostics and executes a specified against the provided source within a testing environment. /// The type of to execute. /// The source to be analyzed and processed by the . + /// The generated instance. /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. /// @@ -43,20 +124,22 @@ public static GeneratorDriverRunResult Run( /// The compilation diagnostics and the result of the execution. public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( string source, + out T generator, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : ISourceGenerator, new() { - var generator = new T(); + generator = new T(); - return Helpers.InternalRunGenerator(generator, source, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); + return Helpers.InternalRunGenerator(generator, [source], cSharpParseOptions, metadataReferences, cSharpCompilationOptions); } /// Executes a specified against the provided source within a testing environment. /// The type of to execute. /// The sources to be analyzed and processed by the . + /// The generated instance. /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. /// @@ -66,13 +149,14 @@ public static (ImmutableArray Diagnostics, GeneratorDriverRunResult /// The result of the execution. public static GeneratorDriverRunResult Run( IEnumerable sources, + out T generator, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : ISourceGenerator, new() { - var generator = new T(); + generator = new T(); return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions).Result; } @@ -80,6 +164,7 @@ public static GeneratorDriverRunResult Run( /// Gather compilation diagnostics and executes a specified against the provided source within a testing environment. /// The type of to execute. /// The sources to be analyzed and processed by the . + /// The generated instance. /// The C# source parsing options to compile with. By default, LangVersion will be set to latest. /// The metadata references to compile with. /// @@ -89,13 +174,14 @@ public static GeneratorDriverRunResult Run( /// The compilation diagnostics and the result of the execution. public static (ImmutableArray Diagnostics, GeneratorDriverRunResult Result) RunWithDiagnostics( IEnumerable sources, + out T generator, CSharpParseOptions? cSharpParseOptions = null, IEnumerable? metadataReferences = null, CSharpCompilationOptions? cSharpCompilationOptions = null ) where T : ISourceGenerator, new() { - var generator = new T(); + generator = new T(); return Helpers.InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions); } diff --git a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj index ce518bf..138d3c5 100644 --- a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj +++ b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj @@ -1,16 +1,16 @@  - netstandard2.0;net6.0;net7.0;net8.0;net9.0;net10.0 + netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0 enable enable SourceGeneratorTestHelpers latest - 10.0.0 + 10.1.0 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 2026 SourceGeneratorTestHelpers https://github.com/jscarle/SourceGeneratorTestHelpers LICENSE.md @@ -18,15 +18,13 @@ https://github.com/jscarle/SourceGeneratorTestHelpers git testing source-generators - 10.0.0.0 - 10.0.0.0 + 10.1.0.0 + 10.1.0.0 en-US true snupkg latest-All true - true - snupkg true