Skip to content

Commit 8e351fb

Browse files
committed
Created random generator
1 parent a185fdc commit 8e351fb

7 files changed

Lines changed: 87 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ _ReSharper.*
99
*~
1010
*.log
1111
packages
12-
*.DotSettings
12+
*.DotSettings
13+
*.ncrunchproject
14+
*.ncrunchsolution
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 RandomGeneratorTests
10+
{
11+
[Fact]
12+
public void WhenGeneratingRandomIntegers_ThenShouldAlwaysGenerateIntegerBetweenMinAndMaxValue()
13+
{
14+
var random = new Random();
15+
for (int i = 0; i < 10; i++)
16+
{
17+
int minimumValue = random.Next(0,10);
18+
int maximumValue = random.Next(20,30);
19+
var sut = new RandomGenerator(minimumValue, maximumValue);
20+
21+
var result = sut.Generate();
22+
23+
result.ShouldBeGreaterThanOrEqualTo(minimumValue);
24+
result.ShouldBeLessThanOrEqualTo(maximumValue);
25+
}
26+
}
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+
}
40+
}
41+
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@
4747
<Reference Include="xunit">
4848
<HintPath>..\packages\xunit.1.9.2\lib\net20\xunit.dll</HintPath>
4949
</Reference>
50+
<Reference Include="xunit.extensions">
51+
<HintPath>..\packages\xunit.extensions.1.9.2\lib\net20\xunit.extensions.dll</HintPath>
52+
</Reference>
5053
</ItemGroup>
5154
<ItemGroup>
55+
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
5256
<Compile Include="EquivalenceClasses\StringEquivalenceClassesTests.cs" />
5357
<Compile Include="AsProxyTests.cs" />
5458
<Compile Include="BuildListTests.cs" />

NTestDataBuilder.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<package id="NSubstitute" version="1.7.2.0" targetFramework="net40" />
55
<package id="Shouldly" version="2.2.0" targetFramework="net40" />
66
<package id="xunit" version="1.9.2" targetFramework="net40" />
7+
<package id="xunit.extensions" version="1.9.2" targetFramework="net40" />
78
</packages>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NTestDataBuilder.DataSources.Generators
2+
{
3+
public interface IGenerator
4+
{
5+
int Generate();
6+
}
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace NTestDataBuilder.DataSources.Generators
4+
{
5+
public class RandomGenerator : IGenerator
6+
{
7+
private readonly Random _random;
8+
private readonly int _minValue;
9+
private readonly int _maxValue;
10+
11+
public RandomGenerator(int minValue, int maxValue)
12+
{
13+
if (minValue >= maxValue)
14+
{
15+
throw new ArgumentException("minValue must be less than maxValue");
16+
}
17+
_minValue = minValue;
18+
_maxValue = maxValue;
19+
_random = new Random();
20+
}
21+
22+
public int Generate()
23+
{
24+
return _random.Next(_minValue, _maxValue);
25+
}
26+
}
27+
}

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
</ItemGroup>
5353
<ItemGroup>
5454
<Compile Include="AnonymousValueFixture.cs" />
55+
<Compile Include="DataSources\Generators\IGenerator.cs" />
56+
<Compile Include="DataSources\Generators\RandomGenerator.cs" />
57+
<Compile Include="DataSources\Generators\SequentialGenerator.cs" />
5558
<Compile Include="EquivalenceClasses\StringEquivalenceClasses.cs" />
5659
<Compile Include="IAnonymousValueSupplier.cs" />
5760
<Compile Include="Lists\ListBuilder.cs" />
@@ -68,6 +71,7 @@
6871
<None Include="NTestDataBuilder.nuspec" />
6972
<None Include="packages.config" />
7073
</ItemGroup>
74+
<ItemGroup />
7175
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7276
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7377
Other similar extension points exist, see Microsoft.Common.targets.

0 commit comments

Comments
 (0)