Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Placeholdit faker #107

Merged
merged 3 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ A .NET port of the Ruby [faker](https://github.com/stympy/faker) gem
- [Faker.Name](doc/name.md)
- [Faker.Number](doc/number.md)
- [Faker.PhoneNumber](doc/phone_number.md)
- [Faker.Placeholdit](doc/placeholdit.md)
- [Faker.Pokemon](doc/pokemon.md)
- [Faker.Random](doc/random.md)
- [Faker.RickAndMorty](doc/rick_and_morty.md)
Expand Down
19 changes: 19 additions & 0 deletions doc/placeholdit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Faker.Placeholdit

```cs
Faker.Placeholdit.Image() //=> "https://placehold.it/300x300.png"

Faker.Placeholdit.Image("50x50") //=> "https://placehold.it/50x50.png"

Faker.Placeholdit.Image("50x50", "jpg") //=> "https://placehold.it/50x50.jpg"

Faker.Placeholdit.Image("50x50", "gif", "ffffff") //=> "https://placehold.it/50x50.gif/ffffff"

Faker.Placeholdit.Image("50x50", "jpeg", RandomColor.Value) //=> "https://placehold.it/50x50.jpeg/39eba7"

Faker.Placeholdit.Image("50x50", "jpeg", "ffffff", "000") //=> "https://placehold.it/50x50.jpeg/ffffff/000"

Faker.Placeholdit.Image("50x50", "jpeg", RandomColor.Value, RandomColor.Value) //=> "https://placehold.it/50x50.jpeg/d39e44/888ba7"

Faker.Placeholdit.Image("50x50", "jpg", "ffffff", "000", "Some Custom Text") //=> "https://placehold.it/50x50.jpg/ffffff/000?text=Some Custom Text"
```
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static class Faker
public static INameFaker Name { get; } = Container.Name;
public static INumberFaker Number { get; } = Container.Number;
public static IPhoneNumberFaker PhoneNumber { get; } = Container.PhoneNumber;
public static IPlaceholditFaker Placeholdit { get; } = Container.Placeholdit;
public static IPokemonFaker Pokemon { get; } = Container.Pokemon;
public static IRandomFaker Random { get; } = Container.Random;
public static IRickAndMortyFaker RickAndMorty { get; } = Container.RickAndMorty;
Expand Down
3 changes: 3 additions & 0 deletions src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal interface IFakerContainer
INameFaker Name { get; }
INumberFaker Number { get; }
IPhoneNumberFaker PhoneNumber { get; }
IPlaceholditFaker Placeholdit { get; }
IPokemonFaker Pokemon { get; }
IRandomFaker Random { get; }
IRickAndMortyFaker RickAndMorty { get; }
Expand Down Expand Up @@ -79,6 +80,7 @@ public FakerContainer()
Name = new NameFaker(this);
Number = new NumberFaker();
PhoneNumber = new PhoneNumberFaker(this);
Placeholdit = new PlaceholditFaker(this);
Pokemon = new PokemonFaker(this);
Random = new RandomFaker();
RickAndMorty = new RickAndMortyFaker(this);
Expand Down Expand Up @@ -122,6 +124,7 @@ public FakerContainer()
public INameFaker Name { get; }
public INumberFaker Number { get; }
public IPhoneNumberFaker PhoneNumber { get; }
public IPlaceholditFaker Placeholdit { get; }
public IPokemonFaker Pokemon { get; }
public IRandomFaker Random { get; }
public IRickAndMortyFaker RickAndMorty { get; }
Expand Down
90 changes: 90 additions & 0 deletions src/FakerDotNet/Fakers/PlaceholditFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace FakerDotNet.Fakers
{
public interface IPlaceholditFaker
{
string Image(
string size = "300x300",
string format = "png",
string backgroundColor = "",
string textColor = "",
string text = ""
);
}

internal class PlaceholditFaker : IPlaceholditFaker
{
internal static readonly IEnumerable<string> SupportedFormats = new[]
{
"png",
"jpg",
"gif",
"jpeg"
};

private readonly IFakerContainer _fakerContainer;

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

public string Image(
string size = "300x300",
string format = "png",
string backgroundColor = "",
string textColor = "",
string text = ""
)
{
backgroundColor = backgroundColor == RandomColor.Value ? GenerateColor() : backgroundColor;
textColor = textColor == RandomColor.Value ? GenerateColor() : textColor;

if (!IsValidSize(size))
throw new ArgumentException("Size should be specified in format 300x300", nameof(size));

if (!IsSupportedFormat(format))
throw new ArgumentException($"Supported formats are {string.Join(", ", SupportedFormats)}", nameof(format));

if (!IsValidColor(backgroundColor))
throw new ArgumentException("backgroundColor must be a hex value without '#'", nameof(backgroundColor));

if (!IsValidColor(textColor))
throw new ArgumentException("textColor must be a hex value without '#'", nameof(textColor));

return string.Join("",
"https://placehold.it",
$"/{size}.{format}",
string.IsNullOrEmpty(backgroundColor) ? "" : $"/{backgroundColor}",
string.IsNullOrEmpty(textColor) ? "" : $"/{textColor}",
string.IsNullOrEmpty(text) ? "" : $"?text={text}"
);
}

private string GenerateColor()
{
return _fakerContainer.Color.HexColor().Substring(1);
}

private static bool IsValidSize(string size)
{
return Regex.IsMatch(size, "^[0-9]+x[0-9]+$");
}

private static bool IsSupportedFormat(string format)
{
return SupportedFormats.Contains(format);
}

private static bool IsValidColor(string color)
{
return string.IsNullOrEmpty(color) ||
Regex.IsMatch(color, @"^(?:[0-9a-f]{3}$)|(?:[0-9a-f]{6}$)$", RegexOptions.IgnoreCase);
}
}
}

7 changes: 7 additions & 0 deletions src/FakerDotNet/RandomColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FakerDotNet
{
public static class RandomColor
{
public const string Value = "RANDOM";
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public void PhoneNumber_returns_IPhoneNumberFaker()
Assert.IsInstanceOf<IPhoneNumberFaker>(_fakerContainer.PhoneNumber);
}

[Test]
public void Placeholdit_returns_IPlaceholditFaker()
{
Assert.IsInstanceOf<IPlaceholditFaker>(_fakerContainer.Placeholdit);
}

[Test]
public void Pokemon_returns_IPokemonFaker()
{
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 @@ -175,6 +175,12 @@ public void PhoneNumber_returns_IPhoneNumberFaker()
Assert.IsInstanceOf<IPhoneNumberFaker>(Faker.PhoneNumber);
}

[Test]
public void Placeholdit_returns_IPlaceholditFaker()
{
Assert.IsInstanceOf<IPlaceholditFaker>(Faker.Placeholdit);
}

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

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

private IFakerContainer _fakerContainer;
private IPlaceholditFaker _placeholditFaker;

[Test]
public void Image_returns_an_image_url()
{
Assert.AreEqual(
"https://placehold.it/300x300.png",
_placeholditFaker.Image());
}

[Test]
public void Image_returns_an_image_url_with_the_specified_size()
{
Assert.AreEqual(
"https://placehold.it/50x50.png",
_placeholditFaker.Image("50x50"));
}

[Test]
public void Image_throws_ArgumentException_when_size_is_not_in_a_valid_format()
{
var ex = Assert.Throws<ArgumentException>(() =>
_placeholditFaker.Image("ABCxDEF"));

Assert.That(ex.Message.StartsWith("Size should be specified in format 300x300"));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_format()
{
Assert.AreEqual(
"https://placehold.it/50x50.png",
_placeholditFaker.Image("50x50", "png"));
Assert.AreEqual(
"https://placehold.it/50x50.jpg",
_placeholditFaker.Image("50x50", "jpg"));
Assert.AreEqual(
"https://placehold.it/50x50.gif",
_placeholditFaker.Image("50x50", "gif"));
Assert.AreEqual(
"https://placehold.it/50x50.jpeg",
_placeholditFaker.Image("50x50", "jpeg"));
}

[Test]
public void Image_throws_ArgumentException_when_format_is_not_supported()
{
var supportedFormats = string.Join(", ", PlaceholditFaker.SupportedFormats);

var ex = Assert.Throws<ArgumentException>(() =>
_placeholditFaker.Image("50x50", "bmp"));

Assert.That(ex.Message.StartsWith($"Supported formats are {supportedFormats}"));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_background_color()
{
Assert.AreEqual(
"https://placehold.it/50x50.gif/ffffff",
_placeholditFaker.Image("50x50", "gif", "ffffff"));
}

[Test]
public void Image_returns_an_image_url_with_a_random_background_color()
{
A.CallTo(() => _fakerContainer.Color.HexColor())
.Returns("#39eba7");

Assert.AreEqual(
"https://placehold.it/50x50.jpeg/39eba7",
_placeholditFaker.Image("50x50", "jpeg", RandomColor.Value));
}

[Test]
public void Image_throws_ArgumentException_when_background_color_contains_hash()
{
var ex = Assert.Throws<ArgumentException>(() =>
_placeholditFaker.Image("50x50", "gif", "#000"));

Assert.That(ex.Message.StartsWith("backgroundColor must be a hex value without '#'"));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_text_color()
{
Assert.AreEqual(
"https://placehold.it/50x50.jpeg/ffffff/000",
_placeholditFaker.Image("50x50", "jpeg", "ffffff", "000"));
}

[Test]
public void Image_returns_an_image_url_with_a_random_text_color()
{
A.CallTo(() => _fakerContainer.Color.HexColor())
.ReturnsNextFromSequence("#d39e44", "#888ba7");

Assert.AreEqual(
"https://placehold.it/50x50.jpeg/d39e44/888ba7",
_placeholditFaker.Image("50x50", "jpeg", RandomColor.Value, RandomColor.Value));
}

[Test]
public void Image_throws_ArgumentException_when_text_color_contains_hash()
{
var ex = Assert.Throws<ArgumentException>(() =>
_placeholditFaker.Image("50x50", "gif", "ffffff", "#000"));

Assert.That(ex.Message.StartsWith("textColor must be a hex value without '#'"));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_text()
{
Assert.AreEqual(
"https://placehold.it/50x50.jpg/ffffff/000?text=Some Custom Text",
_placeholditFaker.Image("50x50", "jpg", "ffffff", "000", "Some Custom Text"));
}
}
}