Skip to content

Commit

Permalink
Merge 19ddb60 into fd0a458
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstebo committed Oct 24, 2018
2 parents fd0a458 + 19ddb60 commit c22173c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Contents
- [Usage](#usage)
- [Faker.Address](doc/address.md)
- [Faker.App](doc/app.md)
- [Faker.Beer](doc/beer.md)
- [Faker.Beer](doc/beer.md)
- [Faker.Book](doc/book.md)
- [Faker.Boolean](doc/boolean.md)
- [Faker.Business](doc/business.md)
- [Faker.Color](doc/color.md)
- [Faker.Company](doc/company.md)
- [Faker.Date](doc/date.md)
- [Faker.Fake](doc/fake.md)
Expand Down
13 changes: 13 additions & 0 deletions doc/color.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Faker.Color

```cs
Faker.Color.HexColor() //=> "#31a785"
Faker.Color.ColorName() //=> "yellow"
Faker.Color.RgbColor() //=> [54, 233, 67]
Faker.Color.HslColor() //=> [69.87, 0.66, 0.3]
Faker.Color.HslaColor() //=> [154.77, 0.36, 0.9, 0.26170574657729073]
```
42 changes: 42 additions & 0 deletions src/FakerDotNet/Data/ColorData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;

namespace FakerDotNet.Data
{
internal static class ColorData
{
public static readonly IEnumerable<string> Names = new[]
{
"red",
"green",
"blue",
"yellow",
"purple",
"mint green",
"teal",
"white",
"black",
"orange",
"pink",
"grey",
"maroon",
"violet",
"turquoise",
"tan",
"sky blue",
"salmon",
"plum",
"orchid",
"olive",
"magenta",
"lime",
"ivory",
"indigo",
"gold",
"fuchsia",
"cyan",
"azure",
"lavender",
"silver"
};
}
}
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class Faker
public static IBookFaker Book { get; } = Container.Book;
public static IBooleanFaker Boolean { get; } = Container.Boolean;
public static IBusinessFaker Business { get; } = Container.Business;
public static IColorFaker Color { get; } = Container.Color;
public static ICompanyFaker Company { get; } = Container.Company;
public static IDateFaker Date { get; } = Container.Date;
public static IFakeFaker Fake { get; } = Container.Fake;
Expand Down
3 changes: 3 additions & 0 deletions src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal interface IFakerContainer
IBookFaker Book { get; }
IBooleanFaker Boolean { get; }
IBusinessFaker Business { get; }
IColorFaker Color { get; }
ICompanyFaker Company { get; }
IDateFaker Date { get; }
IFakeFaker Fake { get; }
Expand Down Expand Up @@ -38,6 +39,7 @@ public FakerContainer()
Book = new BookFaker(this);
Boolean = new BooleanFaker();
Business = new BusinessFaker(this);
Color = new ColorFaker(this);
Company = new CompanyFaker(this);
Date = new DateFaker();
Fake = new FakeFaker(this);
Expand All @@ -62,6 +64,7 @@ public FakerContainer()
public IBookFaker Book { get; }
public IBooleanFaker Boolean { get; }
public IBusinessFaker Business { get; }
public IColorFaker Color { get; }
public ICompanyFaker Company { get; }
public IDateFaker Date { get; }
public IFakeFaker Fake { get; }
Expand Down
58 changes: 58 additions & 0 deletions src/FakerDotNet/Fakers/ColorFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.Linq;
using FakerDotNet.Data;

namespace FakerDotNet.Fakers
{
public interface IColorFaker
{
string HexColor();
string ColorName();
IEnumerable<byte> RgbColor();
IEnumerable<double> HslColor();
IEnumerable<double> HslaColor();
}

internal class ColorFaker : IColorFaker
{
private readonly IFakerContainer _fakerContainer;

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

public string HexColor()
{
var n = (int) _fakerContainer.Number.Between(0, 0xffffff);
return $"#{n:x}".PadLeft(6, '0');
}

public string ColorName()
{
return _fakerContainer.Random.Element(ColorData.Names);
}

public IEnumerable<byte> RgbColor()
{
return Enumerable.Range(0, 3)
.Select(_ => (byte) _fakerContainer.Number.Between(0, 255))
.ToArray();
}

public IEnumerable<double> HslColor()
{
return Enumerable.Range(0, 3)
.Select(_ => _fakerContainer.Number.Between(0, 255))
.ToArray();
}

public IEnumerable<double> HslaColor()
{
return Enumerable.Range(0, 3)
.Select(_ => _fakerContainer.Number.Between(0, 255))
.Concat(new[] {_fakerContainer.Number.Between(0, 1)})
.ToArray();
}
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public void Business_returns_IBusinessFaker()
Assert.IsInstanceOf<IBusinessFaker>(_fakerContainer.Business);
}

[Test]
public void Color_returns_IColorFaker()
{
Assert.IsInstanceOf<IColorFaker>(_fakerContainer.Color);
}

[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 @@ -43,6 +43,12 @@ public void Business_returns_IBusinessFaker()
Assert.IsInstanceOf<IBusinessFaker>(Faker.Business);
}

[Test]
public void Color_returns_IColorFaker()
{
Assert.IsInstanceOf<IColorFaker>(Faker.Color);
}

[Test]
public void Company_returns_ICompanyFaker()
{
Expand Down
69 changes: 69 additions & 0 deletions tests/FakerDotNet.Tests/Fakers/ColorFakerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using FakeItEasy;
using FakerDotNet.Data;
using FakerDotNet.Fakers;
using NUnit.Framework;

namespace FakerDotNet.Tests.Fakers
{
[TestFixture]
[Parallelizable]
public class ColorFakerTests
{
[SetUp]
public void SetUp()
{
_fakerContainer = A.Fake<IFakerContainer>();
_colorFaker = new ColorFaker(_fakerContainer);
}

private IFakerContainer _fakerContainer;
private IColorFaker _colorFaker;

[Test]
public void HexColor_returns_a_hex_color_string()
{
A.CallTo(() => _fakerContainer.Number.Between(0, 0xffffff))
.Returns(3254149);

Assert.AreEqual("#31a785", _colorFaker.HexColor());
}

[Test]
public void ColorName_returns_a_color_name()
{
A.CallTo(() => _fakerContainer.Random.Element(ColorData.Names))
.Returns("yellow");

Assert.AreEqual("yellow", _colorFaker.ColorName());
}

[Test]
public void RgbColor_returns_a_rgb_color()
{
A.CallTo(() => _fakerContainer.Number.Between(0, 255))
.ReturnsNextFromSequence(54, 233, 67);

CollectionAssert.AreEqual(new byte[] {54, 233, 67}, _colorFaker.RgbColor());
}

[Test]
public void HslColor_returns_a_hsl_color()
{
A.CallTo(() => _fakerContainer.Number.Between(0, 255))
.ReturnsNextFromSequence(69.87, 0.66, 0.3);

CollectionAssert.AreEqual(new[] {69.87, 0.66, 0.3}, _colorFaker.HslColor());
}

[Test]
public void HslaColor_returns_a_hsla_color()
{
A.CallTo(() => _fakerContainer.Number.Between(0, 255))
.ReturnsNextFromSequence(154.77, 0.36, 0.9);
A.CallTo(() => _fakerContainer.Number.Between(0, 1))
.Returns(0.26170574657729073);

CollectionAssert.AreEqual(new[] {154.77, 0.36, 0.9, 0.26170574657729073}, _colorFaker.HslaColor());
}
}
}

0 comments on commit c22173c

Please sign in to comment.