From 87b1f2862373c0ffd35ecf0fbda95c8b7092912c Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 25 Jun 2024 19:05:44 +0200 Subject: [PATCH] refactor: image module migrate (#737) - image module migration from class to functions within image namespace Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/Image.h | 26 +++++++++++--------------- src/modules/image/Image.cpp | 24 ++++++++++++------------ tests/modules/image/ImageTest.cpp | 27 ++++++++++++++------------- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/include/faker-cxx/Image.h b/include/faker-cxx/Image.h index 9683567b6..f9bc79a94 100644 --- a/include/faker-cxx/Image.h +++ b/include/faker-cxx/Image.h @@ -3,11 +3,8 @@ #include #include -namespace faker +namespace faker::image { -class Image -{ -public: enum class ImageCategory { Animals, @@ -34,12 +31,12 @@ class Image * @returns Random real image url from external service. * * @code - * Image::imageUrl() // "https://loremflickr.com/640/480" - * Image::imageUrl(800, 600) // "https://loremflickr.com/800/600" - * Image::imageUrl(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals" + * image::imageUrl() // "https://loremflickr.com/640/480" + * image::imageUrl(800, 600) // "https://loremflickr.com/800/600" + * image::imageUrl(800, 600, ImageCategory::Animals) // "https://loremflickr.com/800/600/animals" * @endcode */ - static std::string imageUrl(unsigned width = 640, unsigned height = 480, + std::string imageUrl(unsigned width = 640, unsigned height = 480, std::optional category = std::nullopt); /** @@ -48,10 +45,10 @@ class Image * @returns Url to github avatar. * * @code - * Image::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558" + * image::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558" * @endcode */ - static std::string githubAvatarUrl(); + std::string githubAvatarUrl(); /** * @brief Generates a random image dimensions. @@ -59,10 +56,10 @@ class Image * @returns Random image dimensions. * * @code - * Image::dimensions() // "1920x1080" + * image::dimensions() // "1920x1080" * @endcode */ - static std::string dimensions(); + std::string dimensions(); /** * @brief Generates a random type of image. @@ -70,8 +67,7 @@ class Image * @returns Type of image. * * @code - * Image::type() // "png" + * image::type() // "png" */ - static std::string_view type(); -}; + std::string_view type(); } diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index 00d9e4ca1..0bda7f9e4 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -10,24 +10,24 @@ #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" -namespace faker +namespace faker::image { namespace { const std::array imageTypes = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg", "pdf", "png", "psd", "raw", "svg", "tiff", "webp"}; -std::unordered_map imageCategoryToLoremFlickrStringMapping = { - {Image::ImageCategory::Animals, "animals"}, {Image::ImageCategory::Business, "business"}, - {Image::ImageCategory::Cats, "cats"}, {Image::ImageCategory::City, "city"}, - {Image::ImageCategory::Food, "food"}, {Image::ImageCategory::Nightlife, "nightlife"}, - {Image::ImageCategory::Fashion, "fashion"}, {Image::ImageCategory::People, "people"}, - {Image::ImageCategory::Nature, "nature"}, {Image::ImageCategory::Sports, "sports"}, - {Image::ImageCategory::Technics, "technics"}, {Image::ImageCategory::Transport, "transport"}, +std::unordered_map imageCategoryToLoremFlickrStringMapping = { + {ImageCategory::Animals, "animals"}, {ImageCategory::Business, "business"}, + {ImageCategory::Cats, "cats"}, {ImageCategory::City, "city"}, + {ImageCategory::Food, "food"}, {ImageCategory::Nightlife, "nightlife"}, + {ImageCategory::Fashion, "fashion"}, {ImageCategory::People, "people"}, + {ImageCategory::Nature, "nature"}, {ImageCategory::Sports, "sports"}, + {ImageCategory::Technics, "technics"}, {ImageCategory::Transport, "transport"}, }; } -std::string Image::imageUrl(unsigned int width, unsigned int height, std::optional category) +std::string imageUrl(unsigned int width, unsigned int height, std::optional category) { const std::string image_category = category.has_value() ? @@ -37,17 +37,17 @@ std::string Image::imageUrl(unsigned int width, unsigned int height, std::option return FormatHelper::format("https://loremflickr.com/{}/{}{}", width, height, image_category); } -std::string Image::githubAvatarUrl() +std::string githubAvatarUrl() { return FormatHelper::format("https://avatars.githubusercontent.com/u/{}", number::integer(100000000)); } -std::string Image::dimensions() +std::string dimensions() { return FormatHelper::format("{}x{}", number::integer(1, 32720), number::integer(1, 17280)); } -std::string_view Image::type() +std::string_view type() { return Helper::arrayElement(imageTypes); } diff --git a/tests/modules/image/ImageTest.cpp b/tests/modules/image/ImageTest.cpp index 87b61d2bc..0e7101c8f 100644 --- a/tests/modules/image/ImageTest.cpp +++ b/tests/modules/image/ImageTest.cpp @@ -11,6 +11,7 @@ using namespace ::testing; using namespace faker; +using namespace faker::image; class ImageTest : public Test { @@ -19,9 +20,9 @@ class ImageTest : public Test TEST_F(ImageTest, shouldGenerateImageUrlDefault) { - const auto imageUrl = Image::imageUrl(); + const auto generatedImageUrl = imageUrl(); - ASSERT_EQ(imageUrl, "https://loremflickr.com/640/480"); + ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/640/480"); } TEST_F(ImageTest, shouldGenerateImageUrl) @@ -29,39 +30,39 @@ TEST_F(ImageTest, shouldGenerateImageUrl) const auto width = 800; const auto height = 600; - const auto imageUrl = Image::imageUrl(width, height); + const auto generatedImageUrl = imageUrl(width, height); - ASSERT_EQ(imageUrl, "https://loremflickr.com/800/600"); + ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600"); } TEST_F(ImageTest, shouldGenerateImageUrlCategory) { const auto width = 800; const auto height = 600; - const auto category = Image::ImageCategory::Fashion; + const auto category = ImageCategory::Fashion; - const auto imageUrl = Image::imageUrl(width, height, category); + const auto generatedImageUrl = imageUrl(width, height, category); - ASSERT_EQ(imageUrl, "https://loremflickr.com/800/600/fashion"); + ASSERT_EQ(generatedImageUrl, "https://loremflickr.com/800/600/fashion"); } TEST_F(ImageTest, shouldGenerateGithubAvatarUrl) { - const auto githubAvatarUrl = Image::githubAvatarUrl(); + const auto generatedGithubAvatarUrl = githubAvatarUrl(); const std::string expectedGithubAvatarPrefix = "https://avatars.githubusercontent.com/u/"; - const auto userNumber = std::stoi(githubAvatarUrl.substr(expectedGithubAvatarPrefix.size())); + const auto userNumber = std::stoi(generatedGithubAvatarUrl.substr(expectedGithubAvatarPrefix.size())); - ASSERT_TRUE(githubAvatarUrl.starts_with(expectedGithubAvatarPrefix)); + ASSERT_TRUE(generatedGithubAvatarUrl.starts_with(expectedGithubAvatarPrefix)); ASSERT_TRUE(userNumber >= 0 && userNumber <= 100000000); } TEST_F(ImageTest, shouldGenerateDimensions) { - const auto dimensions = Image::dimensions(); + const auto generatedDimensions = dimensions(); - const auto split_dimensions = StringHelper::split(dimensions, "x"); + const auto split_dimensions = StringHelper::split(generatedDimensions, "x"); const auto width_dimension = std::stoi(split_dimensions[0]); @@ -77,7 +78,7 @@ TEST_F(ImageTest, shouldGenerateType) const std::array imageTypes = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg", "pdf", "png", "psd", "raw", "svg", "tiff", "webp"}; - const auto generatedType = Image::type(); + const auto generatedType = type(); ASSERT_TRUE(std::ranges::any_of(imageTypes, [generatedType](const std::string_view& type) { return type == generatedType; }));