Skip to content

Commit

Permalink
Merge pull request #51 from Tynamix/feature_fill_arrays
Browse files Browse the repository at this point in the history
Simple Arrays and Jagged Arrays are now supported.
  • Loading branch information
Tynamix committed Aug 2, 2015
2 parents 19fc788 + 3093e0e commit e229a8f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
19 changes: 5 additions & 14 deletions ObjectFiller.Test/ListFillingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public void TestUseEnumerable()
Filler<EntityCollection> eFiller = new Filler<EntityCollection>();
eFiller.Setup()
.ListItemCount(20)
.OnProperty(x => x.EntityArray, x => x.EntityICollection,
.OnProperty(x => x.EntityICollection,
x => x.EntityIList, x => x.ObservableCollection,
x => x.EntityIEnumerable).IgnoreIt()
.OnProperty(x => x.EntityArray).IgnoreIt()
.SetupFor<Entity>()
.OnProperty(x => x.Id).Use(Enumerable.Range(1, 22).Select(x => (int)Math.Pow(2, x)));

Expand Down Expand Up @@ -143,22 +144,12 @@ public void GenerateDictionaryWithEnumeration()
Assert.AreEqual(amountOfEnumValues, result.Count);
}

private Entity[] GetArray()
private Entity[,] GetArray()
{
Filler<Entity> of = new Filler<Entity>();
var entity = new Entity[,] { { of.Create() } };

List<Entity> entities = new List<Entity>();
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());
entities.Add(of.Create());

return entities.ToArray();
return entity;
}
}
}
18 changes: 13 additions & 5 deletions ObjectFiller.Test/PatternGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Reflection.Emit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using ObjectFiller.Test.TestPoco.Person;
using Tynamix.ObjectFiller;

namespace ObjectFiller.Test
Expand All @@ -15,8 +17,8 @@ public void Must_be_able_to_handle_private_setters()
var filler = new Filler<ClassWithPrivateStuffSealed>();
filler.Setup()
.OnProperty(x => x.NameStyle).DoIt(At.TheEnd).Use(() => NameStyle.FirstNameLastName)
.OnProperty(x=>x.WithPrivateSetter);
.OnProperty(x => x.WithPrivateSetter);


var obj = filler.Create();

Expand All @@ -37,7 +39,7 @@ public void Must_be_able_to_handle_inheritance_and_sealed()
Assert.AreNotEqual(0, obj.SealedOverrideNormalNumber);
}

[TestMethod, Ignore]
[TestMethod]
public void Must_be_able_to_handle_arrays()
{
var filler = new Filler<WithArrays>();
Expand All @@ -47,7 +49,10 @@ public void Must_be_able_to_handle_arrays()

Assert.IsNotNull(obj.Ints);
Assert.IsNotNull(obj.Strings);
Assert.IsNotNull(obj.Interfaces);
Assert.IsNotNull(obj.JaggedStrings);
Assert.IsNotNull(obj.ThreeJaggedDimensional);
Assert.IsNotNull(obj.ThreeJaggedPoco);

}

[TestMethod]
Expand Down Expand Up @@ -274,6 +279,9 @@ public class WithArrays
{
public int[] Ints { get; set; }
public string[] Strings { get; set; }
public IDisposable[] Interfaces { get; set; }
public string[][] JaggedStrings { get; set; }
public string[][][] ThreeJaggedDimensional { get; set; }

public Address[][][] ThreeJaggedPoco { get; set; }
}
}
2 changes: 1 addition & 1 deletion ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class EntityCollection

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

public Entity[] EntityArray { get; set; }
public Entity[,] EntityArray { get; set; }
}
}
43 changes: 42 additions & 1 deletion ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,16 @@ private object CreateAndFillObject(
if (TypeIsList(type))
{
IList list = this.GetFilledList(type, currentSetupItem, typeTracker);

return list;
}

if (TypeIsArray(type))
{
Array array = this.GetFilledArray(type, currentSetupItem, typeTracker);
return array;
}

if (type.IsInterface || type.IsAbstract)
{
return this.CreateInstanceOfInterfaceOrAbstractClass(type, currentSetupItem, typeTracker);
Expand All @@ -427,6 +434,29 @@ private object CreateAndFillObject(
return newValue;
}

/// <summary>
/// Creates and filles an array or jagged array
/// </summary>
/// <param name="type">Array type to create and fill</param>
/// <param name="currentSetupItem">Current ObjectFiller.NET Setup item</param>
/// <param name="typeTracker">
/// The type tracker to find circular dependencies
/// </param>
/// <returns>Created and filled array</returns>
private Array GetFilledArray(Type type, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
{
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(type.GetElementType());

var list = GetFilledList(constructedListType, currentSetupItem, typeTracker);

var array = (Array)Activator.CreateInstance(type, new object[] { list.Count });

list.CopyTo(array, 0);

return array;
}

/// <summary>
/// Creates a instance of an interface or abstract class. Like an IoC-Framework
/// </summary>
Expand All @@ -437,7 +467,7 @@ private object CreateAndFillObject(
/// The setup item.
/// </param>
/// <param name="typeTracker">
/// The dictionaryType tracker to find circular dependencies
/// The type tracker to find circular dependencies
/// </param>
/// <returns>
/// The created and filled instance of the <see cref="interfaceType"/>
Expand Down Expand Up @@ -944,6 +974,17 @@ private static bool TypeIsNullableEnum(Type type)
return (u != null) && u.IsEnum;
}

/// <summary>
/// Checks if the given <see cref="type"/> is a supported array
/// </summary>
/// <param name="type">Type to check</param>
/// <returns>True if the type is a array</returns>
private bool TypeIsArray(Type type)
{
return type.IsArray && type.GetArrayRank() == 1;
}


#endregion
}
}

0 comments on commit e229a8f

Please sign in to comment.