Skip to content

Commit 132f412

Browse files
author
Roman Köhler
committed
Merge branch 'master' into 69_IgnoreInheritanc
2 parents 1c6a62b + 05a7c0b commit 132f412

File tree

14 files changed

+434
-46
lines changed

14 files changed

+434
-46
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,5 @@ FakesAssemblies/
176176
ObjectFillerNET.v2.ncrunchsolution
177177
ObjectFiller/ObjectFiller.v2.ncrunchproject
178178
ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
179+
.vs/config/applicationhost.config
180+
Output-Build.txt
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace ObjectFiller.Test.BugfixTests
7+
{
8+
using Tynamix.ObjectFiller;
9+
10+
using Xunit;
11+
12+
public class Bug68HashsetCanNotBeCreated
13+
{
14+
[Fact]
15+
public void AHashsetShouldBeGenerated()
16+
{
17+
Filler<HashSet<string>> filler = new Filler<HashSet<string>>();
18+
19+
var hashset = filler.Create();
20+
21+
Assert.NotNull(hashset);
22+
Assert.True(hashset.Any());
23+
}
24+
}
25+
}

Tynamix.ObjectFiller.Test/ListFillingTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public void TestFillAllListsExceptArray()
2626
Assert.NotNull(entity.EntityICollection);
2727
Assert.NotNull(entity.EntityIEnumerable);
2828
Assert.NotNull(entity.EntityIList);
29+
Assert.NotNull(entity.EntityHashset);
30+
2931
}
3032

3133
[Fact]
@@ -64,7 +66,7 @@ public void TestFillList()
6466
Assert.NotNull(entity.EntityIEnumerable);
6567
Assert.NotNull(entity.EntityIList);
6668
Assert.NotNull(entity.EntityArray);
67-
69+
Assert.NotNull(entity.EntityHashset);
6870
}
6971

7072
[Fact]
@@ -79,6 +81,7 @@ public void TestIgnoreAllUnknownTypesWithOutException()
7981
Assert.NotNull(entity.EntityICollection);
8082
Assert.NotNull(entity.EntityIEnumerable);
8183
Assert.NotNull(entity.EntityIList);
84+
Assert.NotNull(entity.EntityHashset);
8285
}
8386

8487
[Fact]
@@ -117,6 +120,7 @@ public void GenerateTestDataForASimpleList()
117120
Assert.True(entityCollection.EntityIEnumerable.Any());
118121
Assert.True(entityCollection.EntityIList.Any());
119122
Assert.True(entityCollection.EntityList.Any());
123+
Assert.NotNull(entityCollection.EntityHashset.Any());
120124
}
121125
}
122126

Tynamix.ObjectFiller.Test/LoremIpsumPluginTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Linq;
3-
using Xunit;
3+
using Xunit;
44
using ObjectFiller.Test.TestPoco.Library;
55
using Tynamix.ObjectFiller;
66

@@ -17,7 +17,7 @@ public void Test_With_Many_MinWords_And_Many_MinSentences()
1717
{
1818
Filler<Book> book = new Filler<Book>();
1919
book.Setup()
20-
.OnProperty(x => x.ISBN).Use(new Lipsum(LipsumFlavor.InDerFremde, 3, 9, minWords: 51));
20+
.OnProperty(x => x.ISBN).Use(new Lipsum(LipsumFlavor.InDerFremde, 51, 100, 100));
2121

2222
var b = book.Create();
2323

@@ -67,7 +67,7 @@ public void Test_With_LoremIpsum_Seed_Settings()
6767
{
6868
Filler<Book> book = new Filler<Book>();
6969
book.Setup()
70-
.OnProperty(x => x.ISBN).Use(new Lipsum(LipsumFlavor.LoremIpsum, seed: 1234));
70+
.OnProperty(x => x.ISBN).Use(new Lipsum(LipsumFlavor.LoremIpsum, 3, 5, 1, 5, 3, 1234));
7171

7272
var b = book.Create();
7373
var b1 = book.Create();

Tynamix.ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class EntityCollection
1515

1616
public IList<Entity> EntityIList { get; set; }
1717

18+
public HashSet<Entity> EntityHashset { get; set; }
19+
1820
public Entity[,] EntityArray { get; set; }
1921
}
2022
}

Tynamix.ObjectFiller/Filler.cs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Tynamix.ObjectFiller
1313
using System;
1414
using System.Collections;
1515
using System.Collections.Generic;
16+
using System.Collections.ObjectModel;
1617
using System.Linq;
1718
using System.Reflection;
1819

@@ -254,9 +255,29 @@ private static bool TypeIsDictionary(Type type)
254255
/// </returns>
255256
private static bool TypeIsList(Type type)
256257
{
257-
return !type.IsArray && type.IsGenericType() && type.GetGenericTypeArguments().Length != 0
258+
return !type.IsArray
259+
&& type.IsGenericType()
260+
&& type.GetGenericTypeArguments().Length != 0
258261
&& (type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
259-
|| type.GetImplementedInterfaces().Any(x => x == typeof(IEnumerable)));
262+
|| type.GetImplementedInterfaces().Any(x => x == typeof(IList)));
263+
}
264+
265+
/// <summary>
266+
/// Checks if the given <see cref="type"/> is a list
267+
/// </summary>
268+
/// <param name="type">
269+
/// The type to check
270+
/// </param>
271+
/// <returns>
272+
/// True if the target <see cref="type"/> is a list
273+
/// </returns>
274+
private static bool TypeIsCollection(Type type)
275+
{
276+
return !type.IsArray
277+
&& type.IsGenericType()
278+
&& type.GetGenericTypeArguments().Length != 0
279+
&& (type.GetGenericTypeDefinition() == typeof(ICollection<>)
280+
|| type.GetImplementedInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(ICollection<>)));
260281
}
261282

262283
/// <summary>
@@ -409,6 +430,13 @@ private object CreateAndFillObject(
409430
return list;
410431
}
411432

433+
if (TypeIsCollection(type))
434+
{
435+
IEnumerable collection = this.GetFilledCollection(type, currentSetupItem, typeTracker);
436+
437+
return collection;
438+
}
439+
412440
if (TypeIsArray(type))
413441
{
414442
Array array = this.GetFilledArray(type, currentSetupItem, typeTracker);
@@ -720,7 +748,7 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
720748

721749
IList list;
722750
if (!propertyType.IsInterface()
723-
&& propertyType.GetImplementedInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(ICollection<>)))
751+
&& propertyType.GetImplementedInterfaces().Any(x => x == typeof(IList)))
724752
{
725753
list = (IList)Activator.CreateInstance(propertyType);
726754
}
@@ -746,6 +774,60 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
746774
return list;
747775
}
748776

777+
/// <summary>
778+
/// Creates and fills a list of the given <see cref="propertyType"/>
779+
/// </summary>
780+
/// <param name="propertyType">
781+
/// Type of the list
782+
/// </param>
783+
/// <param name="currentSetupItem">
784+
/// The current setup item.
785+
/// </param>
786+
/// <param name="typeTracker">
787+
/// The dictionaryType tracker to find circular dependencies
788+
/// </param>
789+
/// <returns>
790+
/// Created and filled list of the given <see cref="propertyType"/>
791+
/// </returns>
792+
private IEnumerable GetFilledCollection(Type propertyType, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
793+
{
794+
Type genType = propertyType.GetGenericTypeArguments()[0];
795+
796+
if (this.CheckForCircularReference(genType, typeTracker, currentSetupItem))
797+
{
798+
return null;
799+
}
800+
801+
IEnumerable target;
802+
803+
if (!propertyType.IsInterface()
804+
&& propertyType.GetImplementedInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(ICollection<>)))
805+
{
806+
target = (IEnumerable)Activator.CreateInstance(propertyType);
807+
}
808+
else if (propertyType.IsGenericType() && propertyType.GetGenericTypeDefinition() == typeof(ICollection<>)
809+
|| propertyType.GetImplementedInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(ICollection<>)))
810+
{
811+
Type openListType = typeof(List<>);
812+
Type genericListType = openListType.MakeGenericType(genType);
813+
target = (IEnumerable)Activator.CreateInstance(genericListType);
814+
}
815+
else
816+
{
817+
target = (IEnumerable)Activator.CreateInstance(propertyType);
818+
}
819+
820+
int maxListItems = Random.Next(currentSetupItem.ListMinCount, currentSetupItem.ListMaxCount);
821+
for (int i = 0; i < maxListItems; i++)
822+
{
823+
object listObject = this.CreateAndFillObject(genType, currentSetupItem, typeTracker);
824+
MethodInfo method = target.GetType().GetMethod("Add");
825+
method.Invoke(target, new object[] { listObject });
826+
}
827+
828+
return target;
829+
}
830+
749831
/// <summary>
750832
/// Creates and fills a POCO class
751833
/// </summary>

Tynamix.ObjectFiller/Plugins/DateTime/DateTimeRange.cs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,87 @@
22

33
namespace Tynamix.ObjectFiller
44
{
5+
/// <summary>
6+
/// The date time range plugin.
7+
/// </summary>
58
public class DateTimeRange : IRandomizerPlugin<DateTime>, IRandomizerPlugin<DateTime?>
69
{
7-
private readonly DateTime _earliestDate;
8-
private readonly DateTime _latestDate;
10+
/// <summary>
11+
/// The earliest date.
12+
/// </summary>
13+
private readonly DateTime earliestDate;
914

15+
/// <summary>
16+
/// The latest date.
17+
/// </summary>
18+
private readonly DateTime latestDate;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
22+
/// </summary>
23+
public DateTimeRange()
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
29+
/// </summary>
30+
/// <param name="earliestDate">
31+
/// The earliest date.
32+
/// </param>
1033
public DateTimeRange(DateTime earliestDate)
1134
: this(earliestDate, DateTime.Now)
1235
{
1336
}
1437

38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="DateTimeRange"/> class.
40+
/// </summary>
41+
/// <param name="earliestDate">
42+
/// The earliest date.
43+
/// </param>
44+
/// <param name="latestDate">
45+
/// The latest date.
46+
/// </param>
1547
public DateTimeRange(DateTime earliestDate, DateTime latestDate)
1648
{
17-
1849
if (earliestDate > latestDate)
1950
{
20-
this._latestDate = earliestDate;
21-
this._earliestDate = latestDate;
51+
this.latestDate = earliestDate;
52+
this.earliestDate = latestDate;
2253
}
2354
else if (earliestDate == latestDate)
2455
{
25-
this._latestDate = latestDate.AddMonths(Random.Next(1, 120));
26-
this._earliestDate = earliestDate;
56+
this.latestDate = latestDate.AddMonths(Random.Next(1, 120));
57+
this.earliestDate = earliestDate;
2758
}
2859
else
2960
{
30-
this._earliestDate = earliestDate;
31-
this._latestDate = latestDate;
61+
this.earliestDate = earliestDate;
62+
this.latestDate = latestDate;
3263
}
3364
}
3465

66+
/// <summary>
67+
/// Gets random data for type <see cref="T"/>
68+
/// </summary>
69+
/// <returns>Random data for type <see cref="T"/></returns>
3570
public DateTime GetValue()
3671
{
37-
var timeSpan = _latestDate.Subtract(_earliestDate);
72+
var timeSpan = this.latestDate.Subtract(this.earliestDate);
3873

3974
var diff = Random.NextLong(0, timeSpan.Ticks);
4075

41-
return _latestDate.AddTicks(diff * -1);
76+
return this.latestDate.AddTicks(diff * -1);
4277
}
4378

79+
/// <summary>
80+
/// Gets random data for type <see cref="T"/>
81+
/// </summary>
82+
/// <returns>Random data for type <see cref="T"/></returns>
4483
DateTime? IRandomizerPlugin<DateTime?>.GetValue()
4584
{
46-
return GetValue();
85+
return this.GetValue();
4786
}
4887
}
4988
}

0 commit comments

Comments
 (0)