From 692892176fc9421c3ef83be5e882bb3b5751781a Mon Sep 17 00:00:00 2001 From: bchavez Date: Mon, 6 Nov 2017 10:37:50 -0800 Subject: [PATCH] Fixes #102. Uuid draws randomness from local / global seed via .Bytes() --- HISTORY.md | 3 ++ Source/Bogus.Tests/Bogus.Tests.csproj | 1 + Source/Bogus.Tests/GitHubIssues/Issue102.cs | 35 +++++++++++++++++++++ Source/Bogus.Tests/SeededTest.cs | 5 +++ Source/Bogus/Randomizer.cs | 6 +++- 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Source/Bogus.Tests/GitHubIssues/Issue102.cs diff --git a/HISTORY.md b/HISTORY.md index 75285573..ec20cbed 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,6 @@ +## v20.0.2 +* Fixed Issue 102: `f.Random.Uuid()` is now deterministic based on global or local seed. + ## v20.0.1 * Added `Faker.Clone()`: Clones internal state of a `Faker` and allows for complex faking scenarios and rule combinations. * Added `Faker.UseSeed(n)`: Allows you to specify a localized seed value on a `Faker` instead of a global static `Randomizer.Seed`. diff --git a/Source/Bogus.Tests/Bogus.Tests.csproj b/Source/Bogus.Tests/Bogus.Tests.csproj index 3c5ba372..ff91e82a 100644 --- a/Source/Bogus.Tests/Bogus.Tests.csproj +++ b/Source/Bogus.Tests/Bogus.Tests.csproj @@ -92,6 +92,7 @@ + diff --git a/Source/Bogus.Tests/GitHubIssues/Issue102.cs b/Source/Bogus.Tests/GitHubIssues/Issue102.cs new file mode 100644 index 00000000..f734fcf2 --- /dev/null +++ b/Source/Bogus.Tests/GitHubIssues/Issue102.cs @@ -0,0 +1,35 @@ +using System; +using FluentAssertions; +using Xunit; +using Xunit.Abstractions; + +namespace Bogus.Tests.GitHubIssues +{ + public class Issue102 : SeededTest + { + [Fact] + public void deterministic_uuid_using_global_seed() + { + var r = new Randomizer(); + r.Uuid().Should().Be("{4c9c23da-c4e0-d72d-e3c4-a89617f255b2}"); + ResetGlobalSeed(); //should have an effect only if a new randomizer is created + r.Uuid().Should().Be("{bd59c865-cda5-44f4-a28e-db17c014d586}"); + + r = new Randomizer(); + r.Uuid().Should().Be("{4c9c23da-c4e0-d72d-e3c4-a89617f255b2}"); + } + + [Fact] + public void deterministic_uuid_using_local_seed() + { + var r = new Randomizer(1337); + r.Uuid().Should().Be("{c7f40068-5e43-aa02-c27c-4fd927fc2227}"); + ResetGlobalSeed(); //should have no effect + r.Uuid().Should().Be("{b254896a-12e5-1eef-9af7-227ef036e328}"); + + ResetGlobalSeed(); //should have no effect + r = new Randomizer(1337); + r.Uuid().Should().Be("{c7f40068-5e43-aa02-c27c-4fd927fc2227}"); + } + } +} \ No newline at end of file diff --git a/Source/Bogus.Tests/SeededTest.cs b/Source/Bogus.Tests/SeededTest.cs index d9f5e23d..8f18a892 100644 --- a/Source/Bogus.Tests/SeededTest.cs +++ b/Source/Bogus.Tests/SeededTest.cs @@ -14,6 +14,11 @@ public class SeededTest public SeededTest() { //set the random gen manually to a seeded value + ResetGlobalSeed(); + } + + protected static void ResetGlobalSeed() + { Randomizer.Seed = new System.Random(3116); } } diff --git a/Source/Bogus/Randomizer.cs b/Source/Bogus/Randomizer.cs index 30d80225..2e7aad78 100644 --- a/Source/Bogus/Randomizer.cs +++ b/Source/Bogus/Randomizer.cs @@ -22,6 +22,9 @@ public class Randomizer /// /// Constructor that uses the global static `. + /// Changing the global static seed after this constructor runs + /// will have no effect. A new randomizer is needed to capture a new + /// global seed. /// public Randomizer() { @@ -557,7 +560,8 @@ public string[] WordsArray(int count) /// public Guid Uuid() { - return Guid.NewGuid(); + var guidBytes = this.Bytes(16); + return new Guid(guidBytes); } ///