-
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.
Added MiddleNameValueGenerator (#99)
* Added MiddleNameValueGenerator * Updated documentation
- Loading branch information
1 parent
f903cb4
commit f785683
Showing
12 changed files
with
355 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ModelBuilder.UnitTests.Models | ||
{ | ||
public class Names | ||
{ | ||
public string FirstName { get; set; } | ||
|
||
public Gender Gender { get; set; } | ||
public string LastName { get; set; } | ||
public string MiddleName { get; set; } | ||
} | ||
} |
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,10 @@ | ||
namespace ModelBuilder.UnitTests.Models | ||
{ | ||
public class NamesWithoutGender | ||
{ | ||
public string FirstName { get; set; } | ||
|
||
public string LastName { get; set; } | ||
public string MiddleName { get; set; } | ||
} | ||
} |
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
157 changes: 157 additions & 0 deletions
157
ModelBuilder.UnitTests/ValueGenerators/MiddleNameValueGeneratorTests.cs
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,157 @@ | ||
namespace ModelBuilder.UnitTests.ValueGenerators | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using ModelBuilder.Data; | ||
using ModelBuilder.UnitTests.Models; | ||
using ModelBuilder.ValueGenerators; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
public class MiddleNameValueGeneratorTests | ||
{ | ||
[Fact] | ||
public void GeneratorReturnsFemaleNameWhenGenderIsFemale() | ||
{ | ||
var person = new Names | ||
{ | ||
Gender = Gender.Female | ||
}; | ||
var buildChain = new BuildHistory(); | ||
var executeStrategy = Substitute.For<IExecuteStrategy>(); | ||
|
||
executeStrategy.BuildChain.Returns(buildChain); | ||
|
||
buildChain.Push(person); | ||
|
||
var sut = new Wrapper(); | ||
|
||
var actual = (string) sut.RunGenerate(typeof(string), "MiddleName", executeStrategy); | ||
|
||
TestData.FemaleNames.Any(x => x == actual).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GeneratorReturnsFemaleNameWhenGenderIsUnknown() | ||
{ | ||
var person = new Names | ||
{ | ||
Gender = Gender.Unknown | ||
}; | ||
var buildChain = new BuildHistory(); | ||
var executeStrategy = Substitute.For<IExecuteStrategy>(); | ||
|
||
executeStrategy.BuildChain.Returns(buildChain); | ||
|
||
buildChain.Push(person); | ||
|
||
var sut = new Wrapper(); | ||
|
||
var actual = (string) sut.RunGenerate(typeof(string), "MiddleName", executeStrategy); | ||
|
||
TestData.FemaleNames.Any(x => x == actual).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GeneratorReturnsMaleNameWhenGenderIsMale() | ||
{ | ||
var person = new Names | ||
{ | ||
Gender = Gender.Male | ||
}; | ||
var buildChain = new BuildHistory(); | ||
var executeStrategy = Substitute.For<IExecuteStrategy>(); | ||
|
||
executeStrategy.BuildChain.Returns(buildChain); | ||
|
||
buildChain.Push(person); | ||
|
||
var sut = new Wrapper(); | ||
|
||
var actual = (string) sut.RunGenerate(typeof(string), "MiddleName", executeStrategy); | ||
|
||
TestData.MaleNames.Any(x => x == actual).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GeneratorReturnsNameWhenTypeLacksGender() | ||
{ | ||
var person = new NamesWithoutGender(); | ||
var buildChain = new BuildHistory(); | ||
var executeStrategy = Substitute.For<IExecuteStrategy>(); | ||
|
||
executeStrategy.BuildChain.Returns(buildChain); | ||
|
||
buildChain.Push(person); | ||
|
||
var sut = new Wrapper(); | ||
|
||
var actual = (string) sut.RunGenerate(typeof(string), "MiddleName", executeStrategy); | ||
|
||
if (TestData.MaleNames.Any(x => x == actual)) | ||
{ | ||
// This is a match on a male name so all good | ||
} | ||
else | ||
{ | ||
// Not a male name so it must be a female name | ||
TestData.FemaleNames.Any(x => x == actual).Should().BeTrue(); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(Stream), "middlename", false)] | ||
[InlineData(typeof(string), null, false)] | ||
[InlineData(typeof(string), "", false)] | ||
[InlineData(typeof(string), "Stuff", false)] | ||
[InlineData(typeof(string), "MiddleName", true)] | ||
[InlineData(typeof(string), "middlename", true)] | ||
[InlineData(typeof(string), "SECONDNAME", true)] | ||
[InlineData(typeof(string), "Middle_Name", true)] | ||
[InlineData(typeof(string), "middle_name", true)] | ||
[InlineData(typeof(string), "SECOND_NAME", true)] | ||
[InlineData(typeof(string), "SecondName", true)] | ||
[InlineData(typeof(string), "secondname", true)] | ||
[InlineData(typeof(string), "MIDDLENAME", true)] | ||
[InlineData(typeof(string), "Second_Name", true)] | ||
[InlineData(typeof(string), "second_name", true)] | ||
[InlineData(typeof(string), "MIDDLE_NAME", true)] | ||
public void IsMatchReturnsWhetherTypeAndNameAreSupportedTest(Type type, string referenceName, bool expected) | ||
{ | ||
var person = new Names(); | ||
var buildChain = new BuildHistory(); | ||
|
||
buildChain.Push(person); | ||
|
||
var sut = new Wrapper(); | ||
|
||
var actual = sut.RunIsMatch(type, referenceName, buildChain); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void PriorityReturnsHigherPriorityThanStringValidator() | ||
{ | ||
var sut = new Wrapper(); | ||
var other = new StringValueGenerator(); | ||
|
||
sut.Priority.Should().BeGreaterThan(other.Priority); | ||
} | ||
|
||
private class Wrapper : MiddleNameValueGenerator | ||
{ | ||
public object RunGenerate(Type type, string referenceName, IExecuteStrategy executeStrategy) | ||
{ | ||
return Generate(executeStrategy, type, referenceName); | ||
} | ||
|
||
public bool RunIsMatch(Type type, string referenceName, IBuildChain buildChain) | ||
{ | ||
return IsMatch(buildChain, type, referenceName); | ||
} | ||
} | ||
} | ||
} |
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
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,35 @@ | ||
namespace ModelBuilder.ValueGenerators | ||
{ | ||
using System; | ||
using ModelBuilder.Data; | ||
|
||
/// <summary> | ||
/// The <see cref="MiddleNameValueGenerator" /> | ||
/// class is used to generate random middle name values. | ||
/// </summary> | ||
public class MiddleNameValueGenerator : RelativeValueGenerator | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MiddleNameValueGenerator" />. | ||
/// </summary> | ||
public MiddleNameValueGenerator() : base(NameExpression.MiddleName, typeof(string)) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override object Generate(IExecuteStrategy executeStrategy, Type type, string referenceName) | ||
{ | ||
if (IsMale(executeStrategy)) | ||
{ | ||
// Use a male first name | ||
return TestData.MaleNames.Next(); | ||
} | ||
|
||
// Use a female name | ||
return TestData.FemaleNames.Next(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int Priority { get; } = 1000; | ||
} | ||
} |
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
Oops, something went wrong.