Skip to content

Commit

Permalink
Add setting to ignore default value checker
Browse files Browse the repository at this point in the history
Fixes MisterJames#121

Add a setting to ignore the default value checker

* Add a new boolean property `IgnoreDefaultValueChecker` to the `GenFu` class in `src/GenFu/GenFu.cs`.
* Modify the `New` method in the `GenFu` class to check the `IgnoreDefaultValueChecker` property before using the `DefaultValueChecker`.
* Add a method to set the `IgnoreDefaultValueChecker` property in the `GenFuConfigurator` class in `src/GenFu/GenFuConfigurator.cs`.
* Add tests in `tests/GenFu.Tests/GenFuTests.cs` to verify the new behavior of ignoring the default value checker.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/MisterJames/GenFu/issues/121?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
dylan-smith committed Sep 5, 2024
1 parent cb5780a commit 930dc78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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


}



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

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);
}
}

0 comments on commit 930dc78

Please sign in to comment.