Skip to content

Commit

Permalink
#13 fixed netstandard2.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MelGrubb committed Aug 7, 2022
1 parent 7f13276 commit 70f5fc8
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 4 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Mel Grubb

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/AuditableEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public abstract class AuditableEntity : Entity
{
public DateTime CreatedAt { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedAt { get; set; }
public string UpdatedBy { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public class CollectionTypesSample
{
public string[] ReadOnlyArray { get; } = Array.Empty<string>();
public ICollection<string> ReadOnlyCollection { get; } = new List<string>();
public IEnumerable<string> ReadOnlyEnumerable { get; } = new List<string>();
public HashSet<string> ReadOnlyHashSet { get; } = new HashSet<string>();
public List<string> ReadOnlyList { get; } = new List<string>();

public string[] ReadWriteArray { get; set; } = Array.Empty<string>();

public ICollection<string> ReadWriteCollection { get; set; } = new List<string>();

public IEnumerable<string> ReadWriteEnumerable { get; set; } = new List<string>();

public HashSet<string> ReadWriteHashSet { get; set; } = new HashSet<string>();

public List<string> ReadWriteList { get; set; } = new List<string>();
}
}
9 changes: 9 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public abstract class Entity
{
public Guid Id { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.Test.Core.Models.Enums;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public class Order : AuditableEntity
{
public DateTime OrderDate { get; set; }
public ICollection<OrderItem> Orders { get; set; } = new List<OrderItem>();
public OrderStatus Status { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/OrderItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public class OrderItem : AuditableEntity
{
public Guid ItemId { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Entities #

This folder contains sample entities for which builders will be created. It's your standard, generic Foo/Bar/Baz hierarchy, with edge cases exercised as described blow.

## Hierarchy ##

Foo contains a read-only collection or Bars.
Bars contains a read-only collection of Bazs.
Baz has no children of its own.

Each layer also contains writable collections of strings in various formats to test the Builder's ability to adapt and choose appropriate properties to handle.
14 changes: 14 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Entities/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#nullable enable

using System.Collections.Generic;

namespace BuilderGenerator.Test.Core.Models.Entities
{
public class User : AuditableEntity
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? MiddleName { get; set; }
public ICollection<Order> Orders { get; set; } = new List<Order>();
}
}
11 changes: 11 additions & 0 deletions src/BuilderGenerator.Test.Core/Models/Enums/OrderStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BuilderGenerator.Test.Core.Models.Enums
{
public enum OrderStatus
{
Received = 1,
Processing = 2,
Shipping = 3,
Shipped = 4,
Completed = 5,
}
}
3 changes: 3 additions & 0 deletions src/BuilderGenerator.Test.Core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BuilderGenerator.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.
18 changes: 18 additions & 0 deletions src/BuilderGenerator.Test.Net60/BuilderGenerator.Test.Net60.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.Test.Core\BuilderGenerator.Test.Core.csproj" />
<ProjectReference Include="..\BuilderGenerator\BuilderGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Shouldly" Version="4.0.3" />
</ItemGroup>

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

namespace BuilderGenerator.Test.Net60.Builders;

[BuilderFor(typeof(CollectionTypesSample))]
public partial class CollectionTypeSampleBuilder
{
}
16 changes: 16 additions & 0 deletions src/BuilderGenerator.Test.Net60/Builders/OrderBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using BuilderGenerator.Test.Core.Models.Entities;

namespace BuilderGenerator.Test.Net60.Builders;

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

return builder;
}
}
16 changes: 16 additions & 0 deletions src/BuilderGenerator.Test.Net60/Builders/OrderItemBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using BuilderGenerator.Test.Core.Models.Entities;

namespace BuilderGenerator.Test.Net60.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.Test.Net60/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.
32 changes: 32 additions & 0 deletions src/BuilderGenerator.Test.Net60/Builders/UserBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.Test.Core.Models.Entities;

namespace BuilderGenerator.Test.Net60.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.Test.Net60/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# BuilderGenerator.Test.Net60 #

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.

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.
65 changes: 65 additions & 0 deletions src/BuilderGenerator.Test.Net60/Tests/BuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using BuilderGenerator.Test.Core.Models.Entities;
using BuilderGenerator.Test.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.Test.Net60.Tests;

[TestFixture]
public class BuilderTests
{
private string _firstName;
private Guid _id;
private string _lastName;
private string _middleName;
private User _result;

[Test]
public void Simple_does_not_populate_Orders()
{
var actual = UserBuilder.Simple().Build();
actual.ShouldBeOfType<User>();
actual.Orders.ShouldBeNull();
}

[Test]
public void Simple_returns_a_UserBuilder() => UserBuilder.Simple().ShouldBeOfType<UserBuilder>();

[Test]
public void Typical_populates_Orders()
{
var actual = UserBuilder.Typical().Build();
actual.ShouldBeOfType<User>();
actual.Orders.ShouldNotBeNull();
}

[Test]
public void Typical_returns_a_UserBuilder() => UserBuilder.Typical().ShouldBeOfType<UserBuilder>();

[Test]
public void UserBuilder_can_set_properties()
{
_result.Id.ShouldBe(_id);
_result.FirstName.ShouldBe(_firstName);
_result.MiddleName.ShouldBe(_middleName);
_result.LastName.ShouldBe(_lastName);
}

[OneTimeSetUp]
public void SetUp()
{
_id = Guid.NewGuid();
_firstName = Guid.NewGuid().ToString();
_middleName = Guid.NewGuid().ToString();
_lastName = Guid.NewGuid().ToString();

_result = UserBuilder
.Typical()
.WithId(_id)
.WithFirstName(_firstName)
.WithMiddleName(_middleName)
.WithLastName(_lastName)
.Build();
}
}
11 changes: 7 additions & 4 deletions src/BuilderGenerator/BuilderGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<!--Is preview still Needed?-->
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<AssemblyVersion>2.0.6</AssemblyVersion>
<FileVersion>2.0.6</FileVersion>
<Version>2.0.6</Version>
<AssemblyVersion>2.0.7</AssemblyVersion>
<FileVersion>2.0.7</FileVersion>
<Version>2.0.7</Version>
<PackageId>BuilderGenerator</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Mel Grubb</Authors>
Expand All @@ -19,7 +19,10 @@
<RepositoryUrl>https://github.com/MelGrubb/BuilderGenerator</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>BDD;TDD;Testing;Builders;Code Generation;Source Generators</PackageTags>
<PackageReleaseNotes>v2.0.6
<PackageReleaseNotes>v2.0.7
- Fixed #13, NetStandard2.0 compatibility

v2.0.6
- Fixed #12, Generated files now marked with auth-generated header

v2.0.5
Expand Down

0 comments on commit 70f5fc8

Please sign in to comment.