Skip to content

Commit

Permalink
Updated extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Mar 26, 2024
1 parent 8d8646d commit 93b34ad
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 111 deletions.
84 changes: 53 additions & 31 deletions src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using SourceGeneratorTestHelpers.Common;

namespace SourceGeneratorTestHelpers.MSTest;

Expand All @@ -10,12 +11,12 @@ internal abstract class GeneratorDriverTestBase : VerifyBase
/// <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(GeneratorDriverRunResult result, string filePathEndsWith, string expectedSource, bool assertOnErrors = true)
public static void ShouldProduce(
GeneratorDriverRunResult result,
string filePathEndsWith,
string expectedSource
)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
Expand All @@ -24,23 +25,22 @@ public static void ShouldProduce(GeneratorDriverRunResult result, string filePat
throw new ArgumentNullException(nameof(result));
#endif

result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
result.InternalShouldProduce(filePathEndsWith, expectedSource, false, false, _ => { });
}

/// <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>
/// <param name="assertOnErrors"><see langword="true"/> to assert on reported errors, <see langword="false"/> othwerwise. Defaults to <see langword="true"/>.</param>
/// <param name="assertOnWarnings"><see langword="true"/> to assert on reported warnings, <see langword="false"/> othwerwise. Defaults to <see langword="false"/>.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public static void ShouldProduce(
(ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
(ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
string expectedSource,
bool assertOnErrors = true
bool assertOnErrors = true,
bool assertOnWarnings = false
)
{
#if NET6_0_OR_GREATER
Expand All @@ -50,10 +50,26 @@ public static void ShouldProduce(
throw new ArgumentNullException(nameof(result));
#endif

if (assertOnErrors && result.Diagnostics.Length > 0)
throw new AssertFailedException(result.Diagnostics.GetExceptionMessage());
result.CompilationDiagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertFailedException(message),
"There were errors in the compilation."
);
result.Result.Diagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertFailedException(message),
"There were errors in the output generated by the source generator."
);

result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertFailedException(message));
result.Result.InternalShouldProduce(
filePathEndsWith,
expectedSource,
assertOnErrors,
assertOnWarnings,
message => throw new AssertFailedException(message)
);
}

/// <summary>
Expand All @@ -62,17 +78,12 @@ public static void ShouldProduce(
/// </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="assertOnErrors">
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
/// <see langword="true"/>.
/// </param>
/// <param name="verifySettings">The verify settings.</param>
/// <param name="sourceFile">The source file.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public SettingsTask VerifyAsync(
GeneratorDriverRunResult result,
string filePathEndsWith,
bool assertOnErrors = true,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
Expand All @@ -84,10 +95,11 @@ public SettingsTask VerifyAsync(
throw new ArgumentNullException(nameof(result));
#endif

var generatedSource = result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertFailedException(message));
var generatedSource = result.InternalGetSource(filePathEndsWith);
var source = generatedSource.HasValue ? generatedSource.Value.Source : "";

// ReSharper disable once ExplicitCallerInfoArgument
return Verify(generatedSource, "txt", verifySettings, sourceFile);
return Verify(source, "txt", verifySettings, sourceFile);
}

/// <summary>
Expand All @@ -96,17 +108,16 @@ public SettingsTask VerifyAsync(
/// </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="assertOnErrors">
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
/// <see langword="true"/>.
/// </param>
/// <param name="assertOnErrors"><see langword="true"/> to assert on reported errors, <see langword="false"/> othwerwise. Defaults to <see langword="true"/>.</param>
/// <param name="assertOnWarnings"><see langword="true"/> to assert on reported warnings, <see langword="false"/> othwerwise. Defaults to <see langword="false"/>.</param>
/// <param name="verifySettings">The verify settings.</param>
/// <param name="sourceFile">The source file.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public SettingsTask VerifyAsync(
(ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
(ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
Expand All @@ -118,12 +129,23 @@ public SettingsTask VerifyAsync(
throw new ArgumentNullException(nameof(result));
#endif

if (assertOnErrors && result.Diagnostics.Length > 0)
throw new AssertFailedException(result.Diagnostics.GetExceptionMessage());
result.CompilationDiagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertFailedException(message),
"There were errors in the compilation."
);
result.Result.Diagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertFailedException(message),
"There were errors in the output generated by the source generator."
);

var generatedSource = result.Result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertFailedException(message));
var generatedSource = result.Result.InternalGetSource(filePathEndsWith);
var source = generatedSource.HasValue ? generatedSource.Value.Source : "";

// ReSharper disable once ExplicitCallerInfoArgument
return Verify(generatedSource, "txt", verifySettings, sourceFile);
return Verify(source, "txt", verifySettings, sourceFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers.MSTest</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.14</Version>
<Version>8.1.0</Version>
<Title>SourceGeneratorTestHelpers.MSTest</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
Expand All @@ -18,8 +18,8 @@
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>testing source-generators mstest</PackageTags>
<AssemblyVersion>8.0.14.0</AssemblyVersion>
<FileVersion>8.0.14.0</FileVersion>
<AssemblyVersion>8.1.0.0</AssemblyVersion>
<FileVersion>8.1.0.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand All @@ -32,7 +32,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1"/>
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.0.14"/>
<PackageReference Include="SourceGeneratorTestHelpers" Version="8.1.0"/>
<PackageReference Include="MSTest.TestFramework" Version="3.1.1"/>
<PackageReference Include="Verify.MSTest" Version="20.8.2"/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using SourceGeneratorTestHelpers.Common;

namespace SourceGeneratorTestHelpers.NUnit;

Expand All @@ -11,12 +12,12 @@ public static class GeneratorDriverRunResultExtensions
/// <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)
public static void ShouldProduce(
this GeneratorDriverRunResult result,
string filePathEndsWith,
string expectedSource
)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(result);
Expand All @@ -25,23 +26,22 @@ public static void ShouldProduce(this GeneratorDriverRunResult result, string fi
throw new ArgumentNullException(nameof(result));
#endif

result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
result.InternalShouldProduce(filePathEndsWith, expectedSource, false, false, _ => { });
}

/// <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>
/// <param name="assertOnErrors"><see langword="true"/> to assert on reported errors, <see langword="false"/> othwerwise. Defaults to <see langword="true"/>.</param>
/// <param name="assertOnWarnings"><see langword="true"/> to assert on reported warnings, <see langword="false"/> othwerwise. Defaults to <see langword="false"/>.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public static void ShouldProduce(
this (ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
this (ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
string expectedSource,
bool assertOnErrors = true
bool assertOnErrors = true,
bool assertOnWarnings = false
)
{
#if NET6_0_OR_GREATER
Expand All @@ -51,10 +51,26 @@ public static void ShouldProduce(
throw new ArgumentNullException(nameof(result));
#endif

if (assertOnErrors && result.Diagnostics.Length > 0)
throw new AssertionException(result.Diagnostics.GetExceptionMessage());
result.CompilationDiagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertionException(message),
"There were errors in the compilation."
);
result.Result.Diagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertionException(message),
"There were errors in the output generated by the source generator."
);

result.Result.InternalShouldProduce(filePathEndsWith, expectedSource, assertOnErrors, message => throw new AssertionException(message));
result.Result.InternalShouldProduce(
filePathEndsWith,
expectedSource,
assertOnErrors,
assertOnWarnings,
message => throw new AssertionException(message)
);
}

/// <summary>
Expand All @@ -63,17 +79,12 @@ public static void ShouldProduce(
/// </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="assertOnErrors">
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
/// <see langword="true"/>.
/// </param>
/// <param name="verifySettings">The verify settings.</param>
/// <param name="sourceFile">The source file.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public static SettingsTask VerifyAsync(
this GeneratorDriverRunResult result,
string filePathEndsWith,
bool assertOnErrors = true,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
Expand All @@ -85,10 +96,11 @@ public static SettingsTask VerifyAsync(
throw new ArgumentNullException(nameof(result));
#endif

var generatedSource = result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertionException(message));

var generatedSource = result.InternalGetSource(filePathEndsWith);
var source = generatedSource.HasValue ? generatedSource.Value.Source : "";

// ReSharper disable once ExplicitCallerInfoArgument
return Verify(generatedSource, "txt", verifySettings, sourceFile);
return Verify(source, "txt", verifySettings, sourceFile);
}

/// <summary>
Expand All @@ -97,17 +109,16 @@ public static SettingsTask VerifyAsync(
/// </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="assertOnErrors">
/// <see langword="true"/> to assert on reported errors by the source generator, <see langword="false"/> othwerwise. Defaults to
/// <see langword="true"/>.
/// </param>
/// <param name="assertOnErrors"><see langword="true"/> to assert on reported errors, <see langword="false"/> othwerwise. Defaults to <see langword="true"/>.</param>
/// <param name="assertOnWarnings"><see langword="true"/> to assert on reported warnings, <see langword="false"/> othwerwise. Defaults to <see langword="false"/>.</param>
/// <param name="verifySettings">The verify settings.</param>
/// <param name="sourceFile">The source file.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is null.</exception>
public static SettingsTask VerifyAsync(
this (ImmutableArray<Diagnostic> Diagnostics, GeneratorDriverRunResult Result) result,
this (ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
Expand All @@ -119,12 +130,23 @@ public static SettingsTask VerifyAsync(
throw new ArgumentNullException(nameof(result));
#endif

if (assertOnErrors && result.Diagnostics.Length > 0)
throw new AssertionException(result.Diagnostics.GetExceptionMessage());
result.CompilationDiagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertionException(message),
"There were errors in the compilation."
);
result.Result.Diagnostics.InternalAssertOnDiagnostics(
assertOnErrors,
assertOnWarnings,
message => throw new AssertionException(message),
"There were errors in the output generated by the source generator."
);

var generatedSource = result.Result.InternalGetSource(filePathEndsWith, assertOnErrors, message => throw new AssertionException(message));
var generatedSource = result.Result.InternalGetSource(filePathEndsWith);
var source = generatedSource.HasValue ? generatedSource.Value.Source : "";

// ReSharper disable once ExplicitCallerInfoArgument
return Verify(generatedSource, "txt", verifySettings, sourceFile);
return Verify(source, "txt", verifySettings, sourceFile);
}
}
Loading

0 comments on commit 93b34ad

Please sign in to comment.