diff --git a/src/GenFu/FillerManager.cs b/src/GenFu/FillerManager.cs index fc74a23..8af90fb 100644 --- a/src/GenFu/FillerManager.cs +++ b/src/GenFu/FillerManager.cs @@ -202,6 +202,10 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo) { result = new EnumFiller(propertyInfo.PropertyType); } + else if (propertyInfo.PropertyType.GetNullableUnderlyingType().IsEnum) + { + result = new NullableEnumFiller(propertyInfo.PropertyType.GetNullableUnderlyingType()); + } else { //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.) diff --git a/src/GenFu/Fillers/EnumFiller.cs b/src/GenFu/Fillers/EnumFiller.cs index 585e6ab..f8f329e 100644 --- a/src/GenFu/Fillers/EnumFiller.cs +++ b/src/GenFu/Fillers/EnumFiller.cs @@ -17,3 +17,19 @@ public override object GetValue(object instance) return values.GetValue(new Random().Next(values.Length)); } } + +public class NullableEnumFiller : PropertyFiller +{ + readonly Type _type; + public NullableEnumFiller(Type type) + : base(typeof(object), "*", true) + { + this._type = type; + } + + public override object GetValue(object instance) + { + var values = Enum.GetValues(_type); + return values.GetValue(new Random().Next(values.Length)); + } +} diff --git a/src/GenFu/ReflectionHelpers.cs b/src/GenFu/ReflectionHelpers.cs index c44568c..0be17c3 100644 --- a/src/GenFu/ReflectionHelpers.cs +++ b/src/GenFu/ReflectionHelpers.cs @@ -1,6 +1,11 @@ -namespace GenFu; +using System; + +namespace GenFu; internal static class ReflectionHelpers { - + public static Type GetNullableUnderlyingType(this Type type) + { + return Nullable.GetUnderlyingType(type); + } } diff --git a/tests/GenFu.Tests/TestEntities/Nullables.cs b/tests/GenFu.Tests/TestEntities/Nullables.cs index ec58fdb..819e88b 100644 --- a/tests/GenFu.Tests/TestEntities/Nullables.cs +++ b/tests/GenFu.Tests/TestEntities/Nullables.cs @@ -14,4 +14,5 @@ public class Nullables public short? NullableShort { get; set; } public bool? NullableBoolean { get; set; } public char? NullableChar { get; set; } + public PropertyType? NullableEnum { get; set; } } diff --git a/tests/GenFu.Tests/When_filling_nullables.cs b/tests/GenFu.Tests/When_filling_nullables.cs index 159e6f0..72b4247 100644 --- a/tests/GenFu.Tests/When_filling_nullables.cs +++ b/tests/GenFu.Tests/When_filling_nullables.cs @@ -59,4 +59,10 @@ void A_nullable_char_should_be_filled() { Assert.True(A.New().NullableChar.HasValue); } + + [Fact] + void A_nullable_enum_should_be_filled() + { + Assert.True(A.New().NullableEnum.HasValue); + } }