Skip to content

Commit

Permalink
Fixes #102. Uuid draws randomness from local / global seed via .Bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Nov 6, 2017
1 parent ea687b9 commit 6928921
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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<T>.Clone()`: Clones internal state of a `Faker<T>` and allows for complex faking scenarios and rule combinations.
* Added `Faker<T>.UseSeed(n)`: Allows you to specify a localized seed value on a `Faker<T>` instead of a global static `Randomizer.Seed`.
Expand Down
1 change: 1 addition & 0 deletions Source/Bogus.Tests/Bogus.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="FluentTests.cs" />
<Compile Include="GitHubIssues\Issue10.cs" />
<Compile Include="GitHubIssues\Issue100.cs" />
<Compile Include="GitHubIssues\Issue102.cs" />
<Compile Include="GitHubIssues\Issue13.cs" />
<Compile Include="GitHubIssues\Issue23.cs" />
<Compile Include="GitHubIssues\Issue45.cs" />
Expand Down
35 changes: 35 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue102.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
5 changes: 5 additions & 0 deletions Source/Bogus.Tests/SeededTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
6 changes: 5 additions & 1 deletion Source/Bogus/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Randomizer

/// <summary>
/// Constructor that uses the global static `<see cref="Seed"/>.
/// Changing the global static seed after this constructor runs
/// will have no effect. A new randomizer is needed to capture a new
/// global seed.
/// </summary>
public Randomizer()
{
Expand Down Expand Up @@ -557,7 +560,8 @@ public string[] WordsArray(int count)
/// </summary>
public Guid Uuid()
{
return Guid.NewGuid();
var guidBytes = this.Bytes(16);
return new Guid(guidBytes);
}

/// <summary>
Expand Down

0 comments on commit 6928921

Please sign in to comment.