Roslyn Source Generator #11
Replies: 9 comments 50 replies
-
I would love this. I tried once, but realized that it needs quite much experties to get it working and I did not have time to dig into that. If you have expereicnce with it, especially with the incremental generators, I would be happy to discuss this in detail, so that maybe you can do a POC for it. (Others can of course help too.) |
Beta Was this translation helpful? Give feedback.
-
As far as I know, source generators are not available for .NET Framework. |
Beta Was this translation helpful? Give feedback.
-
As promised, here is my brain dump of my ideas about using the Roslyn code generators. I was thinking about that a year ago, so some things might have changed. Looking forward to your thoughts. I have seen two levels of implementation. Level 1On this level, basically we would generate more or less the same code what is generated through MsBuild right now, but with Roslyn code generators. What we would win:
As part of this level, we have to consider the following questions:
Level 2So once we cleared a couple of questions with Level 1, we can move on to level 2 where the things get even more exciting in my opinion. In level 2, we would not only generate the "code-behind" files for the feature files, but we would also process the step definitions during compile time (not easy, but maybe somehow possible). With that information, we would already be able to have a good guess which step definition method will be called if a particular step is going to be executed (for most of the cases we will now it 100%). Knowing that, we can even include lines to the generated code that actually invoke that step definition method. There are two key benefits of this
So for example if there is a step
Of course this is just a simple case, but maybe the concept is visible. We would still need to handle multi-matches (for scenario outlines) and step definition parameters and probably a couple of other tricks as well. Also we need to handle step definitions from external assemblies. For that my idea would be that once we compile and discover the step definitions anyway, we keep this discovery info and save it as a separate file or as an embedded resource. So if you consume an external step definition assembly, you immediately get the step definition information as well. This could be used by the IDE integration as well BTW, currently the VS integration is doing something similar. |
Beta Was this translation helpful? Give feedback.
-
Cross-linking the discussion Directly provide ITestDiscoverer and ITestExecutor, instead of a host test framework that is basically trying to achieve a similar goal with a different approach. But some of the questions (e.g. the necessity of generator plugins) is the same. |
Beta Was this translation helpful? Give feedback.
-
Ideas for the proof of concept (POC):The best would be to have a POC that is somewhat integrates to the Reqnroll infrastructure, so that we can see the impact, but uses as many shortcuts as possible, so that we can get a quick validation. I'm not hoping for this, but it is even possible that we need to cancel this, so keep it simple. Here are my thoughts, everything is flexible: Integrated to Reqnroll infrastructure
ShortcutsBasically it is enough if it is able to run super basic scenarios from a simple Reqnroll project.
|
Beta Was this translation helpful? Give feedback.
-
I've created a prototype branch for a generator here: https://github.com/reqnroll/Reqnroll/tree/source-generator-poc Two projects have been added:
The following functions are demonstrated:
Not demonstrated:
Apologies for the delay in getting this out there; my life has been hectic for the last week. Hopefully this branch will serve as a basis for further conversation and investigation. |
Beta Was this translation helpful? Give feedback.
-
Regarding Generator Plug-InsPlugins remain my biggest concern. The Easy Path We gain a great deal of simplicity, we lose the ability for users to customize their test-generation without going through an existing mechanism. For simple scenarios that just want to include an attribute, I can imagine generic options driven through config or user code. For more complex things (like scenarios driven by external data sources) we'd have to directly incorporate that feature into the generator. The Dangerous Path
I quite like being able to write plug-ins for the generator. I've found them to be quite difficult to get right. Very, incredibly difficult. Obviously it's quite a niche space - most users just want something they can install that works and does what they need. I could not find any examples of Roslyn components doing this - the ones I see being produced are designed to do very specific, very narrow tasks: create a strongly-typed ResX wrapper, create native Regex matcher, create a JSON serializer for a type. My gut feeling is to narrow the scope to what Roslyn seems best suited for and take the easy path. What do your respective guts have to say? |
Beta Was this translation helpful? Give feedback.
-
Implementation is progressing, and I thought I'd try integration with our Spec project as a way to prove out the Roslyn implementation, first as a way to test within the test project as an alternative configuration (using It was at this point I noticed that we have a plug-in for generating different flavours of each feature: target framework, test framework, etc. This has long been something I think is broadly useful and have often wanted to implement: a way to have a whole test suite run with multiple base configurations. It comes up very often dealing with cross-browser compatibility testing, but I'm sure there are many opportunities this would be valuable. I propose we bake this into
We'd need to determine how to name the generated classes based on the values of the flavor parameters. It could just be a case of calling We'd also need to determine how an end-user could supply all the permutations in a way that is visible at generation-time. Attributes are easiest, but have the most limitations (can't do complex types):
There's probably some additional effort in supporting this for bindings outside of the feature assembly, but I think that can come later. |
Beta Was this translation helpful? Give feedback.
-
Just been looking at adding scenario outline support. The current implementation leverages parameterised tests to achieve this and I wanted to know if there was any specific reason for this approach? I was contemplating just emitting additional methods for each example and giving them display names to organise them accordingly, as this would avoid the need to handle inline test data and lets us put tag attributes on individual examples. The only immediate downside I am aware of is an increase in the size output assembly (since it has additional methods) but I could personally live with that. 😅 |
Beta Was this translation helpful? Give feedback.
-
Should Reqnroll's code-generation be moved to a Roslyn source generator? It was mentioned a few times on the SpecFlow project and wondered if it was relevant to Reqnroll's future plans.
I think there are numerous minor benefits to a source generator over the existing code-generator:
I have been experimenting with writing a source generator for this purpose and got as far as getting a generator to produce a simple output every time I changed a feature file.
Beta Was this translation helpful? Give feedback.
All reactions