-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+semver:major Moved builders into conventions
- Loading branch information
1 parent
d51a0da
commit 89a6202
Showing
12 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Rocket.Surgery.Builders | ||
{ | ||
/// <summary> | ||
/// A common interface for creating builder classes | ||
/// </summary> | ||
public interface IBuilder | ||
{ | ||
/// <summary> | ||
/// A central location for sharing state between components during the convention building process. | ||
/// </summary> | ||
IDictionary<object, object> Properties { get; } | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Builders.Abstractions/Rocket.Surgery.Builders.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Rocket.Surgery.Builders | ||
{ | ||
/// <summary> | ||
/// Abstract base class for implementing a builder | ||
/// </summary> | ||
public abstract class Builder : IBuilder | ||
{ | ||
private readonly IDictionary<object, object> _items; | ||
|
||
protected Builder(IDictionary<object, object> properties) | ||
{ | ||
_items = properties ?? new Dictionary<object, object>(); | ||
} | ||
|
||
public virtual object this[object item] | ||
{ | ||
get => _items.TryGetValue(item, out var value) ? value : null; | ||
set => _items[item] = value; | ||
} | ||
|
||
public IDictionary<object, object> Properties => _items; | ||
} | ||
|
||
/// <summary> | ||
/// Abstract base class for creating builders that are attached to some parent builder | ||
/// Useful for creating sub builds that live for a short period to augment the parent. | ||
/// </summary> | ||
/// <typeparam name="TBuilder"></typeparam> | ||
public abstract class Builder<TBuilder> : Builder | ||
where TBuilder : class | ||
{ | ||
/// <summary> | ||
/// Constructs a Builder<TBuilder> with the parent instance | ||
/// </summary> | ||
/// <param name="parent">The parent builder TBuilder</param> | ||
protected Builder(TBuilder parent, IDictionary<object, object> properties) : base(properties) | ||
{ | ||
Parent = parent ?? throw new ArgumentNullException(nameof(parent)); | ||
} | ||
|
||
/// <summary> | ||
/// Get the parent value from this builder | ||
/// </summary> | ||
public TBuilder Parent { get; } | ||
|
||
/// <summary> | ||
/// Exit the current builder and return the parent | ||
/// </summary> | ||
/// <returns></returns> | ||
public virtual TBuilder Exit() | ||
{ | ||
return Parent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Builders.Abstractions\Rocket.Surgery.Builders.Abstractions.csproj" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Rocket.Surgery.Extensions.Testing; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Rocket.Surgery.Builders.Tests | ||
{ | ||
public class BuilderTests : AutoTestBase | ||
{ | ||
class TestGenericValueContainer : Builder | ||
{ | ||
public TestGenericValueContainer() : base(new Dictionary<object, object>()) {} | ||
} | ||
|
||
|
||
public BuilderTests(ITestOutputHelper outputHelper) : base(outputHelper) { } | ||
|
||
[Fact] | ||
public void ReturnsNullOfNoValue() | ||
{ | ||
var container = new TestGenericValueContainer(); | ||
|
||
container[typeof(string)].Should().BeNull(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void SetAValue() | ||
{ | ||
var container = new TestGenericValueContainer(); | ||
|
||
container[typeof(string)] = "abc"; | ||
|
||
container[typeof(string)].Should().Be("abc"); | ||
} | ||
|
||
class ChildBuilder : Builder<TestGenericValueContainer> | ||
{ | ||
public ChildBuilder(TestGenericValueContainer parent) : base(parent, new Dictionary<object, object>()) | ||
{ | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldHaveAParent() | ||
{ | ||
var parent = new TestGenericValueContainer(); | ||
var builder = new ChildBuilder(parent); | ||
builder.Parent.Should().BeSameAs(parent); | ||
} | ||
|
||
[Fact] | ||
public void ShouldExitProperly() | ||
{ | ||
var parent = new TestGenericValueContainer(); | ||
var builder = new ChildBuilder(parent); | ||
builder.Exit().Should().BeSameAs(parent); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Builders.Abstractions\Rocket.Surgery.Builders.Abstractions.csproj" /> | ||
<ProjectReference Include="..\..\src\Builders\Rocket.Surgery.Builders.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"shadowCopy": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters