Skip to content

Commit 19fc788

Browse files
committed
Fixed #48
1 parent fddf1a0 commit 19fc788

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

ObjectFiller.Test/EnumTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ public void Must_support_class_with_enums_as_ctor_out_of_the_box()
7373
}
7474
}
7575

76+
[TestMethod]
77+
public void FillNullableEnum()
78+
{
79+
var filler = new Filler<ClassWithNullableEnum>();
80+
81+
var c = filler.Create();
82+
Assert.IsTrue(
83+
c.NullableEnum == StandardEnum.A ||
84+
c.NullableEnum == StandardEnum.B ||
85+
c.NullableEnum == StandardEnum.C);
86+
}
87+
7688

7789
public enum StandardEnum
7890
{
@@ -119,7 +131,7 @@ public class MyClass
119131

120132
public class MyClassWithCstr
121133
{
122-
public MyClassWithCstr(StandardEnum standard ,NumberedEnum numbered, FlagsEnum flags, ManualSetupEnum manual, IgnoredEnum ignored)
134+
public MyClassWithCstr(StandardEnum standard, NumberedEnum numbered, FlagsEnum flags, ManualSetupEnum manual, IgnoredEnum ignored)
123135
{
124136
this.Standard = standard;
125137
this.Numbered = numbered;
@@ -135,5 +147,10 @@ public MyClassWithCstr(StandardEnum standard ,NumberedEnum numbered, FlagsEnum f
135147
public ManualSetupEnum Manual { get; set; }
136148
public IgnoredEnum Ignored { get; set; }
137149
}
150+
151+
public class ClassWithNullableEnum
152+
{
153+
public StandardEnum? NullableEnum { get; set; }
154+
}
138155
}
139156
}

ObjectFiller/Filler.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private static bool TypeIsClrType(Type type)
300300
/// </returns>
301301
private static bool TypeIsValidForObjectFiller(Type type, FillerSetupItem currentSetupItem)
302302
{
303-
var result= HasTypeARandomFunc(type, currentSetupItem)
303+
var result = HasTypeARandomFunc(type, currentSetupItem)
304304
|| (TypeIsList(type) && ListParamTypeIsValid(type, currentSetupItem))
305305
|| (TypeIsDictionary(type) && DictionaryParamTypesAreValid(type, currentSetupItem))
306306
|| TypeIsPoco(type)
@@ -342,7 +342,7 @@ private bool CheckForCircularReference(
342342
{
343343
throw new InvalidOperationException(
344344
string.Format(
345-
"The dictionaryType {0} was already encountered before, which probably means you have a circular reference in your model. Either ignore the properties which cause this or specify explicit creation rules for them which do not rely on types.",
345+
"The type {0} was already encountered before, which probably means you have a circular reference in your model. Either ignore the properties which cause this or specify explicit creation rules for them which do not rely on types.",
346346
targetType.Name));
347347
}
348348

@@ -413,7 +413,7 @@ private object CreateAndFillObject(
413413
return this.CreateInstanceOfInterfaceOrAbstractClass(type, currentSetupItem, typeTracker);
414414
}
415415

416-
if (TypeIsEnum(type))
416+
if (TypeIsEnum(type) || TypeIsNullableEnum(type))
417417
{
418418
return this.GetRandomEnumValue(type);
419419
}
@@ -467,7 +467,7 @@ private object CreateInstanceOfInterfaceOrAbstractClass(
467467
{
468468
string message =
469469
string.Format(
470-
"ObjectFiller Interface mocker missing and dictionaryType [{0}] not registered",
470+
"ObjectFiller Interface mocker missing and type [{0}] not registered",
471471
interfaceType.Name);
472472
throw new InvalidOperationException(message);
473473
}
@@ -525,7 +525,7 @@ private object CreateInstanceOfType(Type type, FillerSetupItem currentSetupItem,
525525

526526
if (constructorArgs.Count == 0)
527527
{
528-
var message = "Could not found a constructor for dictionaryType [" + type.Name
528+
var message = "Could not found a constructor for type [" + type.Name
529529
+ "] where the parameters can be filled with the current objectfiller setup";
530530
throw new InvalidOperationException(message);
531531
}
@@ -779,7 +779,10 @@ private IEnumerable<PropertyInfo> GetPropertyFromProperties(
779779
private object GetRandomEnumValue(Type type)
780780
{
781781
// performance: Enum.GetValues() is slow due to reflection, should cache it
782-
Array values = Enum.GetValues(type);
782+
783+
var enumType = type.IsEnum ? type : Nullable.GetUnderlyingType(type);
784+
785+
Array values = Enum.GetValues(enumType);
783786
if (values.Length > 0)
784787
{
785788
int index = Random.Next() % values.Length;
@@ -816,7 +819,7 @@ private object GetRandomValue(Type propertyType, FillerSetupItem setupItem)
816819
return GetDefaultValueOfType(propertyType);
817820
}
818821

819-
string message = "The dictionaryType [" + propertyType.Name + "] was not registered in the randomizer.";
822+
string message = "The type [" + propertyType.Name + "] was not registered in the randomizer.";
820823
throw new TypeInitializationException(propertyType.FullName, new Exception(message));
821824
}
822825

@@ -930,6 +933,17 @@ private static bool TypeIsEnum(Type type)
930933
return type.IsEnum;
931934
}
932935

936+
/// <summary>
937+
/// Checks if the given <see cref="type"/> is a nullable enum
938+
/// </summary>
939+
/// <param name="type">Type to check</param>
940+
/// <returns>True if the type is a nullable enum</returns>
941+
private static bool TypeIsNullableEnum(Type type)
942+
{
943+
Type u = Nullable.GetUnderlyingType(type);
944+
return (u != null) && u.IsEnum;
945+
}
946+
933947
#endregion
934948
}
935949
}

0 commit comments

Comments
 (0)