-
Notifications
You must be signed in to change notification settings - Fork 256
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
Add support for testing IIncrementalGenerator #952
Draft
mhutch
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
mhutch:incremental-generators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+383
−141
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...osoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest/IncrementalGeneratorVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.MSTest | ||
{ | ||
public static class IncrementalGeneratorVerifier | ||
{ | ||
public static IncrementalGeneratorVerifier<TIncrementalGenerator> Create<TIncrementalGenerator>() | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
return new IncrementalGeneratorVerifier<TIncrementalGenerator>(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...oft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest/IncrementalGeneratorVerifier`1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.MSTest | ||
{ | ||
public class IncrementalGeneratorVerifier<TIncrementalGenerator> : CSharpIncrementalGeneratorVerifier<TIncrementalGenerator, MSTestVerifier> | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ing/Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.IncrementalGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.IncrementalGeneratorVerifier<TIncrementalGenerator>.IncrementalGeneratorVerifier() -> void | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.SourceGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.SourceGeneratorVerifier<TSourceGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.MSTest.SourceGeneratorVerifier<TSourceGenerator>.SourceGeneratorVerifier() -> void | ||
static Microsoft.CodeAnalysis.CSharp.Testing.MSTest.IncrementalGeneratorVerifier.Create<TIncrementalGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.MSTest.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
static Microsoft.CodeAnalysis.CSharp.Testing.MSTest.SourceGeneratorVerifier.Create<TSourceGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.MSTest.SourceGeneratorVerifier<TSourceGenerator> |
15 changes: 15 additions & 0 deletions
15
...rosoft.CodeAnalysis.CSharp.SourceGenerators.Testing.NUnit/IncrementalGeneratorVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.NUnit | ||
{ | ||
public static class IncrementalGeneratorVerifier | ||
{ | ||
public static IncrementalGeneratorVerifier<TIncrementalGenerator> Create<TIncrementalGenerator>() | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
return new IncrementalGeneratorVerifier<TIncrementalGenerator>(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...soft.CodeAnalysis.CSharp.SourceGenerators.Testing.NUnit/IncrementalGeneratorVerifier`1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.NUnit | ||
{ | ||
public class IncrementalGeneratorVerifier<TIncrementalGenerator> : CSharpIncrementalGeneratorVerifier<TIncrementalGenerator, NUnitVerifier> | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ting/Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.NUnit/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.IncrementalGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.IncrementalGeneratorVerifier<TIncrementalGenerator>.IncrementalGeneratorVerifier() -> void | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.SourceGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.SourceGeneratorVerifier<TSourceGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.SourceGeneratorVerifier<TSourceGenerator>.SourceGeneratorVerifier() -> void | ||
static Microsoft.CodeAnalysis.CSharp.Testing.NUnit.IncrementalGeneratorVerifier.Create<TIncrementalGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.NUnit.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
static Microsoft.CodeAnalysis.CSharp.Testing.NUnit.SourceGeneratorVerifier.Create<TSourceGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.NUnit.SourceGeneratorVerifier<TSourceGenerator> |
15 changes: 15 additions & 0 deletions
15
...rosoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit/IncrementalGeneratorVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.XUnit | ||
{ | ||
public static class IncrementalGeneratorVerifier | ||
{ | ||
public static IncrementalGeneratorVerifier<TIncrementalGenerator> Create<TIncrementalGenerator>() | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
return new IncrementalGeneratorVerifier<TIncrementalGenerator>(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...soft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit/IncrementalGeneratorVerifier`1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing.XUnit | ||
{ | ||
public class IncrementalGeneratorVerifier<TIncrementalGenerator> : CSharpIncrementalGeneratorVerifier<TIncrementalGenerator, XUnitVerifier> | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ting/Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.IncrementalGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.IncrementalGeneratorVerifier<TIncrementalGenerator>.IncrementalGeneratorVerifier() -> void | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.SourceGeneratorVerifier | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.SourceGeneratorVerifier<TSourceGenerator> | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.SourceGeneratorVerifier<TSourceGenerator>.SourceGeneratorVerifier() -> void | ||
static Microsoft.CodeAnalysis.CSharp.Testing.XUnit.IncrementalGeneratorVerifier.Create<TIncrementalGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.XUnit.IncrementalGeneratorVerifier<TIncrementalGenerator> | ||
static Microsoft.CodeAnalysis.CSharp.Testing.XUnit.SourceGeneratorVerifier.Create<TSourceGenerator>() -> Microsoft.CodeAnalysis.CSharp.Testing.XUnit.SourceGeneratorVerifier<TSourceGenerator> |
43 changes: 43 additions & 0 deletions
43
...icrosoft.CodeAnalysis.CSharp.SourceGenerators.Testing/CSharpIncrementalGeneratorTest`2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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.Collections.Immutable; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing | ||
{ | ||
public class CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier> : IncrementalGeneratorTest<TVerifier> | ||
where TIncrementalGenerator : IIncrementalGenerator, new() | ||
where TVerifier : IVerifier, new() | ||
{ | ||
private static readonly LanguageVersion DefaultLanguageVersion = | ||
Enum.TryParse("Default", out LanguageVersion version) ? version : LanguageVersion.CSharp6; | ||
|
||
protected override (IEnumerable<ISourceGenerator> sourceGenerators, IEnumerable<IIncrementalGenerator> incrementalGenerators) GetGenerators() | ||
=> (Enumerable.Empty<ISourceGenerator>(), new IIncrementalGenerator[] { new TIncrementalGenerator() }); | ||
|
||
protected override string DefaultFileExt => "cs"; | ||
|
||
public override string Language => LanguageNames.CSharp; | ||
|
||
protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators, ImmutableArray<IIncrementalGenerator> incrementalGenerators) | ||
{ | ||
return CSharpGeneratorDriver.Create( | ||
sourceGenerators.Concat(incrementalGenerators.Select(g => g.AsSourceGenerator())), | ||
project.AnalyzerOptions.AdditionalFiles, | ||
(CSharpParseOptions)project.ParseOptions!, | ||
project.AnalyzerOptions.AnalyzerConfigOptionsProvider); | ||
} | ||
|
||
protected override CompilationOptions CreateCompilationOptions() | ||
=> new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); | ||
|
||
protected override ParseOptions CreateParseOptions() | ||
=> new CSharpParseOptions(DefaultLanguageVersion, DocumentationMode.Diagnose); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...soft.CodeAnalysis.CSharp.SourceGenerators.Testing/CSharpIncrementalGeneratorVerifier`2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Testing | ||
{ | ||
public class CSharpIncrementalGeneratorVerifier<TSourceGenerator, TVerifier> : IncrementalGeneratorVerifier<TSourceGenerator, CSharpIncrementalGeneratorTest<TSourceGenerator, TVerifier>, TVerifier> | ||
where TSourceGenerator : IIncrementalGenerator, new() | ||
where TVerifier : IVerifier, new() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...is.Testing/Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ting/Microsoft.CodeAnalysis.SourceGenerators.Testing/EmptyIncrementalGeneratorProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.Testing | ||
{ | ||
/// <summary> | ||
/// Defines a <see cref="IIncrementalGenerator"/> which does not add any sources. | ||
/// </summary> | ||
public sealed class EmptyIncrementalGeneratorProvider : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to make new types (I think), one can add
, IIncrementalGenerator, new()
to the contraints on the ones already accepting anISourceGenerator
that I see so far.Let me go to definition dive for all that needs changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In SourceGeneratorTest that is inherited from this change:
GetSourceGenerators
to returnobject
instead of ISourceGenerator so that way CSharpSourceGeneratorTest would just work with it (have it down cast to object) then have SourceGeneratorTest do a type check then cast operation to ISourceGenerator or toIIncrementalGenerator
.CreateGeneratorDriver
to be defined asprotected abstract GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<object> sourceGenerators);
(again when using it would need to type check and cast to use the instances).ApplySourceGeneratorAsync
to be defined asprivate async Task<(Project project, ImmutableArray<Diagnostic> diagnostics)> ApplySourceGeneratorAsync(ImmutableArray<object> sourceGenerators, Project project, IVerifier verifier, CancellationToken cancellationToken)
VerifySourceGeneratorAsync
to be defined asprivate async Task<ImmutableArray<Diagnostic>> VerifySourceGeneratorAsync(string language, ImmutableArray<object> sourceGenerators, SolutionState testState, Func<ImmutableArray<object>, Project, IVerifier, CancellationToken, Task<(Project project, ImmutableArray<Diagnostic> diagnostics)>> getFixedProject, IVerifier verifier, CancellationToken cancellationToken)