-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Moving image Url generating from Internet module to Image module
- Loading branch information
Showing
9 changed files
with
185 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters