From 3093e0e294a74ae3efe54ae9553646f2dc15030b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20K=C3=B6hler?= Date: Sun, 2 Aug 2015 23:01:26 +0200 Subject: [PATCH] Simple Arrays and Jagged Arrays are now supported. Multidimensional arrays are more complicated... I have to think about it. #50 --- ObjectFiller.Test/ListFillingTest.cs | 19 +++----- ObjectFiller.Test/PatternGeneratorTest.cs | 18 +++++--- .../TestPoco/ListTest/EntityCollection.cs | 2 +- ObjectFiller/Filler.cs | 43 ++++++++++++++++++- 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/ObjectFiller.Test/ListFillingTest.cs b/ObjectFiller.Test/ListFillingTest.cs index c479c81..e563ba0 100644 --- a/ObjectFiller.Test/ListFillingTest.cs +++ b/ObjectFiller.Test/ListFillingTest.cs @@ -34,9 +34,10 @@ public void TestUseEnumerable() Filler eFiller = new Filler(); 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() .OnProperty(x => x.Id).Use(Enumerable.Range(1, 22).Select(x => (int)Math.Pow(2, x))); @@ -143,22 +144,12 @@ public void GenerateDictionaryWithEnumeration() Assert.AreEqual(amountOfEnumValues, result.Count); } - private Entity[] GetArray() + private Entity[,] GetArray() { Filler of = new Filler(); + var entity = new Entity[,] { { of.Create() } }; - List entities = new List(); - 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; } } } diff --git a/ObjectFiller.Test/PatternGeneratorTest.cs b/ObjectFiller.Test/PatternGeneratorTest.cs index ea3e014..7729d28 100644 --- a/ObjectFiller.Test/PatternGeneratorTest.cs +++ b/ObjectFiller.Test/PatternGeneratorTest.cs @@ -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 @@ -15,8 +17,8 @@ public void Must_be_able_to_handle_private_setters() var filler = new Filler(); filler.Setup() .OnProperty(x => x.NameStyle).DoIt(At.TheEnd).Use(() => NameStyle.FirstNameLastName) - .OnProperty(x=>x.WithPrivateSetter); - + .OnProperty(x => x.WithPrivateSetter); + var obj = filler.Create(); @@ -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(); @@ -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] @@ -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; } } } diff --git a/ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs b/ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs index 9cd0c8c..84e5620 100644 --- a/ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs +++ b/ObjectFiller.Test/TestPoco/ListTest/EntityCollection.cs @@ -15,6 +15,6 @@ public class EntityCollection public IList EntityIList { get; set; } - public Entity[] EntityArray { get; set; } + public Entity[,] EntityArray { get; set; } } } \ No newline at end of file diff --git a/ObjectFiller/Filler.cs b/ObjectFiller/Filler.cs index 9281541..9ea4dba 100644 --- a/ObjectFiller/Filler.cs +++ b/ObjectFiller/Filler.cs @@ -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); @@ -427,6 +434,29 @@ private object CreateAndFillObject( return newValue; } + /// + /// Creates and filles an array or jagged array + /// + /// Array type to create and fill + /// Current ObjectFiller.NET Setup item + /// + /// The type tracker to find circular dependencies + /// + /// Created and filled array + private Array GetFilledArray(Type type, FillerSetupItem currentSetupItem, HashStack 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; + } + /// /// Creates a instance of an interface or abstract class. Like an IoC-Framework /// @@ -437,7 +467,7 @@ private object CreateAndFillObject( /// The setup item. /// /// - /// The dictionaryType tracker to find circular dependencies + /// The type tracker to find circular dependencies /// /// /// The created and filled instance of the @@ -944,6 +974,17 @@ private static bool TypeIsNullableEnum(Type type) return (u != null) && u.IsEnum; } + /// + /// Checks if the given is a supported array + /// + /// Type to check + /// True if the type is a array + private bool TypeIsArray(Type type) + { + return type.IsArray && type.GetArrayRank() == 1; + } + + #endregion } } \ No newline at end of file