Skip to content

Instantly share code, notes, and snippets.

@sergey-shambir
Created December 20, 2024 12:43
Show Gist options
  • Select an option

  • Save sergey-shambir/fb32a3011d8049b1b9ee76ed417ccc89 to your computer and use it in GitHub Desktop.

Select an option

Save sergey-shambir/fb32a3011d8049b1b9ee76ed417ccc89 to your computer and use it in GitHub Desktop.
Unit testing for source generator that use <AdditionalFiles> as input, based on https://gist.github.com/tm-nti/bb374b36d815a1166bc0cbc4febb8515

How to add XUnit test for source generator that use <AdditionalFiles> (AdditionalText) as input:

  1. Add TestAdditionalText class
  2. Add TestDataLoader class
  3. In Unit test, use method CSharpGeneratorDriver.AddAdditionalTexts to add generator input file
return CSharpGeneratorDriver.Create(generator)
    .AddAdditionalTexts(texts)
    .RunGenerators(compilation);
using Example.Tests.TestData;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Immutable;
namespace Example.UnitTests.TestData;
public class OpenApiClientGeneratorTest
{
private const string ExampleApiYaml = "example_api.yaml";
private readonly GeneratorDriver _driver = RunOpenApiClientGenerator();
[Fact]
public void Can_parse_OpenAPI_schema()
{
GeneratorDriverRunResult driverResult = _driver.GetRunResult();
Assert.True(driverResult.Diagnostics.IsEmpty);
// TODO: add more asserts here.
}
private static GeneratorDriver RunOpenApiClientGenerator()
{
OpenApiClientGenerator generator = new();
ImmutableArray<AdditionalText> texts =
[
TestDataLoader.LoadAdditionalText(ExampleApiYaml)
];
CSharpCompilation compilation = CSharpCompilation.Create(GetTestAssemblyName());
return CSharpGeneratorDriver.Create(generator)
.AddAdditionalTexts(texts)
.RunGenerators(compilation);
}
private static string? GetTestAssemblyName()
{
return typeof(OpenApiClientGeneratorTest).Assembly.FullName;
}
}
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace Example.UnitTests;
public class TestAdditionalText(string path) : AdditionalText
{
private readonly SourceText _sourceText = SourceText.From(File.ReadAllText(path), System.Text.Encoding.UTF8);
public override string Path { get; } = path;
public override SourceText? GetText(CancellationToken cancellationToken = default)
{
return _sourceText;
}
}
using Example.UnitTests;
using Microsoft.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Example.UnitTests.TestData;
// NOTE: this file must be placed in the test files directory
public static class TestDataLoader
{
public static AdditionalText LoadAdditionalText(string fileName)
{
string filePath = Path.Combine(GetDataDirectoryPath(), fileName);
return new TestAdditionalText(filePath);
}
private static string GetDataDirectoryPath([CallerFilePath] string path = "")
{
return Path.GetDirectoryName(path)
?? throw new ArgumentException($"Cannot get directory path from {path}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment