Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support .net 6 DateOnly TimeOnly structs #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AutoBogus.Playground/AutoBogus.Playground.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFrameworks>netcoreapp2.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -11,7 +11,7 @@


<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
Copy link
Author

@pdevito3 pdevito3 Apr 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to update FA to accommodate the dateonly timeonly handling

<PackageReference Include="Google.Protobuf" Version="3.13.0" />
<PackageReference Include="NodaTime" Version="3.0.3" />
<PackageReference Include="Xero.NetStandard.OAuth2" Version="3.14.2" />
Expand Down
50 changes: 50 additions & 0 deletions src/AutoBogus.Playground/DateTimeOnlyFixture.cs
Original file line number Diff line number Diff line change
@@ -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<DateTimeKeeper>().UseSeed(seed);
var faker2 = new AutoFaker<DateTimeKeeper>().UseSeed(seed);
var faker3 = new AutoFaker<DateTimeKeeper>();
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
}
}
8 changes: 4 additions & 4 deletions src/AutoBogus.Playground/SeedFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void DateTimeOffsetTest()
var faker1 = new AutoFaker<MyEntity>().UseSeed(seed);
var faker2 = new AutoFaker<MyEntity>().UseSeed(seed);
var faker3 = new AutoFaker<MyEntity>();
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));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new fluent assertion syntax


entity3.Name.Should().NotBe(entity2.Name);
entity3.DeprecationDate.Should().NotBeCloseTo(entity2.DeprecationDate, 500);
entity3.DeprecationDate.Should().NotBeCloseTo(entity2.DeprecationDate, TimeSpan.FromMilliseconds(500));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/AutoBogus.Playground/TemplateFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestAutoFaker()
},
options => options
.Using<string>(context => context.Subject.Should().NotBeNull())
.When(info => info.SelectedMemberPath == "Status")
.When(info => info.Path.Contains("Status"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new fluent assertion syntax

);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/AutoBogus/AutoBogus.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net40;net452;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net40;net452;netstandard1.3;netstandard2.0;net6.0</TargetFrameworks>
<Description>A C# library complementing the Bogus generator by adding auto creation and population capabilities.</Description>
<Version>2.13.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Bogus" Version="31.0.3" />
<PackageReference Include="Bogus" Version="34.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/AutoBogus/AutoGeneratorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
Expand Down
13 changes: 13 additions & 0 deletions src/AutoBogus/DateOnlyGenerator.cs
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions src/AutoBogus/TimeOnlyGenerator.cs
Original file line number Diff line number Diff line change
@@ -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
}