Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ result.ShouldProduce("TestId.g.cs", "expected source", false);
### Verify the difference

Support for [Verify](https://github.com/VerifyTests/Verify) is built-in using the `VerifyAsync` method.
Generated source text is normalized to LF line endings before it is passed to Verify.

#### XUnit

Expand All @@ -105,6 +106,12 @@ public class SourceGeneratorTests
}
```

For parameterized tests, pass a stable snapshot name to avoid framework-specific parameter text in the snapshot file name:

```cs
return result.VerifyAsync("TestId.g.cs", snapshotName: "SourceGeneratorTests.ShouldProduceTestId");
```

#### NUnit

```cs
Expand Down
77 changes: 73 additions & 4 deletions src/SourceGeneratorTestHelpers.MSTest/GeneratorDriverTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,93 @@ public SettingsTask VerifyAsync(
ArgumentNullException.ThrowIfNull(result);

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

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

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="VerifyBase.Verify(string?, string, VerifySettings?, string)"/>.</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="snapshotName">The stable snapshot file name to use.</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>
/// <exception cref="ArgumentException">If <paramref name="snapshotName"/> is null or whitespace.</exception>
public SettingsTask VerifyAsync(
GeneratorDriverRunResult result,
string filePathEndsWith,
string snapshotName,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result);
ArgumentException.ThrowIfNullOrWhiteSpace(snapshotName);

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

// ReSharper disable once ExplicitCallerInfoArgument
return VerifySource(source, verifySettings, sourceFile, snapshotName);
}

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="VerifyBase.Verify(string?, string, VerifySettings?, string)"/>.</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, <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> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result.Result);

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);
var source = generatedSource.HasValue ? generatedSource.Value.Source.ReplaceLineEndings("\n") : "";

// ReSharper disable once ExplicitCallerInfoArgument
return VerifySource(source, verifySettings, sourceFile);
}

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="VerifyBase.Verify(string?, string, VerifySettings?, string)"/>.</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="snapshotName">The stable snapshot file name to use.</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>
/// <exception cref="ArgumentException">If <paramref name="snapshotName"/> is null or whitespace.</exception>
public SettingsTask VerifyAsync(
(ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
string snapshotName,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result.Result);
ArgumentException.ThrowIfNullOrWhiteSpace(snapshotName);

result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertFailedException(message),
"There were errors in the compilation."
Expand All @@ -97,9 +160,15 @@ public SettingsTask VerifyAsync(
);

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

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

private SettingsTask VerifySource(string source, VerifySettings? verifySettings, string sourceFile, string? snapshotName = null)
{
var task = Verify(source, "txt", verifySettings, sourceFile);
return snapshotName is null ? task : task.UseFileName(snapshotName);
}
}
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>10.1.0</Version>
<Version>10.1.2</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>10.1.0.0</AssemblyVersion>
<FileVersion>10.1.0.0</FileVersion>
<AssemblyVersion>10.1.2.0</AssemblyVersion>
<FileVersion>10.1.2.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,93 @@ public static SettingsTask VerifyAsync(
ArgumentNullException.ThrowIfNull(result);

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

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

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="Verifier.Verify(string?, string, VerifySettings?, string)"/>.</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="snapshotName">The stable snapshot file name to use.</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>
/// <exception cref="ArgumentException">If <paramref name="snapshotName"/> is null or whitespace.</exception>
public static SettingsTask VerifyAsync(
this GeneratorDriverRunResult result,
string filePathEndsWith,
string snapshotName,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result);
ArgumentException.ThrowIfNullOrWhiteSpace(snapshotName);

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

// ReSharper disable once ExplicitCallerInfoArgument
return VerifySource(source, verifySettings, sourceFile, snapshotName);
}

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="Verifier.Verify(string?, string, VerifySettings?, string)"/>.</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, <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> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result.Result);

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);
var source = generatedSource.HasValue ? generatedSource.Value.Source.ReplaceLineEndings("\n") : "";

// ReSharper disable once ExplicitCallerInfoArgument
return VerifySource(source, verifySettings, sourceFile);
}

/// <summary>Verifies that the generated source from a <see cref="GeneratorDriverRunResult"/> with a specific file path using <see cref="Verifier.Verify(string?, string, VerifySettings?, string)"/>.</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="snapshotName">The stable snapshot file name to use.</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>
/// <exception cref="ArgumentException">If <paramref name="snapshotName"/> is null or whitespace.</exception>
public static SettingsTask VerifyAsync(
this (ImmutableArray<Diagnostic> CompilationDiagnostics, GeneratorDriverRunResult Result) result,
string filePathEndsWith,
string snapshotName,
bool assertOnErrors = true,
bool assertOnWarnings = false,
VerifySettings? verifySettings = null,
[CallerFilePath] string sourceFile = ""
)
{
ArgumentNullException.ThrowIfNull(result.Result);
ArgumentException.ThrowIfNullOrWhiteSpace(snapshotName);

result.CompilationDiagnostics.InternalAssertOnDiagnostics(assertOnErrors, assertOnWarnings, message => throw new AssertionException(message),
"There were errors in the compilation."
Expand All @@ -97,9 +160,15 @@ public static SettingsTask VerifyAsync(
);

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

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

private static SettingsTask VerifySource(string source, VerifySettings? verifySettings, string sourceFile, string? snapshotName = null)
{
var task = Verify(source, "txt", verifySettings, sourceFile);
return snapshotName is null ? task : task.UseFileName(snapshotName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers.NUnit</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>10.1.0</Version>
<Version>10.1.2</Version>
<Title>SourceGeneratorTestHelpers.NUnit</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 nunit</PackageTags>
<AssemblyVersion>10.1.0.0</AssemblyVersion>
<FileVersion>10.1.0.0</FileVersion>
<AssemblyVersion>10.1.2.0</AssemblyVersion>
<FileVersion>10.1.2.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
Loading
Loading