Skip to content

Commit

Permalink
Add TestBehaviors.SkipGeneratedSourcesCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Mar 11, 2021
1 parent 1e28e66 commit e0c5578
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Microsoft.CodeAnalysis.Testing.StateInheritanceMode.Explicit = 1 -> Microsoft.Co
Microsoft.CodeAnalysis.Testing.TestBehaviors
Microsoft.CodeAnalysis.Testing.TestBehaviors.None = 0 -> Microsoft.CodeAnalysis.Testing.TestBehaviors
Microsoft.CodeAnalysis.Testing.TestBehaviors.SkipGeneratedCodeCheck = 1 -> Microsoft.CodeAnalysis.Testing.TestBehaviors
Microsoft.CodeAnalysis.Testing.TestBehaviors.SkipGeneratedSourcesCheck = 4 -> Microsoft.CodeAnalysis.Testing.TestBehaviors
Microsoft.CodeAnalysis.Testing.TestBehaviors.SkipSuppressionCheck = 2 -> Microsoft.CodeAnalysis.Testing.TestBehaviors
Microsoft.CodeAnalysis.Testing.TestFileMarkupParser
abstract Microsoft.CodeAnalysis.Testing.AnalyzerTest<TVerifier>.CreateCompilationOptions() -> Microsoft.CodeAnalysis.CompilationOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@ public enum TestBehaviors
/// at the beginning of the file.
/// </summary>
SkipSuppressionCheck = 0x02,

/// <summary>
/// Skip a verification check that the contents of <see cref="ProjectState.GeneratedSources"/> match the sources
/// produced by the active source generators (if any).
/// </summary>
/// <remarks>
/// When this flag is set, the <see cref="ProjectState.GeneratedSources"/> property is completely ignored; tests
/// are encouraged to leave it empty for optimal readability.
/// </remarks>
SkipGeneratedSourcesCheck = 0x04,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ private async Task<ImmutableArray<Diagnostic>> VerifySourceGeneratorAsync(
(project, diagnostics) = await getFixedProject(sourceGenerators, project, verifier, cancellationToken).ConfigureAwait(false);

// After applying the source generator, compare the resulting string to the inputted one
var updatedDocuments = project.Documents.ToArray();
var expectedSources = testState.Sources.Concat(testState.GeneratedSources).ToArray();

verifier.Equal(expectedSources.Length, updatedDocuments.Length, $"expected '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' to match '{nameof(updatedDocuments)}', but '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' contains '{expectedSources.Length}' documents and '{nameof(updatedDocuments)}' contains '{updatedDocuments.Length}' documents");

for (var i = 0; i < updatedDocuments.Length; i++)
if (!TestBehaviors.HasFlag(TestBehaviors.SkipGeneratedSourcesCheck))
{
var actual = await GetSourceTextFromDocumentAsync(updatedDocuments[i], cancellationToken).ConfigureAwait(false);
verifier.EqualOrDiff(expectedSources[i].content.ToString(), actual.ToString(), $"content of '{expectedSources[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(expectedSources[i].content.Encoding, actual.Encoding, $"encoding of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.Encoding}' but was '{actual.Encoding}'");
verifier.Equal(expectedSources[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(expectedSources[i].filename, updatedDocuments[i].Name, $"file name was expected to be '{expectedSources[i].filename}' but was '{updatedDocuments[i].Name}'");
var updatedDocuments = project.Documents.ToArray();
var expectedSources = testState.Sources.Concat(testState.GeneratedSources).ToArray();

verifier.Equal(expectedSources.Length, updatedDocuments.Length, $"expected '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' to match '{nameof(updatedDocuments)}', but '{nameof(testState)}.{nameof(SolutionState.Sources)}' with '{nameof(testState)}.{nameof(SolutionState.GeneratedSources)}' contains '{expectedSources.Length}' documents and '{nameof(updatedDocuments)}' contains '{updatedDocuments.Length}' documents");

for (var i = 0; i < updatedDocuments.Length; i++)
{
var actual = await GetSourceTextFromDocumentAsync(updatedDocuments[i], cancellationToken).ConfigureAwait(false);
verifier.EqualOrDiff(expectedSources[i].content.ToString(), actual.ToString(), $"content of '{expectedSources[i].filename}' did not match. Diff shown with expected as baseline:");
verifier.Equal(expectedSources[i].content.Encoding, actual.Encoding, $"encoding of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.Encoding}' but was '{actual.Encoding}'");
verifier.Equal(expectedSources[i].content.ChecksumAlgorithm, actual.ChecksumAlgorithm, $"checksum algorithm of '{expectedSources[i].filename}' was expected to be '{expectedSources[i].content.ChecksumAlgorithm}' but was '{actual.ChecksumAlgorithm}'");
verifier.Equal(expectedSources[i].filename, updatedDocuments[i].Name, $"file name was expected to be '{expectedSources[i].filename}' but was '{updatedDocuments[i].Name}'");
}
}

return diagnostics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ public async Task AddSimpleFileWithDiagnostic()
}.RunAsync();
}

[Fact]
public async Task AddImplicitSimpleFileWithDiagnostic()
{
await new CSharpSourceGeneratorTest<AddEmptyFileWithDiagnostic>
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestState =
{
Sources =
{
@"{|#0:|}// Comment",
},
ExpectedDiagnostics =
{
// /0/Test0.cs(1,1): warning SG0001: Message
new DiagnosticResult(AddEmptyFileWithDiagnostic.Descriptor).WithLocation(0),
},
},
}.RunAsync();
}

private class CSharpSourceGeneratorTest<TSourceGenerator> : SourceGeneratorTest<DefaultVerifier>
where TSourceGenerator : ISourceGenerator, new()
{
Expand Down

0 comments on commit e0c5578

Please sign in to comment.