Remove the ISourceGenerator type constraint
This change simplifies the pattern for testing IIncrementalGenerator without removing the ability to test ISourceGenerator.
This commit is contained in:
Родитель
d4e8061f34
Коммит
ed0da4913f
|
@ -281,21 +281,21 @@ namespace Microsoft.CodeAnalysis.Testing
|
|||
return ImmutableArray<Diagnostic>.Empty;
|
||||
}
|
||||
|
||||
return await VerifySourceGeneratorAsync(Language, GetSourceGenerators().ToImmutableArray(), testState, ApplySourceGeneratorAsync, verifier.PushContext("Source generator application"), cancellationToken);
|
||||
return await VerifySourceGeneratorAsync(Language, sourceGenerators, testState, ApplySourceGeneratorsAsync, verifier.PushContext("Source generator application"), cancellationToken);
|
||||
}
|
||||
|
||||
private protected async Task<ImmutableArray<Diagnostic>> VerifySourceGeneratorAsync(
|
||||
string language,
|
||||
ImmutableArray<Type> sourceGenerators,
|
||||
SolutionState testState,
|
||||
Func<ImmutableArray<Type>, Project, IVerifier, CancellationToken, Task<(Project project, ImmutableArray<Diagnostic> diagnostics)>> getFixedProject,
|
||||
Func<ImmutableArray<Type>, Project, IVerifier, CancellationToken, Task<(Project project, ImmutableArray<Diagnostic> diagnostics)>> applySourceGenerators,
|
||||
IVerifier verifier,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var project = await CreateProjectAsync(new EvaluatedProjectState(testState, ReferenceAssemblies), testState.AdditionalProjects.Values.Select(additionalProject => new EvaluatedProjectState(additionalProject, ReferenceAssemblies)).ToImmutableArray(), cancellationToken);
|
||||
|
||||
ImmutableArray<Diagnostic> diagnostics;
|
||||
(project, diagnostics) = await getFixedProject(sourceGenerators, project, verifier, cancellationToken).ConfigureAwait(false);
|
||||
(project, diagnostics) = await applySourceGenerators(sourceGenerators, project, verifier, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// After applying the source generator, compare the resulting string to the inputted one
|
||||
if (!TestBehaviors.HasFlag(TestBehaviors.SkipGeneratedSourcesCheck))
|
||||
|
@ -1153,11 +1153,11 @@ namespace Microsoft.CodeAnalysis.Testing
|
|||
|
||||
protected virtual async Task<(Compilation compilation, ImmutableArray<Diagnostic> generatorDiagnostics)> GetProjectCompilationAsync(Project project, IVerifier verifier, CancellationToken cancellationToken)
|
||||
{
|
||||
var (finalProject, generatorDiagnostics) = await ApplySourceGeneratorAsync(GetSourceGenerators().ToImmutableArray(), project, verifier, cancellationToken).ConfigureAwait(false);
|
||||
var (finalProject, generatorDiagnostics) = await ApplySourceGeneratorsAsync(GetSourceGenerators().ToImmutableArray(), project, verifier, cancellationToken).ConfigureAwait(false);
|
||||
return ((await finalProject.GetCompilationAsync(cancellationToken).ConfigureAwait(false))!, generatorDiagnostics);
|
||||
}
|
||||
|
||||
private protected async Task<(Project project, ImmutableArray<Diagnostic> diagnostics)> ApplySourceGeneratorAsync(ImmutableArray<Type> sourceGeneratorTypes, Project project, IVerifier verifier, CancellationToken cancellationToken)
|
||||
private protected async Task<(Project project, ImmutableArray<Diagnostic> diagnostics)> ApplySourceGeneratorsAsync(ImmutableArray<Type> sourceGeneratorTypes, Project project, IVerifier verifier, CancellationToken cancellationToken)
|
||||
{
|
||||
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
|
||||
verifier.True(compilation is { });
|
||||
|
@ -1228,6 +1228,7 @@ namespace Microsoft.CodeAnalysis.Testing
|
|||
else
|
||||
{
|
||||
var iincrementalGeneratorType = isourceGeneratorType.GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.IIncrementalGenerator");
|
||||
verifier.True(iincrementalGeneratorType?.IsAssignableFrom(sourceGenerators[i].GetType()) ?? false, $"'{sourceGenerators[i].GetType().FullName}' must implement '{iincrementalGeneratorType.FullName}' or '{isourceGeneratorType.FullName}'");
|
||||
var asGeneratorMethod = (from method in isourceGeneratorType.GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.GeneratorExtensions")!.GetMethods()
|
||||
where method is { Name: "AsSourceGenerator", IsStatic: true, IsPublic: true }
|
||||
let parameterTypes = method.GetParameters().Select(parameter => parameter.ParameterType).ToArray()
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Testing.MSTest
|
|||
public static class SourceGeneratorVerifier
|
||||
{
|
||||
public static SourceGeneratorVerifier<TSourceGenerator> Create<TSourceGenerator>()
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
return new SourceGeneratorVerifier<TSourceGenerator>();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.Testing.Verifiers;
|
|||
namespace Microsoft.CodeAnalysis.CSharp.Testing.MSTest
|
||||
{
|
||||
public class SourceGeneratorVerifier<TSourceGenerator> : CSharpSourceGeneratorVerifier<TSourceGenerator, MSTestVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Testing.NUnit
|
|||
public static class SourceGeneratorVerifier
|
||||
{
|
||||
public static SourceGeneratorVerifier<TSourceGenerator> Create<TSourceGenerator>()
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
return new SourceGeneratorVerifier<TSourceGenerator>();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.Testing.Verifiers;
|
|||
namespace Microsoft.CodeAnalysis.CSharp.Testing.NUnit
|
||||
{
|
||||
public class SourceGeneratorVerifier<TSourceGenerator> : CSharpSourceGeneratorVerifier<TSourceGenerator, NUnitVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Testing.XUnit
|
|||
public static class SourceGeneratorVerifier
|
||||
{
|
||||
public static SourceGeneratorVerifier<TSourceGenerator> Create<TSourceGenerator>()
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
return new SourceGeneratorVerifier<TSourceGenerator>();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.Testing.Verifiers;
|
|||
namespace Microsoft.CodeAnalysis.CSharp.Testing.XUnit
|
||||
{
|
||||
public class SourceGeneratorVerifier<TSourceGenerator> : CSharpSourceGeneratorVerifier<TSourceGenerator, XUnitVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ using Microsoft.CodeAnalysis.Testing;
|
|||
namespace Microsoft.CodeAnalysis.CSharp.Testing
|
||||
{
|
||||
public class CSharpSourceGeneratorTest<TSourceGenerator, TVerifier> : SourceGeneratorTest<TVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
where TVerifier : IVerifier, new()
|
||||
{
|
||||
private static readonly LanguageVersion DefaultLanguageVersion =
|
||||
|
|
|
@ -7,7 +7,7 @@ using Microsoft.CodeAnalysis.Testing;
|
|||
namespace Microsoft.CodeAnalysis.CSharp.Testing
|
||||
{
|
||||
public class CSharpSourceGeneratorVerifier<TSourceGenerator, TVerifier> : SourceGeneratorVerifier<TSourceGenerator, CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>, TVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
where TVerifier : IVerifier, new()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace Microsoft.CodeAnalysis.Testing
|
|||
/// <summary>
|
||||
/// Returns the source generators being tested - to be implemented in non-abstract class.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="ISourceGenerator"/> to be used.</returns>
|
||||
/// <returns>The <see cref="ISourceGenerator"/> and/or <see cref="T:Microsoft.CodeAnalysis.IIncrementalGenerator"/> to be used.</returns>
|
||||
protected override abstract IEnumerable<Type> GetSourceGenerators();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,11 @@ namespace Microsoft.CodeAnalysis.Testing
|
|||
/// <summary>
|
||||
/// A default verifier for source generators.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSourceGenerator">The <see cref="ISourceGenerator"/> to test.</typeparam>
|
||||
/// <typeparam name="TSourceGenerator">The <see cref="ISourceGenerator"/> or <see cref="T:Microsoft.CodeAnalysis.IIncrementalGenerator"/> to test.</typeparam>
|
||||
/// <typeparam name="TTest">The test implementation to use.</typeparam>
|
||||
/// <typeparam name="TVerifier">The type of verifier to use.</typeparam>
|
||||
public class SourceGeneratorVerifier<TSourceGenerator, TTest, TVerifier>
|
||||
where TSourceGenerator : ISourceGenerator, new()
|
||||
where TSourceGenerator : new()
|
||||
where TTest : SourceGeneratorTest<TVerifier>, new()
|
||||
where TVerifier : IVerifier, new()
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
Module SourceGeneratorVerifier
|
||||
Public Function Create(Of TSourceGenerator As {ISourceGenerator, New})() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Public Function Create(Of TSourceGenerator As New)() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Return New SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
End Function
|
||||
End Module
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
Imports Microsoft.CodeAnalysis.Testing.Verifiers
|
||||
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As {ISourceGenerator, New})
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As New)
|
||||
Inherits VisualBasicSourceGeneratorVerifier(Of TSourceGenerator, MSTestVerifier)
|
||||
End Class
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
Module SourceGeneratorVerifier
|
||||
Public Function Create(Of TSourceGenerator As {ISourceGenerator, New})() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Public Function Create(Of TSourceGenerator As New)() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Return New SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
End Function
|
||||
End Module
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
Imports Microsoft.CodeAnalysis.Testing.Verifiers
|
||||
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As {ISourceGenerator, New})
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As New)
|
||||
Inherits VisualBasicSourceGeneratorVerifier(Of TSourceGenerator, NUnitVerifier)
|
||||
End Class
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
Module SourceGeneratorVerifier
|
||||
Public Function Create(Of TSourceGenerator As {ISourceGenerator, New})() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Public Function Create(Of TSourceGenerator As New)() As SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
Return New SourceGeneratorVerifier(Of TSourceGenerator)
|
||||
End Function
|
||||
End Module
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
Imports Microsoft.CodeAnalysis.Testing.Verifiers
|
||||
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As {ISourceGenerator, New})
|
||||
Public Class SourceGeneratorVerifier(Of TSourceGenerator As New)
|
||||
Inherits VisualBasicSourceGeneratorVerifier(Of TSourceGenerator, XUnitVerifier)
|
||||
End Class
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
Imports Microsoft.CodeAnalysis.Testing
|
||||
|
||||
Public Class VisualBasicSourceGeneratorTest(Of TSourceGenerator As {ISourceGenerator, New}, TVerifier As {IVerifier, New})
|
||||
Public Class VisualBasicSourceGeneratorTest(Of TSourceGenerator As New, TVerifier As {IVerifier, New})
|
||||
Inherits SourceGeneratorTest(Of TVerifier)
|
||||
|
||||
Private Shared ReadOnly DefaultLanguageVersion As LanguageVersion =
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
Imports Microsoft.CodeAnalysis.Testing
|
||||
|
||||
Public Class VisualBasicSourceGeneratorVerifier(Of TSourceGenerator As {ISourceGenerator, New}, TVerifier As {IVerifier, New})
|
||||
Public Class VisualBasicSourceGeneratorVerifier(Of TSourceGenerator As New, TVerifier As {IVerifier, New})
|
||||
Inherits SourceGeneratorVerifier(Of TSourceGenerator, VisualBasicSourceGeneratorTest(Of TSourceGenerator, TVerifier), TVerifier)
|
||||
End Class
|
||||
|
|
Загрузка…
Ссылка в новой задаче