Skip to content

Commit 610fc18

Browse files
committed
Added SequentialGenerator and Guard class
1 parent 8e351fb commit 610fc18

7 files changed

Lines changed: 170 additions & 24 deletions

File tree

NTestDataBuilder.Tests/DataSources/Generators/RandomGeneratorTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ namespace NTestDataBuilder.Tests.DataSources.Generators
88
{
99
public class RandomGeneratorTests
1010
{
11+
[Theory,
12+
InlineData(0,0),
13+
InlineData(0,-1),
14+
InlineData(-1,-2),
15+
InlineData(5,4),
16+
InlineData(5,5)]
17+
public void WhenCreatingRandomGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize)
18+
{
19+
Action factory = () => new RandomGenerator(startIndex, listSize);
20+
Should.Throw<ArgumentException>(factory)
21+
.Message.ShouldBe(string.Format("startIndex must be less than listSize"));
22+
}
23+
1124
[Fact]
12-
public void WhenGeneratingRandomIntegers_ThenShouldAlwaysGenerateIntegerBetweenMinAndMaxValue()
25+
public void WhenGeneratingRandomIntegers_ThenShouldAlwaysGenerateIntegerBetweenStartIndexAndListSize()
1326
{
1427
var random = new Random();
1528
for (int i = 0; i < 10; i++)
@@ -24,18 +37,5 @@ public void WhenGeneratingRandomIntegers_ThenShouldAlwaysGenerateIntegerBetweenM
2437
result.ShouldBeLessThanOrEqualTo(maximumValue);
2538
}
2639
}
27-
28-
[Theory,
29-
InlineData(0,0),
30-
InlineData(0,-1),
31-
InlineData(-1,-2),
32-
InlineData(5,4),
33-
InlineData(5,5)]
34-
public void WhenCreatingRandomGenerator_minimumValueMustBeLessThanMaximumValue(int minValue, int maxValue)
35-
{
36-
Action factory = () => new RandomGenerator(minValue, maxValue);
37-
Should.Throw<ArgumentException>(factory)
38-
.Message.ShouldBe(string.Format("minValue must be less than maxValue"));
39-
}
4040
}
4141
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using NTestDataBuilder.DataSources.Generators;
3+
using Shouldly;
4+
using Xunit;
5+
using Xunit.Extensions;
6+
7+
namespace NTestDataBuilder.Tests.DataSources.Generators
8+
{
9+
public class SequentiaGeneratorTests
10+
{
11+
[Theory,
12+
InlineData(0, 0),
13+
InlineData(5, 4),
14+
InlineData(5, 5)]
15+
public void WhenCreatingRandomGenerator_ThenStartIndexMustBeLessThanListSize(int startIndex, int listSize)
16+
{
17+
Action factory = () => new SequentialGenerator(startIndex, listSize);
18+
Should.Throw<ArgumentException>(factory)
19+
.Message.ShouldBe(string.Format("startIndex must be less than listSize"));
20+
}
21+
22+
[Fact]
23+
public void WhenCreatingRandomGenerator_ThenStartIndexMustBeZeroOrMore()
24+
{
25+
Action factory = () => new SequentialGenerator(-1, 1);
26+
Should.Throw<ArgumentException>(factory)
27+
.Message.ShouldBe(string.Format("startIndex must be zero or more"));
28+
}
29+
30+
[Theory,
31+
InlineData(0, 0),
32+
InlineData(0, -1)]
33+
public void WhenCreatingRandomGenerator_ThenListSizeMustBeGreaterThanZero(int startIndex, int listSize)
34+
{
35+
Action factory = () => new SequentialGenerator(startIndex, listSize);
36+
Should.Throw<ArgumentException>(factory)
37+
.Message.ShouldBe(string.Format("listSize must be greater than zero"));
38+
}
39+
40+
41+
[Fact]
42+
public void WhenGeneratingIntegers_ThenShouldBeSequential()
43+
{
44+
var sut = new SequentialGenerator(0, 11);
45+
for (int index = sut.StartIndex; index <= sut.ListSize; index++)
46+
{
47+
sut.Generate().ShouldBe(index);
48+
}
49+
}
50+
51+
[Fact]
52+
public void GivenGeneratorIsNotUnique_WhenGeneratingIntegers_ThenShouldResetAtEndOfList()
53+
{
54+
var sut = new SequentialGenerator(0, 2);
55+
for (int index = sut.StartIndex; index <= sut.ListSize; index++)
56+
{
57+
sut.Generate().ShouldBe(index);
58+
}
59+
60+
sut.Generate().ShouldBe(sut.StartIndex);
61+
}
62+
63+
[Fact]
64+
public void GivenGeneratorIsUnique_WhenGeneratingIntegers_ThenShouldResetAtEndOfList()
65+
{
66+
var sut = new SequentialGenerator(0, 2, true);
67+
for (int index = sut.StartIndex; index <= sut.ListSize; index++)
68+
{
69+
sut.Generate().ShouldBe(index);
70+
}
71+
72+
Should.Throw<InvalidOperationException>(()=>sut.Generate())
73+
.Message.ShouldBe("There are not enough elements in the data source to continue adding items");
74+
}
75+
76+
77+
}
78+
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</ItemGroup>
5454
<ItemGroup>
5555
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
56+
<Compile Include="DataSources\Generators\SequentiaGeneratorTests.cs" />
5657
<Compile Include="EquivalenceClasses\StringEquivalenceClassesTests.cs" />
5758
<Compile Include="AsProxyTests.cs" />
5859
<Compile Include="BuildListTests.cs" />

NTestDataBuilder/DataSources/Generators/RandomGenerator.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ namespace NTestDataBuilder.DataSources.Generators
55
public class RandomGenerator : IGenerator
66
{
77
private readonly Random _random;
8-
private readonly int _minValue;
9-
private readonly int _maxValue;
8+
public int StartIndex { get; private set; }
9+
public int ListSize { get; private set; }
1010

11-
public RandomGenerator(int minValue, int maxValue)
11+
public RandomGenerator(int startIndex, int listSize)
1212
{
13-
if (minValue >= maxValue)
14-
{
15-
throw new ArgumentException("minValue must be less than maxValue");
16-
}
17-
_minValue = minValue;
18-
_maxValue = maxValue;
13+
Guard.Against(startIndex >= listSize, "startIndex must be less than listSize");
14+
15+
StartIndex = startIndex;
16+
ListSize = listSize;
1917
_random = new Random();
2018
}
2119

2220
public int Generate()
2321
{
24-
return _random.Next(_minValue, _maxValue);
22+
return _random.Next(StartIndex, ListSize-1);
2523
}
2624
}
2725
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
namespace NTestDataBuilder.DataSources.Generators
4+
{
5+
public class SequentialGenerator : IGenerator
6+
{
7+
private int _currentListIndex;
8+
public int StartIndex { get; private set; }
9+
public int ListSize { get; private set; }
10+
public bool ListShouldBeUnique { get; private set; }
11+
12+
public SequentialGenerator(int startIndex, int listSize, bool listShouldBeUnique = false)
13+
{
14+
Guard.Against(startIndex < 0, "startIndex must be zero or more");
15+
Guard.Against(listSize < 1, "listSize must be greater than zero");
16+
Guard.Against(startIndex >= listSize, "startIndex must be less than listSize");
17+
18+
StartIndex = startIndex;
19+
ListSize = listSize;
20+
ListShouldBeUnique = listShouldBeUnique;
21+
_currentListIndex = startIndex - 1;
22+
}
23+
24+
public int Generate()
25+
{
26+
_currentListIndex++;
27+
CheckForEndOfList();
28+
return _currentListIndex;
29+
}
30+
31+
private void CheckForEndOfList()
32+
{
33+
if (_currentListIndex <= ListSize)
34+
{
35+
return;
36+
}
37+
38+
if (ListShouldBeUnique)
39+
{
40+
throw new InvalidOperationException(
41+
"There are not enough elements in the data source to continue adding items");
42+
}
43+
else
44+
{
45+
_currentListIndex = StartIndex;
46+
}
47+
48+
}
49+
}
50+
}

NTestDataBuilder/Guard.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NTestDataBuilder
7+
{
8+
public static class Guard
9+
{
10+
public static void Against(bool condition, string errorMessage)
11+
{
12+
if (condition)
13+
{
14+
throw new ArgumentException(errorMessage);
15+
}
16+
}
17+
}
18+
}

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
</ItemGroup>
5353
<ItemGroup>
5454
<Compile Include="AnonymousValueFixture.cs" />
55+
<Compile Include="Guard.cs" />
5556
<Compile Include="DataSources\Generators\IGenerator.cs" />
5657
<Compile Include="DataSources\Generators\RandomGenerator.cs" />
5758
<Compile Include="DataSources\Generators\SequentialGenerator.cs" />

0 commit comments

Comments
 (0)