From 9902e6775b96841f3cd34e9c2cd96f7f20f7f0f7 Mon Sep 17 00:00:00 2001 From: Paul DeVito Date: Sat, 27 Jan 2024 19:42:10 -0500 Subject: [PATCH] feat: add WithDateTimeKind to builder --- src/Config/AutoFakerConfig.cs | 4 ++ src/Config/AutoFakerConfigBuilder.cs | 19 ++++++++++ .../Base/IBaseAutoFakerConfigBuilder.cs | 14 +++++++ src/Generators/Types/DateTimeGenerator.cs | 6 ++- .../AutoConfigBuilderFixture.cs | 38 +++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/Config/AutoFakerConfig.cs b/src/Config/AutoFakerConfig.cs index a5bdd96..cd3f071 100644 --- a/src/Config/AutoFakerConfig.cs +++ b/src/Config/AutoFakerConfig.cs @@ -15,8 +15,10 @@ internal sealed class AutoFakerConfig internal static readonly Func DefaultDataTableRowCount = _ => 15; internal static readonly Func DefaultRecursiveDepth = _ => 2; internal static readonly Func DefaultTreeDepth = _ => null; + internal static readonly Func DefaultDateTimeKind = _ => System.DateTimeKind.Local; internal string Locale { get; set; } + internal Func DateTimeKind { get; set; } internal Func RepeatCount { get; set; } internal Func DataTableRowCount { get; set; } internal Func RecursiveDepth { get; set; } @@ -40,6 +42,7 @@ internal AutoFakerConfig() DataTableRowCount = DefaultDataTableRowCount; RecursiveDepth = DefaultRecursiveDepth; TreeDepth = DefaultTreeDepth; + DateTimeKind = DefaultDateTimeKind; FakerBinder = new AutoFakerBinder(); SkipTypes = []; SkipPaths = []; @@ -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; diff --git a/src/Config/AutoFakerConfigBuilder.cs b/src/Config/AutoFakerConfigBuilder.cs index c79c5f7..818bcb9 100644 --- a/src/Config/AutoFakerConfigBuilder.cs +++ b/src/Config/AutoFakerConfigBuilder.cs @@ -20,6 +20,11 @@ internal AutoFakerConfigBuilder(AutoFakerConfig fakerConfig) internal object[] Args { get; private set; } IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder.WithLocale(string locale) => WithLocale(locale, this); + + IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(Func dateTimeKind) => WithDateTimeKind(dateTimeKind, this); + IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this); + + IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(int count) => WithRepeatCount(context => count, this); IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(Func count) => @@ -50,6 +55,10 @@ IAutoFakerDefaultConfigBuilder IBaseAutoFakerConfigBuilder(generatorOverride, this); IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithLocale(string locale) => WithLocale(locale, this); + + IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(Func dateTimeKind) => WithDateTimeKind(dateTimeKind, this); + IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this); + IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(int count) => WithRepeatCount(context => count, this); IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(Func count) => WithRepeatCount(count, this); IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder.WithDataTableRowCount(int count) => WithDataTableRowCount(context => count, this); @@ -71,6 +80,10 @@ IAutoGenerateConfigBuilder IBaseAutoFakerConfigBuilder(generatorOverride, this); IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithLocale(string locale) => WithLocale(locale, this); + + IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(Func dateTimeKind) => WithDateTimeKind(dateTimeKind, this); + IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this); + IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(int count) => WithRepeatCount(context => count, this); IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithRepeatCount(Func count) => WithRepeatCount(count, this); IAutoFakerConfigBuilder IBaseAutoFakerConfigBuilder.WithDataTableRowCount(int count) => WithDataTableRowCount(context => count, this); @@ -92,6 +105,12 @@ internal TBuilder WithLocale(string? locale, TBuilder builder) FakerConfig.Locale = locale ?? AutoFakerConfig.DefaultLocale; return builder; } + + internal TBuilder WithDateTimeKind(Func kind, TBuilder builder) + { + FakerConfig.DateTimeKind = kind ?? AutoFakerConfig.DefaultDateTimeKind; + return builder; + } internal TBuilder WithRepeatCount(Func? count, TBuilder builder) { diff --git a/src/Config/Base/IBaseAutoFakerConfigBuilder.cs b/src/Config/Base/IBaseAutoFakerConfigBuilder.cs index c2a12cb..1dedbe1 100644 --- a/src/Config/Base/IBaseAutoFakerConfigBuilder.cs +++ b/src/Config/Base/IBaseAutoFakerConfigBuilder.cs @@ -18,6 +18,20 @@ public interface IBaseAutoFakerConfigBuilder /// The locale to use. /// The current configuration builder instance. TBuilder WithLocale(string locale); + + /// + /// Registers the DateTimeKind to use when generating date and time values. + /// + /// The dateTimeKind to use. + /// The current configuration builder instance. + TBuilder WithDateTimeKind(Func dateTimeKind); + + /// + /// Registers the DateTimeKind to use when generating date and time values. + /// + /// The dateTimeKind to use. + /// The current configuration builder instance. + TBuilder WithDateTimeKind(DateTimeKind dateTimeKind); /// /// Registers the number of items to generate for a collection. diff --git a/src/Generators/Types/DateTimeGenerator.cs b/src/Generators/Types/DateTimeGenerator.cs index 728bf9f..b687f6b 100644 --- a/src/Generators/Types/DateTimeGenerator.cs +++ b/src/Generators/Types/DateTimeGenerator.cs @@ -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(); } } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoConfigBuilderFixture.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoConfigBuilderFixture.cs index bc80142..bcd85b9 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoConfigBuilderFixture.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoConfigBuilderFixture.cs @@ -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(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(null, null); + _fakerConfig.DateTimeKind.Invoke(null).Should().Be(kind); + } + + private sealed record Obj(DateTime Birthday); + + [Fact] + public void Should_ConvertToUtc() + { + var obj = AutoFaker.Generate(builder => + { + builder.WithDateTimeKind(DateTimeKind.Utc); + }); + obj.Birthday.Should().Be(obj.Birthday.ToUniversalTime()); + } + + [Fact] + public void Should_BeLocal() + { + var obj = AutoFaker.Generate(); + obj.Birthday.Should().Be(obj.Birthday.ToLocalTime()); + } + } + public class WithRepeatCount : AutoConfigBuilderFixture {