From ca8ac993bbc86576aacf362257ac5a77092298e7 Mon Sep 17 00:00:00 2001 From: Jake Soenneker Date: Thu, 26 Sep 2024 06:11:26 -0500 Subject: [PATCH] UseSeed and UseDateTimeReference implemented --- src/Abstract/IAutoFaker.cs | 15 +++++++++++++++ src/AutoFaker.cs | 10 ++++++++++ src/Extensions/AutoGenerateContextExtension.cs | 4 ++-- .../AutoFakerTests.cs | 16 +++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Abstract/IAutoFaker.cs b/src/Abstract/IAutoFaker.cs index 357fbae..e1c2d6b 100644 --- a/src/Abstract/IAutoFaker.cs +++ b/src/Abstract/IAutoFaker.cs @@ -46,4 +46,19 @@ public interface IAutoFaker void Configure(Action configure); + + /// + /// Creates a seed locally scoped within the ignoring the globally scoped . + /// If this method is never called the global is used. + /// + /// The seed value to use within the instance. + void UseSeed(int seed); + + /// + /// Sets a local time reference for all DateTime calculations used by + /// the instance; unless refDate parameters are specified + /// with the corresponding Date.Methods(). + /// + /// The anchored DateTime reference to use. + void UseDateTimeReference(DateTime? refDate); } \ No newline at end of file diff --git a/src/AutoFaker.cs b/src/AutoFaker.cs index 4f402ec..64d50aa 100644 --- a/src/AutoFaker.cs +++ b/src/AutoFaker.cs @@ -125,4 +125,14 @@ public static List GenerateStatic(int count, Action(count); } + + public void UseSeed(int seed) + { + Faker.Random = new Randomizer(seed); + } + + public void UseDateTimeReference(DateTime? refDate) + { + Faker.DateTimeReference = refDate; + } } \ No newline at end of file diff --git a/src/Extensions/AutoGenerateContextExtension.cs b/src/Extensions/AutoGenerateContextExtension.cs index 22f6e84..1233edb 100644 --- a/src/Extensions/AutoGenerateContextExtension.cs +++ b/src/Extensions/AutoGenerateContextExtension.cs @@ -79,7 +79,7 @@ internal static List GenerateMany(AutoFakerContext context, int co { var items = new List(count); - for (int i = 0; i < count; i++) + for (var i = 0; i < count; i++) { TType? item = generate.Invoke(); @@ -92,7 +92,7 @@ internal static List GenerateMany(AutoFakerContext context, int co else { var hashSet = new HashSet(); - int attempts = 0; + var attempts = 0; int totalAttempts = count + maxAttempts - 1; while (hashSet.Count < count && attempts < totalAttempts) diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs index b7f9208..3195f47 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs @@ -339,7 +339,7 @@ public void Generate_ImmutableList_should_generate() immutableListDto.List.Should().NotBeNullOrEmpty(); immutableListDto.IList.Should().NotBeNullOrEmpty(); } - + [Fact] public void Generate_ImmutableArray_should_generate() { @@ -420,6 +420,20 @@ public void TestClassWithPrivateProperty_should_be_null() obj.GetValue().Should().BeNull(); } + [Fact] + public void UseSeed_should_generate_same_value() + { + var faker1 = new AutoFaker(); + faker1.UseSeed(1); + var value1 = faker1.Generate(); + + var faker2 = new AutoFaker(); + faker2.UseSeed(1); + var value2 = faker2.Generate(); + + value1.Should().Be(value2); + } + [Fact] public void Generate_with_recursive_depth_0_should_generate() {