Skip to content

Commit

Permalink
Merge 95fdc48 into 2b61e29
Browse files Browse the repository at this point in the history
  • Loading branch information
roryprimrose committed Mar 28, 2020
2 parents 2b61e29 + 95fdc48 commit 7707aca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 106 deletions.
23 changes: 14 additions & 9 deletions ModelBuilder.UnitTests/Scenarios/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,22 @@ public void CreatePopulatesReadOnlyReferenceTypeProperties()
}

[Fact]
public void CreateReadOnlyCollectionWithAutoPopulatedItems()
public void CreateReadOnlyCollectionWithItemCountBetweenMinAndMax()
{
var actual = Model.Create<ReadOnlyCollection<int>>();

actual.Should().HaveCount(EnumerableTypeCreator.DefaultAutoPopulateCount);
actual.Count.Should().BeGreaterOrEqualTo(10);
actual.Count.Should().BeLessOrEqualTo(30);
actual.All(x => x == 0).Should().BeFalse();
}

[Fact]
public void CreateReturnsCollectionWithAutoPopulatedItemsInstance()
public void CreateReturnsCollectionWithItemCountBetweenMinAndMax()
{
var actual = Model.Create<ICollection<int>>();

actual.Should().HaveCount(EnumerableTypeCreator.DefaultAutoPopulateCount);
actual.Count.Should().BeGreaterOrEqualTo(10);
actual.Count.Should().BeLessOrEqualTo(30);
actual.All(x => x == 0).Should().BeFalse();
}

Expand All @@ -320,11 +322,12 @@ public void CreateReturnsCompanyWithEnumerableTypeCreatorUsage()
}

[Fact]
public void CreateReturnsEnumerableWithAutoPopulatedItemsInstance()
public void CreateReturnsEnumerableWithItemCountBetweenMinAndMax()
{
var actual = Model.Create<IEnumerable<int>>().ToList();

actual.Should().HaveCount(EnumerableTypeCreator.DefaultAutoPopulateCount);
actual.Count.Should().BeGreaterOrEqualTo(10);
actual.Count.Should().BeLessOrEqualTo(30);
actual.All(x => x == 0).Should().BeFalse();
}

Expand All @@ -337,11 +340,12 @@ public void CreateReturnsGuid()
}

[Fact]
public void CreateReturnsListWithAutoPopulatedItemsInstance()
public void CreateReturnsListWithItemCountBetweenMinAndMax()
{
var actual = Model.Create<IList<int>>();

actual.Should().HaveCount(EnumerableTypeCreator.DefaultAutoPopulateCount);
actual.Count.Should().BeGreaterOrEqualTo(10);
actual.Count.Should().BeLessOrEqualTo(30);
actual.All(x => x == 0).Should().BeFalse();
}

Expand Down Expand Up @@ -607,7 +611,8 @@ public void PopulateUsesResolvedTypeCreatorToPopulateInstance()
actual = Model.Populate(actual);

actual.Should().NotBeEmpty();
actual.Count.Should().Be(EnumerableTypeCreator.DefaultAutoPopulateCount);
actual.Count.Should().BeGreaterOrEqualTo(10);
actual.Count.Should().BeLessOrEqualTo(30);
}

[Fact]
Expand Down
65 changes: 26 additions & 39 deletions ModelBuilder.UnitTests/TypeCreators/ArrayTypeCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,32 @@ public void CreateInstanceThrowsExceptionWithNullType()
action.Should().Throw<ArgumentNullException>();
}

[Fact]
public void CreateReturnsBetweenMinAndMaxCountItems()
{
var buildChain = new BuildHistory();

var executeStrategy = Substitute.For<IExecuteStrategy>();
var typeResolver = Substitute.For<ITypeResolver>();
var configuration = Substitute.For<IBuildConfiguration>();

configuration.TypeResolver.Returns(typeResolver);
typeResolver.GetBuildType(configuration, Arg.Any<Type>()).Returns(x => x.Arg<Type>());
executeStrategy.BuildChain.Returns(buildChain);
executeStrategy.Configuration.Returns(configuration);

var sut = new ArrayTypeCreator
{
MinCount = 5,
MaxCount = 15
};

var actual = (Person[]) sut.Create(executeStrategy, typeof(Person[]));

actual.Length.Should().BeGreaterOrEqualTo(sut.MinCount);
actual.Length.Should().BeLessOrEqualTo(sut.MaxCount);
}

[Theory]
[InlineData(typeof(byte[]))]
[InlineData(typeof(int[]))]
Expand Down Expand Up @@ -261,12 +287,6 @@ public void CreateValidatesWhetherTypeIsSupportedTest(Type type, bool supported)
}
}

[Fact]
public void DefaultMaxCountIsPositive()
{
ArrayTypeCreator.DefaultMaxCount.Should().BeGreaterThan(0);
}

[Fact]
public void DisablesAutoConstructorDetection()
{
Expand Down Expand Up @@ -479,39 +499,6 @@ public void PriorityReturnsHigherThanDefaultTypeCreator()
sut.Priority.Should().BeGreaterThan(other.Priority);
}

[Fact]
public void SettingDefaultMaxCountOnlyAffectsNewInstances()
{
var expected = ArrayTypeCreator.DefaultMaxCount;

try
{
var first = new ArrayTypeCreator();

ArrayTypeCreator.DefaultMaxCount = 11;

var second = new ArrayTypeCreator();

first.MaxCount.Should().Be(expected);
second.MaxCount.Should().Be(11);
}
finally
{
ArrayTypeCreator.DefaultMaxCount = expected;
}
}

[Fact]
public void SettingMaxCountShouldNotChangeDefaultMaxCount()
{
var sut = new ArrayTypeCreator
{
MaxCount = Environment.TickCount
};

ArrayTypeCreator.DefaultMaxCount.Should().NotBe(sut.MaxCount);
}

private class ArrayTypeCreatorWrapper : ArrayTypeCreator
{
public void CreateItem(Type type, IExecuteStrategy executeStrategy, object item)
Expand Down
51 changes: 9 additions & 42 deletions ModelBuilder.UnitTests/TypeCreators/EnumerableTypeCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ public void PopulateAddsItemsToCollectionFromExecuteStrategy()

var sut = new EnumerableTypeCreator
{
AutoPopulateCount = 15
MinCount = 5,
MaxCount = 15
};

var actual = sut.Populate(executeStrategy, expected);
Expand All @@ -390,7 +391,8 @@ public void PopulateAddsItemsToCollectionFromExecuteStrategy()

var set = (Collection<Guid>) actual;

set.Should().HaveCount(sut.AutoPopulateCount);
set.Count.Should().BeGreaterOrEqualTo(sut.MinCount);
set.Count.Should().BeLessOrEqualTo(sut.MaxCount);
set.All(x => x != Guid.Empty).Should().BeTrue();
}

Expand Down Expand Up @@ -438,18 +440,16 @@ public void PopulateAddsItemsToListFromExecuteStrategy()
executeStrategy.BuildChain.Returns(buildChain);
executeStrategy.Create(typeof(Guid)).Returns(Guid.NewGuid());

var sut = new EnumerableTypeCreator
{
AutoPopulateCount = 15
};
var sut = new EnumerableTypeCreator();

var actual = sut.Populate(executeStrategy, expected);

actual.Should().BeSameAs(expected);

var set = (List<Guid>) actual;

set.Should().HaveCount(sut.AutoPopulateCount);
set.Count.Should().BeGreaterOrEqualTo(sut.MinCount);
set.Count.Should().BeLessOrEqualTo(sut.MaxCount);
set.All(x => x != Guid.Empty).Should().BeTrue();
}

Expand All @@ -465,9 +465,9 @@ public void PopulateCanAddItemsBasedOnPreviousItem()
var result = (List<int>) sut.Populate(executeStrategy, actual);

var baseValue = result[0];
var expected = new List<int>(sut.AutoPopulateCount);
var expected = new List<int>(result.Count);

for (var index = 0; index < sut.AutoPopulateCount; index++)
for (var index = 0; index < result.Count; index++)
{
expected.Add(baseValue + index);
}
Expand Down Expand Up @@ -506,39 +506,6 @@ public void PriorityReturnsHigherThanDefaultTypeCreator()
sut.Priority.Should().BeGreaterThan(other.Priority);
}

[Fact]
public void SettingAutoPopulateCountShouldNotChangeDefaultAutoPopulateCount()
{
var sut = new EnumerableTypeCreator
{
AutoPopulateCount = Environment.TickCount
};

EnumerableTypeCreator.DefaultAutoPopulateCount.Should().NotBe(sut.AutoPopulateCount);
}

[Fact]
public void SettingDefaultAutoPopulateCountOnlyAffectsNewInstances()
{
var expected = EnumerableTypeCreator.DefaultAutoPopulateCount;

try
{
var first = new EnumerableTypeCreator();

EnumerableTypeCreator.DefaultAutoPopulateCount = 11;

var second = new EnumerableTypeCreator();

first.AutoPopulateCount.Should().Be(expected);
second.AutoPopulateCount.Should().Be(11);
}
finally
{
EnumerableTypeCreator.DefaultAutoPopulateCount = expected;
}
}

private class EnumerableTypeCreatorWrapper : EnumerableTypeCreator
{
public void CreateItem(Type type, IExecuteStrategy executeStrategy, object item)
Expand Down
14 changes: 7 additions & 7 deletions ModelBuilder/TypeCreators/ArrayTypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected virtual object CreateChildItem(Type type, IExecuteStrategy executeStra
throw new ArgumentNullException(nameof(type));
}

var count = Generator.NextValue(1, MaxCount);
var count = Generator.NextValue(MinCount, MaxCount);

var parameters = new object[]
{
Expand Down Expand Up @@ -146,11 +146,6 @@ protected override object PopulateInstance(IExecuteStrategy executeStrategy, obj
return instance;
}

/// <summary>
/// Gets or sets the default maximum count that can be generated.
/// </summary>
public static int DefaultMaxCount { get; set; } = 30;

/// <inheritdoc />
public override bool AutoDetectConstructor => false;

Expand All @@ -160,7 +155,12 @@ protected override object PopulateInstance(IExecuteStrategy executeStrategy, obj
/// <summary>
/// Gets or sets the maximum count generated by this instance.
/// </summary>
public int MaxCount { get; set; } = DefaultMaxCount;
public int MaxCount { get; set; } = 30;

/// <summary>
/// Gets or sets the minimum count generated by this instance.
/// </summary>
public int MinCount { get; set; } = 10;

/// <inheritdoc />
public override int Priority { get; } = 100;
Expand Down
19 changes: 10 additions & 9 deletions ModelBuilder/TypeCreators/EnumerableTypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ protected override object PopulateInstance(IExecuteStrategy executeStrategy, obj

object previousItem = null;

for (var index = 0; index < AutoPopulateCount; index++)
var count = Generator.NextValue(MinCount, MaxCount);

for (var index = 0; index < count; index++)
{
var childInstance = CreateChildItem(internalType, executeStrategy, previousItem);

Expand Down Expand Up @@ -303,22 +305,21 @@ private static bool IsUnsupportedType(Type type)
return false;
}

/// <summary>
/// Gets or sets how many instances will be auto-populated into the list by default when the
/// <see cref="EnumerableTypeCreator" /> is created.
/// </summary>
public static int DefaultAutoPopulateCount { get; set; } = 10;

/// <inheritdoc />
public override bool AutoDetectConstructor => false;

/// <inheritdoc />
public override bool AutoPopulate => false;

/// <summary>
/// Gets or sets how many instances will be auto-populated into the list.
/// Gets or sets the maximum count generated by this instance.
/// </summary>
public int MaxCount { get; set; } = 30;

/// <summary>
/// Gets or sets the minimum count generated by this instance.
/// </summary>
public int AutoPopulateCount { get; set; } = DefaultAutoPopulateCount;
public int MinCount { get; set; } = 10;

/// <inheritdoc />
public override int Priority => 100;
Expand Down

0 comments on commit 7707aca

Please sign in to comment.