Skip to content

Commit

Permalink
Added image type functionality (#500)
Browse files Browse the repository at this point in the history
* Added image type functionality

* Added image type functionality

* Header added
  • Loading branch information
PlungedInCode committed Feb 5, 2024
1 parent 447b3a9 commit c3b53f1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
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"};
}

0 comments on commit c3b53f1

Please sign in to comment.