Sparkdo.SourceGenerationTesting 2.0.0-preview.1

This is a prerelease version of Sparkdo.SourceGenerationTesting.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Sparkdo.SourceGenerationTesting --version 2.0.0-preview.1
                    
NuGet\Install-Package Sparkdo.SourceGenerationTesting -Version 2.0.0-preview.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sparkdo.SourceGenerationTesting" Version="2.0.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.SourceGenerationTesting" Version="2.0.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.SourceGenerationTesting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sparkdo.SourceGenerationTesting --version 2.0.0-preview.1
                    
#r "nuget: Sparkdo.SourceGenerationTesting, 2.0.0-preview.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sparkdo.SourceGenerationTesting@2.0.0-preview.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sparkdo.SourceGenerationTesting&version=2.0.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.SourceGenerationTesting&version=2.0.0-preview.1&prerelease
                    
Install as a Cake Tool

Sparkdo.SourceGenerationTesting

Sparkdo.SourceGenerationTesting 是仓库统一的 Roslyn source-generation 测试宿主。Generator 测试应通过 SourceGenerationSpec 运行,不应自行创建 CSharpGeneratorDriver host。

职责

  • 组合 source、metadata reference、reference source、AdditionalFiles 与 analyzer config。
  • 同时运行 IIncrementalGeneratorISourceGenerator
  • 产出 generated artifacts、generator diagnostics、generated compilation diagnostics 与 provenance。
  • 验证 required/forbidden artifact、无 diagnostic、generated compilation 和 deterministic output。
  • 将规范化结果写入可审阅的 Markdown snapshot。

非职责

  • 不验证 Runtime、Hosting 或真实应用启动行为。
  • 不替代 package smoke、.nupkg consumer、trim、AOT 或性能 gate。
  • 不为生产代码提供 Roslyn runtime。

关键入口

API 用途
SourceGenerationSpec.Create() 创建测试宿主。
WithSource(...) / WithReference(...) 提供 compilation 输入。
WithReferenceSource(...) 编译并引用内存 assembly。
WithAdditionalFile(...) 提供 AdditionalFiles 及 metadata。
WithAnalyzerConfigOption(...) 提供 analyzer config。
WithGenerator(...) 注册 generator 与 provenance source。
VerifyAsync(...) 运行验证并返回 GeneratedOutputResult

使用

var result = await SourceGenerationSpec
    .Create()
    .WithSource("namespace App; public sealed class Marker { }")
    .WithGenerator(new MyGenerator(), source: "MyPackage.Compiler")
    .VerifyAsync(options => options
        .UseSnapshotName("my-generator")
        .RequireArtifact("MyGenerator.g.cs")
        .RequireGeneratedCompilation()
        .RequireDeterministicOutput());

默认 snapshot 目录是当前目录下的 __snapshots__。缺少 snapshot 时默认创建;调用 UseStrictSnapshotMode() 或设置 SPARKDO_SOURCEGEN_SNAPSHOT_STRICT=true 后会直接失败。Mismatch 写入 __snapshots__/__mismatch__

依赖

项目面向 net10.0,仅依赖 Microsoft.CodeAnalysis.CSharp

验证

dotnet test src/source-generation-testing/test/Sparkdo.SourceGenerationTesting.Tests/Sparkdo.SourceGenerationTesting.Tests.csproj --no-restore -p:UseSharedCompilation=false -nr:false
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-preview.2 77 7/20/2026
2.0.0-preview.1 63 7/18/2026