Skip to content

Commit

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

* Implemented the LoremFlickr faker.

* Added the LoremFlickrFaker to the main faker classes.
  • Loading branch information
mrstebo authored Feb 4, 2019
1 parent d22a8f0 commit 062f435
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 0 deletions.
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.Internet](doc/internet.md)
- [Faker.LordOfTheRings](doc/lord_of_the_rings.md)
- [Faker.Lorem](doc/lorem.md)
- [Faker.LoremFlickr](doc/lorem_flickr.md)
- [Faker.Matz](doc/matz.md)
- [Faker.Music](doc/music.md)
- [Faker.Name](doc/name.md)
Expand Down
46 changes: 46 additions & 0 deletions doc/lorem_flickr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Faker.LoremFlickr


```cs
Faker.LoremFlickr.Image() //=> "http://loremflickr.Com/300/300"
Faker.LoremFlickr.Image("50x60") //=> "http://loremflickr.Com/50/60"
Faker.LoremFlickr.Image("50x60", new[] {"sports"}) //=> "http://loremflickr.Com/50/60/sports"
Faker.LoremFlickr.Image("50x60", new[] {"sports", "fitness"}) //=> "http://loremflickr.Com/50/60/sports,fitness"
Faker.LoremFlickr.Image("50x60", new[] {"sports", "fitness"}, true) //=> "http://loremflickr.Com/50/60/sports,fitness/all"
Faker.LoremFlickr.GrayscaleImage() //=> "http://loremflickr.Com/g/300/300/all"
Faker.LoremFlickr.GrayscaleImage("50x60") //=> "http://loremflickr.Com/g/50/60/all"
Faker.LoremFlickr.GrayscaleImage("50x60", new[] {"sports"}) //=> "http://loremflickr.Com/g/50/60/sports"
Faker.LoremFlickr.GrayscaleImage("50x60", new[] {"sports", "fitness"}) //=> "http://loremflickr.Com/g/50/60/sports,fitness"
Faker.LoremFlickr.GrayscaleImage("50x60", new[] {"sports", "fitness"}, true) //=> "http://loremflickr.Com/g/50/60/sports,fitness/all"
Faker.LoremFlickr.PixelatedImage() //=> "http://loremflickr.Com/p/300/300/all"
Faker.LoremFlickr.PixelatedImage("50x60") //=> "http://loremflickr.Com/p/50/60/all"
Faker.LoremFlickr.PixelatedImage("50x60", new[] {"sports"}) //=> "http://loremflickr.Com/p/50/60/sports"
Faker.LoremFlickr.PixelatedImage("50x60", new[] {"sports", "fitness"}) //=> "http://loremflickr.Com/p/50/60/sports,fitness"
Faker.LoremFlickr.PixelatedImage("50x60", new[] {"sports", "fitness"}, true) //=> "http://loremflickr.Com/p/50/60/sports,fitness/all"
Faker.LoremFlickr.ColorizedImage() //=> "http://loremflickr.Com/red/300/300/all"
Faker.LoremFlickr.ColorizedImage("50x60") //=> "http://loremflickr.Com/red/50/60/all"
Faker.LoremFlickr.ColorizedImage("50x60", 'red') //=> "http://loremflickr.Com/red/50/60/all"
Faker.LoremFlickr.ColorizedImage("50x60", 'red', new[] {"sports"}) //=> "http://loremflickr.Com/red/50/60/sports"
Faker.LoremFlickr.ColorizedImage("50x60", 'red', new[] {"sports", "fitness"}) //=> "http://loremflickr.Com/red/50/60/sports,fitness"
Faker.LoremFlickr.ColorizedImage("50x60", 'red', new[] {"sports", "fitness"}, true) //=> "http://loremflickr.Com/red/50/60/sports,fitness/all"
```
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 IInternetFaker Internet { get; } = Container.Internet;
public static ILordOfTheRingsFaker LordOfTheRings { get; } = Container.LordOfTheRings;
public static ILoremFaker Lorem { get; } = Container.Lorem;
public static ILoremFlickrFaker LoremFlickr { get; } = Container.LoremFlickr;
public static IMatzFaker Matz { get; } = Container.Matz;
public static IMusicFaker Music { get; } = Container.Music;
public static INameFaker Name { get; } = Container.Name;
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
IInternetFaker Internet { get; }
ILordOfTheRingsFaker LordOfTheRings { get; }
ILoremFaker Lorem { get; }
ILoremFlickrFaker LoremFlickr { get; }
IMatzFaker Matz { get; }
IMusicFaker Music { get; }
INameFaker Name { get; }
Expand Down Expand Up @@ -87,6 +88,7 @@ public FakerContainer()
Internet = new InternetFaker(this);
LordOfTheRings = new LordOfTheRingsFaker(this);
Lorem = new LoremFaker(this);
LoremFlickr = new LoremFlickrFaker(this);
Matz = new MatzFaker(this);
Music = new MusicFaker(this);
Name = new NameFaker(this);
Expand Down Expand Up @@ -138,6 +140,7 @@ public FakerContainer()
public IInternetFaker Internet { get; }
public ILordOfTheRingsFaker LordOfTheRings { get; }
public ILoremFaker Lorem { get; }
public ILoremFlickrFaker LoremFlickr { get; }
public IMatzFaker Matz { get; }
public IMusicFaker Music { get; }
public INameFaker Name { get; }
Expand Down
81 changes: 81 additions & 0 deletions src/FakerDotNet/Fakers/LoremFlickrFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace FakerDotNet.Fakers
{
public interface ILoremFlickrFaker
{
string Image(
string size = "300x300",
string[] searchTerms = null,
bool matchAll = false);

string GrayscaleImage(
string size = "300x300",
string[] searchTerms = null,
bool matchAll = false);

string PixelatedImage(
string size = "300x300",
string[] searchTerms = null,
bool matchAll = false);

string ColorizedImage(
string size = "300x300",
string color = "red",
string[] searchTerms = null,
bool matchAll = false);
}

internal class LoremFlickrFaker : ILoremFlickrFaker
{
private readonly IFakerContainer _fakerContainer;

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

public string Image(string size = "300x300", string[] searchTerms = null, bool matchAll = false)
{
return BuildUrl(size, null, searchTerms, matchAll);
}

public string GrayscaleImage(string size = "300x300", string[] searchTerms = null, bool matchAll = false)
{
return BuildUrl(size, "g", searchTerms, matchAll);
}

public string PixelatedImage(string size = "300x300", string[] searchTerms = null, bool matchAll = false)
{
return BuildUrl(size, "p", searchTerms, matchAll);
}

public string ColorizedImage(string size = "300x300", string color = "red", string[] searchTerms = null,
bool matchAll = false)
{
return BuildUrl(size, color, searchTerms, matchAll);
}

private static string BuildUrl(string size, string format, string[] searchTerms, bool matchAll)
{
if (!IsValidSize(size))
throw new ArgumentException("Size should be specified in format 300x300", nameof(size));

return string.Join("/", new[]
{
"https://loremflickr.com",
format,
string.Join("/", size.Split('x')),
string.Join(",", searchTerms ?? new string[] { }),
matchAll ? "all" : null
}.Where(x => !string.IsNullOrEmpty(x)));
}

private static bool IsValidSize(string size)
{
return Regex.IsMatch(size, "^[0-9]+x[0-9]+$");
}
}
}
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 Lorem_returns_ILoremFaker()
Assert.IsInstanceOf<ILoremFaker>(_fakerContainer.Lorem);
}

[Test]
public void LoremFlickr_returns_ILoremFlickrFaker()
{
Assert.IsInstanceOf<ILoremFlickrFaker>(_fakerContainer.LoremFlickr);
}

[Test]
public void Matz_returns_IMatzFaker()
{
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 Lorem_returns_ILoremFaker()
Assert.IsInstanceOf<ILoremFaker>(Faker.Lorem);
}

[Test]
public void LoremFlickr_returns_ILoremFlickrFaker()
{
Assert.IsInstanceOf<ILoremFlickrFaker>(Faker.LoremFlickr);
}

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

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

private IFakerContainer _fakerContainer;
private ILoremFlickrFaker _loremflickrFaker;

[Test]
public void Image_returns_an_image_url()
{
Assert.AreEqual(
"https://loremflickr.com/300/300",
_loremflickrFaker.Image());
}

[Test]
public void Image_returns_an_image_url_with_the_specified_size()
{
Assert.AreEqual(
"https://loremflickr.com/50/60",
_loremflickrFaker.Image("50x60"));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_search_term()
{
Assert.AreEqual(
"https://loremflickr.com/50/60/sports",
_loremflickrFaker.Image("50x60", new[] {"sports"}));
}

[Test]
public void Image_returns_an_image_url_with_the_specified_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/50/60/sports,fitness",
_loremflickrFaker.Image("50x60", new[] {"sports", "fitness"}));
}

[Test]
public void Image_returns_an_image_url_that_specifies_it_should_match_all_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/50/60/sports,fitness/all",
_loremflickrFaker.Image("50x60", new[] {"sports", "fitness"}, true));
}

[Test]
public void GrayscaleImage_returns_a_grayscale_image_url()
{
Assert.AreEqual(
"https://loremflickr.com/g/300/300",
_loremflickrFaker.GrayscaleImage());
}

[Test]
public void GrayscaleImage_returns_a_grayscale_image_url_with_the_specified_size()
{
Assert.AreEqual(
"https://loremflickr.com/g/50/60",
_loremflickrFaker.GrayscaleImage("50x60"));
}

[Test]
public void GrayscaleImage_returns_a_grayscale_image_url_with_the_specified_search_term()
{
Assert.AreEqual(
"https://loremflickr.com/g/50/60/sports",
_loremflickrFaker.GrayscaleImage("50x60", new[] {"sports"}));
}

[Test]
public void GrayscaleImage_returns_a_grayscale_image_url_with_the_specified_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/g/50/60/sports,fitness",
_loremflickrFaker.GrayscaleImage("50x60", new[] {"sports", "fitness"}));
}

[Test]
public void GrayscaleImage_returns_a_grayscale_image_url_that_specifies_it_should_match_all_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/g/50/60/sports,fitness/all",
_loremflickrFaker.GrayscaleImage("50x60", new[] {"sports", "fitness"}, true));
}

[Test]
public void PixelatedImage_returns_a_pixelated_image_url()
{
Assert.AreEqual(
"https://loremflickr.com/p/300/300",
_loremflickrFaker.PixelatedImage());
}

[Test]
public void PixelatedImage_returns_a_pixelated_image_url_with_the_specified_size()
{
Assert.AreEqual(
"https://loremflickr.com/p/50/60",
_loremflickrFaker.PixelatedImage("50x60"));
}

[Test]
public void PixelatedImage_returns_a_pixelated_image_url_with_the_specified_search_term()
{
Assert.AreEqual(
"https://loremflickr.com/p/50/60/sports",
_loremflickrFaker.PixelatedImage("50x60", new[] {"sports"}));
}

[Test]
public void PixelatedImage_returns_a_pixelated_image_url_with_the_specified_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/p/50/60/sports,fitness",
_loremflickrFaker.PixelatedImage("50x60", new[] {"sports", "fitness"}));
}

[Test]
public void PixelatedImage_returns_a_pixelated_image_url_that_specifies_it_should_match_all_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/p/50/60/sports,fitness/all",
_loremflickrFaker.PixelatedImage("50x60", new[] {"sports", "fitness"}, true));
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url()
{
Assert.AreEqual(
"https://loremflickr.com/red/300/300",
_loremflickrFaker.ColorizedImage());
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url_with_the_specified_size()
{
Assert.AreEqual(
"https://loremflickr.com/red/50/60",
_loremflickrFaker.ColorizedImage("50x60"));
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url_with_the_specified_color()
{
Assert.AreEqual(
"https://loremflickr.com/red/50/60",
_loremflickrFaker.ColorizedImage("50x60", "red"));
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url_with_the_specified_search_term()
{
Assert.AreEqual(
"https://loremflickr.com/red/50/60/sports",
_loremflickrFaker.ColorizedImage("50x60", "red", new[] {"sports"}));
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url_with_the_specified_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/red/50/60/sports,fitness",
_loremflickrFaker.ColorizedImage("50x60", "red", new[] {"sports", "fitness"}));
}

[Test]
public void ColorizedImage_returns_a_colorized_image_url_that_specifies_it_should_match_all_search_terms()
{
Assert.AreEqual(
"https://loremflickr.com/red/50/60/sports,fitness/all",
_loremflickrFaker.ColorizedImage("50x60", "red", new[] {"sports", "fitness"}, true));
}
}
}

0 comments on commit 062f435

Please sign in to comment.