data-builder-generator icon indicating copy to clipboard operation
data-builder-generator copied to clipboard

Add the generated builders in the Test project?

Open JoanComasFdz opened this issue 2 years ago • 2 comments

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?

JoanComasFdz avatar Mar 01 '22 14:03 JoanComasFdz

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

dasMulli avatar Mar 02 '22 10:03 dasMulli

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: https://github.com/dotnet/roslyn-sdk/issues/972#

JoanComasFdz avatar Mar 05 '22 09:03 JoanComasFdz