diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index f1ec5c2e..21dea892 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -46,6 +46,19 @@ class System */ static std::string fileExt(const std::optional& 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. * diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index 563be55b..ca741038 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -76,4 +77,21 @@ const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; +enum class MimeType +{ + Video, + Audio, + Image, + Text, + Application +}; +inline std::string toString(MimeType type) +{ + std::map enumToStringMapping{{MimeType::Video, "video"}, + {MimeType::Audio, "audio"}, + {MimeType::Image, "image"}, + {MimeType::Text, "text"}, + {MimeType::Application, "application"}}; + return enumToStringMapping.at(type); +} } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index dc786c9b..2185f0d5 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -78,6 +78,21 @@ std::string System::fileExt(const std::optional& mimeType) return ""; } +std::string System::fileExt(MimeType mimeType) +{ + const auto mimeTypeName = toString(mimeType); + std::vector 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(extensions); +} std::string System::commonFileName(const std::optional& ext) { diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index d6edc471..83a24eb9 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -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 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 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 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 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 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) {