-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
1 deletion.
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,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] | ||
``` |
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,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" | ||
}; | ||
} | ||
} |
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
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(); | ||
} | ||
} | ||
} |
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
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()); | ||
} | ||
} | ||
} |