From e2ee7a3575af6d9de620dffcf3cd7c07b991b762 Mon Sep 17 00:00:00 2001 From: Paul DeVito Date: Sat, 30 Apr 2022 10:57:49 -0400 Subject: [PATCH] feat: support .net 6 DateOnly TimeOnly structs --- .../AutoBogus.Playground.csproj | 4 +- .../DateTimeOnlyFixture.cs | 50 +++++++++++++++++++ src/AutoBogus.Playground/SeedFixture.cs | 8 +-- src/AutoBogus.Playground/TemplateFixture.cs | 2 +- src/AutoBogus/AutoBogus.csproj | 4 +- src/AutoBogus/AutoGeneratorFactory.cs | 4 ++ src/AutoBogus/DateOnlyGenerator.cs | 13 +++++ src/AutoBogus/TimeOnlyGenerator.cs | 13 +++++ 8 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/AutoBogus.Playground/DateTimeOnlyFixture.cs create mode 100644 src/AutoBogus/DateOnlyGenerator.cs create mode 100644 src/AutoBogus/TimeOnlyGenerator.cs diff --git a/src/AutoBogus.Playground/AutoBogus.Playground.csproj b/src/AutoBogus.Playground/AutoBogus.Playground.csproj index b2c7c1c..27f938f 100644 --- a/src/AutoBogus.Playground/AutoBogus.Playground.csproj +++ b/src/AutoBogus.Playground/AutoBogus.Playground.csproj @@ -1,7 +1,7 @@ - netcoreapp2.0 + netcoreapp2.0;net6.0 false @@ -11,7 +11,7 @@ - + diff --git a/src/AutoBogus.Playground/DateTimeOnlyFixture.cs b/src/AutoBogus.Playground/DateTimeOnlyFixture.cs new file mode 100644 index 0000000..db22c92 --- /dev/null +++ b/src/AutoBogus.Playground/DateTimeOnlyFixture.cs @@ -0,0 +1,50 @@ +using Bogus; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Xunit; + +namespace AutoBogus.Playground +{ + public class DateTimeOnlyFixture + { +#if NET6_0 + private sealed class DateTimeKeeper + { + public DateTime Old { get; set; } + public DateOnly DeprecationDate { get; set; } + public TimeOnly DeprecationTime { get; set; } + } + + [Fact] + public void DateOnlyTimeOnlyTest() + { + int seed = 1; + + var faker1 = new AutoFaker().UseSeed(seed); + var faker2 = new AutoFaker().UseSeed(seed); + var faker3 = new AutoFaker(); + var entity1 = faker1.Generate(); + var entity2 = faker2.Generate(); + var entity3 = faker3.Generate(); + + entity2.DeprecationDate.ToDateTime(TimeOnly.MinValue) + .Should() + .BeCloseTo(entity1.DeprecationDate.ToDateTime(TimeOnly.MinValue), TimeSpan.FromMilliseconds(500)); + + entity3.DeprecationDate.ToDateTime(TimeOnly.MinValue) + .Should() + .NotBeCloseTo(entity2.DeprecationDate.ToDateTime(TimeOnly.MinValue), TimeSpan.FromMilliseconds(500)); + + entity2.DeprecationTime.ToTimeSpan() + .Should() + .BeCloseTo(entity1.DeprecationTime.ToTimeSpan(), TimeSpan.FromMilliseconds(500)); + + entity3.DeprecationTime.ToTimeSpan() + .Should() + .NotBeCloseTo(entity2.DeprecationTime.ToTimeSpan(), TimeSpan.FromMilliseconds(500)); + } +#endif + } +} diff --git a/src/AutoBogus.Playground/SeedFixture.cs b/src/AutoBogus.Playground/SeedFixture.cs index d4fdc2d..9a4a690 100644 --- a/src/AutoBogus.Playground/SeedFixture.cs +++ b/src/AutoBogus.Playground/SeedFixture.cs @@ -39,15 +39,15 @@ public void DateTimeOffsetTest() var faker1 = new AutoFaker().UseSeed(seed); var faker2 = new AutoFaker().UseSeed(seed); var faker3 = new AutoFaker(); - var entity1 = faker1.Generate(); + var entity1 = faker1.Generate(); var entity2 = faker2.Generate(); var entity3 = faker3.Generate(); - entity2.Name.Should().Be(entity1.Name); - entity2.DeprecationDate.Should().BeCloseTo(entity1.DeprecationDate, 500); + entity2.Name.Should().Be(entity1.Name); + entity2.DeprecationDate.Should().BeCloseTo(entity1.DeprecationDate, TimeSpan.FromMilliseconds(500)); entity3.Name.Should().NotBe(entity2.Name); - entity3.DeprecationDate.Should().NotBeCloseTo(entity2.DeprecationDate, 500); + entity3.DeprecationDate.Should().NotBeCloseTo(entity2.DeprecationDate, TimeSpan.FromMilliseconds(500)); } [Fact] diff --git a/src/AutoBogus.Playground/TemplateFixture.cs b/src/AutoBogus.Playground/TemplateFixture.cs index cee1133..347a299 100644 --- a/src/AutoBogus.Playground/TemplateFixture.cs +++ b/src/AutoBogus.Playground/TemplateFixture.cs @@ -35,7 +35,7 @@ public void TestAutoFaker() }, options => options .Using(context => context.Subject.Should().NotBeNull()) - .When(info => info.SelectedMemberPath == "Status") + .When(info => info.Path.Contains("Status")) ); } } diff --git a/src/AutoBogus/AutoBogus.csproj b/src/AutoBogus/AutoBogus.csproj index 8f2f6fa..935650b 100644 --- a/src/AutoBogus/AutoBogus.csproj +++ b/src/AutoBogus/AutoBogus.csproj @@ -1,7 +1,7 @@ - net40;net452;netstandard1.3;netstandard2.0 + net40;net452;netstandard1.3;netstandard2.0;net6.0 A C# library complementing the Bogus generator by adding auto creation and population capabilities. 2.13.1 true @@ -11,7 +11,7 @@ - + diff --git a/src/AutoBogus/AutoGeneratorFactory.cs b/src/AutoBogus/AutoGeneratorFactory.cs index 1706606..c7c0b55 100644 --- a/src/AutoBogus/AutoGeneratorFactory.cs +++ b/src/AutoBogus/AutoGeneratorFactory.cs @@ -14,6 +14,10 @@ internal static class AutoGeneratorFactory {typeof(bool), new BoolGenerator()}, {typeof(byte), new ByteGenerator()}, {typeof(char), new CharGenerator()}, +#if NET6_0 + {typeof(DateOnly), new DateOnlyGenerator()}, + {typeof(TimeOnly), new TimeOnlyGenerator()}, +#endif {typeof(DateTime), new DateTimeGenerator()}, {typeof(DateTimeOffset), new DateTimeOffsetGenerator()}, {typeof(decimal), new DecimalGenerator()}, diff --git a/src/AutoBogus/DateOnlyGenerator.cs b/src/AutoBogus/DateOnlyGenerator.cs new file mode 100644 index 0000000..9b24d68 --- /dev/null +++ b/src/AutoBogus/DateOnlyGenerator.cs @@ -0,0 +1,13 @@ +namespace AutoBogus.Generators +{ +#if NET6_0 + internal sealed class DateOnlyGenerator + : IAutoGenerator + { + object IAutoGenerator.Generate(AutoGenerateContext context) + { + return context.Faker.Date.RecentDateOnly(); + } + } +#endif +} diff --git a/src/AutoBogus/TimeOnlyGenerator.cs b/src/AutoBogus/TimeOnlyGenerator.cs new file mode 100644 index 0000000..f496da8 --- /dev/null +++ b/src/AutoBogus/TimeOnlyGenerator.cs @@ -0,0 +1,13 @@ +namespace AutoBogus.Generators +{ +#if NET6_0 + internal sealed class TimeOnlyGenerator + : IAutoGenerator + { + object IAutoGenerator.Generate(AutoGenerateContext context) + { + return context.Faker.Date.RecentTimeOnly(); + } + } +#endif +}