Skip to content

Commit

Permalink
Added the Commerce faker. (#110)
Browse files Browse the repository at this point in the history
* Added the documentation.

* Added the data.

* Implemented  the Commerce faker.

* Added the CommerceFaker to the main faker classes.

* Fixed the test.
  • Loading branch information
mrstebo committed Feb 3, 2019
1 parent ae67270 commit 21a6873
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A .NET port of the Ruby [faker](https://github.com/stympy/faker) gem
- [Faker.ChuckNorris](doc/chuck_norris.md)
- [Faker.Coffee](doc/coffee.md)
- [Faker.Color](doc/color.md)
- [Faker.Commerce](doc/commerce.md)
- [Faker.Company](doc/company.md)
- [Faker.Date](doc/date.md)
- [Faker.DragonBall](doc/dragon_ball.md)
Expand Down
22 changes: 22 additions & 0 deletions doc/commerce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Faker.Commerce

```cs
Faker.Commerce.Color() //=> "lavender"
# Optional arguments max=3, fixedAmount=false
Faker.Commerce.Department() //=> "Grocery, Health & Beauty"
Faker.Commerce.Department(5) //=> "Grocery, Books, Health & Beauty"
Faker.Commerce.Department(2, true) //=> "Books & Tools"
Faker.Commerce.Material() //=> "Steel"
Faker.Commerce.ProductName() //=> "Practical Granite Shirt"
Faker.Commerce.Price() //=> "44.6"
Faker.Commerce.Price(mew Range<double>(0, 10)) //=> "2.18"
# Generate a random promotion code.
# Optional argument digits = 6 for number of random digits in suffix
Faker.Commerce.PromotionCode() //=> "AmazingDeal829102"
Faker.Commerce.PromotionCode(digits = 2) //=> "AmazingPrice57"
```
126 changes: 126 additions & 0 deletions src/FakerDotNet/Data/CommerceData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.Collections.Generic;

namespace FakerDotNet.Data
{
internal static class CommerceData
{
public static readonly IEnumerable<string> Departments = new[]
{
"Books",
"Movies",
"Music",
"Games",
"Electronics",
"Computers",
"Home",
"Garden",
"Tools",
"Grocery",
"Health",
"Beauty",
"Toys",
"Kids",
"Baby",
"Clothing",
"Shoes",
"Jewelry",
"Sports",
"Outdoors",
"Automotive",
"Industrial"
};

public static readonly IEnumerable<string> ProductNames = new[]
{
"Chair",
"Car",
"Computer",
"Gloves",
"Pants",
"Shirt",
"Table",
"Shoes",
"Hat",
"Plate",
"Knife",
"Bottle",
"Coat",
"Lamp",
"Keyboard",
"Bag",
"Bench",
"Clock",
"Watch",
"Wallet"
};

public static readonly IEnumerable<string> ProductAdjectives = new[]
{
"Small",
"Ergonomic",
"Rustic",
"Intelligent",
"Gorgeous",
"Incredible",
"Fantastic",
"Practical",
"Sleek",
"Awesome",
"Enormous",
"Mediocre",
"Synergistic",
"Heavy Duty",
"Lightweight",
"Aerodynamic",
"Durable"
};

public static readonly IEnumerable<string> Materials = new[]
{
"Steel",
"Wooden",
"Concrete",
"Plastic",
"Cotton",
"Granite",
"Rubber",
"Leather",
"Silk",
"Wool",
"Linen",
"Marble",
"Iron",
"Bronze",
"Copper",
"Aluminum",
"Paper"
};

public static readonly IEnumerable<string> PromotionCodeAdjectives = new[]
{
"Amazing",
"Awesome",
"Cool",
"Good",
"Great",
"Incredible",
"Killer",
"Premium",
"Special",
"Stellar",
"Sweet"
};

public static readonly IEnumerable<string> PromotionCodeNouns = new[]
{
"Code",
"Deal",
"Discount",
"Price",
"Promo",
"Promotion",
"Sale",
"Savings"
};
}
}
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Faker
public static IChuckNorrisFaker ChuckNorris { get; } = Container.ChuckNorris;
public static ICoffeeFaker Coffee { get; } = Container.Coffee;
public static IColorFaker Color { get; } = Container.Color;
public static ICommerceFaker Commerce { get; } = Container.Commerce;
public static ICompanyFaker Company { get; } = Container.Company;
public static IDateFaker Date { get; } = Container.Date;
public static IDragonBallFaker DragonBall { get; } = Container.DragonBall;
Expand Down
3 changes: 3 additions & 0 deletions src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal interface IFakerContainer
IChuckNorrisFaker ChuckNorris { get; }
ICoffeeFaker Coffee { get; }
IColorFaker Color { get; }
ICommerceFaker Commerce { get; }
ICompanyFaker Company { get; }
IDateFaker Date { get; }
IDragonBallFaker DragonBall { get; }
Expand Down Expand Up @@ -68,6 +69,7 @@ public FakerContainer()
ChuckNorris = new ChuckNorrisFaker(this);
Coffee = new CoffeeFaker(this);
Color = new ColorFaker(this);
Commerce = new CommerceFaker(this);
Company = new CompanyFaker(this);
Date = new DateFaker();
DragonBall = new DragonBallFaker(this);
Expand Down Expand Up @@ -116,6 +118,7 @@ public FakerContainer()
public IChuckNorrisFaker ChuckNorris { get; }
public ICoffeeFaker Coffee { get; }
public IColorFaker Color { get; }
public ICommerceFaker Commerce { get; }
public ICompanyFaker Company { get; }
public IDateFaker Date { get; }
public IDragonBallFaker DragonBall { get; }
Expand Down
72 changes: 72 additions & 0 deletions src/FakerDotNet/Fakers/CommerceFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Linq;
using System.Text;
using FakerDotNet.Data;

namespace FakerDotNet.Fakers
{
public interface ICommerceFaker
{
string Color();
string Department(int max = 3, bool fixedAmount = false);
string Material();
string ProductName();
string Price(Range<double> range = null);
string PromotionCode(int digits = 6);
}

internal class CommerceFaker : ICommerceFaker
{
private readonly IFakerContainer _fakerContainer;

public CommerceFaker(IFakerContainer fakerContainer)
{
_fakerContainer = fakerContainer;
}

public string Color()
{
return _fakerContainer.Color.ColorName();
}

public string Department(int max = 3, bool fixedAmount = false)
{
var num = fixedAmount ? max : Convert.ToInt32(_fakerContainer.Number.Between(1, max));

if (num <= 1) return _fakerContainer.Random.Element(CommerceData.Departments);

var departments = _fakerContainer.Random.Assortment(CommerceData.Departments, num).ToArray();

return string.Join(" & ",
string.Join(", ", departments.Take(departments.Length - 1)),
departments.Last());
}

public string Material()
{
return _fakerContainer.Random.Element(CommerceData.Materials);
}

public string ProductName()
{
return string.Join(" ",
_fakerContainer.Random.Element(CommerceData.ProductAdjectives),
_fakerContainer.Random.Element(CommerceData.Materials),
_fakerContainer.Random.Element(CommerceData.ProductNames));
}

public string Price(Range<double> range = null)
{
range = range ?? new Range<double>(0, 100);
return _fakerContainer.Number.Between(range.Minimum, range.Maximum).ToString("#.##");
}

public string PromotionCode(int digits = 6)
{
return string.Join("",
_fakerContainer.Random.Element(CommerceData.PromotionCodeAdjectives),
_fakerContainer.Random.Element(CommerceData.PromotionCodeNouns),
_fakerContainer.Number.Number(digits));
}
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public void Color_returns_IColorFaker()
Assert.IsInstanceOf<IColorFaker>(_fakerContainer.Color);
}

[Test]
public void Commerce_returns_ICommerceFaker()
{
Assert.IsInstanceOf<ICommerceFaker>(_fakerContainer.Commerce);
}

[Test]
public void Company_returns_ICompanyFaker()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public void Color_returns_IColorFaker()
Assert.IsInstanceOf<IColorFaker>(Faker.Color);
}

[Test]
public void Commerce_returns_ICommerceFaker()
{
Assert.IsInstanceOf<ICommerceFaker>(Faker.Commerce);
}

[Test]
public void Company_returns_ICompanyFaker()
{
Expand Down
Loading

0 comments on commit 21a6873

Please sign in to comment.