Skip to content

Commit e0899c2

Browse files
committed
Merge pull request #76 from Tynamix/66_FloatRange
Added FloatRange Plugin
2 parents b566a80 + 409acde commit e0899c2

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

Tynamix.ObjectFiller.Test/RangePluginTest.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,48 @@ namespace ObjectFiller.Test
99
public class RangePluginTest
1010
{
1111
[Fact]
12-
public void TestRangeWithMaxValue()
12+
public void TestIntRangeWithMaxValue()
1313
{
1414
int max = 100;
15-
Filler<SimpleList> filler = new Filler<SimpleList>();
15+
Filler<SimpleList<int>> filler = new Filler<SimpleList<int>>();
1616

1717
filler.Setup().OnType<int>().Use(new IntRange(max));
1818
var sl = filler.Create();
1919

2020
Assert.NotNull(sl);
21-
Assert.NotNull(sl.IntegerList);
22-
Assert.True(sl.IntegerList.All(x => x < 100));
23-
Assert.False(sl.IntegerList.All(x => x == sl.IntegerList[0]));
21+
Assert.NotNull(sl.ChildList);
22+
Assert.True(sl.ChildList.All(x => x < 100));
23+
Assert.False(sl.ChildList.All(x => x == sl.ChildList[0]));
2424
}
2525

2626
[Fact]
27-
public void TestRangeWithMinMaxValue()
27+
public void TestIntRangeWithMinMaxValue()
2828
{
2929
int max = 100;
3030
int min = 50;
31-
Filler<SimpleList> filler = new Filler<SimpleList>();
31+
Filler<SimpleList<int>> filler = new Filler<SimpleList<int>>();
3232

3333
filler.Setup().OnType<int>().Use(new IntRange(min, max));
3434
var sl = filler.Create();
3535

3636
Assert.NotNull(sl);
37-
Assert.NotNull(sl.IntegerList);
38-
Assert.True(sl.IntegerList.All(x => x >= min && x <= max));
37+
Assert.NotNull(sl.ChildList);
38+
Assert.True(sl.ChildList.All(x => x >= min && x <= max));
39+
}
40+
41+
[Fact]
42+
public void TestFloateRangeWithMinMaxValue()
43+
{
44+
int max = 100;
45+
int min = 50;
46+
Filler<SimpleList<float>> filler = new Filler<SimpleList<float>>();
47+
48+
filler.Setup().OnType<float>().Use(new FloatRange(min, max));
49+
var sl = filler.Create();
50+
51+
Assert.NotNull(sl);
52+
Assert.NotNull(sl.ChildList);
53+
Assert.True(sl.ChildList.All(x => x >= min && x <= max));
3954
}
4055
}
4156
}

Tynamix.ObjectFiller.Test/TestPoco/SimpleList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace ObjectFiller.Test.TestPoco
44
{
5-
public class SimpleList
5+
public class SimpleList<T>
66
{
7-
public List<int> IntegerList { get; set; }
7+
public List<T> ChildList { get; set; }
88
}
99
}

Tynamix.ObjectFiller/Plugins/Double/DoubleRange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public DoubleRange(double minValue, double maxValue)
5050
/// Use this to generate a double value between double.MinValue and double.MaxValue
5151
/// </summary>
5252
public DoubleRange()
53-
: this(int.MinValue, int.MaxValue)
53+
: this(double.MinValue, double.MaxValue)
5454
{
5555

5656
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace Tynamix.ObjectFiller
2+
{
3+
using System;
4+
5+
public class FloatRange : IRandomizerPlugin<float>, IRandomizerPlugin<float?>
6+
{
7+
private readonly float _minValue;
8+
private readonly float _maxValue;
9+
10+
/// <summary>
11+
/// Use to define just a max value for the double randomizer. Min value will be 0!
12+
/// </summary>
13+
/// <param name="maxValue">Maximum double value</param>
14+
public FloatRange(float maxValue)
15+
: this(0, maxValue)
16+
{
17+
18+
}
19+
20+
/// <summary>
21+
/// Use to define a min and max double value for the randomizer
22+
/// </summary>
23+
/// <param name="minValue">Min value</param>
24+
/// <param name="maxValue">Max value</param>
25+
public FloatRange(float minValue, float maxValue)
26+
{
27+
this._minValue = minValue;
28+
this._maxValue = maxValue;
29+
}
30+
31+
/// <summary>
32+
/// Use this to generate a double value between double.MinValue and double.MaxValue
33+
/// </summary>
34+
public FloatRange()
35+
: this(float.MinValue, float.MaxValue)
36+
{
37+
38+
}
39+
40+
public float GetValue()
41+
{
42+
var value = Random.NextDouble();
43+
44+
return Convert.ToSingle(value) * (this._maxValue - this._minValue) + this._minValue;
45+
}
46+
47+
float? IRandomizerPlugin<float?>.GetValue()
48+
{
49+
return this.GetValue();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)