Skip to content

Commit

Permalink
Adding support for filling enums
Browse files Browse the repository at this point in the history
  • Loading branch information
stimms committed Oct 25, 2017
1 parent caf68f4 commit e74459f
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/GenFu/DefaultValueChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ internal static bool HasValue(object instance, PropertyInfo property)
return false;
else if (property.PropertyType == typeof(DateTimeOffset) && ((DateTimeOffset)value).Equals(default(DateTimeOffset)))
return false;
else if (property.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum) && ((int)value) == 0)
return false;
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/GenFu/FillerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
{
result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType];
}
else if(propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum))
{
result = new EnumFiller(propertyInfo.PropertyType);
}
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
21 changes: 21 additions & 0 deletions src/GenFu/Fillers/EnumFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;

namespace GenFu
{
public class EnumFiller : PropertyFiller<Enum>
{
readonly Type _type;
public EnumFiller(Type type)
: base(new[] { "object" }, new[] { "*" }, true)
{
this._type = type;
}

public override object GetValue(object instance)
{
var values = Enum.GetValues(_type);
return values.GetValue(new Random().Next(values.Length-1));
}
}
}
1 change: 0 additions & 1 deletion src/GenFu/Fillers/StringFillers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public override object GetValue(object instance)
return BaseValueGenerator.Word();
}
}

public class ArticleTitleFiller : PropertyFiller<string>
{
public ArticleTitleFiller()
Expand Down
1 change: 1 addition & 0 deletions tests/GenFu.Tests/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GenFu;
using Xunit;
using System.Linq;
using GenFu.Tests.TestEntities;

namespace GenFu.Tests
{
Expand Down
1 change: 1 addition & 0 deletions tests/GenFu.Tests/Fillers/BasicFillTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using GenFu.ValueGenerators.Geospatial;
using GenFu.ValueGenerators.Medical;
using GenFu.ValueGenerators.People;
using GenFu.Tests.TestEntities;

namespace GenFu.Tests
{
Expand Down
17 changes: 17 additions & 0 deletions tests/GenFu.Tests/Fillers/EnumFillerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GenFu.Tests.TestEntities;
using System;
using Xunit;

namespace GenFu.Tests.Fillers
{
public class EnumFillerTests
{
[Fact]
void Can_get_a_value()
{
var sut = new EnumFiller(typeof(BlogTypeEnum));
var result = (BlogTypeEnum)sut.GetValue(null);
Assert.NotNull(Enum.GetName(typeof(BlogTypeEnum), result));
}
}
}
7 changes: 5 additions & 2 deletions tests/GenFu.Tests/GenFu.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
</PropertyGroup>

<ItemGroup>
<None Update="TestData\singlename.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -28,8 +31,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" />
<PackageReference Include="xunit" Version="2.2.0-beta5-build3474" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.0.2" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions tests/GenFu.Tests/GenFuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ public void DateTimesHaveDateInitialized()
Assert.True(post.CreateDate.Year > 1);
}

[Fact]
public void An_enum_should_be_filled()
{
var post = A.New<BlogPost>();
Assert.True((int)post.Type > 0);
}

[Fact]
public void DateTimesStayWithinConfiguredDates()
{
Expand Down
3 changes: 2 additions & 1 deletion tests/GenFu.Tests/TestEntities/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System.Linq;
using System.Collections.Generic;

namespace GenFu.Tests
namespace GenFu.Tests.TestEntities
{
internal class BlogPost
{
public int BlogPostId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public BlogTypeEnum Type { get; set; }
public virtual ICollection<BlogComment> Comments { get; set; }
public DateTime CreateDate { get; set; }
}
Expand Down
13 changes: 13 additions & 0 deletions tests/GenFu.Tests/TestEntities/BlogTypeEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GenFu.Tests.TestEntities
{
enum BlogTypeEnum
{
Post = 1,
Update = 2,
ProductRelease = 3
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GenFu.Tests.TestEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GenFu.Tests.TestEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
1 change: 1 addition & 0 deletions tests/GenFu.Tests/When_filling_object_graph.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using GenFu;
using Xunit;
using GenFu.Tests.TestEntities;

namespace GenFu.Tests
{
Expand Down
17 changes: 16 additions & 1 deletion tests/GenFu.Tests/When_testing_for_default_values.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GenFu.Tests.TestEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -107,5 +108,19 @@ public void Filled_datetime_should_return_true()
var post = new BlogPost { CreateDate = DateTime.Now };
Assert.True(DefaultValueChecker.HasValue(post, typeof(BlogPost).GetProperties().First(x => x.Name == "CreateDate")));
}

[Fact]
public void Filled_enum_should_return_true()
{
var post = new BlogPost { Type = BlogTypeEnum.Post};
Assert.True(DefaultValueChecker.HasValue(post, typeof(BlogPost).GetProperties().First(x => x.Name == "Type")));
}

[Fact]
public void Empty_enum_should_return_false()
{
var post = new BlogPost();
Assert.False(DefaultValueChecker.HasValue(post, typeof(BlogPost).GetProperties().First(x => x.Name == "Type")));
}
}
}

0 comments on commit e74459f

Please sign in to comment.