-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All unit tests passing with localized randomizer. #100
- Loading branch information
Showing
19 changed files
with
529 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Bogus.DataSets; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Bogus.Tests.GitHubIssues | ||
{ | ||
public class Issue100 : SeededTest | ||
{ | ||
private readonly ITestOutputHelper console; | ||
|
||
public Issue100(ITestOutputHelper console) | ||
{ | ||
this.console = console; | ||
} | ||
|
||
[Fact] | ||
public void dataset_test() | ||
{ | ||
var c = new Commerce | ||
{ | ||
Random = new Randomizer(54321) | ||
}; | ||
|
||
c.Product().Should().Be("Shirt"); | ||
Randomizer.Seed = new Random(7331); | ||
c.Product().Should().Be("Soap"); | ||
|
||
Randomizer.Seed = new Random(1337); | ||
c.Random = new Randomizer(54321); | ||
|
||
c.Product().Should().Be("Shirt"); | ||
Randomizer.Seed = new Random(3173); | ||
c.Product().Should().Be("Soap"); | ||
} | ||
|
||
[Fact] | ||
public void faker_test() | ||
{ | ||
var f = new Faker() | ||
{ | ||
Random = new Randomizer(54321) | ||
}; | ||
|
||
f.Commerce.Product().Should().Be("Shirt"); | ||
Randomizer.Seed = new Random(7331); | ||
f.Commerce.Product().Should().Be("Soap"); | ||
|
||
Randomizer.Seed = new Random(1337); | ||
f.Random = new Randomizer(54321); | ||
|
||
f.Commerce.Product().Should().Be("Shirt"); | ||
Randomizer.Seed = new Random(3173); | ||
f.Commerce.Product().Should().Be("Soap"); | ||
} | ||
|
||
[Fact] | ||
public void randomizer_twice() | ||
{ | ||
var r = new Randomizer(88); | ||
Enumerable.Range(1, 4) | ||
.Select(i => r.Number(1, 3)) | ||
.Should().Equal(3, 2, 1, 2); | ||
|
||
r = new Randomizer(88); | ||
Enumerable.Range(1, 4) | ||
.Select(i => r.Number(1, 3)) | ||
.Should().Equal(3, 2, 1, 2); | ||
} | ||
|
||
[Fact] | ||
public void faker_test_2() | ||
{ | ||
var f = new Faker(); | ||
f.Random = new Randomizer(88); | ||
|
||
Enumerable.Range(1, 4) | ||
.Select(i => f.Random.Number(1, 3)) | ||
.Should().Equal(3, 2, 1, 2); | ||
|
||
f.Random = new Randomizer(88); | ||
|
||
Enumerable.Range(1, 4) | ||
.Select(i => f.Random.Number(1, 3)) | ||
.Should().Equal(3, 2, 1, 2); | ||
} | ||
|
||
[Fact] | ||
public void simple_faker_t_test() | ||
{ | ||
var orderFaker = new Faker<Examples.Order>() | ||
.UseSeed(88) | ||
.RuleFor(o => o.Quantity, f => | ||
{ | ||
return f.Random.Number(1, 3); | ||
}); | ||
|
||
var order = orderFaker.Generate(); | ||
|
||
order.Quantity.Should().Be(3); | ||
|
||
orderFaker.UseSeed(88); | ||
|
||
order = orderFaker.Generate(); | ||
|
||
order.Quantity.Should().Be(3); | ||
} | ||
|
||
[Fact] | ||
public void complex_faker_t_test() | ||
{ | ||
Randomizer.Seed = new Random(7331); | ||
|
||
var orderFaker = new Faker<Examples.Order>() | ||
.UseSeed(88) | ||
.RuleFor(o => o.OrderId, f => f.IndexVariable++) | ||
.RuleFor(o => o.Quantity, f => f.Random.Number(1, 3)) | ||
.RuleFor(o => o.Item, f => f.Commerce.Product()); | ||
|
||
var items = Enumerable.Range(1, 4) | ||
.Select(i => orderFaker.Generate()) | ||
.ToArray(); | ||
|
||
console.WriteLine(items.DumpString()); | ||
|
||
items.Select(i => i.OrderId).Should().Equal(0, 1, 2, 3); | ||
|
||
CheckSequence(items); | ||
|
||
//using the same seed again should | ||
//reset the state. | ||
orderFaker.UseSeed(88); | ||
//and it should override a set seeded test | ||
Randomizer.Seed = new Random(1337); | ||
|
||
items = Enumerable.Range(1, 4) | ||
.Select(i => orderFaker.Generate()) | ||
.ToArray(); | ||
|
||
items.Select(i => i.OrderId).Should().Equal(4, 5, 6, 7); | ||
|
||
console.WriteLine(items.DumpString()); | ||
|
||
CheckSequence(items); | ||
} | ||
|
||
//[Fact] | ||
//public void parallel_determinism() | ||
//{ | ||
// var orderFaker = new Faker<Examples.Order>() | ||
// .RuleFor(o => o.OrderId, f => f.IndexVariable++) | ||
// .RuleFor(o => o.Quantity, f => f.Random.Number(1, 3)) | ||
// .RuleFor(o => o.Item, f => f.Commerce.Product()); | ||
|
||
// var orders = ParallelEnumerable.Range(1, 5) | ||
// .Select(threadId => | ||
// orderFaker | ||
// .UseSeed(88) | ||
// .Generate(4).ToArray() | ||
// ).ToArray(); | ||
|
||
// foreach( var orderOfFour in orders ) | ||
// { | ||
// CheckSequence(orderOfFour); | ||
// } | ||
//} | ||
|
||
private void CheckSequence(Examples.Order[] items) | ||
{ | ||
items.Select(i => i.Item) | ||
.Should().Equal("Tuna", "Pants", "Shoes", "Soap"); | ||
|
||
items.Select(i => i.Quantity) | ||
.Should().Equal(3, 1, 3, 2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.