Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source generator test improvements #758

Merged
merged 2 commits into from
Mar 10, 2021
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
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