From 056e8fd44b37f993b092f2548740977396a2612d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Fri, 29 May 2026 13:53:42 +0800
Subject: [PATCH 1/9] public GeneratorDriverTestBase
---
.../GeneratorDriverTestBase.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
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.
From 7cb70d8e8ca6c33b60a379680c0564095e932438 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Fri, 29 May 2026 13:58:32 +0800
Subject: [PATCH 2/9] Remove duplicates + update version and copyright
---
.../SourceGeneratorTestHelpers.MSTest.csproj | 10 ++++------
.../SourceGeneratorTestHelpers.NUnit.csproj | 12 +++++-------
.../SourceGeneratorTestHelpers.XUnit.csproj | 10 ++++------
.../SourceGeneratorTestHelpers.csproj | 10 ++++------
4 files changed, 17 insertions(+), 25 deletions(-)
diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
index b9ad3f1..457e0ad 100644
--- a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
+++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
@@ -6,11 +6,11 @@
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,15 +18,13 @@
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
diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
index a376d8b..6c2dbc8 100644
--- a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
+++ b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
@@ -6,11 +6,11 @@
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..7a694e3 100644
--- a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
+++ b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
@@ -6,11 +6,11 @@
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/SourceGeneratorTestHelpers.csproj b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
index ce518bf..bfd85b6 100644
--- a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
+++ b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
@@ -6,11 +6,11 @@
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
From 9605c6a5ac2790064f87f2e877d888c22967ccd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 11:04:27 +0800
Subject: [PATCH 3/9] Add features + Upgrade dependencies + Simplify code
---
.../SourceGeneratorTestHelpers.MSTest.csproj | 4 +-
.../Common/Helpers.cs | 10 +-
.../IncrementalGenerator.cs | 105 +++++++++++++++---
3 files changed, 96 insertions(+), 23 deletions(-)
diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
index 457e0ad..3415e03 100644
--- a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
+++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
@@ -30,13 +30,13 @@
-
+
-
+
True
\
diff --git a/src/SourceGeneratorTestHelpers/Common/Helpers.cs b/src/SourceGeneratorTestHelpers/Common/Helpers.cs
index 6c7fb55..0771375 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;
@@ -21,11 +21,7 @@ internal static (ImmutableArray CompilationDiagnostics, GeneratorDri
IEnumerable? metadataReferences,
CSharpCompilationOptions? cSharpCompilationOptions
)
- {
- var sources = new[] { source };
-
- return InternalRunGenerator(generator, sources, cSharpParseOptions, metadataReferences, cSharpCompilationOptions);
- }
+ => InternalRunGenerator(generator, [source], cSharpParseOptions, metadataReferences, cSharpCompilationOptions);
internal static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator(
ISourceGenerator generator,
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);
}
}
From 78c62d5accb7de638096563cc1c0aa41730df13b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 11:32:40 +0800
Subject: [PATCH 4/9] Add Chinese (It differs from the English version.)
---
README.md | 3 ++
doc/zh-Hans_README.md | 114 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 117 insertions(+)
create mode 100644 doc/zh-Hans_README.md
diff --git a/README.md b/README.md
index 5224077..e6cf425 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,9 @@
Test helpers and extension methods to simplify testing of .NET source generators.
+# More languages
+ [Chinese](./doc/zh-Hans_README.md)
+
[](https://github.com/jscarle/SourceGeneratorTestHelpers)
[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
diff --git a/doc/zh-Hans_README.md b/doc/zh-Hans_README.md
new file mode 100644
index 0000000..3f3d534
--- /dev/null
+++ b/doc/zh-Hans_README.md
@@ -0,0 +1,114 @@
+# Source Generator Test Helpers(源生成器测试辅助工具)
+
+用于简化 .NET 源生成器测试的测试辅助方法和扩展方法。
+
+[](https://github.com/jscarle/SourceGeneratorTestHelpers)
+[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
+[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
+
+## 测试一个源生成器
+
+```csharp
+var result = SourceGenerator.Run("要测试的代码");
+```
+
+## 测试一个增量源生成器
+
+```csharp
+var result = IncrementalGenerator.Run("要测试的代码");
+```
+
+## 获取生成的源代码
+
+### 获取所有生成的源代码
+
+```csharp
+var generatedSources = result.GetSources();
+```
+
+### 获取特定文件的源代码
+
+```csharp
+var generatedSource = result.GetSource("TestId.g.cs");
+```
+
+### 对比生成的源代码与期望的源代码
+
+你可以对比生成的源代码与期望的源代码之间的差异。结果包含一个bool值 hasDifferences 以及按行区分的差异列表 differences。
+
+```csharp
+var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource);
+```
+
+## 进行验证
+
+### 使用断言
+
+借助下面其中一个测试框架包,你还可以断言生成的源代码与期望源的代码之间的差异。
+
+[](https://www.nuget.org/packages/SourceGeneratorTestHelpers.XUnit)
+[](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit)
+[](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");
+ }
+}
+```
From eb4f372b97a6d1437d83a780f8d3c26e893e3190 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 14:04:22 +0800
Subject: [PATCH 5/9] Remove a Helpers.InternalRunGenerator+ synchronization
features
---
.../Common/Helpers.cs | 13 +--
.../SourceGenerator.cs | 100 ++++++++++++++++--
2 files changed, 94 insertions(+), 19 deletions(-)
diff --git a/src/SourceGeneratorTestHelpers/Common/Helpers.cs b/src/SourceGeneratorTestHelpers/Common/Helpers.cs
index 0771375..8d588b7 100644
--- a/src/SourceGeneratorTestHelpers/Common/Helpers.cs
+++ b/src/SourceGeneratorTestHelpers/Common/Helpers.cs
@@ -14,15 +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
- )
- => InternalRunGenerator(generator, [source], cSharpParseOptions, metadataReferences, cSharpCompilationOptions);
-
internal static (ImmutableArray CompilationDiagnostics, GeneratorDriverRunResult Result) InternalRunGenerator(
ISourceGenerator generator,
IEnumerable sources,
@@ -101,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/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);
}
From aa1c7eda6f1d016db51ae4675ccb8c57f5f5e0fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 14:47:26 +0800
Subject: [PATCH 6/9] Remove net6 and net7 + Add netstandard2.1
---
.../SourceGeneratorTestHelpers.MSTest.csproj | 2 +-
.../SourceGeneratorTestHelpers.NUnit.csproj | 2 +-
.../SourceGeneratorTestHelpers.XUnit.csproj | 2 +-
src/SourceGeneratorTestHelpers/Common/Diff.cs | 9 +++++----
src/SourceGeneratorTestHelpers/GeneratedSource.cs | 4 ++--
.../SourceGeneratorTestHelpers.csproj | 2 +-
6 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
index 3415e03..f7dadb0 100644
--- a/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
+++ b/src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
@@ -1,7 +1,7 @@
- netstandard2.0;net6.0;net7.0;net8.0
+ netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0
true
enable
SourceGeneratorTestHelpers.MSTest
diff --git a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
index 6c2dbc8..2dc6687 100644
--- a/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
+++ b/src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
@@ -1,7 +1,7 @@
- netstandard2.0;net6.0;net7.0;net8.0
+ netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0
true
enable
SourceGeneratorTestHelpers.NUnit
diff --git a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
index 7a694e3..89fb9f2 100644
--- a/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
+++ b/src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
@@ -1,7 +1,7 @@
- netstandard2.0;net6.0;net7.0;net8.0
+ netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0
true
enable
SourceGeneratorTestHelpers.XUnit
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/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/SourceGeneratorTestHelpers.csproj b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
index bfd85b6..138d3c5 100644
--- a/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
+++ b/src/SourceGeneratorTestHelpers/SourceGeneratorTestHelpers.csproj
@@ -1,7 +1,7 @@
- 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
From a4aea6205349db79774f282f1e75067f3ec4b715 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 15:06:58 +0800
Subject: [PATCH 7/9] Add zh-Hans_README (Add .NET environment and
dependencies) doc
---
doc/zh-Hans_README.md | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/doc/zh-Hans_README.md b/doc/zh-Hans_README.md
index 3f3d534..0d1a870 100644
--- a/doc/zh-Hans_README.md
+++ b/doc/zh-Hans_README.md
@@ -18,6 +18,22 @@ var result = SourceGenerator.Run("要测试的代码");
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);
+```
+
## 获取生成的源代码
### 获取所有生成的源代码
From 890109b55e625915460e0d5242a0d5455eaea6ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 15:17:10 +0800
Subject: [PATCH 8/9] Fix doc README + Add zh-Hans_README (add obtaining
created source generators / incremental source generators.) doc
---
README.md | 2 +-
doc/zh-Hans_README.md | 7 +++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index e6cf425..9dc570c 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
Test helpers and extension methods to simplify testing of .NET source generators.
# More languages
- [Chinese](./doc/zh-Hans_README.md)
+[Chinese](./doc/zh-Hans_README.md)
[](https://github.com/jscarle/SourceGeneratorTestHelpers)
[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
diff --git a/doc/zh-Hans_README.md b/doc/zh-Hans_README.md
index 0d1a870..14bb69d 100644
--- a/doc/zh-Hans_README.md
+++ b/doc/zh-Hans_README.md
@@ -34,6 +34,13 @@ var result = IncrementalGenerator.Run("要测试的代码")
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);
+```
## 获取生成的源代码
### 获取所有生成的源代码
From 8fd4af2f50a589ba50d4cbf1095a6ea6e841eea4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B0=B8=E5=AE=89404?=
<101850798+YongAn404@users.noreply.github.com>
Date: Wed, 3 Jun 2026 15:20:59 +0800
Subject: [PATCH 9/9] Adjust README layout
---
README.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 9dc570c..44e587f 100644
--- a/README.md
+++ b/README.md
@@ -2,13 +2,13 @@
Test helpers and extension methods to simplify testing of .NET source generators.
-# More languages
-[Chinese](./doc/zh-Hans_README.md)
-
[](https://github.com/jscarle/SourceGeneratorTestHelpers)
[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
[](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
+# More languages
+[Chinese](./doc/zh-Hans_README.md)
+
## Testing a source generator
```csharp