-
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.
* Added the documentation. * Implemented the LoremPixel faker. * Added the LoremPixelFaker to the main faker classes.
- Loading branch information
Showing
8 changed files
with
266 additions
and
0 deletions.
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,17 @@ | ||
# Faker.LoremPixel | ||
|
||
```cs | ||
Faker.LoremPixel.Image() //=> "https://lorempixel.Com/300/300" | ||
Faker.LoremPixel.Image("50x60") //=> "https://lorempixel.Com/50/60" | ||
Faker.LoremPixel.Image("50x60", true) //=> "https://lorempixel.Com/g/50/60" | ||
Faker.LoremPixel.Image("50x60", false, "sports") //=> "https://lorempixel.Com/50/60/sports" | ||
Faker.LoremPixel.Image("50x60", false, "sports", 3) //=> "https://lorempixel.Com/50/60/sports/3" | ||
Faker.LoremPixel.Image("50x60", false, "sports", 3, "Dummy-text") //=> "https://lorempixel.Com/50/60/sports/3/Dummy-text" | ||
Faker.LoremPixel.Image("50x60", false, "sports", null, "Dummy-text") //=> "https://lorempixel.Com/50/60/sports/Dummy-text" | ||
``` |
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,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FakerDotNet.Fakers | ||
{ | ||
public interface ILoremPixelFaker | ||
{ | ||
string Image( | ||
string size = "300x300", | ||
bool isGray = false, | ||
string category = null, | ||
int? number = null, | ||
string text = null); | ||
} | ||
|
||
internal class LoremPixelFaker : ILoremPixelFaker | ||
{ | ||
internal static readonly IEnumerable<string> SupportedCategories = new[] | ||
{ | ||
"abstract", | ||
"animals", | ||
"business", | ||
"cats", | ||
"city", | ||
"food", | ||
"nightlife", | ||
"fashion", | ||
"people", | ||
"nature", | ||
"sports", | ||
"technics", | ||
"transport" | ||
}; | ||
|
||
private readonly IFakerContainer _fakerContainer; | ||
|
||
public LoremPixelFaker(IFakerContainer fakerContainer) | ||
{ | ||
_fakerContainer = fakerContainer; | ||
} | ||
|
||
public string Image( | ||
string size = "300x300", | ||
bool isGray = false, | ||
string category = null, | ||
int? number = null, | ||
string text = null) | ||
{ | ||
if (!IsValidSize(size)) | ||
throw new ArgumentException("Size should be specified in format 300x300", nameof(size)); | ||
|
||
if (!IsSupportedCategory(category)) | ||
throw new ArgumentException($"Supported categories are {string.Join(", ", SupportedCategories)}", | ||
nameof(category)); | ||
|
||
if (!IsValidNumber(number)) | ||
throw new ArgumentException("Number must be between 1 and 10", nameof(number)); | ||
|
||
if (!IsValidCategoryNumber(category, number)) | ||
throw new ArgumentException("Category required when number is passed", nameof(number)); | ||
|
||
if (!IsValidCategoryText(category, text)) | ||
throw new ArgumentException("Category and number must be passed when text is passed", nameof(text)); | ||
|
||
return string.Join("/", new[] | ||
{ | ||
"https://lorempixel.com", | ||
isGray ? "g" : "", | ||
string.Join("/", size.Split('x')), | ||
category, | ||
Convert.ToString(number), | ||
text | ||
}.Where(x => !string.IsNullOrEmpty(x))); | ||
} | ||
|
||
private static bool IsValidSize(string size) | ||
{ | ||
return Regex.IsMatch(size, "^[0-9]+x[0-9]+$"); | ||
} | ||
|
||
private static bool IsSupportedCategory(string category) | ||
{ | ||
return string.IsNullOrEmpty(category) || SupportedCategories.Contains(category); | ||
} | ||
|
||
private static bool IsValidNumber(int? number) | ||
{ | ||
var n = number.GetValueOrDefault(1); | ||
return n >= 1 && n <= 10; | ||
} | ||
|
||
private static bool IsValidCategoryNumber(string category, int? number) | ||
{ | ||
return !number.HasValue || !string.IsNullOrEmpty(category); | ||
} | ||
|
||
private static bool IsValidCategoryText(string category, string text) | ||
{ | ||
return string.IsNullOrEmpty(text) || !string.IsNullOrEmpty(category); | ||
} | ||
} | ||
} |
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,128 @@ | ||
using System; | ||
using FakeItEasy; | ||
using FakerDotNet.Fakers; | ||
using NUnit.Framework; | ||
|
||
namespace FakerDotNet.Tests.Fakers | ||
{ | ||
[TestFixture] | ||
[Parallelizable] | ||
public class LoremPixelFakerTests | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_fakerContainer = A.Fake<IFakerContainer>(); | ||
_lorempixelFaker = new LoremPixelFaker(_fakerContainer); | ||
} | ||
|
||
private IFakerContainer _fakerContainer; | ||
private ILoremPixelFaker _lorempixelFaker; | ||
|
||
[Test] | ||
public void Image_returns_an_image_url() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/300/300", | ||
_lorempixelFaker.Image()); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_an_image_url_with_the_specified_size() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/50/60", | ||
_lorempixelFaker.Image("50x60")); | ||
} | ||
|
||
[Test] | ||
public void Image_throws_ArgumentException_when_size_is_not_in_a_valid_format() | ||
{ | ||
var ex = Assert.Throws<ArgumentException>(() => | ||
_lorempixelFaker.Image("ABCxDEF")); | ||
|
||
Assert.That(ex.Message.StartsWith("Size should be specified in format 300x300")); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_a_gray_image_url_when_is_gray_is_specified() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/g/50/60", | ||
_lorempixelFaker.Image("50x60", true)); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_an_image_url_with_the_specified_category() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/g/50/60/sports", | ||
_lorempixelFaker.Image("50x60", true, "sports")); | ||
} | ||
|
||
[Test] | ||
public void Image_throws_ArgumentException_when_format_is_not_supported() | ||
{ | ||
var supportedCategories = string.Join(", ", LoremPixelFaker.SupportedCategories); | ||
|
||
var ex = Assert.Throws<ArgumentException>(() => | ||
_lorempixelFaker.Image("50x50", false, "bad")); | ||
|
||
Assert.That(ex.Message.StartsWith($"Supported categories are {supportedCategories}")); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_an_image_url_with_the_specified_number() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/g/50/60/sports/3", | ||
_lorempixelFaker.Image("50x60", true, "sports", 3)); | ||
} | ||
|
||
[Test] | ||
[TestCase(-1)] | ||
[TestCase(0)] | ||
[TestCase(11)] | ||
public void Image_throws_ArgumentException_when_number_is_not_in_valid_range(int number) | ||
{ | ||
var ex = Assert.Throws<ArgumentException>(() => | ||
_lorempixelFaker.Image("50x50", false, "sports", number)); | ||
|
||
Assert.That(ex.Message.StartsWith("Number must be between 1 and 10")); | ||
} | ||
|
||
[Test] | ||
public void Image_throws_ArgumentException_when_number_is_not_supplied_with_category() | ||
{ | ||
var ex = Assert.Throws<ArgumentException>(() => | ||
_lorempixelFaker.Image("50x50", false, null, 3)); | ||
|
||
Assert.That(ex.Message.StartsWith("Category required when number is passed")); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_an_image_url_with_the_specified_text() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/g/50/60/sports/3/Dummy-text", | ||
_lorempixelFaker.Image("50x60", true, "sports", 3, "Dummy-text")); | ||
} | ||
|
||
[Test] | ||
public void Image_returns_an_image_url_with_the_specified_category_and_text() | ||
{ | ||
Assert.AreEqual( | ||
"https://lorempixel.com/g/50/60/sports/Dummy-text", | ||
_lorempixelFaker.Image("50x60", true, "sports", null, "Dummy-text")); | ||
} | ||
|
||
[Test] | ||
public void Image_throw_ArgumentException_when_category_and_number_is_not_supplied_with_text() | ||
{ | ||
var ex = Assert.Throws<ArgumentException>(() => | ||
_lorempixelFaker.Image("50x50", false, null, null, "Dummy-text")); | ||
|
||
Assert.That(ex.Message.StartsWith("Category and number must be passed when text is passed")); | ||
} | ||
} | ||
} |