Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions ObjectFiller.Test/GermanStreetNamesPluginTest.cs

This file was deleted.

46 changes: 46 additions & 0 deletions ObjectFiller.Test/ListFillingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,51 @@ public void TestIgnoreAllUnknownTypesWithException()
filler.Create();
}

[TestMethod]
public void GenerateTestDataForASortedList()
{
Filler<SortedList<int, string>> filler = new Filler<SortedList<int, string>>();
filler.Setup().OnType<int>().Use(Enumerable.Range(1, 1000));
var result = filler.Create(10).ToList();

Assert.AreEqual(10, result.Count);
foreach (var sortedList in result)
{
Assert.IsTrue(sortedList.Any());
}
}

[TestMethod]
public void GenerateTestDataForASimpleList()
{
Filler<IList<EntityCollection>> filler = new Filler<IList<EntityCollection>>();
filler.Setup().IgnoreAllUnknownTypes();
var createdList = filler.Create();

Assert.IsTrue(createdList.Any());

foreach (EntityCollection entityCollection in createdList)
{
Assert.IsTrue(entityCollection.EntityICollection.Any());
Assert.IsTrue(entityCollection.EntityIEnumerable.Any());
Assert.IsTrue(entityCollection.EntityIList.Any());
Assert.IsTrue(entityCollection.EntityList.Any());
}
}

[TestMethod]
public void GenerateTestDataForADictionary()
{
Filler<Dictionary<int, string>> filler = new Filler<Dictionary<int, string>>();
var result = filler.Create(10).ToList();

Assert.AreEqual(10, result.Count);
foreach (var sortedList in result)
{
Assert.IsTrue(sortedList.Any());
}
}

private Entity[] GetArray()
{
Filler<Entity> of = new Filler<Entity>();
Expand All @@ -104,5 +149,6 @@ private Entity[] GetArray()

return entities.ToArray();
}

}
}
2 changes: 1 addition & 1 deletion ObjectFiller.Test/ObjectFiller.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Compile Include="CreateInstanceTest.cs" />
<Compile Include="DefaultDatatypeMappingsTest.cs" />
<Compile Include="EnumTest.cs" />
<Compile Include="GermanStreetNamesPluginTest.cs" />
<Compile Include="StreetNamesPluginTest.cs" />
<Compile Include="HashStackTests.cs" />
<Compile Include="LibraryFillingTest.cs" />
<Compile Include="ListFillingTest.cs" />
Expand Down
40 changes: 40 additions & 0 deletions ObjectFiller.Test/StreetNamesPluginTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tynamix.ObjectFiller.Plugins.String;

namespace ObjectFiller.Test
{
using Tynamix.ObjectFiller;

[TestClass]
public class StreetNamesPluginTest
{
[TestMethod]
public void RandomNameIsReturned()
{
var sut = new StreetName(City.Dresden);
var value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));


sut = new StreetName(City.NewYork);
value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));

sut = new StreetName(City.London);
value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));

sut = new StreetName(City.Moscow);
value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));

sut = new StreetName(City.Paris);
value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));

sut = new StreetName(City.Tokyo);
value = sut.GetValue();
Assert.IsFalse(string.IsNullOrEmpty(value));
}
}
}
44 changes: 38 additions & 6 deletions ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ public Filler()
/// </returns>
public T Create()
{
T objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), new HashStack<Type>());

this.Fill(objectToFill);
T objectToFill;
var hashStack = new HashStack<Type>();
if (!TypeIsClrType(typeof(T)))
{
objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), hashStack);
this.Fill(objectToFill);
}
else
{
objectToFill = (T)this.CreateAndFillObject(typeof(T), this.setupManager.GetFor<T>(), hashStack);
}

return objectToFill;
}
Expand All @@ -77,13 +85,26 @@ public T Create()
/// </returns>
public IEnumerable<T> Create(int count)
{
IList<T> items = new List<T>();
var typeStack = new HashStack<Type>();
Type targetType = typeof(T);
for (int n = 0; n < count; n++)
{
T objectToFill = (T)this.CreateInstanceOfType(typeof(T), this.setupManager.GetFor<T>(), typeStack);
this.Fill(objectToFill);
yield return objectToFill;
T objectToFill;
if (!TypeIsClrType(targetType))
{
objectToFill = (T)this.CreateInstanceOfType(targetType, this.setupManager.GetFor<T>(), typeStack);
this.Fill(objectToFill);
}
else
{
objectToFill = (T)this.CreateAndFillObject(typeof(T), this.setupManager.GetFor<T>(), typeStack);
}

items.Add(objectToFill);
}

return items;
}

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

/// <summary>
/// Check if the given type is a type from the common language library
/// </summary>
/// <param name="type">Type to check</param>
/// <returns>True if the given type is a type from the common language library</returns>
private static bool TypeIsClrType(Type type)
{
return (type.Namespace != null && (type.Namespace.StartsWith("System") || type.Namespace.StartsWith("Microsoft")))
|| type.Module.ScopeName == "CommonLanguageRuntimeLibrary";
}

/// <summary>
/// Checks if the given <see cref="type"/> can be used by the object filler
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion ObjectFiller/ObjectFiller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<ItemGroup>
<Compile Include="Plugins\String\CityNames.cs" />
<Compile Include="Plugins\String\EmailAddresses.cs" />
<Compile Include="Plugins\String\GermanStreetNames.cs" />
<Compile Include="Plugins\String\StreetName.cs" />
<Compile Include="Plugins\String\Lipsum.cs" />
<Compile Include="HashStack.cs" />
<Compile Include="Randomizer.cs" />
Expand Down
45 changes: 0 additions & 45 deletions ObjectFiller/Plugins/String/GermanStreetNames.cs

This file was deleted.

104 changes: 104 additions & 0 deletions ObjectFiller/Plugins/String/StreetName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StreetName.cs" company="Tynamix">
// � 2015 by Hendrik L�sch and Roman K�hler
// </copyright>
// <summary>
// Generate streetnames for type <see cref="string" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Tynamix.ObjectFiller
{
using System.Collections.Generic;
using System.Linq;

using Tynamix.ObjectFiller.Properties;

/// <summary>
/// The city of which the street names shall come from
/// </summary>
public enum City
{
/// <summary>
/// Dresden is a city in germany
/// </summary>
Dresden,

/// <summary>
/// New York is a city in the USA
/// </summary>
NewYork,

/// <summary>
/// London is a city in Great Britain
/// </summary>
London,

/// <summary>
/// Paris is a city in France
/// </summary>
Paris,

/// <summary>
/// Tokyo is a city in Japan
/// </summary>
Tokyo,

/// <summary>
/// Moscow is a city in russia
/// </summary>
Moscow
}

/// <summary>
/// Generate street names for type <see cref="string"/>
/// </summary>
public class StreetName : IRandomizerPlugin<string>
{
/// <summary>
/// The names.
/// </summary>
protected readonly List<string> AllStreetNames;

/// <summary>
/// Initializes a new instance of the <see cref="StreetName"/> class.
/// </summary>
/// <param name="targetCity">
/// The city for which the street names will be get from
/// </param>
public StreetName(City targetCity)
{
switch (targetCity)
{
case City.Dresden:
this.AllStreetNames = Resources.germanStreetNames.Split(';').ToList();
break;
case City.Moscow:
this.AllStreetNames = Resources.moscowStreetNames.Split(';').ToList();
break;
case City.NewYork:
this.AllStreetNames = Resources.newYorkStreetNames.Split(';').ToList();
break;
case City.Tokyo:
this.AllStreetNames = Resources.tokyoStreetNames.Split(';').ToList();
break;
case City.Paris:
this.AllStreetNames = Resources.parisStreetNames.Split(';').ToList();
break;
case City.London:
this.AllStreetNames = Resources.londonStreetNames.Split(';').ToList();
break;
}
}

/// <summary>
/// Gets random data for type <see cref="T"/>
/// </summary>
/// <returns>Random data for type <see cref="T"/></returns>
public string GetValue()
{
var index = Random.Next(this.AllStreetNames.Count - 1);
return this.AllStreetNames[index];
}
}
}
Loading