From bccddbe2538332925544e0277b9bef0cbf9755b7 Mon Sep 17 00:00:00 2001 From: PlungedInCode Date: Sun, 4 Feb 2024 21:09:15 +0300 Subject: [PATCH 1/3] Added image type functionality --- include/faker-cxx/Image.h | 20 +++++++++++++++----- src/modules/image/Image.cpp | 6 ++++++ src/modules/image/ImageTest.cpp | 9 +++++++++ src/modules/image/data/Type.h | 10 ++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/modules/image/data/Type.h diff --git a/include/faker-cxx/Image.h b/include/faker-cxx/Image.h index a9617f755..21dd3f555 100644 --- a/include/faker-cxx/Image.h +++ b/include/faker-cxx/Image.h @@ -21,9 +21,9 @@ class Image * @returns Random real image url from external service. * * @code - * Internet::imageUrl() // "https://loremflickr.com/640/480" - * Internet::imageUrl(800, 600) // "https://loremflickr.com/800/600" - * Internet::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, @@ -35,7 +35,7 @@ class Image * @returns Url to github avatar. * * @code - * Internet::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558" + * Image::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558" * @endcode */ static std::string githubAvatarUrl(); @@ -46,9 +46,19 @@ class Image * @returns Random image dimensions. * * @code - * Internet::dimensions() // "1920x1080" + * Image::dimensions() // "1920x1080" * @endcode */ static std::string dimensions(); + + /** + * @brief Generates a random type of image. + * + * @returns Type of image. + * + * @code + * Image::type() // "png" + */ + static std::string type(); }; } diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index bd5e0f6ec..27ac2bf04 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -1,6 +1,8 @@ #include "faker-cxx/Image.h" #include "../../common/FormatHelper.h" +#include "data/Type.h" +#include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" namespace faker @@ -37,4 +39,8 @@ std::string Image::dimensions() return FormatHelper::format("{}x{}", Number::integer(1, 32720), Number::integer(1, 17280)); } +std::string Image::type() +{ + return Helper::arrayElement(image_types); +} } diff --git a/src/modules/image/ImageTest.cpp b/src/modules/image/ImageTest.cpp index 86bf9bf6e..f8c3b2b50 100644 --- a/src/modules/image/ImageTest.cpp +++ b/src/modules/image/ImageTest.cpp @@ -5,6 +5,7 @@ #include "gtest/gtest.h" #include "../src/common/StringHelper.h" +#include "data/Type.h" using namespace ::testing; using namespace faker; @@ -66,3 +67,11 @@ TEST_F(ImageTest, shouldGenerateDimensions) auto height_dimension = std::stoi(split_dimensions[1]); ASSERT_TRUE(height_dimension >= 1 && height_dimension <= 17280); } + +TEST_F(ImageTest, shouldGenerateType) +{ + const auto generatedType = Image::type(); + + ASSERT_TRUE(std::any_of(image_types.begin(), image_types.end(), + [generatedType](const std::string& type) { return type == generatedType; })); +} \ No newline at end of file diff --git a/src/modules/image/data/Type.h b/src/modules/image/data/Type.h new file mode 100644 index 000000000..ebc6fca9f --- /dev/null +++ b/src/modules/image/data/Type.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +namespace faker +{ +const std::vector image_types = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg", + "pdf", "png", "psd", "raw", "svg", "tiff", "webp"}; +} \ No newline at end of file From 1d6239d15df2d27c3e276ca3a39f4782875e1580 Mon Sep 17 00:00:00 2001 From: PlungedInCode Date: Sun, 4 Feb 2024 21:55:32 +0300 Subject: [PATCH 2/3] Added image type functionality --- src/modules/image/Image.cpp | 2 +- src/modules/image/ImageTest.cpp | 2 +- src/modules/image/data/Type.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index 27ac2bf04..691b63d4b 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -41,6 +41,6 @@ std::string Image::dimensions() std::string Image::type() { - return Helper::arrayElement(image_types); + return Helper::arrayElement(imageTypes); } } diff --git a/src/modules/image/ImageTest.cpp b/src/modules/image/ImageTest.cpp index f8c3b2b50..ad91e8693 100644 --- a/src/modules/image/ImageTest.cpp +++ b/src/modules/image/ImageTest.cpp @@ -72,6 +72,6 @@ TEST_F(ImageTest, shouldGenerateType) { const auto generatedType = Image::type(); - ASSERT_TRUE(std::any_of(image_types.begin(), image_types.end(), + ASSERT_TRUE(std::any_of(imageTypes.begin(), imageTypes.end(), [generatedType](const std::string& type) { return type == generatedType; })); } \ No newline at end of file diff --git a/src/modules/image/data/Type.h b/src/modules/image/data/Type.h index ebc6fca9f..c9dd7f444 100644 --- a/src/modules/image/data/Type.h +++ b/src/modules/image/data/Type.h @@ -5,6 +5,6 @@ namespace faker { -const std::vector image_types = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg", +const std::vector imageTypes = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg", "pdf", "png", "psd", "raw", "svg", "tiff", "webp"}; } \ No newline at end of file From 800757f411faee59529a3dd2254686398ae8e19b Mon Sep 17 00:00:00 2001 From: PlungedInCode Date: Mon, 5 Feb 2024 08:44:09 +0300 Subject: [PATCH 3/3] Header added --- src/modules/image/ImageTest.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/image/ImageTest.cpp b/src/modules/image/ImageTest.cpp index ad91e8693..ae4eb975a 100644 --- a/src/modules/image/ImageTest.cpp +++ b/src/modules/image/ImageTest.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/Image.h" #include +#include #include "gtest/gtest.h" @@ -72,6 +73,6 @@ TEST_F(ImageTest, shouldGenerateType) { const auto generatedType = Image::type(); - ASSERT_TRUE(std::any_of(imageTypes.begin(), imageTypes.end(), - [generatedType](const std::string& type) { return type == generatedType; })); + ASSERT_TRUE(std::ranges::any_of(imageTypes, [generatedType](const std::string& type) + { return type == generatedType; })); } \ No newline at end of file