Skip to content

Fix: Dictionaries and SortedLists caused an exception #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2015
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
14 changes: 12 additions & 2 deletions ObjectFiller.Test/ListFillingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace ObjectFiller.Test
{
using ObjectFiller.Test.TestPoco;

[TestClass]
public class ListFillingTest
{
Expand Down Expand Up @@ -131,6 +133,16 @@ public void GenerateTestDataForADictionary()
}
}

[TestMethod]
public void GenerateDictionaryWithEnumeration()
{
var amountOfEnumValues = Enum.GetValues(typeof(TestEnum)).Length;
var filler = new Filler<Dictionary<TestEnum, string>>();
var result = filler.Create();

Assert.AreEqual(amountOfEnumValues, result.Count);
}

private Entity[] GetArray()
{
Filler<Entity> of = new Filler<Entity>();
Expand All @@ -146,9 +158,7 @@ private Entity[] GetArray()
entities.Add(of.Create());
entities.Add(of.Create());


return entities.ToArray();
}

}
}
19 changes: 19 additions & 0 deletions ObjectFiller.Test/LoremIpsumPluginTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace ObjectFiller.Test
{
using System.Collections.Generic;

[TestClass]
public class LoremIpsumPluginTest
{
Expand Down Expand Up @@ -74,5 +76,22 @@ public void Test_With_LoremIpsum_Seed_Settings()
Assert.IsNotNull(b1);
Assert.AreEqual(b.ISBN, b1.ISBN);
}

[TestMethod]
public void LoremIpsum_should_provide_different_data()
{
var alowedDelta = 2;

var filler = new Filler<Book>();
filler.Setup()
.OnProperty(foo => foo.Description)
.Use(new Lipsum(LipsumFlavor.LoremIpsum));

var resultElements = filler.Create(100);

var groupedResult = resultElements.GroupBy(x => x.Description);

Assert.AreEqual(100, groupedResult.Count(), alowedDelta);
}
}
}
1 change: 1 addition & 0 deletions ObjectFiller.Test/ObjectFiller.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="TestPoco\SimpleList.cs" />
<Compile Include="SaveFillerSetupTest.cs" />
<Compile Include="EmailAddressesPluginTest.cs" />
<Compile Include="TestPoco\TestEnum.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObjectFiller\ObjectFiller.csproj">
Expand Down
12 changes: 11 additions & 1 deletion ObjectFiller.Test/RandomizerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ObjectFiller.Test
{
using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -45,7 +46,16 @@ public void TryingToCreateAnObjectWithAnInterfaceShallFailAndHaveAnInnerexceptio
Assert.IsNotNull(ex.InnerException);
throw;
}

}

[TestMethod]
public void RandomizerCreatesAListOfRandomItemsIfNeeded()
{
int amount = 5;

IEnumerable<int> result = Randomizer<int>.Create(amount);

Assert.AreEqual(amount, result.Count());
}

}
Expand Down
13 changes: 13 additions & 0 deletions ObjectFiller.Test/TestPoco/TestEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ObjectFiller.Test.TestPoco
{
using System;

[Flags]
public enum TestEnum
{
ValueOne,
ValueTwo,
ValueThree,
ValueFour
}
}
36 changes: 20 additions & 16 deletions ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,9 @@ public T Create()
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;
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);
items.Add(this.Create());
}

return items;
Expand Down Expand Up @@ -635,13 +622,30 @@ private IDictionary GetFilledDictionary(
Type keyType = propertyType.GetGenericArguments()[0];
Type valueType = propertyType.GetGenericArguments()[1];

int maxDictionaryItems = Random.Next(
int maxDictionaryItems = 0;

if (keyType.IsEnum)
{
maxDictionaryItems = Enum.GetValues(keyType).Length;
}
else
{
maxDictionaryItems = Random.Next(
currentSetupItem.DictionaryKeyMinCount,
currentSetupItem.DictionaryKeyMaxCount);
}

for (int i = 0; i < maxDictionaryItems; i++)
{
object keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
object keyObject = null;
if (keyType.IsEnum)
{
keyObject = Enum.GetValues(keyType).GetValue(i);
}
else
{
keyObject = this.CreateAndFillObject(keyType, currentSetupItem, typeTracker);
}

if (dictionary.Contains(keyObject))
{
Expand Down
22 changes: 14 additions & 8 deletions ObjectFiller/Plugins/String/Lipsum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Lipsum : IRandomizerPlugin<string>
private readonly int maxSentences;
private readonly int minWords;
private readonly int maxWords;
private readonly int seed;
private readonly int? seed;

/// <summary>
/// Words for the standard lorem ipsum text.
Expand Down Expand Up @@ -130,6 +130,8 @@ public class Lipsum : IRandomizerPlugin<string>
/// </summary>
private readonly Dictionary<LipsumFlavor, string[]> map;

private System.Random random;

/// <summary>
/// Initializes a new instance of the <see cref="Lipsum"/> class.
/// </summary>
Expand All @@ -152,7 +154,7 @@ public class Lipsum : IRandomizerPlugin<string>
/// The max words of the generated text.
/// </param>
/// <param name="seed">
/// The seed for the random to get the same result with the same seed.
/// The seed for randomizer to get the same result with the same seed.
/// </param>
public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int maxSentences = 8,
int minWords = 10, int maxWords = 50, int? seed = null)
Expand All @@ -172,7 +174,8 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
{ LipsumFlavor.LeMasque, LeMasque }
};

this.seed = seed.HasValue ? seed.Value : Environment.TickCount;
this.seed = seed;
this.random = new System.Random();
}

/// <summary>
Expand All @@ -181,20 +184,23 @@ public Lipsum(LipsumFlavor flavor, int paragraphs = 3, int minSentences = 3, int
/// <returns>Random data for type <see cref="T"/></returns>
public string GetValue()
{
System.Random rnd = new System.Random(this.seed);
var array = this.map[this.flavor];
if (this.seed.HasValue)
{
this.random = new System.Random(this.seed.Value);
}

var array = this.map[this.flavor];
var result = new StringBuilder();

for (var i = 0; i < this.paragraphs; i++)
{
var sentences = rnd.Next(this.minSentences, this.maxSentences + 1);
var sentences = this.random.Next(this.minSentences, this.maxSentences + 1);
for (var j = 0; j < sentences; j++)
{
var words = rnd.Next(this.minWords, this.maxWords + 1);
var words = this.random.Next(this.minWords, this.maxWords + 1);
for (var k = 0; k < words; k++)
{
var word = array[rnd.Next(array.Length)];
var word = array[this.random.Next(array.Length)];
if (k == 0)
{
word = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);
Expand Down
18 changes: 18 additions & 0 deletions ObjectFiller/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Tynamix.ObjectFiller
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

/// <summary>
/// This class is a easy way to get random values.
Expand Down Expand Up @@ -65,6 +67,22 @@ public static T Create()
return (T)Setup.TypeToRandomFunc[typeof(T)]();
}

/// <summary>
/// Creates a set of random items of the given type. It will use a <see cref="IRandomizerPlugin{T}"/> for that.
/// </summary>
/// <param name="amount">Amount of items created.</param>
/// <returns>Set of random items of the given type.</returns>
public static IEnumerable<T> Create(int amount)
{
var resultSet = new List<T>();
for (int i = 0; i < amount; i++)
{
resultSet.Add(Create());
}

return resultSet;
}

/// <summary>
/// Creates a random value of the target type. It will use a <see cref="IRandomizerPlugin{T}"/> for that
/// </summary>
Expand Down