From 1ee88f25da1e07805de3e305c282027ef7739765 Mon Sep 17 00:00:00 2001 From: gokTyBalD Date: Mon, 7 Jul 2014 00:13:40 +0200 Subject: [PATCH] IgnoreAllOfUnknownType implemented --- ObjectFiller.Test/ListFillingTest.cs | 22 ++++++ ObjectFiller/Filler.cs | 91 ++++++++++++++----------- ObjectFiller/Setup/FluentFillerApi.cs | 12 ++++ ObjectFiller/Setup/ObjectFillerSetup.cs | 6 ++ 4 files changed, 90 insertions(+), 41 deletions(-) diff --git a/ObjectFiller.Test/ListFillingTest.cs b/ObjectFiller.Test/ListFillingTest.cs index cdef3f9..8157c79 100644 --- a/ObjectFiller.Test/ListFillingTest.cs +++ b/ObjectFiller.Test/ListFillingTest.cs @@ -64,6 +64,28 @@ public void TestFillList() } + [TestMethod] + public void TestIgnoreAllUnknownTypesWithOutException() + { + Filler filler = new Filler(); + filler.Setup().IgnoreAllUnknownTypes(); + var entity = filler.Create(); + Assert.IsNull(entity.EntityArray); + Assert.IsNotNull(entity); + Assert.IsNotNull(entity.EntityList); + Assert.IsNotNull(entity.EntityICollection); + Assert.IsNotNull(entity.EntityIEnumerable); + Assert.IsNotNull(entity.EntityIList); + } + + [TestMethod] + [ExpectedException(typeof(TypeInitializationException))] + public void TestIgnoreAllUnknownTypesWithException() + { + Filler filler = new Filler(); + filler.Create(); + } + private Entity[] GetArray() { Filler of = new Filler(); diff --git a/ObjectFiller/Filler.cs b/ObjectFiller/Filler.cs index b4d0091..908a1e5 100644 --- a/ObjectFiller/Filler.cs +++ b/ObjectFiller/Filler.cs @@ -63,12 +63,12 @@ public T Create() /// public IEnumerable Create(int count) { - for (int n = 0; n < count; n++) - { - T objectToFill = (T) CreateInstanceOfType(typeof (T), SetupManager.GetFor()); - Fill(objectToFill); - yield return objectToFill; - } + for (int n = 0; n < count; n++) + { + T objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor()); + Fill(objectToFill); + yield return objectToFill; + } } /// @@ -122,9 +122,9 @@ private object CreateInstanceOfType(Type type, ObjectFillerSetup currentSetup) if (constructorArgs.Count == 0) { - var message = "Could not found a constructor for type [" + type.Name + "] where the parameters can be filled with the current objectfiller setup"; - Debug.WriteLine("ObjectFiller: " + message); - throw new InvalidOperationException(message); + var message = "Could not found a constructor for type [" + type.Name + "] where the parameters can be filled with the current objectfiller setup"; + Debug.WriteLine("ObjectFiller: " + message); + throw new InvalidOperationException(message); } } } @@ -268,10 +268,10 @@ private object GetFilledObject(Type type, ObjectFillerSetup currentSetup) return GetFilledPoco(type, currentSetup); } - if (TypeIsEnum(type)) - { - return GetRandomEnumValue(type); - } + if (TypeIsEnum(type)) + { + return GetRandomEnumValue(type); + } object newValue = GetRandomValue(type, currentSetup); return newValue; @@ -279,19 +279,19 @@ private object GetFilledObject(Type type, ObjectFillerSetup currentSetup) - private object GetRandomEnumValue(Type type) - { - // performance: Enum.GetValues() is slow due to reflection, should cache it - Array values = Enum.GetValues(type); - if (values.Length > 0) - { - int index = Random.Next() % values.Length; - return values.GetValue(index); - } - return 0; - } + private object GetRandomEnumValue(Type type) + { + // performance: Enum.GetValues() is slow due to reflection, should cache it + Array values = Enum.GetValues(type); + if (values.Length > 0) + { + int index = Random.Next() % values.Length; + return values.GetValue(index); + } + return 0; + } - private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup) + private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup) { object result = CreateInstanceOfType(type, currentSetup); @@ -313,12 +313,12 @@ private IDictionary GetFilledDictionary(Type propertyType, ObjectFillerSetup cur if (dictionary.Contains(keyObject)) { - string message = string.Format("Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup.", keyType); - Debug.WriteLine("ObjectFiller: " + message); - throw new ArgumentException(message); + string message = string.Format("Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup.", keyType); + Debug.WriteLine("ObjectFiller: " + message); + throw new ArgumentException(message); } - object valueObject = GetFilledObject(valueType, currentSetup); + object valueObject = GetFilledObject(valueType, currentSetup); dictionary.Add(keyObject, valueObject); } return dictionary; @@ -378,12 +378,12 @@ private object GetInterfaceInstance(Type interfaceType, ObjectFillerSetup setup) { if (setup.InterfaceMocker == null) { - string message = string.Format("ObjectFiller Interface mocker missing and type [{0}] not registered", interfaceType.Name); - Debug.WriteLine("ObjectFiller: " + message); - throw new InvalidOperationException(message); + string message = string.Format("ObjectFiller Interface mocker missing and type [{0}] not registered", interfaceType.Name); + Debug.WriteLine("ObjectFiller: " + message); + throw new InvalidOperationException(message); } - MethodInfo method = setup.InterfaceMocker.GetType().GetMethod("Create"); + MethodInfo method = setup.InterfaceMocker.GetType().GetMethod("Create"); MethodInfo genericMethod = method.MakeGenericMethod(new[] { interfaceType }); result = genericMethod.Invoke(setup.InterfaceMocker, null); } @@ -398,9 +398,18 @@ private object GetRandomValue(Type propertyType, ObjectFillerSetup setup) return setup.TypeToRandomFunc[propertyType](); } - string message = "The type [" + propertyType.Name + "] was not registered in the randomizer."; - Debug.WriteLine("ObjectFiller: " + message); - throw new TypeInitializationException(propertyType.FullName, new Exception(message)); + if (setup.IgnoreAllUnknownTypes) + { + if (propertyType.IsValueType) + { + return Activator.CreateInstance(propertyType); + } + return null; + } + + string message = "The type [" + propertyType.Name + "] was not registered in the randomizer."; + Debug.WriteLine("ObjectFiller: " + message); + throw new TypeInitializationException(propertyType.FullName, new Exception(message)); } private static bool TypeIsValidForObjectFiller(Type type, ObjectFillerSetup currentSetup) @@ -465,9 +474,9 @@ private static bool TypeIsList(Type type) || type.GetInterfaces().Any(x => x == typeof(IEnumerable))); } - private static bool TypeIsEnum(Type type) - { - return type.IsEnum; - } - } + private static bool TypeIsEnum(Type type) + { + return type.IsEnum; + } + } } diff --git a/ObjectFiller/Setup/FluentFillerApi.cs b/ObjectFiller/Setup/FluentFillerApi.cs index 076eed9..fc04864 100644 --- a/ObjectFiller/Setup/FluentFillerApi.cs +++ b/ObjectFiller/Setup/FluentFillerApi.cs @@ -68,6 +68,18 @@ public FluentFillerApi ListItemCount(int maxCount) return this; } + /// + /// Call this if the ObjectFiller should ignore all unknown types which can not filled automatically by the ObjectFiller. + /// When you not call this method, the ObjectFiller raises an exception when it is not possible to generate a random value for that type! + /// + /// + public FluentFillerApi IgnoreAllUnknownTypes() + { + SetupManager.GetFor().IgnoreAllUnknownTypes = true; + + return this; + } + /// /// Setup the minimum and maximum item count for lists. The ObjectFiller will not generate more or less listitems then this limits. /// The default value for is 1. The default value for is 25. diff --git a/ObjectFiller/Setup/ObjectFillerSetup.cs b/ObjectFiller/Setup/ObjectFillerSetup.cs index 4eb9916..032db17 100644 --- a/ObjectFiller/Setup/ObjectFillerSetup.cs +++ b/ObjectFiller/Setup/ObjectFillerSetup.cs @@ -18,6 +18,7 @@ public ObjectFillerSetup() PropertyOrder = new Dictionary(); TypesToIgnore = new List(); InterfaceToImplementation = new Dictionary(); + IgnoreAllUnknownTypes = false; SetDefaultRandomizer(); } @@ -99,5 +100,10 @@ private void SetDefaultRandomizer() /// public IInterfaceMocker InterfaceMocker { get; set; } + /// + /// True if all unknown types will be ignored by the objectfiller + /// + public bool IgnoreAllUnknownTypes { get; set; } + } }