Skip to content

Commit

Permalink
create fileExt overload for MimeType enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Goutham-AR committed Nov 11, 2023
1 parent 6d3782e commit 3f83453
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
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);

/**
* @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);

/**
* 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

0 comments on commit 3f83453

Please sign in to comment.