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

create fileExt function overload with MimeType enum as parameter #235

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions include/faker-cxx/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class System
*/
static std::string fileExt(const std::optional<std::string>& mimeType = std::nullopt);
cieslarmichal marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Returns a file extension.
*
* @param mimeType value of MimeType enum.
*
* @returns A file extension.
*
* @code
* System::fileExt(MimeType::Image) // "png"
* @endcode
*/
static std::string fileExt(MimeType mimeType);
cieslarmichal marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a random file name with a given extension or a commonly used extension.
*
Expand Down
18 changes: 18 additions & 0 deletions include/faker-cxx/types/MimeTypes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -76,4 +77,21 @@ const std::vector<std::string> commonMimeTypes = {"application/pdf", "audio/mpeg

const std::vector<std::string> commonFileTypes = {"video", "audio", "image", "text", "application"};

enum class MimeType
{
Video,
Audio,
Image,
Text,
Application
};
inline std::string toString(MimeType type)
{
std::map<MimeType, std::string> enumToStringMapping{{MimeType::Video, "video"},
{MimeType::Audio, "audio"},
{MimeType::Image, "image"},
{MimeType::Text, "text"},
{MimeType::Application, "application"}};
return enumToStringMapping.at(type);
}
}
15 changes: 15 additions & 0 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ std::string System::fileExt(const std::optional<std::string>& mimeType)

return "";
}
std::string System::fileExt(MimeType mimeType)
{
const auto mimeTypeName = toString(mimeType);
std::vector<std::string> extensions;
for (const auto& mime : mimeTypes)
{
size_t pos = mime.find_first_of('/');
const auto mt = mime.substr(0, pos);
if (mimeTypeName == mt)
{
extensions.push_back(mime.substr(pos + 1));
}
}
return Helper::arrayElement<std::string>(extensions);
}

std::string System::commonFileName(const std::optional<std::string>& ext)
{
Expand Down
71 changes: 71 additions & 0 deletions src/modules/system/SystemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,77 @@ TEST_F(SystemTest, FileExtTestWithMimeType)
EXPECT_EQ(System::fileExt("text/html"), "html");
}

TEST_F(SystemTest, FileExtTestWithMimeTypeEnum)
{
auto image = MimeType::Image;
auto audio = MimeType::Audio;
auto video = MimeType::Video;
auto text = MimeType::Text;
auto application = MimeType::Application;

std::vector<std::string> imageExtensions;
for (const auto& mimeType : mimeTypes)
{
size_t pos = mimeType.find_first_of('/');
const auto ext = mimeType.substr(0, pos);
if (ext == toString(image))
{
imageExtensions.push_back(mimeType.substr(pos + 1));
}
}
std::vector<std::string> audioExtensions;
for (const auto& mimeType : mimeTypes)
{
size_t pos = mimeType.find_first_of('/');
const auto ext = mimeType.substr(0, pos);
if (ext == toString(audio))
{
audioExtensions.push_back(mimeType.substr(pos + 1));
}
}
std::vector<std::string> videoExtensions;
for (const auto& mimeType : mimeTypes)
{
size_t pos = mimeType.find_first_of('/');
const auto ext = mimeType.substr(0, pos);
if (ext == toString(video))
{
videoExtensions.push_back(mimeType.substr(pos + 1));
}
}
std::vector<std::string> textExtensions;
for (const auto& mimeType : mimeTypes)
{
size_t pos = mimeType.find_first_of('/');
const auto ext = mimeType.substr(0, pos);
if (ext == toString(text))
{
textExtensions.push_back(mimeType.substr(pos + 1));
}
}
std::vector<std::string> applicationExtensions;
for (const auto& mimeType : mimeTypes)
{
size_t pos = mimeType.find_first_of('/');
const auto ext = mimeType.substr(0, pos);
if (ext == toString(application))
{
applicationExtensions.push_back(mimeType.substr(pos + 1));
}
}
auto imageExt = System::fileExt(image);
auto audioExt = System::fileExt(audio);
auto videoExt = System::fileExt(video);
auto textExt = System::fileExt(text);
auto applicationExt = System::fileExt(application);

EXPECT_TRUE(std::ranges::find(imageExtensions, imageExt) != imageExtensions.end());
EXPECT_TRUE(std::ranges::find(audioExtensions, audioExt) != audioExtensions.end());
EXPECT_TRUE(std::ranges::find(videoExtensions, videoExt) != videoExtensions.end());
EXPECT_TRUE(std::ranges::find(textExtensions, textExt) != textExtensions.end());
EXPECT_TRUE(std::ranges::find(applicationExtensions, applicationExt) != applicationExtensions.end());
}

TEST_F(SystemTest, CommonFileNameWithEmptyExtensionTest)
{

Expand Down