Skip to content
Draft
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
7 changes: 3 additions & 4 deletions src/GenFu/GenFu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ static GenFu()
Random = new Random();
}

public static bool IgnoreDefaultValueChecker { get; set; } = false; // P7ffb

public static T New<T>() where T : new()
{
return (T)New(typeof(T));
Expand Down Expand Up @@ -54,7 +56,7 @@ public static object New(object instance)
TypeInfo typeInfo = type.GetTypeInfo();
foreach (var property in typeInfo.GetAllProperties())
{
if (!DefaultValueChecker.HasValue(instance, property) && property.CanWrite)
if ((IgnoreDefaultValueChecker || !DefaultValueChecker.HasValue(instance, property)) && property.CanWrite) // P13e3
{
SetPropertyValue(instance, property);
}
Expand Down Expand Up @@ -253,6 +255,3 @@ public class Defaults


}



11 changes: 11 additions & 0 deletions src/GenFu/GenFuConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ public GenFuConfigurator Data(PropertyType propertyType, string filename)
return this;
}

/// <summary>
/// Sets the IgnoreDefaultValueChecker property in the GenFu class
/// </summary>
/// <param name="ignore">A boolean value to set the IgnoreDefaultValueChecker property</param>
/// <returns>A configurator for the target object type</returns>
public GenFuConfigurator IgnoreDefaultValueChecker(bool ignore)
{
GenFu.IgnoreDefaultValueChecker = ignore;
return this;
}

public GenFu GenFu
{
get { return _genfu; }
Expand Down
18 changes: 18 additions & 0 deletions tests/GenFu.Tests/GenFuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,22 @@ public void StringIsPopulatedIfNullable()

Assert.NotNull(person.FirstName);
}

[Fact]
public void Should_fill_property_even_if_already_filled_when_ignore_default_value_checker_is_true()
{
GenFu.Reset();
GenFu.Configure<BlogPost>().IgnoreDefaultValueChecker(true).Fill(x => x.Tags, () => new List<string> { "default" });
var result = A.New<BlogPost>();
Assert.Equal("default", result.Tags.Single());
}

[Fact]
public void Should_not_fill_property_if_already_filled_when_ignore_default_value_checker_is_false()
{
GenFu.Reset();
GenFu.Configure<BlogPost>().IgnoreDefaultValueChecker(false).Fill(x => x.Tags, () => new List<string> { "default" });
var result = A.New<BlogPost>();
Assert.Empty(result.Tags);
}
}