Skip to content

Commit

Permalink
Add seed constructor parameter to Person(seed) class.
Browse files Browse the repository at this point in the history
Fixed minor issue of Person.DateOfBirth not using Date.SystemClock.
  • Loading branch information
bchavez committed Mar 21, 2019
1 parent 323910a commit 3400c06
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v26.0.2
* New `Person(seed)` constructor for seeding person objects by integer. Thanks @sgoguen!
* Fixed `Person.DateOfBirth` not using `Date.SystemClock` as 'now' reference.

## v26.0.1
* Data and feature parity with faker.js @ d3ce6f1
* New `Vehicle` data set added.
Expand Down
30 changes: 30 additions & 0 deletions Source/Bogus.Tests/PersonTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bogus.DataSets;
using Bogus.Extensions.Brazil;
using Bogus.Extensions.Canada;
using Bogus.Extensions.Denmark;
Expand Down Expand Up @@ -155,6 +156,35 @@ public void person_has_full_name()
var p = new Person();
p.FullName.Should().Be($"{p.FirstName} {p.LastName}");
}

[Fact]
public void can_use_local_seed_for_person()
{
Date.SystemClock = () => new DateTime(2019, 3, 21, 1, 1, 1);

var p1 = new Person(seed: 1337);
var p2 = new Person(seed: 1337);
var q = new Person(seed: 7331);

p1.FullName.Should().Be("Samuel Haley");
p2.FullName.Should().Be(p1.FullName);
q.FullName.Should().Be("Lynette Beatty");
q.FullName.Should().NotBe(p1.FullName);

p1.FirstName.Should().Be(p2.FirstName);
p1.LastName.Should().Be(p2.LastName);
p1.Avatar.Should().Be(p2.Avatar);
p1.DateOfBirth.Should().Be(p2.DateOfBirth);
p1.Email.Should().Be(p2.Email);
p1.Phone.Should().Be(p2.Phone);
p1.UserName.Should().Be(p2.UserName);
p1.Gender.Should().Be(p2.Gender);
p1.Website.Should().Be(p2.Website);

p1.ShouldBeEquivalentTo(p2);

Date.SystemClock = () => DateTime.Now;
}


IEnumerable<string> Get(int times, Func<Person, string> a)
Expand Down
8 changes: 6 additions & 2 deletions Source/Bogus/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public class CardCompany
protected Address DsAddress { get; set; }
protected Company DsCompany { get; set; }

public Person(string locale = "en")
public Person(string locale = "en", int? seed = null)
{
this.GetDataSources(locale);
if( seed.HasValue )
{
this.Random = new Randomizer(seed.Value);
}
this.Populate();
}

Expand Down Expand Up @@ -82,7 +86,7 @@ protected internal virtual void Populate()
this.Website = this.DsInternet.DomainName();
this.Avatar = this.DsInternet.Avatar();

this.DateOfBirth = this.DsDate.Past(50, DateTime.Now.AddYears(-20));
this.DateOfBirth = this.DsDate.Past(50, Date.SystemClock().AddYears(-20));

this.Phone = this.DsPhoneNumbers.PhoneNumber();

Expand Down

0 comments on commit 3400c06

Please sign in to comment.