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

Moved WithObject to generated code, added FromTemplate #43

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Contributing

This project welcomes issues and pull requests, although it is an opinionated project. There are many ways to approach this particular problem, and I (Mel Grubb) captured my own preferences when I started this project.

# Building

# Testing

There are currently five test projects.
- BuilderGenerator.Tests.Core: Defines the domain objects used by other test projects.
- BuilderGenerator.Tests.Unit: Directly exercises the builder mechanism to provide fast feedback and verification.
- BuilderGenerator.Tests.Integration.Net60: Uses the builder generator in a more real-world way to create builders for a sample Net 6 library project.
- BuilderGenerator.Tests.Integration.Net70: Same thing, but targeting .Net 7
- BuilderGenerator.Tests.Integration.Net80: Same thing, but targeting .Net 8

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
</PropertyGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.IntegrationTests.Net60</_Parameter1>
<_Parameter1>BuilderGenerator.Tests.Integration.Net60</_Parameter1>
Copy link
Owner Author

Choose a reason for hiding this comment

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

Reorganized the tests quite a bit.

</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.IntegrationTests.Net60</_Parameter1>
<_Parameter1>BuilderGenerator.Tests.Integration.Net70</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.Tests.Integration.Net80</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public abstract class AuditableEntity : Entity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class CollectionTypesSample
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public abstract class Entity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Enums;
using BuilderGenerator.Tests.Core.Models.Enums;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class Order : AuditableEntity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class OrderItem : AuditableEntity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#nullable enable

using System.Collections.Generic;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public partial class User : AuditableEntity
public class User : AuditableEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string? MiddleName { get; set; }

Check warning on line 9 in src/BuilderGenerator.Tests.Core/Models/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}

public partial class User
{
public ICollection<Order> Orders { get; set; } = new List<Order>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BuilderGenerator.IntegrationTests.Core.Models.Enums;
namespace BuilderGenerator.Tests.Core.Models.Enums;

public enum OrderStatus
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# BuilderGenerator.Core #
# BuilderGenerator.Tests.Core

This project defines the object model used in the other framework-specific tests. It is defined as a netstandard2.0 library so that it can be used with the widest variety of consuming projects.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.Tests.Core\BuilderGenerator.Tests.Core.csproj" />
<ProjectReference Include="..\BuilderGenerator.Tests.Unit\BuilderGenerator.Tests.Unit.csproj" />
<ProjectReference Include="..\BuilderGenerator\BuilderGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.IntegrationTests.Core\BuilderGenerator.IntegrationTests.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(CollectionTypesSample))]
public partial class CollectionTypeSampleBuilder
{
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(Order), true)]
public partial class OrderBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(OrderItem))]
public partial class OrderItemBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(User))]
public partial class UserBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BuilderGenerator.Sample.Net60.FromProject
# BuilderGenerator.Sample.Net70.FromProject

This project provides tests the BuilderGenerator package being used in a .Net 6.0 project using a simple series of inter-related classes defined in the BuiderGenerator.Test.Core project.
This project provides tests the BuilderGenerator package being used in a .Net 7.0 project using a simple series of inter-related classes defined in the BuiderGenerator.Test.Core project.

The reference to the BuilderGenerator is a direct project reference, so it can be used to rapidly check the results of changes, but does not represent the real-world usage of the generator. It tests the functionality of the builder, but not the deployment mechanism.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class BuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Core.Models.Enums;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Enums;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class OrderBuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class UserBuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.Tests.Core\BuilderGenerator.Tests.Core.csproj" />
<ProjectReference Include="..\BuilderGenerator.Tests.Unit\BuilderGenerator.Tests.Unit.csproj" />
<ProjectReference Include="..\BuilderGenerator\BuilderGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(CollectionTypesSample))]
public partial class CollectionTypeSampleBuilder
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(Order), true)]
public partial class OrderBuilder
{
public static OrderBuilder Simple()
{
var builder = new OrderBuilder()
.WithId(Guid.NewGuid);

return builder;
}

public static OrderBuilder Typical()
{
var builder = Simple()
.WithItems(
() => new List<OrderItem>
{
OrderItemBuilder.Simple().Build(),
});

return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(OrderItem))]
public partial class OrderItemBuilder
{
public static OrderItemBuilder Simple()
{
var builder = new OrderItemBuilder()
.WithId(Guid.NewGuid);

return builder;
}
}
15 changes: 15 additions & 0 deletions src/BuilderGenerator.Tests.Integration.Net70/Builders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Partial Builders #

This folder contains the hand-written half of any builders. Create static factory methods for each well-known or named object instance you want to create.

## Suggestions ##

In addition to specific factory methods, you should define some general-purpose methods such as:

### Simple/Minimal ###

Creates the simplest, most minimal instance that will pass validation. Only fill in the required fields, and leave everything else at their default values. This can serve as the starting point for other, more specific factory methods. For instance, a minimal Customer may be missing address information, and would have no orders.

### Typical ###

This method should return a "typical" example of the class. For a Customer entity, this might mean that the shipping and billing addresses are filled in, and the Customer has multiple orders in various states.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(User))]
public partial class UserBuilder
{
public static UserBuilder Simple()
{
var builder = new UserBuilder()
.WithId(Guid.NewGuid)
.WithFirstName(() => Guid.NewGuid().ToString())
.WithMiddleName(() => Guid.NewGuid().ToString())
.WithLastName(() => Guid.NewGuid().ToString());

return builder;
}

public static UserBuilder Typical()
{
var builder = Simple()
.WithOrders(
() => new List<Order>
{
OrderBuilder.Simple().Build(),
});

return builder;
}
}
5 changes: 5 additions & 0 deletions src/BuilderGenerator.Tests.Integration.Net70/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# BuilderGenerator.Sample.Net70.FromProject

This project provides tests the BuilderGenerator package being used in a .Net 7.0 project using a simple series of inter-related classes defined in the BuiderGenerator.Test.Core project.

The reference to the BuilderGenerator is a direct project reference, so it can be used to rapidly check the results of changes, but does not represent the real-world usage of the generator. It tests the functionality of the builder, but not the deployment mechanism.
Loading
Loading