Skip to content
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
22 changes: 22 additions & 0 deletions ObjectFiller.Test/ListFillingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ public void TestFillList()

}

[TestMethod]
public void TestIgnoreAllUnknownTypesWithOutException()
{
Filler<EntityCollection> filler = new Filler<EntityCollection>();
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<EntityCollection> filler = new Filler<EntityCollection>();
filler.Create();
}

private Entity[] GetArray()
{
Filler<Entity> of = new Filler<Entity>();
Expand Down
91 changes: 50 additions & 41 deletions ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public T Create()
/// </summary>
public IEnumerable<T> Create(int count)
{
for (int n = 0; n < count; n++)
{
T objectToFill = (T) CreateInstanceOfType(typeof (T), SetupManager.GetFor<T>());
Fill(objectToFill);
yield return objectToFill;
}
for (int n = 0; n < count; n++)
{
T objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());
Fill(objectToFill);
yield return objectToFill;
}
}

/// <summary>
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -268,30 +268,30 @@ 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;
}



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);

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
12 changes: 12 additions & 0 deletions ObjectFiller/Setup/FluentFillerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public FluentFillerApi<TTargetObject> ListItemCount(int maxCount)
return this;
}

/// <summary>
/// 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!
/// </summary>
/// <returns></returns>
public FluentFillerApi<TTargetObject> IgnoreAllUnknownTypes()
{
SetupManager.GetFor<TTargetObject>().IgnoreAllUnknownTypes = true;

return this;
}

/// <summary>
/// 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 <see cref="minCount"/> is 1. The default value for <see cref="maxCount"/> is 25.
Expand Down
6 changes: 6 additions & 0 deletions ObjectFiller/Setup/ObjectFillerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public ObjectFillerSetup()
PropertyOrder = new Dictionary<PropertyInfo, At>();
TypesToIgnore = new List<Type>();
InterfaceToImplementation = new Dictionary<Type, Type>();
IgnoreAllUnknownTypes = false;

SetDefaultRandomizer();
}
Expand Down Expand Up @@ -99,5 +100,10 @@ private void SetDefaultRandomizer()
/// </summary>
public IInterfaceMocker InterfaceMocker { get; set; }

/// <summary>
/// True if all unknown types will be ignored by the objectfiller
/// </summary>
public bool IgnoreAllUnknownTypes { get; set; }

}
}