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

Added image type functionality #500

Merged
merged 3 commits into from
Feb 5, 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
20 changes: 15 additions & 5 deletions include/faker-cxx/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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();
};
}
6 changes: 6 additions & 0 deletions src/modules/image/Image.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,4 +39,8 @@ std::string Image::dimensions()
return FormatHelper::format("{}x{}", Number::integer<int>(1, 32720), Number::integer<int>(1, 17280));
}

std::string Image::type()
{
return Helper::arrayElement(imageTypes);
}
}
10 changes: 10 additions & 0 deletions src/modules/image/ImageTest.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "faker-cxx/Image.h"

#include <string>
#include <algorithm>

#include "gtest/gtest.h"

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

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -66,3 +68,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::ranges::any_of(imageTypes, [generatedType](const std::string& type)
{ return type == generatedType; }));
}
10 changes: 10 additions & 0 deletions src/modules/image/data/Type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>
#include <vector>

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