Skip to content

Commit

Permalink
Added RegexTypeNameValueGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
roryprimrose committed Jan 25, 2020
1 parent 7f45cb7 commit f18eb1e
Show file tree
Hide file tree
Showing 4 changed files with 428 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
namespace ModelBuilder.UnitTests.ValueGenerators
{
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using FluentAssertions;
using ModelBuilder.UnitTests.Models;
using ModelBuilder.ValueGenerators;
using NSubstitute;
using Xunit;

public class RegexTypeNameValueGeneratorTests
{
[Fact]
public void GenerateForParameterReturnsValue()
{
var parameterInfo = typeof(Person).GetConstructors()
.First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
var expected = Guid.NewGuid().ToString();

var executeStrategy = Substitute.For<IExecuteStrategy>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), expected);

var actual = sut.Generate(parameterInfo, executeStrategy);

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

[Fact]
public void GenerateForPropertyReturnsValue()
{
var propertyInfo = typeof(Person).GetProperty(nameof(Person.FirstName));
var expected = Guid.NewGuid().ToString();

var executeStrategy = Substitute.For<IExecuteStrategy>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), expected);

var actual = sut.Generate(propertyInfo, executeStrategy);

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

[Fact]
public void GenerateForTargetTypeThrowsNotSupportedException()
{
var value = Guid.NewGuid().ToString();

var executeStrategy = Substitute.For<IExecuteStrategy>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

Action action = () => sut.Generate(typeof(string), executeStrategy);

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

[Fact]
public void GenerateThrowsExceptionWithNullParameterInfo()
{
var expected = Guid.NewGuid().ToString();

var executeStrategy = Substitute.For<IExecuteStrategy>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), expected);

Action action = () => sut.Generate((ParameterInfo) null, executeStrategy);

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

[Fact]
public void GenerateThrowsExceptionWithNullPropertyInfo()
{
var expected = Guid.NewGuid().ToString();

var executeStrategy = Substitute.For<IExecuteStrategy>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), expected);

Action action = () => sut.Generate((PropertyInfo) null, executeStrategy);

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

[Theory]
[InlineData(typeof(string), "firstName", true)]
[InlineData(typeof(int), "firstName", false)]
[InlineData(typeof(string), "lastName", false)]
public void IsSupportedForParameterReturnsExpectedValue(Type type, string expression, bool expected)
{
var nameRegex = new Regex(expression);
var parameterInfo = typeof(Person).GetConstructors()
.Single(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();
var value = Guid.NewGuid().ToString();

var buildChain = Substitute.For<IBuildChain>();

var sut = new Wrapper(nameRegex, type, value);

var actual = sut.IsSupported(parameterInfo, buildChain);

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

[Fact]
public void IsSupportedForParameterThrowsExceptionWithNullBuildChain()
{
var value = Guid.NewGuid().ToString();
var parameterInfo = typeof(Person).GetConstructors()
.First(x => x.GetParameters().FirstOrDefault()?.Name == "firstName").GetParameters().First();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

Action action = () => sut.IsSupported(parameterInfo, null);

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

[Fact]
public void IsSupportedForParameterThrowsExceptionWithNullParameterInfo()
{
var value = Guid.NewGuid().ToString();

var buildChain = Substitute.For<IBuildChain>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

Action action = () => sut.IsSupported((ParameterInfo) null, buildChain);

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

[Theory]
[InlineData(typeof(string), "FirstName", true)]
[InlineData(typeof(int), "FirstName", false)]
[InlineData(typeof(string), "LastName", false)]
public void IsSupportedForPropertyReturnsExpectedValue(Type type, string expression, bool expected)
{
var nameRegex = new Regex(expression);
var propertyInfo = typeof(Person).GetProperty(nameof(Person.FirstName));
var value = Guid.NewGuid().ToString();

var buildChain = Substitute.For<IBuildChain>();

var sut = new Wrapper(nameRegex, type, value);

var actual = sut.IsSupported(propertyInfo, buildChain);

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

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

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

Action action = () => sut.IsSupported(propertyInfo, null);

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

[Fact]
public void IsSupportedForPropertyThrowsExceptionWithNullPropertyInfo()
{
var value = Guid.NewGuid().ToString();

var buildChain = Substitute.For<IBuildChain>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

Action action = () => sut.IsSupported((PropertyInfo) null, buildChain);

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

[Fact]
public void IsSupportedForTargetTypeReturnsFalse()
{
var value = Guid.NewGuid().ToString();

var buildChain = Substitute.For<IBuildChain>();

var sut = new Wrapper(PropertyExpression.FirstName, typeof(string), value);

var actual = sut.IsSupported(typeof(string), buildChain);

actual.Should().BeFalse();
}

[Fact]
public void ThrowsExceptionWithNullExpression()
{
var value = Guid.NewGuid().ToString();

Action action = () => new Wrapper(null, typeof(string), value);

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

[Fact]
public void ThrowsExceptionWithNullType()
{
var value = Guid.NewGuid().ToString();

Action action = () => new Wrapper(PropertyExpression.FirstName, null, value);

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

private class Wrapper : RegexTypeNameValueGenerator
{
private readonly object _value;

public Wrapper(Regex nameExpression, Type type, object value) : base(nameExpression, type)
{
_value = value;
}

protected override object GenerateValue(Type type, string referenceName, IExecuteStrategy executeStrategy)
{
return _value;
}

public override int Priority => Environment.TickCount;
}
}
}
2 changes: 1 addition & 1 deletion ModelBuilder/ModelBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
48 changes: 48 additions & 0 deletions ModelBuilder/ValueGenerators/IValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,54 @@ public interface IValueGenerator
/// <returns><c>true</c> if the type is supported; otherwise <c>false</c>.</returns>
bool IsSupported(Type type, string referenceName, IBuildChain buildChain);

///// <summary>
///// Generates a new value of the specified type.
///// </summary>
///// <param name="type">The type of value to generate.</param>
///// <param name="executeStrategy">The execution strategy.</param>
///// <returns>A new value of the type.</returns>
//object Generate(Type type, IExecuteStrategy executeStrategy);

///// <summary>
///// Generates a new value of the specified type.
///// </summary>
///// <param name="propertyInfo">The property to generate the value for.</param>
///// <param name="executeStrategy">The execution strategy.</param>
///// <returns>A new value of the type.</returns>
//object Generate(PropertyInfo propertyInfo, IExecuteStrategy executeStrategy);

///// <summary>
///// Generates a new value of the specified type.
///// </summary>
///// <param name="parameterInfo">The parameter to generate the value for.</param>
///// <param name="executeStrategy">The execution strategy.</param>
///// <returns>A new value of the type.</returns>
//object Generate(ParameterInfo parameterInfo, IExecuteStrategy executeStrategy);

///// <summary>
///// Returns whether the specified type is supported by this generator.
///// </summary>
///// <param name="type">The type to evaluate.</param>
///// <param name="buildChain">The chain of instances built up to this point.</param>
///// <returns><c>true</c> if the type is supported; otherwise <c>false</c>.</returns>
//bool IsSupported(Type type, IBuildChain buildChain);

///// <summary>
///// Returns whether the specified type is supported by this generator.
///// </summary>
///// <param name="propertyInfo">The property to generate the value for.</param>
///// <param name="buildChain">The chain of instances built up to this point.</param>
///// <returns><c>true</c> if the type is supported; otherwise <c>false</c>.</returns>
//bool IsSupported(PropertyInfo propertyInfo, IBuildChain buildChain);

///// <summary>
///// Returns whether the specified type is supported by this generator.
///// </summary>
///// <param name="parameterInfo">The parameter to generate the value for.</param>
///// <param name="buildChain">The chain of instances built up to this point.</param>
///// <returns><c>true</c> if the type is supported; otherwise <c>false</c>.</returns>
//bool IsSupported(ParameterInfo parameterInfo, IBuildChain buildChain);

/// <summary>
/// Gets the priority for this generator.
/// </summary>
Expand Down
Loading

0 comments on commit f18eb1e

Please sign in to comment.