Skip to content

Commit 0a8beac

Browse files
committed
Updated extensions.
1 parent 71932dc commit 0a8beac

File tree

8 files changed

+261
-42
lines changed

8 files changed

+261
-42
lines changed

src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverRunResultExtensions.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Collections.Immutable;
2+
using System.Runtime.CompilerServices;
23
using Microsoft.CodeAnalysis;
34

45
namespace SourceGeneratorTestHelpers.MSTest;
56

67
internal abstract class GeneratorDriverTestBase : VerifyBase
78
{
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">
14+
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
15+
/// <see langword="true"/>.
16+
/// </param>
17+
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
18+
public static void ShouldProduce(GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
19+
{
20+
#if NET6_0_OR_GREATER
21+
ArgumentNullException.ThrowIfNull(result);
22+
#else
23+
if (result is null)
24+
throw new ArgumentNullException(nameof(result));
25+
#endif
26+
27+
result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
28+
}
29+
30+
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path ending matches the expected source.</summary>
31+
/// <param name="result">The <see cref="GeneratorDriverRunResult"/> to get the source from.</param>
32+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
33+
/// <param name="expectedSource">The expected source that the generated source should match.</param>
34+
/// <param name="assertOnErrors">
35+
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
36+
/// <see langword="true"/>.
37+
/// </param>
38+
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
39+
public static void ShouldProduce(
40+
(ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
41+
string filePathEndsWith,
42+
string expectedSource,
43+
bool assertOnErrors = true
44+
)
45+
{
46+
#if NET6_0_OR_GREATER
47+
ArgumentNullException.ThrowIfNull(result.Result);
48+
#else
49+
if (result.Result is null)
50+
throw new ArgumentNullException(nameof(result));
51+
#endif
52+
53+
if (assertOnErrors && result.Diagnostics.Length > 0)
54+
throw new AssertFailedException(result.Diagnostics.GetExceptionMessage());
55+
56+
result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
57+
}
58+
859
/// <summary>
960
/// Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using
1061
/// <see cref="VerifyBase.Verify(string?, string, VerifySettings?, string)"/>.
@@ -26,9 +77,53 @@ public SettingsTask VerifyAsync(
2677
[CallerFilePath] string sourceFile = ""
2778
)
2879
{
80+
#if NET6_0_OR_GREATER
81+
ArgumentNullException.ThrowIfNull(result);
82+
#else
83+
if (result is null)
84+
throw new ArgumentNullException(nameof(result));
85+
#endif
86+
2987
var generatedSource = result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertFailedException(message));
3088

3189
// ReSharper disable once ExplicitCallerInfoArgument
3290
return Verify(generatedSource, "txt", verifySettings, sourceFile);
3391
}
92+
93+
/// <summary>
94+
/// Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using
95+
/// <see cref="VerifyBase.Verify(string?, string, VerifySettings?, string)"/>.
96+
/// </summary>
97+
/// <param name="result">The <see cref="GeneratorDriverRunResult"/> to get the source from.</param>
98+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
99+
/// <param name="assertOnErrors">
100+
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
101+
/// <see langword="true"/>.
102+
/// </param>
103+
/// <param name="verifySettings">The verify settings.</param>
104+
/// <param name="sourceFile">The source file.</param>
105+
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
106+
public SettingsTask VerifyAsync(
107+
(ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
108+
string filePathEndsWith,
109+
bool assertOnErrors = true,
110+
VerifySettings? verifySettings = null,
111+
[CallerFilePath] string sourceFile = ""
112+
)
113+
{
114+
#if NET6_0_OR_GREATER
115+
ArgumentNullException.ThrowIfNull(result.Result);
116+
#else
117+
if (result.Result is null)
118+
throw new ArgumentNullException(nameof(result));
119+
#endif
120+
121+
if (assertOnErrors && result.Diagnostics.Length > 0)
122+
throw new AssertFailedException(result.Diagnostics.GetExceptionMessage());
123+
124+
var generatedSource = result.Result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertFailedException(message));
125+
126+
// ReSharper disable once ExplicitCallerInfoArgument
127+
return Verify(generatedSource, "txt", verifySettings, sourceFile);
128+
}
34129
}

src/SourceGeneratorTestHelpers.MSTest/SourceGeneratorTestHelpers.MSTest.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<RootNamespace>SourceGeneratorTestHelpers.MSTest</RootNamespace>
88
<LangVersion>latest</LangVersion>
9-
<Version>8.0.13</Version>
9+
<Version>8.0.14</Version>
1010
<Title>SourceGeneratorTestHelpers.MSTest</Title>
1111
<Authors>Jean-Sebastien Carle</Authors>
1212
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
@@ -18,8 +18,8 @@
1818
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
2020
<PackageTags>testing source-generators mstest</PackageTags>
21-
<AssemblyVersion>8.0.13.0</AssemblyVersion>
22-
<FileVersion>8.0.13.0</FileVersion>
21+
<AssemblyVersion>8.0.14.0</AssemblyVersion>
22+
<FileVersion>8.0.14.0</FileVersion>
2323
<NeutralLanguage>en-US</NeutralLanguage>
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
@@ -32,7 +32,7 @@
3232

3333
<ItemGroup>
3434
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
35-
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.13" />
35+
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.14"/>
3636
<PackageReference Include="MSTest.TestFramework" Version="3.1.1"/>
3737
<PackageReference Include="Verify.MSTest" Version="20.8.2"/>
3838
</ItemGroup>

src/SourceGeneratorTestHelpers.NUnit/GeneratorDriverRunResultExtensions.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Collections.Immutable;
2+
using System.Runtime.CompilerServices;
23
using Microsoft.CodeAnalysis;
34

45
namespace SourceGeneratorTestHelpers.NUnit;
@@ -23,9 +24,39 @@ public static void ShouldProduce(this GeneratorDriverRunResult result, string fi
2324
if (result is null)
2425
throw new ArgumentNullException(nameof(result));
2526
#endif
27+
2628
result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
2729
}
2830

31+
/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path ending matches the expected source.</summary>
32+
/// <param name="result">The <see cref="GeneratorDriverRunResult"/> to get the source from.</param>
33+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
34+
/// <param name="expectedSource">The expected source that the generated source should match.</param>
35+
/// <param name="assertOnErrors">
36+
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
37+
/// <see langword="true"/>.
38+
/// </param>
39+
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
40+
public static void ShouldProduce(
41+
this (ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
42+
string filePathEndsWith,
43+
string expectedSource,
44+
bool assertOnErrors = true
45+
)
46+
{
47+
#if NET6_0_OR_GREATER
48+
ArgumentNullException.ThrowIfNull(result.Result);
49+
#else
50+
if (result.Result is null)
51+
throw new ArgumentNullException(nameof(result));
52+
#endif
53+
54+
if (assertOnErrors && result.Diagnostics.Length > 0)
55+
throw new AssertionException(result.Diagnostics.GetExceptionMessage());
56+
57+
result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
58+
}
59+
2960
/// <summary>
3061
/// Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using
3162
/// <see cref="Verifier.Verify(string?, string, VerifySettings?, string)"/>.
@@ -47,9 +78,53 @@ public static SettingsTask VerifyAsync(
4778
[CallerFilePath] string sourceFile = ""
4879
)
4980
{
81+
#if NET6_0_OR_GREATER
82+
ArgumentNullException.ThrowIfNull(result);
83+
#else
84+
if (result is null)
85+
throw new ArgumentNullException(nameof(result));
86+
#endif
87+
5088
var generatedSource = result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertionException(message));
5189

5290
// ReSharper disable once ExplicitCallerInfoArgument
5391
return Verify(generatedSource, "txt", verifySettings, sourceFile);
5492
}
93+
94+
/// <summary>
95+
/// Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using
96+
/// <see cref="Verifier.Verify(string?, string, VerifySettings?, string)"/>.
97+
/// </summary>
98+
/// <param name="result">The <see cref="GeneratorDriverRunResult"/> to get the source from.</param>
99+
/// <param name="filePathEndsWith">The string that the generated source's file path should end with.</param>
100+
/// <param name="assertOnErrors">
101+
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
102+
/// <see langword="true"/>.
103+
/// </param>
104+
/// <param name="verifySettings">The verify settings.</param>
105+
/// <param name="sourceFile">The source file.</param>
106+
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
107+
public static SettingsTask VerifyAsync(
108+
this (ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
109+
string filePathEndsWith,
110+
bool assertOnErrors = true,
111+
VerifySettings? verifySettings = null,
112+
[CallerFilePath] string sourceFile = ""
113+
)
114+
{
115+
#if NET6_0_OR_GREATER
116+
ArgumentNullException.ThrowIfNull(result.Result);
117+
#else
118+
if (result.Result is null)
119+
throw new ArgumentNullException(nameof(result));
120+
#endif
121+
122+
if (assertOnErrors && result.Diagnostics.Length > 0)
123+
throw new AssertionException(result.Diagnostics.GetExceptionMessage());
124+
125+
var generatedSource = result.Result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertionException(message));
126+
127+
// ReSharper disable once ExplicitCallerInfoArgument
128+
return Verify(generatedSource, "txt", verifySettings, sourceFile);
129+
}
55130
}

src/SourceGeneratorTestHelpers.NUnit/SourceGeneratorTestHelpers.NUnit.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<RootNamespace>SourceGeneratorTestHelpers.NUnit</RootNamespace>
88
<LangVersion>latest</LangVersion>
9-
<Version>8.0.13</Version>
9+
<Version>8.0.14</Version>
1010
<Title>SourceGeneratorTestHelpers.NUnit</Title>
1111
<Authors>Jean-Sebastien Carle</Authors>
1212
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
@@ -18,8 +18,8 @@
1818
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
2020
<PackageTags>testing source-generators nunit</PackageTags>
21-
<AssemblyVersion>8.0.13.0</AssemblyVersion>
22-
<FileVersion>8.0.13.0</FileVersion>
21+
<AssemblyVersion>8.0.14.0</AssemblyVersion>
22+
<FileVersion>8.0.14.0</FileVersion>
2323
<NeutralLanguage>en-US</NeutralLanguage>
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
@@ -32,7 +32,7 @@
3232

3333
<ItemGroup>
3434
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
35-
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.13" />
35+
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.14"/>
3636
<PackageReference Include="NUnit" Version="3.13.3"/>
3737
<PackageReference Include="Verify.NUnit" Version="20.8.2"/>
3838
</ItemGroup>

0 commit comments

Comments
 (0)