From 9b7d709354a80d5bae094b6210aab44360b955ff Mon Sep 17 00:00:00 2001 From: bitalec <75757163+bitalec@users.noreply.github.com> Date: Mon, 25 Nov 2024 21:08:11 +0100 Subject: [PATCH] fix: fix imageUrl in Image module to generate images from picsum photos #990 (#998) * create method urlPicsumPhotos for generate random image from https://picsum.photos and changed the name of urlImage in urlLoremFlickr * created imageUrl: Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos" * created imageUrl: Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos" --- include/faker-cxx/image.h | 46 ++++++++++++++++++++++--- src/modules/image.cpp | 36 ++++++++++++++++++- tests/modules/image_test.cpp | 67 +++++++++++++++++++++++++++++++++--- 3 files changed, 139 insertions(+), 10 deletions(-) diff --git a/include/faker-cxx/image.h b/include/faker-cxx/image.h index fec89eef1..1ad84ac74 100644 --- a/include/faker-cxx/image.h +++ b/include/faker-cxx/image.h @@ -24,6 +24,20 @@ enum class ImageCategory Transport }; +/** + * @brief Generates a random image url with `https://loremflickr.com/` or "https://picsum.photos". + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * + * @returns Random real image url from external service. + * + * @code + * faker::image::imageUrl() // "https://loremflickr.com/640/480" or "https://picsum.photos/640/480" + * @endcode + */ +FAKER_CXX_EXPORT std::string imageUrl(unsigned width = 640, unsigned height = 480); + /** * @brief Generates a real image url with `https://loremflickr.com/`. * @@ -34,13 +48,34 @@ enum class ImageCategory * @returns Random real image url from external service. * * @code - * faker::image::imageUrl() // "https://loremflickr.com/640/480" - * faker::image::imageUrl(800, 600) // "https://loremflickr.com/800/600" - * faker::image::imageUrl(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals" + * faker::image::urlLoremFlickr() // "https://loremflickr.com/640/480" + * faker::image::urlLoremFlickr(800, 600) // "https://loremflickr.com/800/600" + * faker::image::urlLoremFlickr(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals" * @endcode */ -FAKER_CXX_EXPORT std::string imageUrl(unsigned width = 640, unsigned height = 480, - std::optional category = std::nullopt); +FAKER_CXX_EXPORT std::string urlLoremFlickr(unsigned width = 640, unsigned height = 480, + std::optional category = std::nullopt); + +/** + * @brief Generates a real image url with "https://picsum.photos" . + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param greyscale The optional greyscale filter. True for apply or false for disabled + * @param blur The optional blur effect between 1 and 10 + * + * @returns Random real image url from external service + * + * @code + * faker::image::urlPicsPhotos() // "https://picsum.photos/640/480" + * faker::image::urlPicsPhotos(800,600,true) // "https://picsum.photos/800/600?grayscale + * faker::image::urlPicsPhotos(800,600,false,5) // "https://picsum.photos/800/600?blur=5 + * faker::image::urlPicsPhotos(800,600,true,1) // "https://picsum.photos/800/600?grayscale&blur=1 + *@endcode + */ +FAKER_CXX_EXPORT std::string urlPicsumPhotos(unsigned width = 640, unsigned height = 480, + std::optional greyscale = std::nullopt, + std::optional blur = std::nullopt); /** * @brief Generates a random avatar from GitHub. @@ -71,6 +106,7 @@ FAKER_CXX_EXPORT std::string dimensions(); * * @code * faker::image::type() // "png" + * @endcode */ FAKER_CXX_EXPORT std::string_view type(); } diff --git a/src/modules/image.cpp b/src/modules/image.cpp index 41700e5b3..cce0c5d8d 100644 --- a/src/modules/image.cpp +++ b/src/modules/image.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/image.h" #include +#include #include #include #include @@ -25,7 +26,17 @@ std::unordered_map imageCategoryToLoremFlickrStringM }; } -std::string imageUrl(unsigned int width, unsigned int height, std::optional category) +std::string imageUrl(unsigned width, unsigned height) +{ + if (faker::datatype::boolean(0.5)) + { + return urlLoremFlickr(); + } + else + return urlPicsumPhotos(); +} + +std::string urlLoremFlickr(unsigned int width, unsigned int height, std::optional category) { const std::string image_category = category.has_value() ? common::format("/{}", imageCategoryToLoremFlickrStringMapping.at(category.value())) : ""; @@ -33,6 +44,29 @@ std::string imageUrl(unsigned int width, unsigned int height, std::optional greyscale, + const std::optional blur) +{ + std::string url = common::format("https://picsum.photos/{}/{}", width, height); + std::string params; + + params += (greyscale.has_value() && greyscale.value()) ? "greyscale" : ""; + + if (blur.has_value()) + { + if (blur.value() < 1 || blur.value() > 10) + return common::format("{}", "Error: blur must be between 1 and 10."); + if (!params.empty()) + params += "&"; + params += common::format("blur={}", blur.value()); + } + if (!params.empty()) + { + url = common::format("{}?{}", url, params); + } + return common::format("{}", url); +} + std::string githubAvatarUrl() { return common::format("https://avatars.githubusercontent.com/u/{}", number::integer(100000000)); diff --git a/tests/modules/image_test.cpp b/tests/modules/image_test.cpp index d89ca210a..0ebdcd39c 100644 --- a/tests/modules/image_test.cpp +++ b/tests/modules/image_test.cpp @@ -17,10 +17,17 @@ class ImageTest : public Test public: }; -TEST_F(ImageTest, shouldGenerateImageUrlDefault) +TEST_F(ImageTest, shouldGenerateRandomImageUrl) { const auto generatedImageUrl = imageUrl(); + ASSERT_TRUE(generatedImageUrl == urlLoremFlickr() || generatedImageUrl == urlPicsumPhotos()); +} + +TEST_F(ImageTest, shouldGenerateurlLoremFlickrDefault) +{ + const auto generatedImageUrl = urlLoremFlickr(); + ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/640/480"); } @@ -29,22 +36,74 @@ TEST_F(ImageTest, shouldGenerateImageUrl) const auto width = 800; const auto height = 600; - const auto generatedImageUrl = imageUrl(width, height); + const auto generatedImageUrl = urlLoremFlickr(width, height); ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600"); } -TEST_F(ImageTest, shouldGenerateImageUrlCategory) +TEST_F(ImageTest, shouldGenerateurlLoremFlickrCategory) { const auto width = 800; const auto height = 600; const auto category = ImageCategory::Fashion; - const auto generatedImageUrl = imageUrl(width, height, category); + const auto generatedImageUrl = urlLoremFlickr(width, height, category); ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600/fashion"); } +TEST_F(ImageTest, shouldGenerateImageUrlPicsumDefault) +{ + const auto generatedImageUrl = urlPicsumPhotos(); + + ASSERT_EQ(generatedImageUrl, "https://picsum.photos/640/480"); +} + +TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotos) +{ + const auto width = 800; + const auto height = 600; + + const auto generatedImageUrl = urlPicsumPhotos(width, height); + + ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600"); +} + +TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosGreyscale) +{ + const auto width = 800; + const auto height = 600; + const auto greyscale = true; + + const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale); + + ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?greyscale"); +} + +TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosBlur) +{ + const auto width = 800; + const auto height = 600; + const auto greyscale = false; + const auto blur = 5; + + const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale, blur); + + ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?blur=5"); +} + +TEST_F(ImageTest, shouldGenerateImageUrlPicsumPhotosBlurandGreyscale) +{ + const auto width = 800; + const auto height = 600; + const auto greyscale = true; + const auto blur = 6; + + const auto generatedImageUrl = urlPicsumPhotos(width, height, greyscale, blur); + + ASSERT_EQ(generatedImageUrl, "https://picsum.photos/800/600?greyscale&blur=6"); +} + TEST_F(ImageTest, shouldGenerateGithubAvatarUrl) { const auto generatedGithubAvatarUrl = githubAvatarUrl();