Skip to content

Commit

Permalink
UseSeed and UseDateTimeReference implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Sep 26, 2024
1 parent fd0029e commit ca8ac99
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Abstract/IAutoFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ public interface IAutoFaker


void Configure(Action<IAutoFakerDefaultConfigBuilder> configure);

/// <summary>
/// Creates a seed locally scoped within the <seealso cref="Faker{T}"/> ignoring the globally scoped <seealso cref="Randomizer.Seed"/>.
/// If this method is never called the global <seealso cref="Randomizer.Seed"/> is used.
/// </summary>
/// <param name="seed">The seed value to use within the <seealso cref="Faker{T}"/> instance.</param>
void UseSeed(int seed);

/// <summary>
/// Sets a local time reference for all DateTime calculations used by
/// the <seealso cref="Faker{T}"/> instance; unless refDate parameters are specified
/// with the corresponding Date.Methods().
/// </summary>
/// <param name="refDate">The anchored DateTime reference to use.</param>
void UseDateTimeReference(DateTime? refDate);
}
10 changes: 10 additions & 0 deletions src/AutoFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@ public static List<TType> GenerateStatic<TType>(int count, Action<IAutoGenerateC
var faker = new AutoFaker(configure);
return faker.Generate<TType>(count);
}

public void UseSeed(int seed)
{
Faker.Random = new Randomizer(seed);
}

public void UseDateTimeReference(DateTime? refDate)
{
Faker.DateTimeReference = refDate;
}
}
4 changes: 2 additions & 2 deletions src/Extensions/AutoGenerateContextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal static List<TType> GenerateMany<TType>(AutoFakerContext context, int co
{
var items = new List<TType>(count);

for (int i = 0; i < count; i++)
for (var i = 0; i < count; i++)
{
TType? item = generate.Invoke();

Expand All @@ -92,7 +92,7 @@ internal static List<TType> GenerateMany<TType>(AutoFakerContext context, int co
else
{
var hashSet = new HashSet<TType>();
int attempts = 0;
var attempts = 0;
int totalAttempts = count + maxAttempts - 1;

while (hashSet.Count < count && attempts < totalAttempts)
Expand Down
16 changes: 15 additions & 1 deletion test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<int>();

var faker2 = new AutoFaker();
faker2.UseSeed(1);
var value2 = faker2.Generate<int>();

value1.Should().Be(value2);
}

[Fact]
public void Generate_with_recursive_depth_0_should_generate()
{
Expand Down

0 comments on commit ca8ac99

Please sign in to comment.