Skip to content

Commit

Permalink
Features/72 update build configuration item (#92)
Browse files Browse the repository at this point in the history
* Added extension methods to configure items registered in IBuildConfiguration.
* Updated EmailValueGenerator to remove special characters when generating the address
* Fixed bug in CultureValueGenerator which would return an empty culture name.

Closes #72
  • Loading branch information
roryprimrose committed May 5, 2020
1 parent f2e13e7 commit 340a9f5
Show file tree
Hide file tree
Showing 32 changed files with 3,034 additions and 2,078 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<CodeAnalysisRuleSet>..\Solution Items\UnitTest.ruleset</CodeAnalysisRuleSet>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
namespace ModelBuilder.UnitTests
{
using System;
using System.Linq;
using System.Linq.Expressions;
using FluentAssertions;
using ModelBuilder.CreationRules;
using ModelBuilder.UnitTests.Models;
using NSubstitute;
using Xunit;

public partial class BuildConfigurationExtensionsTests
{
[Fact]
public void AddCreationRuleAddsRuleToCompiler()
{
var sut = new BuildConfiguration();

sut.AddCreationRule<DummyCreationRule>();

var actual = sut.CreationRules.Single();

actual.Should().BeOfType<DummyCreationRule>();
}

[Fact]
public void AddCreationRuleThrowsExceptionWithNullCompiler()
{
Action action = () => BuildConfigurationExtensions.AddCreationRule<DummyCreationRule>(null);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddCreationRuleWithExpressionAddsRuleToCompiler()
{
var priority = Environment.TickCount;
var value = Guid.NewGuid().ToString();
var propertyInfo = typeof(Person).GetProperty(nameof(Person.FirstName));

var sut = new BuildConfiguration();

sut.AddCreationRule<Person>(x => x.FirstName, priority, value);

var rule = sut.CreationRules.Single();

rule.Priority.Should().Be(priority);

var actual = rule.Create(null, propertyInfo);

actual.Should().Be(value);
}

[Fact]
public void AddCreationRuleWithExpressionThrowsExceptionWithNullCompiler()
{
var priority = Environment.TickCount;
var value = Guid.NewGuid().ToString();

Action action = () =>
BuildConfigurationExtensions.AddCreationRule<Person>(null, x => x.FirstName, priority, value);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddCreationRuleWithExpressionThrowsExceptionWithNullExpression()
{
var priority = Environment.TickCount;
var value = Guid.NewGuid().ToString();

var sut = new BuildConfiguration();

Action action = () => sut.AddCreationRule((Expression<Func<Person, object>>) null, priority, value);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddWithCreationRuleAddsRuleToCompiler()
{
var rule = new ExpressionCreationRule<Person>(x => x.FirstName, (object) null, Environment.TickCount);

var sut = new BuildConfiguration();

sut.Add(rule);

sut.CreationRules.Should().Contain(rule);
}

[Fact]
public void AddWithCreationRuleThrowsExceptionWithNullCompiler()
{
var rule = new ExpressionCreationRule<Person>(x => x.FirstName, (object) null, Environment.TickCount);

Action action = () => BuildConfigurationExtensions.Add(null, rule);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddWithCreationRuleThrowsExceptionWithNullRule()
{
var sut = Substitute.For<IBuildConfiguration>();

Action action = () => sut.Add((ICreationRule) null);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void RemoveCreationRuleRemovesMultipleMatchingRulesFromCompiler()
{
var sut = new BuildConfiguration();

sut.AddCreationRule<DummyCreationRule>();
sut.AddCreationRule<DummyCreationRule>();
sut.AddCreationRule<DummyCreationRule>();
sut.RemoveCreationRule<DummyCreationRule>();

sut.CreationRules.Should().BeEmpty();
}

[Fact]
public void RemoveCreationRuleRemovesRulesFromCompiler()
{
var sut = new BuildConfiguration();

sut.AddCreationRule<DummyCreationRule>();
sut.RemoveCreationRule<DummyCreationRule>();

sut.CreationRules.Should().BeEmpty();
}

[Fact]
public void RemoveCreationRuleThrowsExceptionWithNullCompiler()
{
Action action = () => BuildConfigurationExtensions.RemoveCreationRule<DummyCreationRule>(null);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void UpdateCreationRuleThrowsExceptionWhenRuleNotFound()
{
var sut = new BuildConfiguration();

Action action = () => sut.UpdateCreationRule<RegexCreationRule>(x =>
{
// Do nothing
});

action.Should().Throw<InvalidOperationException>();
}

[Fact]
public void UpdateCreationRuleThrowsExceptionWithNullAction()
{
var sut = Substitute.For<IBuildConfiguration>();

Action action = () => sut.UpdateCreationRule<RegexCreationRule>(null);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void UpdateCreationRuleThrowsExceptionWithNullConfiguration()
{
Action action = () => BuildConfigurationExtensions.UpdateCreationRule<RegexCreationRule>(null, x =>
{
// Do nothing
});

action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void UpdateCreationRuleUpdateMatchingRule()
{
var expected = Guid.NewGuid();
var sut = new BuildConfiguration();
var rule = new DummyCreationRule
{
Value = expected
};

sut.CreationRules.Add(rule);

sut.UpdateCreationRule<DummyCreationRule>(x => { x.Value = expected; });

rule.Value.Should().Be(expected);
}
}
}
Loading

0 comments on commit 340a9f5

Please sign in to comment.