Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jscarle/sourcegeneratortesthelpers
Test helpers and extension methods to simplify testing of .NET source generators.
https://github.com/jscarle/sourcegeneratortesthelpers
csharp dotnet source-generators testing
Last synced: about 1 month ago
JSON representation
Test helpers and extension methods to simplify testing of .NET source generators.
- Host: GitHub
- URL: https://github.com/jscarle/sourcegeneratortesthelpers
- Owner: jscarle
- License: mit
- Created: 2024-02-21T16:09:37.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-05-13T21:54:15.000Z (9 months ago)
- Last Synced: 2024-05-16T08:32:00.380Z (9 months ago)
- Topics: csharp, dotnet, source-generators, testing
- Language: C#
- Homepage:
- Size: 78.1 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Source Generator Test Helpers
Test helpers and extension methods to simplify testing of .NET source generators.
[![main](https://img.shields.io/github/actions/workflow/status/jscarle/SourceGeneratorTestHelpers/main.yml?logo=github)](https://github.com/jscarle/SourceGeneratorTestHelpers)
[![nuget](https://img.shields.io/nuget/v/SourceGeneratorTestHelpers)](https://www.nuget.org/packages/SourceGeneratorTestHelpers)
[![downloads](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers)](https://www.nuget.org/packages/SourceGeneratorTestHelpers)## Testing a source generator
```csharp
var result = SourceGenerator.Run("your source");
```## Testing an incremental source generator
```csharp
var result = IncrementalGenerator.Run("your source");
```## Obtaining the generated source
### Getting all generated sources
```csharp
var generatedSources = result.GetSources();
```### A single source that ends with a specific file path
```csharp
var generatedSource = result.GetSource("TestId.g.cs");
```### Compare the generated source with the expected source
You can produce a diff between the generated source and the expected source. The result will contain a boolean `hasDifferences` and a line by line diff
in `differences`.```csharp
var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource);
```## Assert the difference
Using one of the testing framework packages below, you can also assert the difference between the generated source and the expected source.
[![XUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=XUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.XUnit)
[![NUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.NUnit?label=NUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit)
[![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.MSTest?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest)```csharp
var result = IncrementalGenerator.Run("your source");result.ShouldProduce("TestId.g.cs", "expected source");
```_Note: If you do not wish to assert on errors produced during diagnostics of the source generator run, you can simply disable them as such._
```csharp
var result = IncrementalGenerator.Run("your source");result.ShouldProduce("TestId.g.cs", "expected source", false);
```### Verify the difference
Support for [Verify](https://github.com/VerifyTests/Verify) is built-in using the `VerifyAsync` method.
#### XUnit
```cs
public class SourceGeneratorTests
{
[Fact]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run("your source");
return result.VerifyAsync("TestId.g.cs");
}
}
```#### NUnit
```cs
[TestFixture]
public class SourceGeneratorTests
{
[Test]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run("your source");
return result.VerifyAsync("TestId.g.cs");
}
}
```#### MSTest
```cs
[TestClass]
public class SourceGeneratorTests :
GeneratorDriverTestBase
{
[TestMethod]
public Task ShouldProductTestId()
{
var result = IncrementalGenerator.Run("your source");
return VerifyAsync("TestId.g.cs");
}
}
```