Skip to content

Commit

Permalink
refactor: image module migrate
Browse files Browse the repository at this point in the history
- image module migration from class to functions within image namespace

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 committed Jun 25, 2024
1 parent f0aec69 commit ee5bbd4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
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

0 comments on commit ee5bbd4

Please sign in to comment.