Skip to content

Commit 318414c

Browse files
committed
Fix: Dictionaries and SortedLists caused an exception when enumerations were used as keys.
1 parent 33fea9f commit 318414c

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
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/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">
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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,30 @@ private IDictionary GetFilledDictionary(
635635
Type keyType = propertyType.GetGenericArguments()[0];
636636
Type valueType = propertyType.GetGenericArguments()[1];
637637

638-
int maxDictionaryItems = Random.Next(
638+
int maxDictionaryItems = 0;
639+
640+
if (keyType.IsEnum)
641+
{
642+
maxDictionaryItems = Enum.GetValues(keyType).Length;
643+
}
644+
else
645+
{
646+
maxDictionaryItems = Random.Next(
639647
currentSetupItem.DictionaryKeyMinCount,
640648
currentSetupItem.DictionaryKeyMaxCount);
649+
}
641650

642651
for (int i = 0; i < maxDictionaryItems; i++)
643652
{
644-
object keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
653+
object keyObject = null;
654+
if (keyType.IsEnum)
655+
{
656+
keyObject = Enum.GetValues(keyType).GetValue(i);
657+
}
658+
else
659+
{
660+
keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
661+
}
645662

646663
if (dictionary.Contains(keyObject))
647664
{

0 commit comments

Comments
 (0)