-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add asserts for different testing frameworks.
- Loading branch information
Showing
8 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 25 additions & 0 deletions
25
src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 25 additions & 0 deletions
25
src/SourceGeneratorTestHelpers.XUnit/GeneratorDriverRunResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SourceGeneratorTestHelpers.XUnit/SourceGeneratorTestHelpers.XUnit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |