Skip to content

Commit

Permalink
Tests: Use file-scoped namespaces. (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Dec 24, 2023
1 parent cc799df commit f93dc16
Show file tree
Hide file tree
Showing 103 changed files with 7,377 additions and 7,485 deletions.
216 changes: 107 additions & 109 deletions Source/Bogus.Tests/BsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,133 +6,131 @@
using Xunit;
using Xunit.Abstractions;

namespace Bogus.Tests
namespace Bogus.Tests;

public class ExtraStuff : DataSet
{
public class ExtraStuff : DataSet
public string JunkFood()
{
public string JunkFood()
{
return this.GetRandomArrayItem("junkfood");
}

public string Drink()
{
return this.GetRandomArrayItem("Drink");
}
return this.GetRandomArrayItem("junkfood");
}

public class BsonTests : SeededTest, IDisposable
public string Drink()
{
private readonly ITestOutputHelper console;
return this.GetRandomArrayItem("Drink");
}
}

public BsonTests(ITestOutputHelper console)
{
this.console = console;
}
public class BsonTests : SeededTest, IDisposable
{
private readonly ITestOutputHelper console;

public void Dispose()
{
Database.ResetLocale("en");
Database.ResetLocale("it");
}
public BsonTests(ITestOutputHelper console)
{
this.console = console;
}

[Fact]
public void can_add_new_key_to_database()
{
PatchEnLocaleWithExtraStuff();
public void Dispose()
{
Database.ResetLocale("en");
Database.ResetLocale("it");
}

var extra = new ExtraStuff();
extra.JunkFood().Should().Be("Pizza");
extra.Drink().Should().Be("Pepsi");
}
[Fact]
public void can_add_new_key_to_database()
{
PatchEnLocaleWithExtraStuff();

[Fact]
public void should_be_able_to_reset_a_locale()
{
PatchEnLocaleWithExtraStuff();
var extra = new ExtraStuff();
extra.JunkFood().Should().Be("Pizza");
extra.Drink().Should().Be("Pepsi");
}

var extra = new ExtraStuff();
[Fact]
public void should_be_able_to_reset_a_locale()
{
PatchEnLocaleWithExtraStuff();

extra.JunkFood().Should().Be("Pizza");
var extra = new ExtraStuff();

Database.ResetLocale("en");
extra.JunkFood().Should().Be("Pizza");

Action error = () => extra.JunkFood().Should().Be("Pizza");
Database.ResetLocale("en");

error.Should().Throw<NullReferenceException>();
}
Action error = () => extra.JunkFood().Should().Be("Pizza");

error.Should().Throw<NullReferenceException>();
}

[Fact]
public void can_patch_an_existing_category()
{
//names use only these first names
var names = new[] {"Brian", "Chris", "Anthony", "Charlie", "Megan"};

//make a BArray out of the names we wish to use
var firstNames = names.Aggregate(new BArray(), (ba, name) =>

[Fact]
public void can_patch_an_existing_category()
{
//names use only these first names
var names = new[] {"Brian", "Chris", "Anthony", "Charlie", "Megan"};

//make a BArray out of the names we wish to use
var firstNames = names.Aggregate(new BArray(), (ba, name) =>
{
ba.Add(name);
return ba;
});

//Get the locale we wish to mutate.
var itLocale = Database.GetLocale("it");

//get the locale's name category
var namesObject = itLocale["name"] as BObject;

//In the name category, over-write the first_name category
namesObject["first_name"] = firstNames;

//now test everything. get the 'it' locale.
var nameDataSet = new Name("it");

//get 10 names,
var namesFromDataSet = Make(10, () => nameDataSet.FirstName())
.Distinct();

//and we should only have the one's we have patched.
namesFromDataSet.Should().BeEquivalentTo(names);
}


private void PatchEnLocaleWithExtraStuff()
{
var patch = CreateExtraData();
var enLocale = Database.GetLocale("en");

// DATA SET NAMES ARE LOWERCASE.
enLocale.Add("extrastuff", patch);
}

private BObject CreateExtraData()
{
//patching a data set looks like:
// {
// "category1": [item1, item2,...]
// "category2": [item1, item2,...]
// }
return new BObject
{
{
ba.Add(name);
return ba;
});

//Get the locale we wish to mutate.
var itLocale = Database.GetLocale("it");

//get the locale's name category
var namesObject = itLocale["name"] as BObject;

//In the name category, over-write the first_name category
namesObject["first_name"] = firstNames;

//now test everything. get the 'it' locale.
var nameDataSet = new Name("it");

//get 10 names,
var namesFromDataSet = Make(10, () => nameDataSet.FirstName())
.Distinct();

//and we should only have the one's we have patched.
namesFromDataSet.Should().BeEquivalentTo(names);
}


private void PatchEnLocaleWithExtraStuff()
{
var patch = CreateExtraData();
var enLocale = Database.GetLocale("en");

// DATA SET NAMES ARE LOWERCASE.
enLocale.Add("extrastuff", patch);
}

private BObject CreateExtraData()
{
//patching a data set looks like:
// {
// "category1": [item1, item2,...]
// "category2": [item1, item2,...]
// }
return new BObject
"junkfood", new BArray
{
"Cookies",
"Pizza",
"Chips"
}
},
{
{
"junkfood", new BArray
{
"Cookies",
"Pizza",
"Chips"
}
},
{
"Drink", new BArray
{
"Pepsi",
"Coca-Cola",
"Sprite"
}
}
};
}
"Drink", new BArray
{
"Pepsi",
"Coca-Cola",
"Sprite"
}
}
};
}

}
85 changes: 42 additions & 43 deletions Source/Bogus.Tests/CloneTests.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
using FluentAssertions;
using Xunit;

namespace Bogus.Tests
namespace Bogus.Tests;

public class CloneTests : SeededTest
{
public class CloneTests : SeededTest
[Fact]
public void can_create_a_simple_clone()
{
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 clone = orderFaker.Clone();

var clonedOrder = clone.Generate();

var rootOrder = orderFaker.Generate();

clonedOrder.Should().BeEquivalentTo(rootOrder);
}

[Fact]
public void clone_has_different_rules()
{
[Fact]
public void can_create_a_simple_clone()
{
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 clone = orderFaker.Clone();

var clonedOrder = clone.Generate();

var rootOrder = orderFaker.Generate();

clonedOrder.Should().BeEquivalentTo(rootOrder);
}

[Fact]
public void clone_has_different_rules()
{
var rootFaker = 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 cloneFaker = rootFaker.Clone()
.RuleFor(o => o.Quantity, f => f.Random.Number(4, 6));

var rootOrder = rootFaker.Generate();
var clonedOrder = cloneFaker.Generate();

rootOrder.Quantity.Should()
.BeGreaterOrEqualTo(1).And
.BeLessOrEqualTo(3);

clonedOrder.Quantity.Should()
.BeGreaterOrEqualTo(4).And
.BeLessOrEqualTo(6);
}
var rootFaker = 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 cloneFaker = rootFaker.Clone()
.RuleFor(o => o.Quantity, f => f.Random.Number(4, 6));

var rootOrder = rootFaker.Generate();
var clonedOrder = cloneFaker.Generate();

rootOrder.Quantity.Should()
.BeGreaterOrEqualTo(1).And
.BeLessOrEqualTo(3);

clonedOrder.Quantity.Should()
.BeGreaterOrEqualTo(4).And
.BeLessOrEqualTo(6);
}
}
Loading

0 comments on commit f93dc16

Please sign in to comment.