Skip to content

Commit f37aa42

Browse files
committed
Merge pull request #46 from HerrLoesch/master
Fix: Dictionaries and SortedLists caused an exception
2 parents e1cc489 + 4160db7 commit f37aa42

File tree

8 files changed

+108
-27
lines changed

8 files changed

+108
-27
lines changed

ObjectFiller.Test/ListFillingTest.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace ObjectFiller.Test
99
{
10+
using ObjectFiller.Test.TestPoco;
11+
1012
[TestClass]
1113
public class ListFillingTest
1214
{
@@ -131,6 +133,16 @@ public void GenerateTestDataForADictionary()
131133
}
132134
}
133135

136+
[TestMethod]
137+
public void GenerateDictionaryWithEnumeration()
138+
{
139+
var amountOfEnumValues = Enum.GetValues(typeof(TestEnum)).Length;
140+
var filler = new Filler<Dictionary<TestEnum, string>>();
141+
var result = filler.Create();
142+
143+
Assert.AreEqual(amountOfEnumValues, result.Count);
144+
}
145+
134146
private Entity[] GetArray()
135147
{
136148
Filler<Entity> of = new Filler<Entity>();
@@ -146,9 +158,7 @@ private Entity[] GetArray()
146158
entities.Add(of.Create());
147159
entities.Add(of.Create());
148160

149-
150161
return entities.ToArray();
151162
}
152-
153163
}
154164
}

ObjectFiller.Test/LoremIpsumPluginTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace ObjectFiller.Test
88
{
9+
using System.Collections.Generic;
10+
911
[TestClass]
1012
public class LoremIpsumPluginTest
1113
{
@@ -74,5 +76,22 @@ public void Test_With_LoremIpsum_Seed_Settings()
7476
Assert.IsNotNull(b1);
7577
Assert.AreEqual(b.ISBN, b1.ISBN);
7678
}
79+
80+
[TestMethod]
81+
public void LoremIpsum_should_provide_different_data()
82+
{
83+
var alowedDelta = 2;
84+
85+
var filler = new Filler<Book>();
86+
filler.Setup()
87+
.OnProperty(foo => foo.Description)
88+
.Use(new Lipsum(LipsumFlavor.LoremIpsum));
89+
90+
var resultElements = filler.Create(100);
91+
92+
var groupedResult = resultElements.GroupBy(x => x.Description);
93+
94+
Assert.AreEqual(100, groupedResult.Count(), alowedDelta);
95+
}
7796
}
7897
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="TestPoco\SimpleList.cs" />
8383
<Compile Include="SaveFillerSetupTest.cs" />
8484
<Compile Include="EmailAddressesPluginTest.cs" />
85+
<Compile Include="TestPoco\TestEnum.cs" />
8586
</ItemGroup>
8687
<ItemGroup>
8788
<ProjectReference Include="..\ObjectFiller\ObjectFiller.csproj">

ObjectFiller.Test/RandomizerTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ObjectFiller.Test
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Linq;
56

67
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -45,7 +46,16 @@ public void TryingToCreateAnObjectWithAnInterfaceShallFailAndHaveAnInnerexceptio
4546
Assert.IsNotNull(ex.InnerException);
4647
throw;
4748
}
48-
49+
}
50+
51+
[TestMethod]
52+
public void RandomizerCreatesAListOfRandomItemsIfNeeded()
53+
{
54+
int amount = 5;
55+
56+
IEnumerable<int> result = Randomizer<int>.Create(amount);
57+
58+
Assert.AreEqual(amount, result.Count());
4959
}
5060

5161
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace ObjectFiller.Test.TestPoco
2+
{
3+
using System;
4+
5+
[Flags]
6+
public enum TestEnum
7+
{
8+
ValueOne,
9+
ValueTwo,
10+
ValueThree,
11+
ValueFour
12+
}
13+
}

ObjectFiller/Filler.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,9 @@ public T Create()
8686
public IEnumerable<T> Create(int count)
8787
{
8888
IList<T> items = new List<T>();
89-
var typeStack = new HashStack<Type>();
90-
Type targetType = typeof(T);
9189
for (int n = 0; n < count; n++)
9290
{
93-
T objectToFill;
94-
if (!TypeIsClrType(targetType))
95-
{
96-
objectToFill = (T)this.CreateInstanceOfType(targetType, this.setupManager.GetFor<T>(), typeStack);
97-
this.Fill(objectToFill);
98-
}
99-
else
100-
{
101-
objectToFill = (T)this.CreateAndFillObject(typeof(T), this.setupManager.GetFor<T>(), typeStack);
102-
}
103-
104-
items.Add(objectToFill);
91+
items.Add(this.Create());
10592
}
10693

10794
return items;
@@ -635,13 +622,30 @@ private IDictionary GetFilledDictionary(
635622
Type keyType = propertyType.GetGenericArguments()[0];
636623
Type valueType = propertyType.GetGenericArguments()[1];
637624

638-
int maxDictionaryItems = Random.Next(
625+
int maxDictionaryItems = 0;
626+
627+
if (keyType.IsEnum)
628+
{
629+
maxDictionaryItems = Enum.GetValues(keyType).Length;
630+
}
631+
else
632+
{
633+
maxDictionaryItems = Random.Next(
639634
currentSetupItem.DictionaryKeyMinCount,
640635
currentSetupItem.DictionaryKeyMaxCount);
636+
}
641637

642638
for (int i = 0; i < maxDictionaryItems; i++)
643639
{
644-
object keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
640+
object keyObject = null;
641+
if (keyType.IsEnum)
642+
{
643+
keyObject = Enum.GetValues(keyType).GetValue(i);
644+
}
645+
else
646+
{
647+
keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
648+
}
645649

646650
if (dictionary.Contains(keyObject))
647651
{

ObjectFiller/Plugins/String/Lipsum.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Lipsum : IRandomizerPlugin<string>
3939
private readonly int maxSentences;
4040
private readonly int minWords;
4141
private readonly int maxWords;
42-
private readonly int seed;
42+
private readonly int? seed;
4343

4444
/// <summary>
4545
/// Words for the standard lorem ipsum text.
@@ -130,6 +130,8 @@ public class Lipsum : IRandomizerPlugin<string>
130130
/// </summary>
131131
private readonly Dictionary<LipsumFlavor, string[]> map;
132132

133+
private System.Random random;
134+
133135
/// <summary>
134136
/// Initializes a new instance of the <see cref="Lipsum"/> class.
135137
/// </summary>
@@ -152,7 +154,7 @@ public class Lipsum : IRandomizerPlugin<string>
152154
/// The max words of the generated text.
153155
/// </param>
154156
/// <param name="seed">
155-
/// The seed for the random to get the same result with the same seed.
157+
/// The seed for randomizer to get the same result with the same seed.
156158
/// </param>
157159
public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int maxSentences = 8,
158160
int minWords = 10, int maxWords = 50, int? seed = null)
@@ -172,7 +174,8 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
172174
{ LipsumFlavor.LeMasque, LeMasque }
173175
};
174176

175-
this.seed = seed.HasValue ? seed.Value : Environment.TickCount;
177+
this.seed = seed;
178+
this.random = new System.Random();
176179
}
177180

178181
/// <summary>
@@ -181,20 +184,23 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
181184
/// <returns>Random data for type <see cref="T"/></returns>
182185
public string GetValue()
183186
{
184-
System.Random rnd = new System.Random(this.seed);
185-
var array = this.map[this.flavor];
187+
if (this.seed.HasValue)
188+
{
189+
this.random = new System.Random(this.seed.Value);
190+
}
186191

192+
var array = this.map[this.flavor];
187193
var result = new StringBuilder();
188194

189195
for (var i = 0; i < this.paragraphs; i++)
190196
{
191-
var sentences = rnd.Next(this.minSentences, this.maxSentences + 1);
197+
var sentences = this.random.Next(this.minSentences, this.maxSentences + 1);
192198
for (var j = 0; j < sentences; j++)
193199
{
194-
var words = rnd.Next(this.minWords, this.maxWords + 1);
200+
var words = this.random.Next(this.minWords, this.maxWords + 1);
195201
for (var k = 0; k < words; k++)
196202
{
197-
var word = array[rnd.Next(array.Length)];
203+
var word = array[this.random.Next(array.Length)];
198204
if (k == 0)
199205
{
200206
word = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);

ObjectFiller/Randomizer.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace Tynamix.ObjectFiller
1111
{
1212
using System;
13+
using System.Collections.Generic;
1314
using System.Linq;
15+
using System.Runtime.CompilerServices;
1416

1517
/// <summary>
1618
/// This class is a easy way to get random values.
@@ -65,6 +67,22 @@ public static T Create()
6567
return (T)Setup.TypeToRandomFunc[typeof(T)]();
6668
}
6769

70+
/// <summary>
71+
/// Creates a set of random items of the given type. It will use a <see cref="IRandomizerPlugin{T}"/> for that.
72+
/// </summary>
73+
/// <param name="amount">Amount of items created.</param>
74+
/// <returns>Set of random items of the given type.</returns>
75+
public static IEnumerable<T> Create(int amount)
76+
{
77+
var resultSet = new List<T>();
78+
for (int i = 0; i < amount; i++)
79+
{
80+
resultSet.Add(Create());
81+
}
82+
83+
return resultSet;
84+
}
85+
6886
/// <summary>
6987
/// Creates a random value of the target type. It will use a <see cref="IRandomizerPlugin{T}"/> for that
7088
/// </summary>

0 commit comments

Comments
 (0)