Skip to content

Commit

Permalink
- Introducing Image module
Browse files Browse the repository at this point in the history
- Moving image Url generating from Internet module to Image module
  • Loading branch information
Kubaaa96 committed Nov 8, 2023
1 parent 2b93e95 commit 84979ae
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 60 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ set(FAKER_SOURCES
src/modules/weather/Weather.cpp
src/common/WeatherHelper.cpp
src/modules/airline/Airline.cpp
src/modules/image/Image.cpp
)

set(FAKER_UT_SOURCES
Expand Down Expand Up @@ -84,6 +85,7 @@ set(FAKER_UT_SOURCES
src/modules/weather/WeatherTest.cpp
src/common/WeatherHelperTest.cpp
src/modules/airline/AirlineTest.cpp
src/modules/image/ImageTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})
Expand All @@ -94,7 +96,7 @@ target_include_directories(${LIBRARY_NAME}
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include"
${FMT_INCLUDE_DIR}
)
)

add_subdirectory(externals/fmt)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ target_link_libraries(main faker-cxx)
- 📁 Git - branch names, commit messages, commit hash
- 👨‍💻 Hacker - hacker words
- ✋ Helper - random element from container
- 🌐 Internet - emails, usernames, passwords, images urls, IP, HTTP
- 🌐 Internet - emails, usernames, passwords, IP, HTTP
- 🖼️ Image - images urls, github avatar urls, image dimensions
- 🌍 Location - countries, cities, zip codes, street addresses
- 📚 Lorem - lorem words, sentences, paragraphs
- 🏥 Medicine - conditions, medical tests, specialties
Expand Down
66 changes: 66 additions & 0 deletions include/faker-cxx/Image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <map>
#include <optional>
#include <string>

#include "types/ImageCategory.h"

namespace
{
std::map<faker::ImageCategory, std::string> imageCategoryString = {
{faker::ImageCategory::animals, "animals"}, {faker::ImageCategory::business, "business"},
{faker::ImageCategory::cats, "cats"}, {faker::ImageCategory::city, "city"},
{faker::ImageCategory::food, "food"}, {faker::ImageCategory::nightlife, "nightlife"},
{faker::ImageCategory::fashion, "fashion"}, {faker::ImageCategory::people, "people"},
{faker::ImageCategory::nature, "nature"}, {faker::ImageCategory::sports, "sports"},
{faker::ImageCategory::technics, "technics"}, {faker::ImageCategory::transport, "transport"},
};
}

namespace faker
{
class Image
{
public:
/**
* @brief Generates a real image url with `https://loremflickr.com/`.
*
* @param width The width of the image. Defaults to `640`.
* @param height The height of the image. Defaults to `480`.
* @param category The optional category of generated real 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"
* @endcode
*/
static std::string imageUrl(unsigned width = 640, unsigned height = 480,
std::optional<ImageCategory> category = std::nullopt);

/**
* @brief Generates a random avatar from GitHub.
*
* @returns Url to github avatar.
*
* @code
* Internet::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558"
* @endcode
*/
static std::string githubAvatarUrl();

/**
* @brief Generates a random image dimensions.
*
* @returns Random image dimensions.
*
* @code
* Internet::dimensions() // "1920x1080"
* @endcode
*/
static std::string dimensions();
};
}
26 changes: 0 additions & 26 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,6 @@ class Internet
*/
static std::string password(int length = 15);

/**
* @brief Generates a real image url with `https://source.unsplash.com/`.
*
* @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
* Internet::imageUrl() // "https://source.unsplash.com/640x480"
* Internet::imageUrl(800, 600) // "https://source.unsplash.com/800x600"
* @endcode
*/
static std::string imageUrl(unsigned width = 640, unsigned height = 480);

/**
* @brief Generates a random avatar from GitHub.
*
* @returns Url to github avatar.
*
* @code
* Internet::githubAvatarUrl() // "https://avatars.githubusercontent.com/u/9716558"
* @endcode
*/
static std::string githubAvatarUrl();

/**
* @brief Returns a random emoji.
*
Expand Down
20 changes: 20 additions & 0 deletions include/faker-cxx/types/ImageCategory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

namespace faker
{
enum class ImageCategory
{
animals,
business,
cats,
city,
food,
nightlife,
fashion,
people,
nature,
sports,
technics,
transport
};
}
26 changes: 26 additions & 0 deletions src/modules/image/Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "faker-cxx/Image.h"

#include "faker-cxx/Number.h"
#include "fmt/format.h"

namespace faker
{

std::string Image::imageUrl(unsigned int width, unsigned int height, std::optional<ImageCategory> category)
{
const std::string image_category =
category.has_value() ? fmt::format("/{}", imageCategoryString.at(category.value())) : "";
return fmt::format("https://loremflickr.com/{}/{}{}", width, height, image_category);
}

std::string Image::githubAvatarUrl()
{
return fmt::format("https://avatars.githubusercontent.com/u/{}", Number::integer<int>(100000000));
}

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

}
68 changes: 68 additions & 0 deletions src/modules/image/ImageTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "faker-cxx/Image.h"

#include <string>

#include "gtest/gtest.h"

#include "../src/common/StringHelper.h"

using namespace ::testing;
using namespace faker;

class ImageTest : public Test
{
public:
};

TEST_F(ImageTest, shouldGenerateImageUrlDefault)
{
const auto imageUrl = Image::imageUrl();

ASSERT_EQ(imageUrl, "https://loremflickr.com/640/480");
}

TEST_F(ImageTest, shouldGenerateImageUrl)
{
const auto width = 800;
const auto height = 600;

const auto imageUrl = Image::imageUrl(width, height);

ASSERT_EQ(imageUrl, "https://loremflickr.com/800/600");
}

TEST_F(ImageTest, shouldGenerateImageUrlCategory)
{
const auto width = 800;
const auto height = 600;
const ImageCategory category = ImageCategory::fashion;

const auto imageUrl = Image::imageUrl(width, height, category);

ASSERT_EQ(imageUrl, "https://loremflickr.com/800/600/fashion");
}

TEST_F(ImageTest, shouldGenerateGithubAvatarUrl)
{
const auto githubAvatarUrl = Image::githubAvatarUrl();

const std::string expectedGithubAvatarPrefix = "https://avatars.githubusercontent.com/u/";

const auto userNumber = std::stoi(githubAvatarUrl.substr(expectedGithubAvatarPrefix.size()));

ASSERT_TRUE(githubAvatarUrl.starts_with(expectedGithubAvatarPrefix));
ASSERT_TRUE(userNumber >= 0 && userNumber <= 100000000);
}

TEST_F(ImageTest, shouldGenerateDimensions)
{
const auto dimensions = Image::dimensions();

std::vector<std::string> split_dimensions = StringHelper::split(dimensions, "x");

auto width_dimension = std::stoi(split_dimensions[0]);
ASSERT_TRUE(width_dimension >= 1 && width_dimension <= 32720);

auto height_dimension = std::stoi(split_dimensions[1]);
ASSERT_TRUE(height_dimension >= 1 && height_dimension <= 17280);
}
10 changes: 0 additions & 10 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ std::string Internet::password(int length)
return password;
}

std::string Internet::imageUrl(unsigned int width, unsigned int height)
{
return fmt::format("https://source.unsplash.com/{}x{}", width, height);
}

std::string Internet::githubAvatarUrl()
{
return fmt::format("https://avatars.githubusercontent.com/u/{}", Number::integer<int>(100000000));
}

std::string Internet::emoji(std::optional<EmojiType> type)
{
if (type)
Expand Down
22 changes: 0 additions & 22 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,28 +381,6 @@ TEST_F(InternetTest, shouldGeneratePasswordWithSpecifiedLength)
{ return passwordCharacters.find(passwordCharacter) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGenerateImageUrl)
{
const auto width = 800;
const auto height = 600;

const auto imageUrl = Internet::imageUrl(width, height);

ASSERT_EQ(imageUrl, "https://source.unsplash.com/800x600");
}

TEST_F(InternetTest, shouldGenerateGithubAvatarUrl)
{
const auto githubAvatarUrl = Internet::githubAvatarUrl();

const std::string expectedGithubAvatarPrefix = "https://avatars.githubusercontent.com/u/";

const auto userNumber = std::stoi(githubAvatarUrl.substr(expectedGithubAvatarPrefix.size()));

ASSERT_TRUE(githubAvatarUrl.starts_with(expectedGithubAvatarPrefix));
ASSERT_TRUE(userNumber >= 0 && userNumber <= 100000000);
}

TEST_F(InternetTest, shouldGenerateEmoji)
{
const auto generatedEmoji = Internet::emoji();
Expand Down

0 comments on commit 84979ae

Please sign in to comment.