Skip to content

Commit e229a8f

Browse files
committed
Merge pull request #51 from Tynamix/feature_fill_arrays
Simple Arrays and Jagged Arrays are now supported.
2 parents 19fc788 + 3093e0e commit e229a8f

File tree

4 files changed

+61
-21
lines changed

4 files changed

+61
-21
lines changed

ObjectFiller.Test/ListFillingTest.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public void TestUseEnumerable()
3434
Filler<EntityCollection> eFiller = new Filler<EntityCollection>();
3535
eFiller.Setup()
3636
.ListItemCount(20)
37-
.OnProperty(x => x.EntityArray, x => x.EntityICollection,
37+
.OnProperty(x => x.EntityICollection,
3838
x => x.EntityIList, x => x.ObservableCollection,
3939
x => x.EntityIEnumerable).IgnoreIt()
40+
.OnProperty(x => x.EntityArray).IgnoreIt()
4041
.SetupFor<Entity>()
4142
.OnProperty(x => x.Id).Use(Enumerable.Range(1, 22).Select(x => (int)Math.Pow(2, x)));
4243

@@ -143,22 +144,12 @@ public void GenerateDictionaryWithEnumeration()
143144
Assert.AreEqual(amountOfEnumValues, result.Count);
144145
}
145146

146-
private Entity[] GetArray()
147+
private Entity[,] GetArray()
147148
{
148149
Filler<Entity> of = new Filler<Entity>();
150+
var entity = new Entity[,] { { of.Create() } };
149151

150-
List<Entity> entities = new List<Entity>();
151-
entities.Add(of.Create());
152-
entities.Add(of.Create());
153-
entities.Add(of.Create());
154-
entities.Add(of.Create());
155-
entities.Add(of.Create());
156-
entities.Add(of.Create());
157-
entities.Add(of.Create());
158-
entities.Add(of.Create());
159-
entities.Add(of.Create());
160-
161-
return entities.ToArray();
152+
return entity;
162153
}
163154
}
164155
}

ObjectFiller.Test/PatternGeneratorTest.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Reflection.Emit;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34
using System.Text.RegularExpressions;
45
using System.Collections.Generic;
6+
using ObjectFiller.Test.TestPoco.Person;
57
using Tynamix.ObjectFiller;
68

79
namespace ObjectFiller.Test
@@ -15,8 +17,8 @@ public void Must_be_able_to_handle_private_setters()
1517
var filler = new Filler<ClassWithPrivateStuffSealed>();
1618
filler.Setup()
1719
.OnProperty(x => x.NameStyle).DoIt(At.TheEnd).Use(() => NameStyle.FirstNameLastName)
18-
.OnProperty(x=>x.WithPrivateSetter);
19-
20+
.OnProperty(x => x.WithPrivateSetter);
21+
2022

2123
var obj = filler.Create();
2224

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

40-
[TestMethod, Ignore]
42+
[TestMethod]
4143
public void Must_be_able_to_handle_arrays()
4244
{
4345
var filler = new Filler<WithArrays>();
@@ -47,7 +49,10 @@ public void Must_be_able_to_handle_arrays()
4749

4850
Assert.IsNotNull(obj.Ints);
4951
Assert.IsNotNull(obj.Strings);
50-
Assert.IsNotNull(obj.Interfaces);
52+
Assert.IsNotNull(obj.JaggedStrings);
53+
Assert.IsNotNull(obj.ThreeJaggedDimensional);
54+
Assert.IsNotNull(obj.ThreeJaggedPoco);
55+
5156
}
5257

5358
[TestMethod]
@@ -274,6 +279,9 @@ public class WithArrays
274279
{
275280
public int[] Ints { get; set; }
276281
public string[] Strings { get; set; }
277-
public IDisposable[] Interfaces { get; set; }
282+
public string[][] JaggedStrings { get; set; }
283+
public string[][][] ThreeJaggedDimensional { get; set; }
284+
285+
public Address[][][] ThreeJaggedPoco { get; set; }
278286
}
279287
}

ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public class EntityCollection
1515

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

18-
public Entity[] EntityArray { get; set; }
18+
public Entity[,] EntityArray { get; set; }
1919
}
2020
}

ObjectFiller/Filler.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,16 @@ private object CreateAndFillObject(
405405
if (TypeIsList(type))
406406
{
407407
IList list = this.GetFilledList(type, currentSetupItem, typeTracker);
408+
408409
return list;
409410
}
410411

412+
if (TypeIsArray(type))
413+
{
414+
Array array = this.GetFilledArray(type, currentSetupItem, typeTracker);
415+
return array;
416+
}
417+
411418
if (type.IsInterface || type.IsAbstract)
412419
{
413420
return this.CreateInstanceOfInterfaceOrAbstractClass(type, currentSetupItem, typeTracker);
@@ -427,6 +434,29 @@ private object CreateAndFillObject(
427434
return newValue;
428435
}
429436

437+
/// <summary>
438+
/// Creates and filles an array or jagged array
439+
/// </summary>
440+
/// <param name="type">Array type to create and fill</param>
441+
/// <param name="currentSetupItem">Current ObjectFiller.NET Setup item</param>
442+
/// <param name="typeTracker">
443+
/// The type tracker to find circular dependencies
444+
/// </param>
445+
/// <returns>Created and filled array</returns>
446+
private Array GetFilledArray(Type type, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
447+
{
448+
var listType = typeof(List<>);
449+
var constructedListType = listType.MakeGenericType(type.GetElementType());
450+
451+
var list = GetFilledList(constructedListType, currentSetupItem, typeTracker);
452+
453+
var array = (Array)Activator.CreateInstance(type, new object[] { list.Count });
454+
455+
list.CopyTo(array, 0);
456+
457+
return array;
458+
}
459+
430460
/// <summary>
431461
/// Creates a instance of an interface or abstract class. Like an IoC-Framework
432462
/// </summary>
@@ -437,7 +467,7 @@ private object CreateAndFillObject(
437467
/// The setup item.
438468
/// </param>
439469
/// <param name="typeTracker">
440-
/// The dictionaryType tracker to find circular dependencies
470+
/// The type tracker to find circular dependencies
441471
/// </param>
442472
/// <returns>
443473
/// The created and filled instance of the <see cref="interfaceType"/>
@@ -944,6 +974,17 @@ private static bool TypeIsNullableEnum(Type type)
944974
return (u != null) && u.IsEnum;
945975
}
946976

977+
/// <summary>
978+
/// Checks if the given <see cref="type"/> is a supported array
979+
/// </summary>
980+
/// <param name="type">Type to check</param>
981+
/// <returns>True if the type is a array</returns>
982+
private bool TypeIsArray(Type type)
983+
{
984+
return type.IsArray && type.GetArrayRank() == 1;
985+
}
986+
987+
947988
#endregion
948989
}
949990
}

0 commit comments

Comments
 (0)