From 062f435ab1aa62c89d6165b5aed9bd53c1eccefe Mon Sep 17 00:00:00 2001 From: Steven Atkinson Date: Mon, 4 Feb 2019 08:12:34 +0000 Subject: [PATCH] Added the LoremFlickr faker (#113) * Added the documentation. * Implemented the LoremFlickr faker. * Added the LoremFlickrFaker to the main faker classes. --- README.md | 1 + doc/lorem_flickr.md | 46 +++++ src/FakerDotNet/Faker.cs | 1 + src/FakerDotNet/FakerContainer.cs | 3 + src/FakerDotNet/Fakers/LoremFlickrFaker.cs | 81 ++++++++ .../FakerDotNet.Tests/FakerContainerTests.cs | 6 + tests/FakerDotNet.Tests/FakerTests.cs | 6 + .../Fakers/LoremFlickrFakerTests.cs | 189 ++++++++++++++++++ 8 files changed, 333 insertions(+) create mode 100644 doc/lorem_flickr.md create mode 100644 src/FakerDotNet/Fakers/LoremFlickrFaker.cs create mode 100644 tests/FakerDotNet.Tests/Fakers/LoremFlickrFakerTests.cs diff --git a/README.md b/README.md index 3779180..4ebb1f9 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/doc/lorem_flickr.md b/doc/lorem_flickr.md new file mode 100644 index 0000000..c18f9cb --- /dev/null +++ b/doc/lorem_flickr.md @@ -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" +``` diff --git a/src/FakerDotNet/Faker.cs b/src/FakerDotNet/Faker.cs index 3dc83dc..366764e 100644 --- a/src/FakerDotNet/Faker.cs +++ b/src/FakerDotNet/Faker.cs @@ -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; diff --git a/src/FakerDotNet/FakerContainer.cs b/src/FakerDotNet/FakerContainer.cs index 95dd9b8..31298f2 100644 --- a/src/FakerDotNet/FakerContainer.cs +++ b/src/FakerDotNet/FakerContainer.cs @@ -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; } @@ -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); @@ -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; } diff --git a/src/FakerDotNet/Fakers/LoremFlickrFaker.cs b/src/FakerDotNet/Fakers/LoremFlickrFaker.cs new file mode 100644 index 0000000..e30fc41 --- /dev/null +++ b/src/FakerDotNet/Fakers/LoremFlickrFaker.cs @@ -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]+$"); + } + } +} diff --git a/tests/FakerDotNet.Tests/FakerContainerTests.cs b/tests/FakerDotNet.Tests/FakerContainerTests.cs index 79ead36..247fc58 100644 --- a/tests/FakerDotNet.Tests/FakerContainerTests.cs +++ b/tests/FakerDotNet.Tests/FakerContainerTests.cs @@ -183,6 +183,12 @@ public void Lorem_returns_ILoremFaker() Assert.IsInstanceOf(_fakerContainer.Lorem); } + [Test] + public void LoremFlickr_returns_ILoremFlickrFaker() + { + Assert.IsInstanceOf(_fakerContainer.LoremFlickr); + } + [Test] public void Matz_returns_IMatzFaker() { diff --git a/tests/FakerDotNet.Tests/FakerTests.cs b/tests/FakerDotNet.Tests/FakerTests.cs index 15d3b56..8d0368c 100644 --- a/tests/FakerDotNet.Tests/FakerTests.cs +++ b/tests/FakerDotNet.Tests/FakerTests.cs @@ -175,6 +175,12 @@ public void Lorem_returns_ILoremFaker() Assert.IsInstanceOf(Faker.Lorem); } + [Test] + public void LoremFlickr_returns_ILoremFlickrFaker() + { + Assert.IsInstanceOf(Faker.LoremFlickr); + } + [Test] public void Matz_returns_IMatzFaker() { diff --git a/tests/FakerDotNet.Tests/Fakers/LoremFlickrFakerTests.cs b/tests/FakerDotNet.Tests/Fakers/LoremFlickrFakerTests.cs new file mode 100644 index 0000000..ea99710 --- /dev/null +++ b/tests/FakerDotNet.Tests/Fakers/LoremFlickrFakerTests.cs @@ -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(); + _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)); + } + } +}