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

Add support for testing IIncrementalGenerator #952

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
@@ -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>()
Copy link
Member

@AraHaan AraHaan Apr 11, 2022

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 an ISourceGenerator that I see so far.

Let me go to definition dive for all that needs changed.

Copy link
Member

@AraHaan AraHaan Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public class CSharpSourceGeneratorTest<TSourceGenerator, TVerifier> : SourceGeneratorTest<TVerifier>
-         where TSourceGenerator : ISourceGenerator, new()
+         where TSourceGenerator : ISourceGenerator, IIncrementalGenerator, new()
        where TVerifier : IVerifier, new()

In SourceGeneratorTest that is inherited from this change:

  • GetSourceGenerators to return object 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 to IIncrementalGenerator.
  • CreateGeneratorDriver to be defined as protected 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 as private async Task<(Project project, ImmutableArray<Diagnostic> diagnostics)> ApplySourceGeneratorAsync(ImmutableArray<object> sourceGenerators, Project project, IVerifier verifier, CancellationToken cancellationToken)
  • VerifySourceGeneratorAsync to be defined as private 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)

where TIncrementalGenerator : IIncrementalGenerator, new()
{
return new IncrementalGeneratorVerifier<TIncrementalGenerator>();
}
}
}
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()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
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>
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>();
}
}
}
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()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
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>
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>();
}
}
}
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()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
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>
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);
}
}
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()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.CodeAnalysis.SourceGenerators.Testing\Microsoft.CodeAnalysis.SourceGenerators.Testing.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>
Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.CSharpIncrementalGeneratorTest() -> void
Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorVerifier<TSourceGenerator, TVerifier>
Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorVerifier<TSourceGenerator, TVerifier>.CSharpIncrementalGeneratorVerifier() -> void
Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>
Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>.CSharpSourceGeneratorTest() -> void
Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorVerifier<TSourceGenerator, TVerifier>
Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorVerifier<TSourceGenerator, TVerifier>.CSharpSourceGeneratorVerifier() -> void
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.CreateCompilationOptions() -> Microsoft.CodeAnalysis.CompilationOptions
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.CreateGeneratorDriver(Microsoft.CodeAnalysis.Project project, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.ISourceGenerator> sourceGenerators, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.IIncrementalGenerator> incrementalGenerators) -> Microsoft.CodeAnalysis.GeneratorDriver
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.CreateParseOptions() -> Microsoft.CodeAnalysis.ParseOptions
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.DefaultFileExt.get -> string
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.GetGenerators() -> (System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.ISourceGenerator> sourceGenerators, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.IIncrementalGenerator> incrementalGenerators)
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpIncrementalGeneratorTest<TIncrementalGenerator, TVerifier>.Language.get -> string
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>.CreateCompilationOptions() -> Microsoft.CodeAnalysis.CompilationOptions
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>.CreateGeneratorDriver(Microsoft.CodeAnalysis.Project project, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.ISourceGenerator> sourceGenerators) -> Microsoft.CodeAnalysis.GeneratorDriver
override Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<TSourceGenerator, TVerifier>.CreateParseOptions() -> Microsoft.CodeAnalysis.ParseOptions
Expand Down
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)
{
}
}
}
Loading