Skip to content

Commit 0432679

Browse files
committed
Add asserts for different testing frameworks.
1 parent a4d7920 commit 0432679

File tree

8 files changed

+282
-0
lines changed

8 files changed

+282
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,33 @@ var generatedSources = result.GetSources();
3131
```csharp
3232
var generatedSource = result.GetSource("TestId.g.cs");
3333
```
34+
35+
### Compare the generated source with the expected source
36+
37+
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`.
38+
39+
```csharp
40+
var (hasDifferences, differences) = Diff.Compare(generatedSource, expectedSource);
41+
```
42+
43+
#### Assert the difference
44+
45+
Using one of the testing framework packages below, you can also assert the difference between the generated source and the expected source.
46+
47+
[![XUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=XUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.XUnit)
48+
[![NUnit](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=NUnit)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.NUnit)
49+
[![MSTest](https://img.shields.io/nuget/dt/SourceGeneratorTestHelpers.XUnit?label=MSTest)](https://www.nuget.org/packages/SourceGeneratorTestHelpers.MSTest)
50+
51+
```csharp
52+
var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");
53+
54+
result.ShouldProduce("TestId.g.cs", "expected source");
55+
```
56+
57+
_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._
58+
59+
```csharp
60+
var result = IncrementalGenerator.Run<YourSourceGenerator>("your source");
61+
62+
result.ShouldProduce("TestId.g.cs", "expected source", false);
63+
```

SourceGeneratorTestHelpers.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ VisualStudioVersion = 17.9.34616.47
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorTestHelpers", "src\SourceGeneratorTestHelpers\SourceGeneratorTestHelpers.csproj", "{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.MSTest", "src\SourceGeneratorTestHelpers.MSTest\SourceGeneratorTestHelpers.MSTest.csproj", "{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.NUnit", "src\SourceGeneratorTestHelpers.NUnit\SourceGeneratorTestHelpers.NUnit.csproj", "{CCB277CE-4190-478A-B6BF-D75721A045E1}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTestHelpers.XUnit", "src\SourceGeneratorTestHelpers.XUnit\SourceGeneratorTestHelpers.XUnit.csproj", "{E9304144-727F-4500-9EE6-6DA60A88DD8D}"
13+
EndProject
814
Global
915
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1016
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +21,18 @@ Global
1521
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Debug|Any CPU.Build.0 = Debug|Any CPU
1622
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.ActiveCfg = Release|Any CPU
1723
{C1CBA564-AE5A-454E-9B45-ACF7CEAF5C83}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{3C1B6271-4A7D-4EFB-847D-8F8E0FECB6DA}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{CCB277CE-4190-478A-B6BF-D75721A045E1}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{E9304144-727F-4500-9EE6-6DA60A88DD8D}.Release|Any CPU.Build.0 = Release|Any CPU
1836
EndGlobalSection
1937
GlobalSection(SolutionProperties) = preSolution
2038
HideSolutionNode = FALSE
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace SourceGeneratorTestHelpers.MSTest;
5+
6+
/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
7+
public static class GeneratorDriverRunResultExtensions
8+
{
9+
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
10+
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
11+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
12+
/// <param name="expectedSource">The expected source that the generated source should match.</param>
13+
/// <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>
14+
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
15+
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
16+
{
17+
#if NET6_0_OR_GREATER
18+
ArgumentNullException.ThrowIfNull(result);
19+
#else
20+
if (result is null)
21+
throw new ArgumentNullException(nameof(result));
22+
#endif
23+
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
5+
<ImplicitUsings>true</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>SourceGeneratorTestHelpers.MSTest</RootNamespace>
8+
<LangVersion>latest</LangVersion>
9+
<Version>8.0.5</Version>
10+
<Title>SourceGeneratorTestHelpers.MSTest</Title>
11+
<Authors>Jean-Sebastien Carle</Authors>
12+
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
13+
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
14+
<PackageId>SourceGeneratorTestHelpers.MSTest</PackageId>
15+
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
16+
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
19+
<RepositoryType>git</RepositoryType>
20+
<PackageTags>testing source-generators</PackageTags>
21+
<AssemblyVersion>8.0.5.0</AssemblyVersion>
22+
<FileVersion>8.0.5.0</FileVersion>
23+
<NeutralLanguage>en-US</NeutralLanguage>
24+
<IncludeSymbols>true</IncludeSymbols>
25+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
26+
<AnalysisLevel>latest-All</AnalysisLevel>
27+
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
28+
<IncludeSymbols>true</IncludeSymbols>
29+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
30+
<Optimize>true</Optimize>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
35+
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
36+
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
41+
<None Include="..\..\LICENSE.md">
42+
<Pack>True</Pack>
43+
<PackagePath>\</PackagePath>
44+
<Visible>False</Visible>
45+
</None>
46+
<None Include="..\..\README.md">
47+
<Pack>True</Pack>
48+
<PackagePath>\</PackagePath>
49+
<Visible>False</Visible>
50+
</None>
51+
</ItemGroup>
52+
53+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.CodeAnalysis;
2+
using NUnit.Framework;
3+
4+
namespace SourceGeneratorTestHelpers.NUnit;
5+
6+
/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
7+
public static class GeneratorDriverRunResultExtensions
8+
{
9+
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
10+
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
11+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
12+
/// <param name="expectedSource">The expected source that the generated source should match.</param>
13+
/// <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>
14+
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
15+
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
16+
{
17+
#if NET6_0_OR_GREATER
18+
ArgumentNullException.ThrowIfNull(result);
19+
#else
20+
if (result is null)
21+
throw new ArgumentNullException(nameof(result));
22+
#endif
23+
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
5+
<ImplicitUsings>true</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>SourceGeneratorTestHelpers.NUnit</RootNamespace>
8+
<LangVersion>latest</LangVersion>
9+
<Version>8.0.5</Version>
10+
<Title>SourceGeneratorTestHelpers.NUnit</Title>
11+
<Authors>Jean-Sebastien Carle</Authors>
12+
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
13+
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
14+
<PackageId>SourceGeneratorTestHelpers.NUnit</PackageId>
15+
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
16+
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
19+
<RepositoryType>git</RepositoryType>
20+
<PackageTags>testing source-generators</PackageTags>
21+
<AssemblyVersion>8.0.5.0</AssemblyVersion>
22+
<FileVersion>8.0.5.0</FileVersion>
23+
<NeutralLanguage>en-US</NeutralLanguage>
24+
<IncludeSymbols>true</IncludeSymbols>
25+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
26+
<AnalysisLevel>latest-All</AnalysisLevel>
27+
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
28+
<IncludeSymbols>true</IncludeSymbols>
29+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
30+
<Optimize>true</Optimize>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
35+
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
36+
<PackageReference Include="NUnit" Version="3.0.0"/>
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
41+
<None Include="..\..\LICENSE.md">
42+
<Pack>True</Pack>
43+
<PackagePath>\</PackagePath>
44+
<Visible>False</Visible>
45+
</None>
46+
<None Include="..\..\README.md">
47+
<Pack>True</Pack>
48+
<PackagePath>\</PackagePath>
49+
<Visible>False</Visible>
50+
</None>
51+
</ItemGroup>
52+
53+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.CodeAnalysis;
2+
using Xunit.Sdk;
3+
4+
namespace SourceGeneratorTestHelpers.XUnit;
5+
6+
/// <summary>Provides extension methods for the <see cref="GeneratorDriverRunResult" /> class.</summary>
7+
public static class GeneratorDriverRunResultExtensions
8+
{
9+
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult" /> with a specific file path ending matches the expected source.</summary>
10+
/// <param name="result">The <see cref="GeneratorDriverRunResult" /> to get the source from.</param>
11+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
12+
/// <param name="expectedSource">The expected source that the generated source should match.</param>
13+
/// <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>
14+
/// <exception cref="ArgumentNullException">If <paramref name="result" /> is null.</exception>
15+
public static void ShouldProduce(this GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
16+
{
17+
#if NET6_0_OR_GREATER
18+
ArgumentNullException.ThrowIfNull(result);
19+
#else
20+
if (result is null)
21+
throw new ArgumentNullException(nameof(result));
22+
#endif
23+
result.ShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new XunitException(message));
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
5+
<ImplicitUsings>true</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>SourceGeneratorTestHelpers.XUnit</RootNamespace>
8+
<LangVersion>latest</LangVersion>
9+
<Version>8.0.5</Version>
10+
<Title>SourceGeneratorTestHelpers.XUnit</Title>
11+
<Authors>Jean-Sebastien Carle</Authors>
12+
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
13+
<Copyright>Copyright © Jean-Sebastien Carle 2024</Copyright>
14+
<PackageId>SourceGeneratorTestHelpers.XUnit</PackageId>
15+
<PackageProjectUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</PackageProjectUrl>
16+
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
19+
<RepositoryType>git</RepositoryType>
20+
<PackageTags>testing source-generators</PackageTags>
21+
<AssemblyVersion>8.0.5.0</AssemblyVersion>
22+
<FileVersion>8.0.5.0</FileVersion>
23+
<NeutralLanguage>en-US</NeutralLanguage>
24+
<IncludeSymbols>true</IncludeSymbols>
25+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
26+
<AnalysisLevel>latest-All</AnalysisLevel>
27+
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
28+
<IncludeSymbols>true</IncludeSymbols>
29+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
30+
<Optimize>true</Optimize>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
35+
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.5"/>
36+
<PackageReference Include="xunit.assert" Version="2.0.0"/>
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
41+
<None Include="..\..\LICENSE.md">
42+
<Pack>True</Pack>
43+
<PackagePath>\</PackagePath>
44+
<Visible>False</Visible>
45+
</None>
46+
<None Include="..\..\README.md">
47+
<Pack>True</Pack>
48+
<PackagePath>\</PackagePath>
49+
<Visible>False</Visible>
50+
</None>
51+
</ItemGroup>
52+
53+
</Project>

0 commit comments

Comments
 (0)