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 Generators: how to unit test? #44075

Closed
kzu opened this issue May 8, 2020 · 8 comments
Closed

Source Generators: how to unit test? #44075

kzu opened this issue May 8, 2020 · 8 comments

Comments

@kzu
Copy link
Contributor

kzu commented May 8, 2020

I'm trying to quickly iterate on my generator by simply running it from a unit test and inspecting the output.

I was hoping I could just construct a SourceGeneratorContext with some fake compilation, additional files and so on, run it, and then inspect what it added to it.

But that's not feasible given its internal constructor.

For now, I just added to my generator a method I call from tests instead:

internal IDictionary<string, SourceText> Execute(Compilation compilation, IEnumerable<AdditionalText> additionalFiles)
@kzu kzu changed the title Source Generators: how to unit test by constructing a SourceGeneratorContext? Source Generators: how to unit test? May 8, 2020
@ufcpp
Copy link
Contributor

ufcpp commented May 8, 2020

GeneratorDriver

https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Test/Semantic/SourceGeneration/GeneratorDriverTests.cs#L61

@jcouv
Copy link
Member

jcouv commented May 9, 2020

FYI @cartermp @chsienki

@jcouv jcouv added this to the 16.7 milestone May 9, 2020
@chsienki
Copy link
Contributor

Yes, as of today, you can use GeneratorDriver in unit tests. There are some examples in our own tests here: http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests/SourceGeneration/GeneratorDriverTests.cs,114

This is an area we're actively working on, and expect to ship templates / shims etc to make it easier to work with in the future

@chsienki chsienki added this to Backlog in Source Generators May 11, 2020
@chsienki chsienki moved this from Backlog to V1 Planned Features in Source Generators Jun 6, 2020
@chsienki
Copy link
Contributor

I've added a gist that shows the basics of setting up unit test for generators here: https://gist.github.com/chsienki/2955ed9336d7eb22bcb246840bfeb05c

@ufcpp
Copy link
Contributor

ufcpp commented Jun 12, 2020

A bit offtopic: I tried creating a generator and it works correctly in [VS 16.7 preview 2] but doesn't with Microsoft.CodeAnalysis.CSharp v3.7.0-1.final (latest version on nuget.org) because the Extending partial method feature is not supported in M.C.C 3.7.0 yet.

@chsienki chsienki modified the milestones: 16.7, 16.8 Jul 1, 2020
@davidwengier
Copy link
Contributor

davidwengier commented Aug 7, 2020

Any thoughts on allowing access to the added sources from GeneratorDriver? I think for some tests it will be nice to validate against the generated code, rather than the resulting final compilation.

If there is a way to do it, I couldn't see an obvious one.

EDIT: I've decided i'm fine with just outputCompilation.SyntaxTrees.Last().ToString().

@chsienki chsienki moved this from V1 Planned Features to Preview 4 Planned in Source Generators Sep 2, 2020
@RussKie
Copy link
Member

RussKie commented Nov 10, 2020

FWIW @davidwengier added a SG to Windows Forms and unit tests for it in dotnet/winforms#4043

@jinujoseph jinujoseph modified the milestones: 16.8, 16.9 Nov 12, 2020
@chsienki chsienki moved this from Preview 4 Planned to Backlog in Source Generators Nov 16, 2020
@chsienki
Copy link
Contributor

Added Unit test instructions to the cookbook at https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md#unit-testing-of-generators

Source Generators automation moved this from Compiler Backlog to Done Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

8 participants