Skip to content
 
 

Latest commit

 

History

469 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.SourceGenerators

Discussions Build status NuGet Status

Extends Verify to allow verification of C# Source Generators.

See Milestones for release notes.

NuGet package

https://nuget.org/packages/Verify.SourceGenerators/

Install one of the Verify testing framework adapters NuGet packages.

Initialize

[ModuleInitializer]
public static void Init() =>
    VerifySourceGenerators.Initialize();

snippet source | anchor

Generator

Given a Source Generator:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

[Generator]
public class HelloWorldGenerator :
    ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        var source1 = """
                      using System;
                      public static class Helper
                      {
                          public static void Method()
                          {
                          }
                      }
                      """;
        context.AddSource("helper", SourceText.From(source1, Encoding.UTF8));

        var source2 = """
                      using System;
                      public static class HelloWorld
                      {
                          public static void SayHello()
                          {
                              Console.WriteLine("Hello from generated code!");
                          }
                      }
                      """;
        context.AddSource("helloWorld", SourceText.From(source2, Encoding.UTF8));

        var descriptor = new DiagnosticDescriptor(
            id: "theId",
            title: "the title",
            messageFormat: "the message from {0}",
            category: "the category",
            DiagnosticSeverity.Info,
            isEnabledByDefault: true);

        var location = Location.Create(
            "theFile",
            new(1, 2),
            new(
                new(1, 2),
                new(3, 4)));
        var diagnostic = Diagnostic.Create(descriptor, location, "hello world generator");
        context.ReportDiagnostic(diagnostic);
    }

    public void Initialize(GeneratorInitializationContext context)
    {
    }
}

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.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

public class SampleTest
{
    [Fact]
    public Task Driver()
    {
        var driver = GeneratorDriver();

        return Verify(driver);
    }

    [Fact]
    public Task RunResults()
    {
        var driver = GeneratorDriver();

        var runResults = driver.GetRunResult();
        return Verify(runResults);
    }

    [Fact]
    public Task RunResult()
    {
        var driver = GeneratorDriver();

        var runResult = driver.GetRunResult().Results.Single();
        return Verify(runResult);
    }

    static GeneratorDriver GeneratorDriver()
    {
        var compilation = CSharpCompilation.Create("name");
        var generator = new HelloWorldGenerator();

        var driver = CSharpGeneratorDriver.Create(generator);
        return driver.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: [
    {
      Id: theId,
      Title: the title,
      Severity: Info,
      WarningLevel: 1,
      Location: theFile: (1,2)-(3,4),
      MessageFormat: the message from {0},
      Message: the message from hello world generator,
      Category: the category
    }
  ]
}

snippet source | anchor

Source Files

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

//HintName: helloWorld.cs
using System;
public static class HelloWorld
{
    public static void SayHello()
    {
        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]
public Task ScrubLines()
{
    var driver = GeneratorDriver();

    return Verify(driver)
        .ScrubLines(_ => _.StartsWith("using "));
}

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

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages