Skip to content

Repository files navigation

Verify.SourceGenerators

DiscussionsBuild statusNuGet Status

Extends Verify to allow verification of C# Source Generators.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Initialize

[ModuleInitializer]publicstaticvoidInit()=>VerifySourceGenerators.Initialize();

snippet source | anchor

Generator

Given a Source Generator:

[Generator]publicclassHelloWorldGenerator:ISourceGenerator{publicvoidExecute(GeneratorExecutionContextcontext){varsource1=""" using System; public static class Helper { public static void Method() { } } """;context.AddSource("helper",SourceText.From(source1,Encoding.UTF8));varsource2=""" using System; public static class HelloWorld { public static void SayHello() { Console.WriteLine("Hello from generated code!"); } } """;varsourceText=SourceText.From(source2,Encoding.UTF8);context.AddSource("helloWorld",sourceText);vardescriptor=newDiagnosticDescriptor(id:"theId",title:"the title",messageFormat:"the message from {0}",category:"the category",DiagnosticSeverity.Info,isEnabledByDefault:true);varlocation=Location.Create(Path.Combine("dir","theFile.cs"),new(1,2),new(new(1,2),new(3,4)));vardiagnostic=Diagnostic.Create(descriptor,location,"hello world generator");context.ReportDiagnostic(diagnostic);}publicvoidInitialize(GeneratorInitializationContextcontext){}}

snippet source | anchor

Test

Can be tested as follows:

This snippets assumes use of the XUnit Verify adapter, change the using VerifyXUnit if using other testing frameworks.

publicclassSampleTest{[Fact]publicTaskDriver(){vardriver=BuildDriver();returnVerify(driver);}[Fact]publicTaskRunResults(){vardriver=BuildDriver();varresults=driver.GetRunResult();returnVerify(results);}[Fact]publicTaskRunResult(){vardriver=BuildDriver();varresult=driver.GetRunResult().Results.Single();returnVerify(result);}staticGeneratorDriverBuildDriver(){varcompilation=CSharpCompilation.Create("name");vargenerator=newHelloWorldGenerator();vardriver=CSharpGeneratorDriver.Create(generator);returndriver.RunGenerators(compilation);}}

snippet source | anchor

Results

And will result in the following verified files:

Info file

An info file containing all metadata about the current state. eg any Diagnostics.

{
Diagnostics: [
{
Location: dir\theFile.cs: (1,2)-(3,4),
Message: the message from hello world generator,
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}

snippet source | anchor

Source Files

Multiple source files. One for each GeneratorDriverRunResult.Results.GeneratedSources.

//HintName: helloWorld.csusingSystem;publicstaticclassHelloWorld{publicstaticvoidSayHello(){Console.WriteLine("Hello from generated code!");}}

snippet source | anchor

Manipulating Source

To manipulating the source of the generated cs files, use Scrubbers.

For example to remove all lines start with using:

[Fact]publicTaskScrubLines(){vardriver=GeneratorDriver();returnVerify(driver).ScrubLines(_ =>_.StartsWith("using "));}

snippet source | anchor

Ignoring Files

To ignore specific source text use IgnoreGeneratedResult. This uses an expression of type Func<GeneratedSourceResult, bool> to determine which outputs are ignored.

For example to ignore files with the name helper or that contain the text static void SayHello():

[Fact]publicTaskIgnoreFile(){vardriver=GeneratorDriver();returnVerify(driver).IgnoreGeneratedResult(
_ =>_.HintName.Contains("helper")||_.SourceText.ToString().Contains("static void SayHello()"));}

snippet source | anchor

Notes:

Icon

Sauce designed by April Hsuan from The Noun Project.

About

Extends Verify to allow verification of C# Source Generators.

Resources

Code of conduct

Stars

96 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages