Skip to content

Commit a7c8bf7

Browse files
committed
Merge pull request #44 from Tynamix/ENHANCE_StreetNamePlugin
Enhance street name plugin
2 parents f1ffa5f + 79c7ba3 commit a7c8bf7

File tree

10 files changed

+290
-73
lines changed

10 files changed

+290
-73
lines changed

ObjectFiller.Test/GermanStreetNamesPluginTest.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

ObjectFiller.Test/ListFillingTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,51 @@ public void TestIgnoreAllUnknownTypesWithException()
8686
filler.Create();
8787
}
8888

89+
[TestMethod]
90+
public void GenerateTestDataForASortedList()
91+
{
92+
Filler<SortedList<int, string>> filler = new Filler<SortedList<int, string>>();
93+
filler.Setup().OnType<int>().Use(Enumerable.Range(1, 1000));
94+
var result = filler.Create(10).ToList();
95+
96+
Assert.AreEqual(10, result.Count);
97+
foreach (var sortedList in result)
98+
{
99+
Assert.IsTrue(sortedList.Any());
100+
}
101+
}
102+
103+
[TestMethod]
104+
public void GenerateTestDataForASimpleList()
105+
{
106+
Filler<IList<EntityCollection>> filler = new Filler<IList<EntityCollection>>();
107+
filler.Setup().IgnoreAllUnknownTypes();
108+
var createdList = filler.Create();
109+
110+
Assert.IsTrue(createdList.Any());
111+
112+
foreach (EntityCollection entityCollection in createdList)
113+
{
114+
Assert.IsTrue(entityCollection.EntityICollection.Any());
115+
Assert.IsTrue(entityCollection.EntityIEnumerable.Any());
116+
Assert.IsTrue(entityCollection.EntityIList.Any());
117+
Assert.IsTrue(entityCollection.EntityList.Any());
118+
}
119+
}
120+
121+
[TestMethod]
122+
public void GenerateTestDataForADictionary()
123+
{
124+
Filler<Dictionary<int, string>> filler = new Filler<Dictionary<int, string>>();
125+
var result = filler.Create(10).ToList();
126+
127+
Assert.AreEqual(10, result.Count);
128+
foreach (var sortedList in result)
129+
{
130+
Assert.IsTrue(sortedList.Any());
131+
}
132+
}
133+
89134
private Entity[] GetArray()
90135
{
91136
Filler<Entity> of = new Filler<Entity>();
@@ -104,5 +149,6 @@ private Entity[] GetArray()
104149

105150
return entities.ToArray();
106151
}
152+
107153
}
108154
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<Compile Include="CreateInstanceTest.cs" />
5151
<Compile Include="DefaultDatatypeMappingsTest.cs" />
5252
<Compile Include="EnumTest.cs" />
53-
<Compile Include="GermanStreetNamesPluginTest.cs" />
53+
<Compile Include="StreetNamesPluginTest.cs" />
5454
<Compile Include="HashStackTests.cs" />
5555
<Compile Include="LibraryFillingTest.cs" />
5656
<Compile Include="ListFillingTest.cs" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Tynamix.ObjectFiller.Plugins.String;
3+
4+
namespace ObjectFiller.Test
5+
{
6+
using Tynamix.ObjectFiller;
7+
8+
[TestClass]
9+
public class StreetNamesPluginTest
10+
{
11+
[TestMethod]
12+
public void RandomNameIsReturned()
13+
{
14+
var sut = new StreetName(City.Dresden);
15+
var value = sut.GetValue();
16+
Assert.IsFalse(string.IsNullOrEmpty(value));
17+
18+
19+
sut = new StreetName(City.NewYork);
20+
value = sut.GetValue();
21+
Assert.IsFalse(string.IsNullOrEmpty(value));
22+
23+
sut = new StreetName(City.London);
24+
value = sut.GetValue();
25+
Assert.IsFalse(string.IsNullOrEmpty(value));
26+
27+
sut = new StreetName(City.Moscow);
28+
value = sut.GetValue();
29+
Assert.IsFalse(string.IsNullOrEmpty(value));
30+
31+
sut = new StreetName(City.Paris);
32+
value = sut.GetValue();
33+
Assert.IsFalse(string.IsNullOrEmpty(value));
34+
35+
sut = new StreetName(City.Tokyo);
36+
value = sut.GetValue();
37+
Assert.IsFalse(string.IsNullOrEmpty(value));
38+
}
39+
}
40+
}

ObjectFiller/Filler.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ public Filler()
5858
/// </returns>
5959
public T Create()
6060
{
61-
T objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), new HashStack<Type>());
62-
63-
this.Fill(objectToFill);
61+
T objectToFill;
62+
var hashStack = new HashStack<Type>();
63+
if (!TypeIsClrType(typeof(T)))
64+
{
65+
objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), hashStack);
66+
this.Fill(objectToFill);
67+
}
68+
else
69+
{
70+
objectToFill = (T)this.CreateAndFillObject(typeof(T), this.setupManager.GetFor<T>(), hashStack);
71+
}
6472

6573
return objectToFill;
6674
}
@@ -77,13 +85,26 @@ public T Create()
7785
/// </returns>
7886
public IEnumerable<T> Create(int count)
7987
{
88+
IList<T> items = new List<T>();
8089
var typeStack = new HashStack<Type>();
90+
Type targetType = typeof(T);
8191
for (int n = 0; n < count; n++)
8292
{
83-
T objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), typeStack);
84-
this.Fill(objectToFill);
85-
yield return objectToFill;
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);
86105
}
106+
107+
return items;
87108
}
88109

89110
/// <summary>
@@ -267,6 +288,17 @@ private static bool TypeIsPoco(Type type)
267288
|| (!type.Namespace.StartsWith("System") && !type.Namespace.StartsWith("Microsoft")));
268289
}
269290

291+
/// <summary>
292+
/// Check if the given type is a type from the common language library
293+
/// </summary>
294+
/// <param name="type">Type to check</param>
295+
/// <returns>True if the given type is a type from the common language library</returns>
296+
private static bool TypeIsClrType(Type type)
297+
{
298+
return (type.Namespace != null && (type.Namespace.StartsWith("System") || type.Namespace.StartsWith("Microsoft")))
299+
|| type.Module.ScopeName == "CommonLanguageRuntimeLibrary";
300+
}
301+
270302
/// <summary>
271303
/// Checks if the given <see cref="type"/> can be used by the object filler
272304
/// </summary>

ObjectFiller/ObjectFiller.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<ItemGroup>
3939
<Compile Include="Plugins\String\CityNames.cs" />
4040
<Compile Include="Plugins\String\EmailAddresses.cs" />
41-
<Compile Include="Plugins\String\GermanStreetNames.cs" />
41+
<Compile Include="Plugins\String\StreetName.cs" />
4242
<Compile Include="Plugins\String\Lipsum.cs" />
4343
<Compile Include="HashStack.cs" />
4444
<Compile Include="Randomizer.cs" />

ObjectFiller/Plugins/String/GermanStreetNames.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="StreetName.cs" company="Tynamix">
3+
// © 2015 by Hendrik Lösch and Roman Köhler
4+
// </copyright>
5+
// <summary>
6+
// Generate streetnames for type <see cref="string" />
7+
// </summary>
8+
// --------------------------------------------------------------------------------------------------------------------
9+
10+
namespace Tynamix.ObjectFiller
11+
{
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
15+
using Tynamix.ObjectFiller.Properties;
16+
17+
/// <summary>
18+
/// The city of which the street names shall come from
19+
/// </summary>
20+
public enum City
21+
{
22+
/// <summary>
23+
/// Dresden is a city in germany
24+
/// </summary>
25+
Dresden,
26+
27+
/// <summary>
28+
/// New York is a city in the USA
29+
/// </summary>
30+
NewYork,
31+
32+
/// <summary>
33+
/// London is a city in Great Britain
34+
/// </summary>
35+
London,
36+
37+
/// <summary>
38+
/// Paris is a city in France
39+
/// </summary>
40+
Paris,
41+
42+
/// <summary>
43+
/// Tokyo is a city in Japan
44+
/// </summary>
45+
Tokyo,
46+
47+
/// <summary>
48+
/// Moscow is a city in russia
49+
/// </summary>
50+
Moscow
51+
}
52+
53+
/// <summary>
54+
/// Generate street names for type <see cref="string"/>
55+
/// </summary>
56+
public class StreetName : IRandomizerPlugin<string>
57+
{
58+
/// <summary>
59+
/// The names.
60+
/// </summary>
61+
protected readonly List<string> AllStreetNames;
62+
63+
/// <summary>
64+
/// Initializes a new instance of the <see cref="StreetName"/> class.
65+
/// </summary>
66+
/// <param name="targetCity">
67+
/// The city for which the street names will be get from
68+
/// </param>
69+
public StreetName(City targetCity)
70+
{
71+
switch (targetCity)
72+
{
73+
case City.Dresden:
74+
this.AllStreetNames = Resources.germanStreetNames.Split(';').ToList();
75+
break;
76+
case City.Moscow:
77+
this.AllStreetNames = Resources.moscowStreetNames.Split(';').ToList();
78+
break;
79+
case City.NewYork:
80+
this.AllStreetNames = Resources.newYorkStreetNames.Split(';').ToList();
81+
break;
82+
case City.Tokyo:
83+
this.AllStreetNames = Resources.tokyoStreetNames.Split(';').ToList();
84+
break;
85+
case City.Paris:
86+
this.AllStreetNames = Resources.parisStreetNames.Split(';').ToList();
87+
break;
88+
case City.London:
89+
this.AllStreetNames = Resources.londonStreetNames.Split(';').ToList();
90+
break;
91+
}
92+
}
93+
94+
/// <summary>
95+
/// Gets random data for type <see cref="T"/>
96+
/// </summary>
97+
/// <returns>Random data for type <see cref="T"/></returns>
98+
public string GetValue()
99+
{
100+
var index = Random.Next(this.AllStreetNames.Count - 1);
101+
return this.AllStreetNames[index];
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)