Skip to content

Commit

Permalink
feat: add WithDateTimeKind to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
pdevito3 committed Jan 28, 2024
1 parent c753f42 commit 9902e67
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Config/AutoFakerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ internal sealed class AutoFakerConfig
internal static readonly Func<AutoFakerContext, int> DefaultDataTableRowCount = _ => 15;
internal static readonly Func<AutoFakerContext, int> DefaultRecursiveDepth = _ => 2;
internal static readonly Func<AutoFakerContext, int?> DefaultTreeDepth = _ => null;
internal static readonly Func<AutoFakerContext, DateTimeKind> DefaultDateTimeKind = _ => System.DateTimeKind.Local;

internal string Locale { get; set; }
internal Func<AutoFakerContext, DateTimeKind> DateTimeKind { get; set; }
internal Func<AutoFakerContext, int> RepeatCount { get; set; }
internal Func<AutoFakerContext, int> DataTableRowCount { get; set; }
internal Func<AutoFakerContext, int> RecursiveDepth { get; set; }
Expand All @@ -40,6 +42,7 @@ internal AutoFakerConfig()
DataTableRowCount = DefaultDataTableRowCount;
RecursiveDepth = DefaultRecursiveDepth;
TreeDepth = DefaultTreeDepth;
DateTimeKind = DefaultDateTimeKind;
FakerBinder = new AutoFakerBinder();
SkipTypes = [];
SkipPaths = [];
Expand All @@ -55,6 +58,7 @@ internal AutoFakerConfig(AutoFakerConfig fakerConfig)
DataTableRowCount = fakerConfig.DataTableRowCount;
RecursiveDepth = fakerConfig.RecursiveDepth;
TreeDepth = fakerConfig.TreeDepth;
DateTimeKind = fakerConfig.DateTimeKind;
FakerBinder = fakerConfig.FakerBinder;
SkipTypes = fakerConfig.SkipTypes;
SkipPaths = fakerConfig.SkipPaths;
Expand Down
19 changes: 19 additions & 0 deletions src/Config/AutoFakerConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ internal AutoFakerConfigBuilder(AutoFakerConfig fakerConfig)
internal object[] Args { get; private set; }

IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoFakerDefaultConfigBuilder>(locale, this);

IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithDateTimeKind(Func<AutoFakerContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this);


IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoFakerDefaultConfigBuilder>(context => count, this);

IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithRepeatCount(Func<AutoFakerContext, int> count) =>
Expand Down Expand Up @@ -50,6 +55,10 @@ IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerDefaultConf
WithOverride<IAutoFakerDefaultConfigBuilder>(generatorOverride, this);

IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoGenerateConfigBuilder>(locale, this);

IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithDateTimeKind(Func<AutoFakerContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind<IAutoGenerateConfigBuilder>(_ => dateTimeKind, this);

IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoGenerateConfigBuilder>(context => count, this);
IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithRepeatCount(Func<AutoFakerContext, int> count) => WithRepeatCount<IAutoGenerateConfigBuilder>(count, this);
IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilder>.WithDataTableRowCount(int count) => WithDataTableRowCount<IAutoGenerateConfigBuilder>(context => count, this);
Expand All @@ -71,6 +80,10 @@ IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder<IAutoGenerateConfigBuilde
WithOverride<IAutoGenerateConfigBuilder>(generatorOverride, this);

IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoFakerConfigBuilder>(locale, this);

IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithDateTimeKind(Func<AutoFakerContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this);

IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoFakerConfigBuilder>(context => count, this);
IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithRepeatCount(Func<AutoFakerContext, int> count) => WithRepeatCount<IAutoFakerConfigBuilder>(count, this);
IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder<IAutoFakerConfigBuilder>.WithDataTableRowCount(int count) => WithDataTableRowCount<IAutoFakerConfigBuilder>(context => count, this);
Expand All @@ -92,6 +105,12 @@ internal TBuilder WithLocale<TBuilder>(string? locale, TBuilder builder)
FakerConfig.Locale = locale ?? AutoFakerConfig.DefaultLocale;
return builder;
}

internal TBuilder WithDateTimeKind<TBuilder>(Func<AutoFakerContext, DateTimeKind> kind, TBuilder builder)
{
FakerConfig.DateTimeKind = kind ?? AutoFakerConfig.DefaultDateTimeKind;
return builder;
}

internal TBuilder WithRepeatCount<TBuilder>(Func<AutoFakerContext, int>? count, TBuilder builder)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Config/Base/IBaseAutoFakerConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ public interface IBaseAutoFakerConfigBuilder<TBuilder>
/// <param name="locale">The locale to use.</param>
/// <returns>The current configuration builder instance.</returns>
TBuilder WithLocale(string locale);

/// <summary>
/// Registers the DateTimeKind to use when generating date and time values.
/// </summary>
/// <param name="dateTimeKind">The dateTimeKind to use.</param>
/// <returns>The current configuration builder instance.</returns>
TBuilder WithDateTimeKind(Func<AutoFakerContext, DateTimeKind> dateTimeKind);

/// <summary>
/// Registers the DateTimeKind to use when generating date and time values.
/// </summary>
/// <param name="dateTimeKind">The dateTimeKind to use.</param>
/// <returns>The current configuration builder instance.</returns>
TBuilder WithDateTimeKind(DateTimeKind dateTimeKind);

/// <summary>
/// Registers the number of items to generate for a collection.
Expand Down
6 changes: 5 additions & 1 deletion src/Generators/Types/DateTimeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

namespace Soenneker.Utils.AutoBogus.Generators.Types;

using System;

internal sealed class DateTimeGenerator
: IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
return context.Faker.Date.Recent();
return context.AutoFakerConfig.DateTimeKind.Invoke(context) == DateTimeKind.Utc
? context.Faker.Date.Recent().ToUniversalTime()
: context.Faker.Date.Recent();
}
}
38 changes: 38 additions & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/AutoConfigBuilderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,44 @@ public void Should_Set_Config_Locale_To_Default_If_Null()
}
}

public class WithDateTimeKind : AutoConfigBuilderFixture
{
[Fact]
public void Should_Set_Config_DateTimeKind()
{
var kind = DateTimeKind.Utc;
_builder.WithDateTimeKind<ITestBuilder>(context => kind, null);
_fakerConfig.DateTimeKind.Invoke(null).Should().Be(kind);
}

[Fact]
public void Should_Set_Config_DateTimeKind_To_Default_If_Null()
{
var kind = AutoFakerConfig.DefaultDateTimeKind.Invoke(null);
_builder.WithDateTimeKind<ITestBuilder>(null, null);
_fakerConfig.DateTimeKind.Invoke(null).Should().Be(kind);
}

private sealed record Obj(DateTime Birthday);

[Fact]
public void Should_ConvertToUtc()
{
var obj = AutoFaker.Generate<Obj>(builder =>
{
builder.WithDateTimeKind(DateTimeKind.Utc);
});
obj.Birthday.Should().Be(obj.Birthday.ToUniversalTime());
}

[Fact]
public void Should_BeLocal()
{
var obj = AutoFaker.Generate<Obj>();
obj.Birthday.Should().Be(obj.Birthday.ToLocalTime());
}
}

public class WithRepeatCount
: AutoConfigBuilderFixture
{
Expand Down

0 comments on commit 9902e67

Please sign in to comment.