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 May 2, 2022
1 parent 50db3b1 commit fefeff9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ AutoFaker.Configure(builder =>
{
builder
.WithLocale() // Configures the locale to use
.WithDateTimeKind() // Configures the DateTimeKind to use when generating date and time values. Defaults to `DateTimeKind.Local`.
.WithRepeatCount() // Configures the number of items in a collection
.WithDataTableRowCount() // Configures the number of data table rows to generate
.WithRecursiveDepth() // Configures how deep nested types should recurse
Expand Down
33 changes: 33 additions & 0 deletions src/AutoBogus.Playground/DateTimeKindFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FluentAssertions;
using System;
using Xunit;

namespace AutoBogus.Playground
{
public class DateTimeKindFixture
{
private sealed class Obj
{
public DateTime Birthday { get; set; }
}

[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());
}
}
}
23 changes: 22 additions & 1 deletion src/AutoBogus.Tests/AutoConfigBuilderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ 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);

_config.DateTimeKind.Should().Be(kind);
}

[Fact]
public void Should_Set_Config_DateTimeKind_To_Default_If_Null()
{
_builder.WithRepeatCount<ITestBuilder>(null, null);
_config.DateTimeKind.Invoke(null).Should().Be(DateTimeKind.Utc);
}
}

public class WithRepeatCount
: AutoConfigBuilderFixture
{
Expand Down Expand Up @@ -94,7 +115,7 @@ public void Should_Set_Config_RecursiveDepth_To_Default_If_Null()
}
}


public class WithTreeDepth
: AutoConfigBuilderFixture
{
Expand Down
4 changes: 4 additions & 0 deletions src/AutoBogus/AutoConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed class AutoConfig
internal const string DefaultLocale = "en";
internal const int GenerateAttemptsThreshold = 3;

internal static readonly Func<AutoGenerateContext, DateTimeKind> DefaultDateTimeKind = context => System.DateTimeKind.Local;
internal static readonly Func<AutoGenerateContext, int> DefaultRepeatCount = context => 3;
internal static readonly Func<AutoGenerateContext, int> DefaultDataTableRowCount = context => 15;
internal static readonly Func<AutoGenerateContext, int> DefaultRecursiveDepth = context => 2;
Expand All @@ -18,6 +19,7 @@ internal sealed class AutoConfig
internal AutoConfig()
{
Locale = DefaultLocale;
DateTimeKind = DefaultDateTimeKind;
RepeatCount = DefaultRepeatCount;
DataTableRowCount = DefaultDataTableRowCount;
RecursiveDepth = DefaultRecursiveDepth;
Expand All @@ -31,6 +33,7 @@ internal AutoConfig()
internal AutoConfig(AutoConfig config)
{
Locale = config.Locale;
DateTimeKind = config.DateTimeKind;
RepeatCount = config.RepeatCount;
DataTableRowCount = config.DataTableRowCount;
RecursiveDepth = config.RecursiveDepth;
Expand All @@ -43,6 +46,7 @@ internal AutoConfig(AutoConfig config)
}

internal string Locale { get; set; }
internal Func<AutoGenerateContext, DateTimeKind> DateTimeKind { get; set; }
internal Func<AutoGenerateContext, int> RepeatCount { get; set; }
internal Func<AutoGenerateContext, int> DataTableRowCount { get; set; }
internal Func<AutoGenerateContext, int> RecursiveDepth { get; set; }
Expand Down
16 changes: 14 additions & 2 deletions src/AutoBogus/AutoConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal AutoConfigBuilder(AutoConfig config)
internal object[] Args { get; private set; }

IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoFakerDefaultConfigBuilder>(locale, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithDateTimeKind(Func<AutoGenerateContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoFakerDefaultConfigBuilder>(context => count, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithRepeatCount(Func<AutoGenerateContext, int> count) => WithRepeatCount<IAutoFakerDefaultConfigBuilder>(count, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithDataTableRowCount(int count) => WithDataTableRowCount<IAutoFakerDefaultConfigBuilder>(context => count, this);
Expand All @@ -31,8 +33,10 @@ internal AutoConfigBuilder(AutoConfig config)
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithSkip(Type type, string memberName) => WithSkip<IAutoFakerDefaultConfigBuilder>(type, memberName, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithSkip<TType>(string memberName) => WithSkip<IAutoFakerDefaultConfigBuilder, TType>(memberName, this);
IAutoFakerDefaultConfigBuilder IAutoConfigBuilder<IAutoFakerDefaultConfigBuilder>.WithOverride(AutoGeneratorOverride generatorOverride) => WithOverride<IAutoFakerDefaultConfigBuilder>(generatorOverride, this);

IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoGenerateConfigBuilder>(locale, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithDateTimeKind(Func<AutoGenerateContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind<IAutoGenerateConfigBuilder>(_ => dateTimeKind, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoGenerateConfigBuilder>(context => count, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithRepeatCount(Func<AutoGenerateContext, int> count) => WithRepeatCount<IAutoGenerateConfigBuilder>(count, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithDataTableRowCount(int count) => WithDataTableRowCount<IAutoGenerateConfigBuilder>(context => count, this);
Expand All @@ -47,8 +51,10 @@ internal AutoConfigBuilder(AutoConfig config)
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithSkip(Type type, string memberName) => WithSkip<IAutoGenerateConfigBuilder>(type, memberName, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithSkip<TType>(string memberName) => WithSkip<IAutoGenerateConfigBuilder, TType>(memberName, this);
IAutoGenerateConfigBuilder IAutoConfigBuilder<IAutoGenerateConfigBuilder>.WithOverride(AutoGeneratorOverride generatorOverride) => WithOverride<IAutoGenerateConfigBuilder>(generatorOverride, this);

IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithLocale(string locale) => WithLocale<IAutoFakerConfigBuilder>(locale, this);
IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithDateTimeKind(Func<AutoGenerateContext, DateTimeKind> dateTimeKind) => WithDateTimeKind(dateTimeKind, this);
IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithDateTimeKind(DateTimeKind dateTimeKind) => WithDateTimeKind(_ => dateTimeKind, this);
IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithRepeatCount(int count) => WithRepeatCount<IAutoFakerConfigBuilder>(context => count, this);
IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithRepeatCount(Func<AutoGenerateContext, int> count) => WithRepeatCount<IAutoFakerConfigBuilder>(count, this);
IAutoFakerConfigBuilder IAutoConfigBuilder<IAutoFakerConfigBuilder>.WithDataTableRowCount(int count) => WithDataTableRowCount<IAutoFakerConfigBuilder>(context => count, this);
Expand All @@ -71,6 +77,12 @@ internal TBuilder WithLocale<TBuilder>(string locale, TBuilder builder)
return builder;
}

internal TBuilder WithDateTimeKind<TBuilder>(Func<AutoGenerateContext, DateTimeKind> kind, TBuilder builder)
{
Config.DateTimeKind = kind ?? AutoConfig.DefaultDateTimeKind;
return builder;
}

internal TBuilder WithRepeatCount<TBuilder>(Func<AutoGenerateContext, int> count, TBuilder builder)
{
Config.RepeatCount = count ?? AutoConfig.DefaultRepeatCount;
Expand Down
6 changes: 5 additions & 1 deletion src/AutoBogus/Generators/DateTimeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace AutoBogus.Generators
{
using System;

internal sealed class DateTimeGenerator
: IAutoGenerator
{
object IAutoGenerator.Generate(AutoGenerateContext context)
{
return context.Faker.Date.Recent();
return context.Config.DateTimeKind.Invoke(context) == DateTimeKind.Utc
? context.Faker.Date.Recent().ToUniversalTime()
: context.Faker.Date.Recent();
}
}
}
4 changes: 3 additions & 1 deletion src/AutoBogus/Generators/DateTimeOffsetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ internal sealed class DateTimeOffsetGenerator
{
object IAutoGenerator.Generate(AutoGenerateContext context)
{
var dateTime = context.Faker.Date.Recent();
var dateTime = context.Config.DateTimeKind.Invoke(context) == DateTimeKind.Utc
? context.Faker.Date.Recent().ToUniversalTime()
: context.Faker.Date.Recent();
return new DateTimeOffset(dateTime);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/AutoBogus/IAutoConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ public interface IAutoConfigBuilder<TBuilder>
/// <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<AutoGenerateContext, 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.
/// </summary>
Expand Down

0 comments on commit fefeff9

Please sign in to comment.