Skip to content

Commit 71f8bdd

Browse files
gokTyBalDgokTyBalD
authored andcommitted
Bugfix Circular Reference on lists and dictionary
1 parent 0bb1df5 commit 71f8bdd

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

ObjectFiller.Test/ObjectFillerRecursiveTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34
using Tynamix.ObjectFiller;
45

@@ -43,6 +44,21 @@ private class TestDuplicate
4344
public TestEmptyNode Node2 { get; set; }
4445
}
4546

47+
public class Children
48+
{
49+
public Parent Parent { get; set; }
50+
}
51+
52+
public class Parent
53+
{
54+
public List<Children> Childrens { get; set; }
55+
}
56+
57+
public class ParentDictionary
58+
{
59+
public Dictionary<string, Children> Childrens { get; set; }
60+
}
61+
4662
// ReSharper restore ClassNeverInstantiated.Local
4763

4864
[TestMethod]
@@ -155,5 +171,21 @@ public void RecursiveFill_SelfReferencing_Succeeds()
155171

156172
Assert.IsNull(r.Self.Self);
157173
}
174+
175+
[TestMethod]
176+
public void RecursiveFill_ParentList_Succeeds()
177+
{
178+
var filler = new Filler<Parent>();
179+
var r = filler.Create();
180+
}
181+
182+
[TestMethod]
183+
public void RecursiveFill_ParentDictionary_Succeeds()
184+
{
185+
var filler = new Filler<ParentDictionary>();
186+
var r = filler.Create();
187+
188+
189+
}
158190
}
159191
}

ObjectFiller.Test/RangePluginTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void TestRangeWithMaxValue()
1919

2020
Assert.IsNotNull(sl);
2121
Assert.IsNotNull(sl.IntegerList);
22-
Assert.IsTrue(sl.IntegerList.All(x => x <= 100));
22+
Assert.IsTrue(sl.IntegerList.All(x => x < 100));
2323
Assert.IsFalse(sl.IntegerList.All(x => x == sl.IntegerList[0]));
2424
}
2525

ObjectFiller/Filler.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,20 @@ private object GetFilledObject(Type type, FillerSetupItem currentSetupItem, Hash
242242

243243
if (TypeIsDictionary(type))
244244
{
245-
IDictionary dictionary = GetFilledDictionary(type, currentSetupItem);
245+
IDictionary dictionary = GetFilledDictionary(type, currentSetupItem, typeTracker);
246246

247247
return dictionary;
248248
}
249249

250250
if (TypeIsList(type))
251251
{
252-
IList list = GetFilledList(type, currentSetupItem);
252+
IList list = GetFilledList(type, currentSetupItem, typeTracker);
253253
return list;
254254
}
255255

256256
if (type.IsInterface)
257257
{
258-
return GetInterfaceInstance(type, currentSetupItem);
258+
return GetInterfaceInstance(type, currentSetupItem, typeTracker);
259259
}
260260

261261
if (TypeIsPoco(type))
@@ -317,7 +317,7 @@ private object GetFilledPoco(Type type, FillerSetupItem currentSetupItem, HashSt
317317
return result;
318318
}
319319

320-
private IDictionary GetFilledDictionary(Type propertyType, FillerSetupItem currentSetupItem)
320+
private IDictionary GetFilledDictionary(Type propertyType, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
321321
{
322322
IDictionary dictionary = (IDictionary)Activator.CreateInstance(propertyType);
323323
Type keyType = propertyType.GetGenericArguments()[0];
@@ -327,7 +327,7 @@ private IDictionary GetFilledDictionary(Type propertyType, FillerSetupItem curre
327327
currentSetupItem.DictionaryKeyMaxCount);
328328
for (int i = 0; i < maxDictionaryItems; i++)
329329
{
330-
object keyObject = GetFilledObject(keyType, currentSetupItem);
330+
object keyObject = GetFilledObject(keyType, currentSetupItem, typeTracker);
331331

332332
if (dictionary.Contains(keyObject))
333333
{
@@ -336,7 +336,7 @@ private IDictionary GetFilledDictionary(Type propertyType, FillerSetupItem curre
336336
throw new ArgumentException(message);
337337
}
338338

339-
object valueObject = GetFilledObject(valueType, currentSetupItem);
339+
object valueObject = GetFilledObject(valueType, currentSetupItem, typeTracker);
340340
dictionary.Add(keyObject, valueObject);
341341
}
342342
return dictionary;
@@ -348,7 +348,7 @@ private static bool HasTypeARandomFunc(Type type, FillerSetupItem currentSetupIt
348348
}
349349

350350

351-
private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem)
351+
private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
352352
{
353353
Type genType = propertyType.GetGenericArguments()[0];
354354

@@ -374,13 +374,13 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem)
374374
int maxListItems = Random.Next(currentSetupItem.ListMinCount, currentSetupItem.ListMaxCount);
375375
for (int i = 0; i < maxListItems; i++)
376376
{
377-
object listObject = GetFilledObject(genType, currentSetupItem);
377+
object listObject = GetFilledObject(genType, currentSetupItem, typeTracker);
378378
list.Add(listObject);
379379
}
380380
return list;
381381
}
382382

383-
private object GetInterfaceInstance(Type interfaceType, FillerSetupItem setupItem)
383+
private object GetInterfaceInstance(Type interfaceType, FillerSetupItem setupItem, HashStack<Type> typeTracker)
384384
{
385385
object result;
386386
if (setupItem.TypeToRandomFunc.ContainsKey(interfaceType))
@@ -405,7 +405,7 @@ private object GetInterfaceInstance(Type interfaceType, FillerSetupItem setupIte
405405
MethodInfo genericMethod = method.MakeGenericMethod(new[] { interfaceType });
406406
result = genericMethod.Invoke(setupItem.InterfaceMocker, null);
407407
}
408-
FillInternal(result);
408+
FillInternal(result, typeTracker);
409409
return result;
410410
}
411411

ObjectFiller/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3333
// übernehmen, indem Sie "*" eingeben:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.8.0")]
36-
[assembly: AssemblyFileVersion("1.2.8.0")]
35+
[assembly: AssemblyVersion("1.2.9.0")]
36+
[assembly: AssemblyFileVersion("1.2.9.0")]
3737

3838
[assembly: InternalsVisibleTo("ObjectFiller.Test")]

0 commit comments

Comments
 (0)