Skip to content

Commit 0fe3ab6

Browse files
committed
Merge pull request #18 from Tynamix/FEATURE_IgnoreAllUnknown
IgnoreAllOfUnknownType implemented
2 parents 8e7f666 + 1ee88f2 commit 0fe3ab6

File tree

4 files changed

+90
-41
lines changed

4 files changed

+90
-41
lines changed

ObjectFiller.Test/ListFillingTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public void TestFillList()
6464

6565
}
6666

67+
[TestMethod]
68+
public void TestIgnoreAllUnknownTypesWithOutException()
69+
{
70+
Filler<EntityCollection> filler = new Filler<EntityCollection>();
71+
filler.Setup().IgnoreAllUnknownTypes();
72+
var entity = filler.Create();
73+
Assert.IsNull(entity.EntityArray);
74+
Assert.IsNotNull(entity);
75+
Assert.IsNotNull(entity.EntityList);
76+
Assert.IsNotNull(entity.EntityICollection);
77+
Assert.IsNotNull(entity.EntityIEnumerable);
78+
Assert.IsNotNull(entity.EntityIList);
79+
}
80+
81+
[TestMethod]
82+
[ExpectedException(typeof(TypeInitializationException))]
83+
public void TestIgnoreAllUnknownTypesWithException()
84+
{
85+
Filler<EntityCollection> filler = new Filler<EntityCollection>();
86+
filler.Create();
87+
}
88+
6789
private Entity[] GetArray()
6890
{
6991
Filler<Entity> of = new Filler<Entity>();

ObjectFiller/Filler.cs

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public T Create()
6363
/// </summary>
6464
public IEnumerable<T> Create(int count)
6565
{
66-
for (int n = 0; n < count; n++)
67-
{
68-
T objectToFill = (T) CreateInstanceOfType(typeof (T), SetupManager.GetFor<T>());
69-
Fill(objectToFill);
70-
yield return objectToFill;
71-
}
66+
for (int n = 0; n < count; n++)
67+
{
68+
T objectToFill = (T)CreateInstanceOfType(typeof(T), SetupManager.GetFor<T>());
69+
Fill(objectToFill);
70+
yield return objectToFill;
71+
}
7272
}
7373

7474
/// <summary>
@@ -122,9 +122,9 @@ private object CreateInstanceOfType(Type type, ObjectFillerSetup currentSetup)
122122

123123
if (constructorArgs.Count == 0)
124124
{
125-
var message = "Could not found a constructor for type [" + type.Name + "] where the parameters can be filled with the current objectfiller setup";
126-
Debug.WriteLine("ObjectFiller: " + message);
127-
throw new InvalidOperationException(message);
125+
var message = "Could not found a constructor for type [" + type.Name + "] where the parameters can be filled with the current objectfiller setup";
126+
Debug.WriteLine("ObjectFiller: " + message);
127+
throw new InvalidOperationException(message);
128128
}
129129
}
130130
}
@@ -268,30 +268,30 @@ private object GetFilledObject(Type type, ObjectFillerSetup currentSetup)
268268
return GetFilledPoco(type, currentSetup);
269269
}
270270

271-
if (TypeIsEnum(type))
272-
{
273-
return GetRandomEnumValue(type);
274-
}
271+
if (TypeIsEnum(type))
272+
{
273+
return GetRandomEnumValue(type);
274+
}
275275

276276
object newValue = GetRandomValue(type, currentSetup);
277277
return newValue;
278278
}
279279

280280

281281

282-
private object GetRandomEnumValue(Type type)
283-
{
284-
// performance: Enum.GetValues() is slow due to reflection, should cache it
285-
Array values = Enum.GetValues(type);
286-
if (values.Length > 0)
287-
{
288-
int index = Random.Next() % values.Length;
289-
return values.GetValue(index);
290-
}
291-
return 0;
292-
}
282+
private object GetRandomEnumValue(Type type)
283+
{
284+
// performance: Enum.GetValues() is slow due to reflection, should cache it
285+
Array values = Enum.GetValues(type);
286+
if (values.Length > 0)
287+
{
288+
int index = Random.Next() % values.Length;
289+
return values.GetValue(index);
290+
}
291+
return 0;
292+
}
293293

294-
private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
294+
private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
295295
{
296296
object result = CreateInstanceOfType(type, currentSetup);
297297

@@ -313,12 +313,12 @@ private IDictionary GetFilledDictionary(Type propertyType, ObjectFillerSetup cur
313313

314314
if (dictionary.Contains(keyObject))
315315
{
316-
string message = string.Format("Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup.", keyType);
317-
Debug.WriteLine("ObjectFiller: " + message);
318-
throw new ArgumentException(message);
316+
string message = string.Format("Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup.", keyType);
317+
Debug.WriteLine("ObjectFiller: " + message);
318+
throw new ArgumentException(message);
319319
}
320320

321-
object valueObject = GetFilledObject(valueType, currentSetup);
321+
object valueObject = GetFilledObject(valueType, currentSetup);
322322
dictionary.Add(keyObject, valueObject);
323323
}
324324
return dictionary;
@@ -378,12 +378,12 @@ private object GetInterfaceInstance(Type interfaceType, ObjectFillerSetup setup)
378378
{
379379
if (setup.InterfaceMocker == null)
380380
{
381-
string message = string.Format("ObjectFiller Interface mocker missing and type [{0}] not registered", interfaceType.Name);
382-
Debug.WriteLine("ObjectFiller: " + message);
383-
throw new InvalidOperationException(message);
381+
string message = string.Format("ObjectFiller Interface mocker missing and type [{0}] not registered", interfaceType.Name);
382+
Debug.WriteLine("ObjectFiller: " + message);
383+
throw new InvalidOperationException(message);
384384
}
385385

386-
MethodInfo method = setup.InterfaceMocker.GetType().GetMethod("Create");
386+
MethodInfo method = setup.InterfaceMocker.GetType().GetMethod("Create");
387387
MethodInfo genericMethod = method.MakeGenericMethod(new[] { interfaceType });
388388
result = genericMethod.Invoke(setup.InterfaceMocker, null);
389389
}
@@ -398,9 +398,18 @@ private object GetRandomValue(Type propertyType, ObjectFillerSetup setup)
398398
return setup.TypeToRandomFunc[propertyType]();
399399
}
400400

401-
string message = "The type [" + propertyType.Name + "] was not registered in the randomizer.";
402-
Debug.WriteLine("ObjectFiller: " + message);
403-
throw new TypeInitializationException(propertyType.FullName, new Exception(message));
401+
if (setup.IgnoreAllUnknownTypes)
402+
{
403+
if (propertyType.IsValueType)
404+
{
405+
return Activator.CreateInstance(propertyType);
406+
}
407+
return null;
408+
}
409+
410+
string message = "The type [" + propertyType.Name + "] was not registered in the randomizer.";
411+
Debug.WriteLine("ObjectFiller: " + message);
412+
throw new TypeInitializationException(propertyType.FullName, new Exception(message));
404413
}
405414

406415
private static bool TypeIsValidForObjectFiller(Type type, ObjectFillerSetup currentSetup)
@@ -465,9 +474,9 @@ private static bool TypeIsList(Type type)
465474
|| type.GetInterfaces().Any(x => x == typeof(IEnumerable)));
466475
}
467476

468-
private static bool TypeIsEnum(Type type)
469-
{
470-
return type.IsEnum;
471-
}
472-
}
477+
private static bool TypeIsEnum(Type type)
478+
{
479+
return type.IsEnum;
480+
}
481+
}
473482
}

ObjectFiller/Setup/FluentFillerApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public FluentFillerApi<TTargetObject> ListItemCount(int maxCount)
6868
return this;
6969
}
7070

71+
/// <summary>
72+
/// Call this if the ObjectFiller should ignore all unknown types which can not filled automatically by the ObjectFiller.
73+
/// When you not call this method, the ObjectFiller raises an exception when it is not possible to generate a random value for that type!
74+
/// </summary>
75+
/// <returns></returns>
76+
public FluentFillerApi<TTargetObject> IgnoreAllUnknownTypes()
77+
{
78+
SetupManager.GetFor<TTargetObject>().IgnoreAllUnknownTypes = true;
79+
80+
return this;
81+
}
82+
7183
/// <summary>
7284
/// Setup the minimum and maximum item count for lists. The ObjectFiller will not generate more or less listitems then this limits.
7385
/// The default value for <see cref="minCount"/> is 1. The default value for <see cref="maxCount"/> is 25.

ObjectFiller/Setup/ObjectFillerSetup.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public ObjectFillerSetup()
1818
PropertyOrder = new Dictionary<PropertyInfo, At>();
1919
TypesToIgnore = new List<Type>();
2020
InterfaceToImplementation = new Dictionary<Type, Type>();
21+
IgnoreAllUnknownTypes = false;
2122

2223
SetDefaultRandomizer();
2324
}
@@ -99,5 +100,10 @@ private void SetDefaultRandomizer()
99100
/// </summary>
100101
public IInterfaceMocker InterfaceMocker { get; set; }
101102

103+
/// <summary>
104+
/// True if all unknown types will be ignored by the objectfiller
105+
/// </summary>
106+
public bool IgnoreAllUnknownTypes { get; set; }
107+
102108
}
103109
}

0 commit comments

Comments
 (0)