Skip to content

Commit ce9db60

Browse files
committed
Merge pull request #75 from Tynamix/71_Randomizer_collections
Added overloads to Randomizer.Create
2 parents e0899c2 + 16a75e4 commit ce9db60

File tree

2 files changed

+96
-24
lines changed

2 files changed

+96
-24
lines changed

Tynamix.ObjectFiller.Test/RandomizerTest.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TryingToCreateAnObjectWithAnInterfaceShallFailAndHaveAnInnerexceptio
4646
return;
4747
}
4848

49-
///Should not come here!
49+
// Should not reach this!
5050
Assert.False(true);
5151
}
5252

@@ -60,5 +60,39 @@ public void RandomizerCreatesAListOfRandomItemsIfNeeded()
6060
Assert.Equal(amount, result.Count());
6161
}
6262

63+
[Fact]
64+
public void RandomizerCreatesAListOfRandomItemsWithAPlugin()
65+
{
66+
int amount = 5;
67+
68+
IEnumerable<int> result = Randomizer<int>.Create(new IntRange(1,1), amount);
69+
70+
Assert.Equal(amount, result.Count());
71+
Assert.True(result.Count(x => x == 1) == amount);
72+
}
73+
74+
[Fact]
75+
public void RandomizerCreatesAListOfItemBasedOnAFactory()
76+
{
77+
int amount = 5;
78+
79+
IEnumerable<int> result = Randomizer<int>.Create(amount, () => 1);
80+
81+
Assert.Equal(amount, result.Count());
82+
Assert.True(result.Count(x => x == 1) == amount);
83+
}
84+
85+
[Fact]
86+
public void RandomizerCreatesAListOfItemBasedOnASetup()
87+
{
88+
int amount = 5;
89+
90+
var setup = FillerSetup.Create<Address>().OnType<int>().Use(1).Result;
91+
92+
IEnumerable<Address> result = Randomizer<Address>.Create(setup, amount);
93+
94+
Assert.Equal(amount, result.Count());
95+
Assert.True(result.Count(x => x.HouseNumber == 1) == amount);
96+
}
6397
}
6498
}

Tynamix.ObjectFiller/Randomizer.cs

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,54 @@ public static T Create()
4242
}
4343

4444
/// <summary>
45-
/// Creates a set of random items of the given type. It will use a <see cref="IRandomizerPlugin{T}"/> for that.
45+
/// Creates a set of random items of the given type.
4646
/// </summary>
4747
/// <param name="amount">Amount of items created.</param>
4848
/// <returns>Set of random items of the given type.</returns>
4949
public static IEnumerable<T> Create(int amount)
50+
{
51+
return Create(amount, Create);
52+
}
53+
54+
/// <summary>
55+
/// Creates the specified amount of elements using the given factory.
56+
/// </summary>
57+
/// <param name="amount">The amount to create.</param>
58+
/// <param name="factory">The factory which provides the instance to add.</param>
59+
/// <returns>Set of items created by the factory.</returns>
60+
public static IEnumerable<T> Create(int amount, Func<T> factory)
5061
{
5162
var resultSet = new List<T>();
5263
for (int i = 0; i < amount; i++)
5364
{
54-
resultSet.Add(Create());
65+
resultSet.Add(factory());
5566
}
5667

5768
return resultSet;
5869
}
5970

71+
/// <summary>
72+
/// Creates a set of random items of the given type and will use a <see cref="IRandomizerPlugin{T}"/> for that.
73+
/// </summary>
74+
/// <param name="randomizerPlugin">Plugin to use.</param>
75+
/// <param name="amount">Amount of items created.</param>
76+
/// <returns>Set of random items of the given type.</returns>
77+
public static IEnumerable<T> Create(IRandomizerPlugin<T> randomizerPlugin, int amount)
78+
{
79+
return Create(amount, () => Create(randomizerPlugin));
80+
}
81+
82+
/// <summary>
83+
/// Creates a set of random items of the given type and will use a <see cref="FillerSetup"/> for that.
84+
/// </summary>
85+
/// <param name="setup">Setup to use.</param>
86+
/// <param name="amount">Amount of items created.</param>
87+
/// <returns>Set of random items of the given type.</returns>
88+
public static IEnumerable<T> Create(FillerSetup setup, int amount)
89+
{
90+
return Create(amount, () => Create(setup));
91+
}
92+
6093
/// <summary>
6194
/// Creates a random value of the target type. It will use a <see cref="IRandomizerPlugin{T}"/> for that
6295
/// </summary>
@@ -68,6 +101,31 @@ public static T Create(IRandomizerPlugin<T> randomizerPlugin)
68101
return randomizerPlugin.GetValue();
69102
}
70103

104+
/// <summary>
105+
/// Creates a value base on a filler setup
106+
/// </summary>
107+
/// <param name="setup">Setup for the objectfiller</param>
108+
/// <returns>Created value</returns>
109+
public static T Create(FillerSetup setup)
110+
{
111+
var creationMethod = CreateFactoryMethod(setup);
112+
113+
T result;
114+
try
115+
{
116+
result = creationMethod();
117+
}
118+
catch (Exception ex)
119+
{
120+
throw new InvalidOperationException(
121+
"The type " + typeof(T).FullName + " needs additional information to get created. "
122+
+ "Please use the Filler class and call \"Setup\" to create a setup for that type. See Innerexception for more details.",
123+
ex);
124+
}
125+
126+
return result;
127+
}
128+
71129
/// <summary>
72130
/// Creates a factory method for the given type.
73131
/// </summary>
@@ -78,7 +136,7 @@ internal static Func<T> CreateFactoryMethod(FillerSetup setup)
78136
Type targetType = typeof(T);
79137
if (!Setup.TypeToRandomFunc.ContainsKey(targetType))
80138
{
81-
139+
82140
if (targetType.IsClass())
83141
{
84142
var fillerType = typeof(Filler<>).MakeGenericType(typeof(T));
@@ -99,25 +157,5 @@ internal static Func<T> CreateFactoryMethod(FillerSetup setup)
99157

100158
return () => (T)Setup.TypeToRandomFunc[typeof(T)]();
101159
}
102-
103-
public static T Create(FillerSetup setup)
104-
{
105-
var creationMethod = CreateFactoryMethod(setup);
106-
107-
T result;
108-
try
109-
{
110-
result = creationMethod();
111-
}
112-
catch (Exception ex)
113-
{
114-
throw new InvalidOperationException(
115-
"The type " + typeof(T).FullName + " needs additional information to get created. "
116-
+ "Please use the Filler class and call \"Setup\" to create a setup for that type. See Innerexception for more details.",
117-
ex);
118-
}
119-
120-
return result;
121-
}
122160
}
123161
}

0 commit comments

Comments
 (0)