Skip to content

Commit

Permalink
Add asserts for different testing frameworks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Feb 23, 2024
1 parent a4d7920 commit 0432679
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,33 @@ var generatedSources = result.GetSources();
```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.XUnit?label=NUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit)
[![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest)

```csharp
var result = IncrementalGenerator.Run<YourSourceGenerator>("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<YourSourceGenerator>("your source");

result.ShouldProduce("TestId.g.cs", "expected source", false);
```
18 changes: 18 additions & 0 deletions SourceGeneratorTestHelpers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ VisualStudioVersion = 17.9.34616.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorTestHelpers", "src\SourceGeneratorTestHelpers\SourceGeneratorTestHelpers.csproj", "{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.MSTest", "src\SourceGeneratorTestHelpers.MSTest\SourceGeneratorTestHelpers.MSTest.csproj", "{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.NUnit", "src\SourceGeneratorTestHelpers.NUnit\SourceGeneratorTestHelpers.NUnit.csproj", "{CCB277CE-4190-478A-B6BF-D75721A045E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.XUnit", "src\SourceGeneratorTestHelpers.XUnit\SourceGeneratorTestHelpers.XUnit.csproj", "{E9304144-727F-4500-9EE6-6DA60A88DD8D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +21,18 @@ Global
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.Build.0 = Release|Any CPU
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.Build.0 = Release|Any CPU
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.Build.0 = Release|Any CPU
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SourceGeneratorTestHelpers.MSTest;

/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
public static class GeneratorDriverRunResultExtensions
{
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
/// <param name="expectedSource">The expected source that the generated source should match.</param>
/// <param name="assertOnErrors"><see langword="true" /> to assert on reported errors by the source generator, <see langword="false" /> othwerwise. Defaults to <see langword="true" />.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
throw new ArgumentNullException(nameof(result));
#endif
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers.MSTest</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.5</Version>
<Title>SourceGeneratorTestHelpers.MSTest</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
<PackageId>SourceGeneratorTestHelpers.MSTest</PackageId>
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>testing source-generators</PackageTags>
<AssemblyVersion>8.0.5.0</AssemblyVersion>
<FileVersion>8.0.5.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<AnalysisLevel>latest-All</AnalysisLevel>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;
using NUnit.Framework;

namespace SourceGeneratorTestHelpers.NUnit;

/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
public static class GeneratorDriverRunResultExtensions
{
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
/// <param name="expectedSource">The expected source that the generated source should match.</param>
/// <param name="assertOnErrors"><see langword="true" /> to assert on reported errors by the source generator, <see langword="false" /> othwerwise. Defaults to <see langword="true" />.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
throw new ArgumentNullException(nameof(result));
#endif
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers.NUnit</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.5</Version>
<Title>SourceGeneratorTestHelpers.NUnit</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
<PackageId>SourceGeneratorTestHelpers.NUnit</PackageId>
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>testing source-generators</PackageTags>
<AssemblyVersion>8.0.5.0</AssemblyVersion>
<FileVersion>8.0.5.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<AnalysisLevel>latest-All</AnalysisLevel>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
<PackageReference Include="NUnit" Version="3.0.0"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;
using Xunit.Sdk;

namespace SourceGeneratorTestHelpers.XUnit;

/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
public static class GeneratorDriverRunResultExtensions
{
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
/// <param name="expectedSource">The expected source that the generated source should match.</param>
/// <param name="assertOnErrors"><see langword="true" /> to assert on reported errors by the source generator, <see langword="false" /> othwerwise. Defaults to <see langword="true" />.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
#else
if (result is null)
throw new ArgumentNullException(nameof(result));
#endif
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new XunitException(message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers.XUnit</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.5</Version>
<Title>SourceGeneratorTestHelpers.XUnit</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
<PackageId>SourceGeneratorTestHelpers.XUnit</PackageId>
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>testing source-generators</PackageTags>
<AssemblyVersion>8.0.5.0</AssemblyVersion>
<FileVersion>8.0.5.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<AnalysisLevel>latest-All</AnalysisLevel>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
<PackageReference Include="xunit.assert" Version="2.0.0"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<Visible>False</Visible>
</None>
</ItemGroup>

</Project>

0 comments on commit 0432679

Please sign in to comment.