Skip to content

Commit

Permalink
Merge pull request #758 from sharwell/generated-sources
Browse files Browse the repository at this point in the history
Source generator test improvements
  • Loading branch information
sharwell authored Mar 10, 2021
2 parents 85ffbc0 + 78d4055 commit 01f1c94
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> fixableDiagnostics) -> Microsoft.CodeAnalysis.Testing.SolutionState
Microsoft.CodeAnalysis.Testing.SolutionState.WithProcessedMarkup(Microsoft.CodeAnalysis.Testing.MarkupOptions markupOptions, Microsoft.CodeAnalysis.DiagnosticDescriptor defaultDiagnostic, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.DiagnosticDescriptor> supportedDiagnostics, System.Collections.Immutable.ImmutableArray<string> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ protected override IEnumerable<DiagnosticAnalyzer> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,73 @@ public async Task AddSimpleFile()
}.RunAsync();
}

[Fact]
public async Task AddSimpleFileByGeneratorType()
{
await new CSharpSourceGeneratorTest<AddEmptyFile>
{
TestState =
{
Sources =
{
@"// Comment",
},
},
FixedState =
{
Sources =
{
@"// Comment",
(typeof(AddEmptyFile), "EmptyGeneratedFile.cs", string.Empty),
},
},
}.RunAsync();
}

[Fact]
public async Task AddSimpleFileByGeneratorTypeWithEncoding()
{
await new CSharpSourceGeneratorTest<AddEmptyFile>
{
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<AddEmptyFile>
{
TestState =
{
Sources =
{
},
},
FixedState =
{
Sources =
{
(typeof(AddEmptyFile), "EmptyGeneratedFile.cs", string.Empty),
},
},
}.RunAsync();
}

[Fact]
public async Task AddSimpleFileWithDiagnostic()
{
Expand Down

0 comments on commit 01f1c94

Please sign in to comment.