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 the generated builders in the Test project? #8

Open
JoanComasFdz opened this issue Mar 1, 2022 · 2 comments
Open

Add the generated builders in the Test project? #8

JoanComasFdz opened this issue Mar 1, 2022 · 2 comments

Comments

@JoanComasFdz
Copy link

JoanComasFdz commented Mar 1, 2022

Right now the builders are created in the same project where the decorated classes live in.

Given that the Attributes and the SourceGenerator and already split in two NuGet packages, I would like to be able to decorate the classes in the production project, but create the Data Builders in the test project.

Example:

TestApp

  • Dependencies
    • Packages
      • DataBuilderGenerator.Attributes
  • Person.cs

TestApp.Tests

  • Dependencies
    • Analyzers
      • DataBuilderGenerator
    • Packages
      • DataBuilderGenerator

After building the solution:

TestApp.Tests

  • Dependencies
    • Analyzers
      • DataBuilderGenerator
        • PersonBuilder.cs
    • Packages
      • DataBuilderGenerator

This way, production code is not polluted with testing code.

Do you have any directions on how to achieve this?

@dasMulli
Copy link
Owner

dasMulli commented Mar 2, 2022

The attributes (+ assembly) should be removed from the built projects due to the conditional attribute on them..

I was more thinking of doing something completely different and only in the test project like

[assemby: GenerateDataBuilder(typeof(Model))]

or

[GenerateDataBuilder(typeof(Customer))]
public partial class Builders
{
    public static CustomerBuilder DefaultCustomer => Builders.Customer.WithName("foo").WithAddress(...);
}

That also has the added benefit of being able to look at the metadata of the referenced assembly instead of the syntactic/semantic models of the code being built to generate the code, that could benefit from Roslyn 4's incremental generator feature

@JoanComasFdz
Copy link
Author

So I am trying to follow that same approach where a nuget is added to a unit testing project and it automatically generates whatever files are needed, in this repo:

https://github.com/JoanComasFdz/dotnet-scenario-unittesting

I am analyzing the referenced projects, looking for types and generating some files for them.

Unfortunately, this doesn't seem to be working for xUnit projects: dotnet/roslyn-sdk#972

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants