From 930dc7894419f14fc2466296195562b9aca0725f Mon Sep 17 00:00:00 2001 From: Dylan Smith Date: Thu, 5 Sep 2024 15:58:55 -0500 Subject: [PATCH] Add setting to ignore default value checker Fixes #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). --- src/GenFu/GenFu.cs | 7 +++---- src/GenFu/GenFuConfigurator.cs | 6 ++++++ tests/GenFu.Tests/GenFuTests.cs | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/GenFu/GenFu.cs b/src/GenFu/GenFu.cs index 1110143..c639b44 100644 --- a/src/GenFu/GenFu.cs +++ b/src/GenFu/GenFu.cs @@ -24,6 +24,8 @@ static GenFu() Random = new Random(); } + public static bool IgnoreDefaultValueChecker { get; set; } = false; // P7ffb + public static T New() where T : new() { return (T)New(typeof(T)); @@ -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); } @@ -253,6 +255,3 @@ public class Defaults } - - - diff --git a/src/GenFu/GenFuConfigurator.cs b/src/GenFu/GenFuConfigurator.cs index c488db9..0dfbd79 100644 --- a/src/GenFu/GenFuConfigurator.cs +++ b/src/GenFu/GenFuConfigurator.cs @@ -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; } diff --git a/tests/GenFu.Tests/GenFuTests.cs b/tests/GenFu.Tests/GenFuTests.cs index eed9a91..2c09a26 100644 --- a/tests/GenFu.Tests/GenFuTests.cs +++ b/tests/GenFu.Tests/GenFuTests.cs @@ -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().IgnoreDefaultValueChecker(true).Fill(x => x.Tags, () => new List { "default" }); + var result = A.New(); + 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().IgnoreDefaultValueChecker(false).Fill(x => x.Tags, () => new List { "default" }); + var result = A.New(); + Assert.Empty(result.Tags); + } }