diff --git a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/PublicAPI.Unshipped.txt b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/PublicAPI.Unshipped.txt index 4f6444af7..f488f5620 100644 --- a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/PublicAPI.Unshipped.txt +++ b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/PublicAPI.Unshipped.txt @@ -208,6 +208,8 @@ Microsoft.CodeAnalysis.Testing.SolutionState.SolutionState(string name, string l Microsoft.CodeAnalysis.Testing.SolutionState.WithInheritedValuesApplied(Microsoft.CodeAnalysis.Testing.SolutionState baseState, System.Collections.Immutable.ImmutableArray fixableDiagnostics) -> Microsoft.CodeAnalysis.Testing.SolutionState Microsoft.CodeAnalysis.Testing.SolutionState.WithProcessedMarkup(Microsoft.CodeAnalysis.Testing.MarkupOptions markupOptions, Microsoft.CodeAnalysis.DiagnosticDescriptor defaultDiagnostic, System.Collections.Immutable.ImmutableArray supportedDiagnostics, System.Collections.Immutable.ImmutableArray fixableDiagnostics, string defaultPath) -> Microsoft.CodeAnalysis.Testing.SolutionState Microsoft.CodeAnalysis.Testing.SourceFileCollection +Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((System.Type sourceGeneratorType, string filename, Microsoft.CodeAnalysis.Text.SourceText content) file) -> void +Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((System.Type sourceGeneratorType, string filename, string content) file) -> void Microsoft.CodeAnalysis.Testing.SourceFileCollection.Add((string filename, string content) file) -> void Microsoft.CodeAnalysis.Testing.SourceFileCollection.SourceFileCollection() -> void Microsoft.CodeAnalysis.Testing.SourceFileList diff --git a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/SourceFileCollection.cs b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/SourceFileCollection.cs index 673f852b8..a4d69e536 100644 --- a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/SourceFileCollection.cs +++ b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/SourceFileCollection.cs @@ -2,7 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis.Testing @@ -13,5 +17,17 @@ public void Add((string filename, string content) file) { Add((file.filename, SourceText.From(file.content))); } + + public void Add((Type sourceGeneratorType, string filename, string content) file) + { + var contentWithEncoding = SourceText.From(file.content, Encoding.UTF8); + Add((file.sourceGeneratorType, file.filename, contentWithEncoding)); + } + + public void Add((Type sourceGeneratorType, string filename, SourceText content) file) + { + var generatedPath = Path.Combine(file.sourceGeneratorType.GetTypeInfo().Assembly.GetName().Name ?? string.Empty, file.sourceGeneratorType.FullName!, file.filename); + Add((generatedPath, file.content)); + } } } diff --git a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing/SourceGeneratorTest`1.cs b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing/SourceGeneratorTest`1.cs index 2fd0206cf..7120b5256 100644 --- a/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing/SourceGeneratorTest`1.cs +++ b/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing/SourceGeneratorTest`1.cs @@ -60,8 +60,6 @@ protected override IEnumerable GetDiagnosticAnalyzers() public override async Task RunAsync(CancellationToken cancellationToken = default) { - Verify.NotEmpty($"{nameof(TestState)}.{nameof(SolutionState.Sources)}", TestState.Sources); - var analyzers = GetDiagnosticAnalyzers().ToArray(); var defaultDiagnostic = GetDefaultDiagnostic(analyzers); var supportedDiagnostics = analyzers.SelectMany(analyzer => analyzer.SupportedDiagnostics).ToImmutableArray(); diff --git a/tests/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests/SourceGeneratorValidationTests.cs b/tests/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests/SourceGeneratorValidationTests.cs index 34bd58c72..b226e905f 100644 --- a/tests/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests/SourceGeneratorValidationTests.cs +++ b/tests/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests/SourceGeneratorValidationTests.cs @@ -38,6 +38,73 @@ public async Task AddSimpleFile() }.RunAsync(); } + [Fact] + public async Task AddSimpleFileByGeneratorType() + { + await new CSharpSourceGeneratorTest + { + TestState = + { + Sources = + { + @"// Comment", + }, + }, + FixedState = + { + Sources = + { + @"// Comment", + (typeof(AddEmptyFile), "EmptyGeneratedFile.cs", string.Empty), + }, + }, + }.RunAsync(); + } + + [Fact] + public async Task AddSimpleFileByGeneratorTypeWithEncoding() + { + await new CSharpSourceGeneratorTest + { + TestState = + { + Sources = + { + @"// Comment", + }, + }, + FixedState = + { + Sources = + { + @"// Comment", + (typeof(AddEmptyFile), "EmptyGeneratedFile.cs", SourceText.From(string.Empty, Encoding.UTF8)), + }, + }, + }.RunAsync(); + } + + [Fact] + public async Task AddSimpleFileToEmptyProject() + { + await new CSharpSourceGeneratorTest + { + TestState = + { + Sources = + { + }, + }, + FixedState = + { + Sources = + { + (typeof(AddEmptyFile), "EmptyGeneratedFile.cs", string.Empty), + }, + }, + }.RunAsync(); + } + [Fact] public async Task AddSimpleFileWithDiagnostic() {