Skip to content

Commit ed139a5

Browse files
unknownunknown
authored andcommitted
Randomizer implemented
1 parent ddea6c1 commit ed139a5

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="Properties\AssemblyInfo.cs" />
6161
<Compile Include="ObjectFillerTest.cs" />
6262
<Compile Include="RandomizerPluginFake.cs" />
63+
<Compile Include="RandomizerTest.cs" />
6364
<Compile Include="RangePluginTest.cs" />
6465
<Compile Include="RealNamePluginTest.cs" />
6566
<Compile Include="TestPoco\Library\Book.cs" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace ObjectFiller.Test
2+
{
3+
using System;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using ObjectFiller.Test.TestPoco.Library;
8+
using ObjectFiller.Test.TestPoco.Person;
9+
10+
using Tynamix.ObjectFiller;
11+
12+
[TestClass]
13+
public class RandomizerTest
14+
{
15+
[TestMethod]
16+
public void GetRandomInt()
17+
{
18+
var number = Randomizer<int>.Create(new IntRange(1, 2));
19+
20+
Assert.IsTrue(number == 1 || number == 2);
21+
}
22+
23+
[TestMethod]
24+
public void FillAllAddressProperties()
25+
{
26+
var a = Randomizer<Address>.Create();
27+
Assert.IsNotNull(a.City);
28+
Assert.IsNotNull(a.Country);
29+
Assert.AreNotEqual(0, a.HouseNumber);
30+
Assert.IsNotNull(a.PostalCode);
31+
Assert.IsNotNull(a.Street);
32+
}
33+
34+
[TestMethod]
35+
[ExpectedException(typeof(InvalidOperationException))]
36+
public void TryingToCreateAnObjectWithAnInterfaceShallFailAndHaveAnInnerexception()
37+
{
38+
try
39+
{
40+
Randomizer<Library>.Create();
41+
}
42+
catch (InvalidOperationException ex)
43+
{
44+
Assert.IsNotNull(ex.InnerException);
45+
throw;
46+
}
47+
48+
}
49+
50+
}
51+
}

ObjectFiller/ObjectFiller.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Compile Include="Plugins\String\GermanStreetNames.cs" />
4242
<Compile Include="Plugins\String\Lipsum.cs" />
4343
<Compile Include="HashStack.cs" />
44+
<Compile Include="Randomizer.cs" />
4445
<Compile Include="Setup\FillerSetup.cs" />
4546
<Compile Include="Setup\FluentCircularApi.cs" />
4647
<Compile Include="Setup\FluentFillerApi.cs" />

ObjectFiller/Randomizer.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Tynamix.ObjectFiller
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
/// <summary>
7+
/// This class is a easy way to get random values.
8+
/// </summary>
9+
/// <typeparam name="T">Target type of whom the random value shall been generated</typeparam>
10+
public static class Randomizer<T>
11+
{
12+
/// <summary>
13+
/// The default setup item
14+
/// </summary>
15+
private static readonly FillerSetupItem setup;
16+
17+
/// <summary>
18+
/// Initializes static members of the <see cref="Randomizer{T}"/> class.
19+
/// </summary>
20+
static Randomizer()
21+
{
22+
setup = new FillerSetupItem();
23+
}
24+
25+
/// <summary>
26+
/// Creates a random value of type <see cref="T"/>.
27+
/// </summary>
28+
/// <returns>A value of type <see cref="T"/></returns>
29+
public static T Create()
30+
{
31+
Type targetType = typeof(T);
32+
if (!setup.TypeToRandomFunc.ContainsKey(targetType))
33+
{
34+
if (targetType.IsClass)
35+
{
36+
var fillerType = typeof(Filler<>).MakeGenericType(typeof(T));
37+
var oFiller = Activator.CreateInstance(fillerType);
38+
var methodInfo = oFiller.GetType().GetMethods().Single(x => !x.GetParameters().Any() && x.Name == "Create");
39+
40+
try
41+
{
42+
return (T)methodInfo.Invoke(oFiller, null);
43+
}
44+
catch (Exception ex)
45+
{
46+
throw new InvalidOperationException("The type " + typeof(T).FullName + " needs additional information to get created. " +
47+
"Please use the Filler class and call \"Setup\" to create a setup for that type. See Innerexception for more details.", ex);
48+
}
49+
}
50+
51+
return default(T);
52+
}
53+
54+
return (T)setup.TypeToRandomFunc[typeof(T)]();
55+
}
56+
57+
/// <summary>
58+
/// Creates a random value of the target type. It will use a <see cref="IRandomizerPlugin{T}"/> for that
59+
/// </summary>
60+
/// <param name="randomizerPlugin"><see cref="IRandomizerPlugin{T}"/> to use</param>
61+
/// <returns>A random value of type <see cref="T"/></returns>
62+
public static T Create(IRandomizerPlugin<T> randomizerPlugin)
63+
{
64+
return randomizerPlugin.GetValue();
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)