Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/GenFu/FillerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
16 changes: 16 additions & 0 deletions src/GenFu/Fillers/EnumFiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ public override object GetValue(object instance)
return values.GetValue(new Random().Next(values.Length));
}
}

public class NullableEnumFiller : PropertyFiller<Enum?>
{
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));
}
}
9 changes: 7 additions & 2 deletions src/GenFu/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions tests/GenFu.Tests/TestEntities/Nullables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
6 changes: 6 additions & 0 deletions tests/GenFu.Tests/When_filling_nullables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ void A_nullable_char_should_be_filled()
{
Assert.True(A.New<Nullables>().NullableChar.HasValue);
}

[Fact]
void A_nullable_enum_should_be_filled()
{
Assert.True(A.New<Nullables>().NullableEnum.HasValue);
}
}