Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: image module migrate #737

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions include/faker-cxx/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#include <optional>
#include <string_view>

namespace faker
namespace faker::image
{
class Image
{
public:
enum class ImageCategory
{
Animals,
Expand All @@ -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<ImageCategory> category = std::nullopt);

/**
Expand All @@ -48,30 +45,29 @@ 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.
*
* @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.
*
* @returns Type of image.
*
* @code
* Image::type() // "png"
* image::type() // "png"
*/
static std::string_view type();
};
std::string_view type();
}
24 changes: 12 additions & 12 deletions src/modules/image/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"

namespace faker
namespace faker::image
{
namespace
{
const std::array<std::string_view, 15> imageTypes = {"ai", "bmp", "eps", "gif", "heif", "indd", "jpeg", "jpg",
"pdf", "png", "psd", "raw", "svg", "tiff", "webp"};

std::unordered_map<Image::ImageCategory, std::string> 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<ImageCategory, std::string> 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<ImageCategory> category)
std::string imageUrl(unsigned int width, unsigned int height, std::optional<ImageCategory> category)
{
const std::string image_category =
category.has_value() ?
Expand All @@ -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<int>(100000000));
}

std::string Image::dimensions()
std::string dimensions()
{
return FormatHelper::format("{}x{}", number::integer<int>(1, 32720), number::integer<int>(1, 17280));
}

std::string_view Image::type()
std::string_view type()
{
return Helper::arrayElement(imageTypes);
}
Expand Down
27 changes: 14 additions & 13 deletions tests/modules/image/ImageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using namespace ::testing;
using namespace faker;
using namespace faker::image;

class ImageTest : public Test
{
Expand All @@ -19,49 +20,49 @@ 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)
{
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]);

Expand All @@ -77,7 +78,7 @@ TEST_F(ImageTest, shouldGenerateType)
const std::array<std::string_view, 15> 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; }));
Expand Down
Loading