From 5de4244e24b86567d286cf6f59d90d788882a6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Mon, 24 Jun 2024 20:18:50 +0100 Subject: [PATCH] refactor: change system class to namespace (#724) * refactor: change system class to namespace * fix: fix tests --- CHANGELOG.md | 5 +- include/faker-cxx/System.h | 364 +++++++++++++--------------- src/modules/internet/Internet.cpp | 10 +- src/modules/system/System.cpp | 123 ++++------ src/modules/system/SystemData.h | 49 +++- tests/modules/system/SystemTest.cpp | 116 +++++---- 6 files changed, 338 insertions(+), 329 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87cc82490..b351f4285 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,12 +26,13 @@ All notable changes to this project will be documented in this file * removed `weather.uvIndex` method, use `number.integer` instead * removed `weather.humidity` method, use `number.integer` instead * removed `weather.cloudCover` method, use `number.integer` instead -* changed classes to namespaces for functions grouping, for example use `number::integer` instead of `Number::integer`, applies to all modules +* changed classes to namespaces for functions grouping, for example use `number::integer` instead of `Number::integer`, + applies to all modules * changed std::string to std::string_view in functions where is was possible * changed function name from `sport` to `sportName` in sport module * changed function name from `vehicle` to `vehicleName` in vehicle module * changed function name from `timezone` to `timezoneRandom` in date module - +* deleted function `commonFileType` from `System` module, use `system.fileType` instead ### Features diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index 1c2247de4..9efb266df 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -2,8 +2,9 @@ #include #include +#include -namespace faker +namespace faker::system { struct FileOptions { @@ -37,192 +38,177 @@ struct NetworkInterfaceOptions std::optional interfaceSchema; }; -class System -{ -public: - /** - * @brief Returns a random file name with extension. - * - * @param options An option struct. - * - * @returns Random file name with extension. - * - * @code - * System::fileName() // "injustice.mpeg" - * - * FileOptions options - * options.extensionCount = 3 - * System::fileName(options) // "transformation.wav.mpeg.mp4" - * - * options.extensionRange.min = 1; - * options.extensionRange.max = 3; - * System::fileName(options) // "sparkle.png.pdf" - * @endcode - */ - static std::string fileName(const FileOptions& options = {}); - - /** - * @brief Returns a file extension. - * - * @param mimeType value of MimeType enum. - * - * @returns A file extension. - * - * @code - * System::fileExtension(MimeType::Image) // "png" - * @endcode - */ - static std::string fileExtension(const std::optional& mimeType = std::nullopt); - - /** - * Returns a random file name with a given extension or a commonly used extension. - * - * @param ext Optional extension parameter. - * - * @returns A random file name with a given extension or a commonly used extension. - * - * @code - * System::commonFileName() // "dollar.jpg" - * System::commonFileName("txt") // "global_borders_wyoming.txt" - * @endcode - */ - static std::string commonFileName(const std::optional& ext = std::nullopt); - - /** - * Returns a commonly used file extension. - * - * @returns A commonly used file extension. - * - * @code - * System::commonFileExtension() // "gif" - * @endcode - */ - static std::string commonFileExtension(); - - /** - * Returns a mime-type. - * - * @returns A mime-type. - * - * @code - * System::mimeType() // "video/vnd.vivo" - * @endcode - */ - static std::string mimeType(); - - /** - * Returns a commonly used file type. - * - * @returns A commonly used file type. - * - * @code - * System::commonFileType() // "audio" - * @endcode - */ - static std::string commonFileType(); - - /** - * Returns a commonly used file type. - * - * @returns A commonly used file type. - * - * @code - * System::fileType() // "image" - * @endcode - */ - static std::string fileType(); - - /** - * Returns a directory path. - * - * @returns A directory path. - * - * @code - * System::directoryPath() // "/etc/mail" - * @endcode - */ - static std::string directoryPath(); - - /** - * Returns a file path. - * - * @returns A file path. - * - * @code - * System::filePath() // "/usr/local/src/money.dotx" - * @endcode - */ - static std::string filePath(); - - /** - * Returns a semantic version. - * - * @returns A semantic version. - * - * @code - * System::semver() // "1.1.2" - * @endcode - */ - static std::string semver(); - - /** - * Returns a random network interface. - * - * @param options The options to use. Defaults to an empty options structure @see NetworkInterfaceOptions.h. - * @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`. - * @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`. - * - * @returns A random network interface. - * - * @code - * System::networkInterface() // "enp2s7f8" - * - * NetworkInterfaceOptions options; - * options.interfaceType = "wl"; - * System::networkInterface(options) // "wlsf4d2" - * - * NetworkInterfaceOptions options; - * options.interfaceSchema = "mac"; - * System::networkInterface(options) // "enxd17705ed394f" - * - * NetworkInterfaceOptions options; - * options.interfaceType = "en"; - * options.interfaceSchema = "pci"; - * System::networkInterface(options) // "enp1s9f1d2" - * @endcode - */ - static std::string networkInterface(const std::optional& options = {}); - - /** - * Returns a random cron expression. - * - * @param options The options to use. Defaults to an empty options structure @see CronOptions.h. - * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`. - * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the - * generated expression. Defaults to `false`. - * - * @returns A random cron expression. - * - * @code - * system.cron() // "22 * ? * ?" - * - * CronOptions options - * options.includeYear = true - * std::string cronExpr = System::cron(options) // "16 14 * 11 2 2038" - * - * CronOptions options - * options.includeYear = false - * std::string cronExpr = System::cron(options) // "16 14 * 11 2" - * - * CronOptions options - * options.includeNonStandard = false - * std::string cronExpr = System::cron(options) // 34 2 ? 8 * - * - * CronOptions options - * options.includeNonStandard = true - * std::string cronExpr = System::cron(options) // "@reboot" - * @endcode - */ - static std::string cron(const CronOptions& options = {}); -}; +/** + * @brief Returns a random file name with extension. + * + * @param options An option struct. + * + * @returns Random file name with extension. + * + * @code + * system::fileName() // "injustice.mpeg" + * + * FileOptions options + * options.extensionCount = 3 + * system::fileName(options) // "transformation.wav.mpeg.mp4" + * + * options.extensionRange.min = 1; + * options.extensionRange.max = 3; + * system::fileName(options) // "sparkle.png.pdf" + * @endcode + */ +std::string fileName(const FileOptions& options = {}); + +/** + * @brief Returns a file extension. + * + * @param mimeType value of MimeType enum. + * + * @returns A file extension. + * + * @code + * system::fileExtension(MimeType::Image) // "png" + * @endcode + */ +std::string fileExtension(const std::optional& mimeType = std::nullopt); + +/** + * Returns a random file name with a given extension or a commonly used extension. + * + * @param ext Optional extension parameter. + * + * @returns A random file name with a given extension or a commonly used extension. + * + * @code + * system::commonFileName() // "dollar.jpg" + * system::commonFileName("txt") // "global_borders_wyoming.txt" + * @endcode + */ +std::string commonFileName(const std::optional& ext = std::nullopt); + +/** + * Returns a commonly used file extension. + * + * @returns A commonly used file extension. + * + * @code + * system::commonFileExtension() // "gif" + * @endcode + */ +std::string_view commonFileExtension(); + +/** + * Returns a mime-type. + * + * @returns A mime-type. + * + * @code + * system::mimeType() // "video/vnd.vivo" + * @endcode + */ +std::string_view mimeType(); + +/** + * Returns a commonly used file type. + * + * @returns A commonly used file type. + * + * @code + * system::fileType() // "audio" + * @endcode + */ +std::string_view fileType(); + +/** + * Returns a directory path. + * + * @returns A directory path. + * + * @code + * system::directoryPath() // "/etc/mail" + * @endcode + */ +std::string_view directoryPath(); + +/** + * Returns a file path. + * + * @returns A file path. + * + * @code + * system::filePath() // "/usr/local/src/money.dotx" + * @endcode + */ +std::string filePath(); + +/** + * Returns a semantic version. + * + * @returns A semantic version. + * + * @code + * system::semver() // "1.1.2" + * @endcode + */ +std::string semver(); + +/** + * Returns a random network interface. + * + * @param options The options to use. Defaults to an empty options structure @see NetworkInterfaceOptions.h. + * @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`. + * @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`. + * + * @returns A random network interface. + * + * @code + * system::networkInterface() // "enp2s7f8" + * + * NetworkInterfaceOptions options; + * options.interfaceType = "wl"; + * system::networkInterface(options) // "wlsf4d2" + * + * NetworkInterfaceOptions options; + * options.interfaceSchema = "mac"; + * system::networkInterface(options) // "enxd17705ed394f" + * + * NetworkInterfaceOptions options; + * options.interfaceType = "en"; + * options.interfaceSchema = "pci"; + * system::networkInterface(options) // "enp1s9f1d2" + * @endcode + */ +std::string networkInterface(const std::optional& options = {}); + +/** + * Returns a random cron expression. + * + * @param options The options to use. Defaults to an empty options structure @see CronOptions.h. + * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`. + * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the + * generated expression. Defaults to `false`. + * + * @returns A random cron expression. + * + * @code + * system.cron() // "22 * ? * ?" + * + * CronOptions options + * options.includeYear = true + * std::string cronExpr = system::cron(options) // "16 14 * 11 2 2038" + * + * CronOptions options + * options.includeYear = false + * std::string cronExpr = system::cron(options) // "16 14 * 11 2" + * + * CronOptions options + * options.includeNonStandard = false + * std::string cronExpr = system::cron(options) // 34 2 ? 8 * + * + * CronOptions options + * options.includeNonStandard = true + * std::string cronExpr = system::cron(options) // "@reboot" + * @endcode + */ +std::string cron(const CronOptions& options = {}); } diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index a64d91c01..f3cc2dc82 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -99,13 +99,13 @@ std::string Internet::username(std::optional firstNameInit, std::op username = FormatHelper::format("{}{}{}", firstName, lastName, Number::integer(999)); break; case 1: - username = FormatHelper::format( - "{}{}{}", firstName, Helper::arrayElement(std::vector{".", "_", ""}), lastName); + username = FormatHelper::format("{}{}{}", firstName, + Helper::arrayElement(std::vector{".", "_", ""}), lastName); break; case 2: - username = FormatHelper::format("{}{}{}{}", firstName, - Helper::arrayElement(std::vector{".", "_", ""}), - lastName, Number::integer(99)); + username = + FormatHelper::format("{}{}{}{}", firstName, Helper::arrayElement(std::vector{".", "_", ""}), + lastName, Number::integer(99)); break; } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index be257a896..72b0c7a49 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -1,6 +1,6 @@ #include "faker-cxx/System.h" -#include +#include #include #include #include @@ -17,67 +17,70 @@ #include "faker-cxx/Word.h" #include "SystemData.h" -namespace faker +namespace faker::system { namespace { -const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, - {FileType::Audio, "audio"}, - {FileType::Image, "image"}, - {FileType::Text, "text"}, - {FileType::Application, "application"}}; +const std::unordered_map fileTypeToStringMapping{ + {FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}, +}; std::string_view extension(std::string_view mimeType) { const auto it = mimeTypesExtensions.find(mimeType); + if (it == mimeTypesExtensions.end()) { auto pos = mimeType.find_last_of('/'); + return mimeType.substr(pos + 1); } - else - { - return it->second; - } + + return it->second; } } -std::string System::fileName(const FileOptions& options) +std::string fileName(const FileOptions& options) { - std::string baseName = word::words(); + const auto baseName = word::words(); + std::string extensionsStr; if (options.extensionCount > 0) { std::vector randomExtensions; + if (options.extensionRange.min == options.extensionRange.max) { for (int i = 0; i < options.extensionCount; ++i) { - std::string randomExt = fileExtension(); - randomExtensions.push_back(randomExt); + randomExtensions.push_back(fileExtension()); } + extensionsStr = "." + StringHelper::joinString(randomExtensions, "."); } else { - int numExtensions; - numExtensions = - options.extensionRange.min + rand() % (options.extensionRange.max - options.extensionRange.min + 1); + const std::integral auto numExtensions = + Number::integer(options.extensionRange.min, options.extensionRange.max); for (int i = 0; i < numExtensions; ++i) { - std::string randomExt = fileExtension(); - randomExtensions.push_back(randomExt); + randomExtensions.push_back(fileExtension()); } extensionsStr = "." + StringHelper::joinString(randomExtensions, "."); } } + return baseName + extensionsStr; } -std::string System::fileExtension(const std::optional& mimeType) +std::string fileExtension(const std::optional& mimeType) { if (mimeType.has_value()) { @@ -88,14 +91,16 @@ std::string System::fileExtension(const std::optional& mimeType) 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(std::string(mime.substr(pos + 1))); + extensions.emplace_back(mime.substr(pos + 1)); } } - return Helper::arrayElement(extensions); + return Helper::arrayElement(extensions); } else { @@ -108,11 +113,11 @@ std::string System::fileExtension(const std::optional& mimeType) std::vector extensions(extensionSet.begin(), extensionSet.end()); - return Helper::arrayElement(extensions); + return Helper::arrayElement(extensions); } } -std::string System::commonFileName(const std::optional& ext) +std::string commonFileName(const std::optional& ext) { FileOptions options; @@ -126,18 +131,18 @@ std::string System::commonFileName(const std::optional& ext) } else { - return str + "." + commonFileExtension(); + return str + "." + std::string{commonFileExtension()}; } } -std::string System::commonFileExtension() +std::string_view commonFileExtension() { - auto mimeType = Helper::arrayElement(commonMimeTypes); + auto mimeType = Helper::arrayElement(commonMimeTypes); - return std::string(extension(mimeType)); + return extension(mimeType); } -std::string System::mimeType() +std::string_view mimeType() { std::vector mimeTypeKeys; @@ -148,49 +153,25 @@ std::string System::mimeType() mimeTypeKeys.push_back(entry); } - return std::string(Helper::arrayElement(mimeTypeKeys)); + return Helper::arrayElement(mimeTypeKeys); } -std::string System::commonFileType() +std::string_view fileType() { - return std::string(Helper::arrayElement(commonFileTypes)); -} - -std::string System::fileType() -{ - std::set typeSet; - - const auto& localMimeTypes = mimeTypes; - - for (const auto& entry : localMimeTypes) - { - const std::string& m = std::string(entry); - - size_t pos = m.find('/'); - - if (pos != std::string::npos) - { - std::string type = m.substr(0, pos); - typeSet.insert(type); - } - } - - std::vector types(typeSet.begin(), typeSet.end()); - - return Helper::arrayElement(types); + return Helper::arrayElement(commonFileTypes); } -std::string System::directoryPath() +std::string_view directoryPath() { - return std::string(Helper::arrayElement(directoryPaths)); + return Helper::arrayElement(directoryPaths); } -std::string System::filePath() +std::string filePath() { - return directoryPath() + fileName(); + return std::string{directoryPath()} + fileName(); } -std::string System::semver() +std::string semver() { int major = Number::integer(9); int minor = Number::integer(9); @@ -199,9 +180,9 @@ std::string System::semver() return FormatHelper::format("{}.{}.{}", major, minor, patch); } -std::string System::networkInterface(const std::optional& options) +std::string networkInterface(const std::optional& options) { - const auto defaultInterfaceType = Helper::arrayElement(commonInterfaceTypes); + const auto defaultInterfaceType = Helper::arrayElement(commonInterfaceTypes); const std::string defaultInterfaceSchema = std::string(Helper::objectKey(commonInterfaceSchemas)); std::string interfaceType = std::string(defaultInterfaceType); @@ -248,7 +229,7 @@ std::string System::networkInterface(const std::optional(minutes); - auto hour = Helper::arrayElement(hours); - auto day = Helper::arrayElement(days); - auto month = Helper::arrayElement(months); - auto dayOfWeek = Helper::arrayElement(daysOfWeek); - auto year = Helper::arrayElement(years); + auto minute = Helper::arrayElement(minutes); + auto hour = Helper::arrayElement(hours); + auto day = Helper::arrayElement(days); + auto month = Helper::arrayElement(months); + auto dayOfWeek = Helper::arrayElement(daysOfWeek); + auto year = Helper::arrayElement(years); std::string standardExpression = minute + " " + hour + " " + day + " " + month + " " + dayOfWeek; if (includeYear) diff --git a/src/modules/system/SystemData.h b/src/modules/system/SystemData.h index 764d53e06..07feb9f85 100644 --- a/src/modules/system/SystemData.h +++ b/src/modules/system/SystemData.h @@ -5,14 +5,30 @@ #include "faker-cxx/System.h" -namespace faker +namespace faker::system { -const auto commonInterfaceTypes = std::to_array({"en", "wl", "ww"}); +const auto commonInterfaceTypes = std::to_array({ + "en", + "wl", + "ww", +}); const std::unordered_map commonInterfaceSchemas = { - {"index", "o"}, {"slot", "s"}, {"mac", "x"}, {"pci", "p"}}; + {"index", "o"}, + {"slot", "s"}, + {"mac", "x"}, + {"pci", "p"}, +}; -const auto cronDayOfWeek = std::to_array({"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}); +const auto cronDayOfWeek = std::to_array({ + "SUN", + "MON", + "TUE", + "WED", + "THU", + "FRI", + "SAT", +}); const auto directoryPaths = std::to_array({ "/etc/mail", @@ -152,17 +168,13 @@ const std::unordered_map mimeTypesExtensions {"application/x-7z-compressed", "7z"}, {"application/x-tar", "tart"}, {"application/xhtml+xml", "xhtml"}, - {"audio/ogg", "oga"}, {"audio/webm", "weba"}, {"audio/mpeg", "mp3"}, - {"image/svg+xml", "svg"}, - {"text/calendar", "ics"}, {"text/javascript", "js"}, {"text/plain", "txt"}, - {"video/3gpp", "3gp"}, {"video/3gpp2", "3g2"}, {"video/mp2t", "ts"}, @@ -171,11 +183,22 @@ const std::unordered_map mimeTypesExtensions }; const auto commonMimeTypes = std::to_array({ - "application/pdf", "audio/mpeg", "audio/wav", - "image/png", "image/jpeg", "image/gif", - "video/mp4", "video/mpeg", "text/html", + "application/pdf", + "audio/mpeg", + "audio/wav", + "image/png", + "image/jpeg", + "image/gif", + "video/mp4", + "video/mpeg", + "text/html", }); -const auto commonFileTypes = std::to_array({"video", "audio", "image", "text", "application"}); - +const auto commonFileTypes = std::to_array({ + "video", + "audio", + "image", + "text", + "application", +}); } diff --git a/tests/modules/system/SystemTest.cpp b/tests/modules/system/SystemTest.cpp index 199cd62c6..f0350227a 100644 --- a/tests/modules/system/SystemTest.cpp +++ b/tests/modules/system/SystemTest.cpp @@ -1,7 +1,6 @@ #include "faker-cxx/System.h" #include -#include #include #include #include @@ -14,13 +13,15 @@ #include "system/SystemData.h" using namespace ::testing; -using namespace faker; - -const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, - {FileType::Audio, "audio"}, - {FileType::Image, "image"}, - {FileType::Text, "text"}, - {FileType::Application, "application"}}; +using namespace faker::system; + +const std::unordered_map fileTypeToStringMapping{ + {FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}, +}; class SystemTest : public Test { @@ -39,7 +40,7 @@ TEST_F(SystemTest, FileNameTestWithExtensionCount) FileOptions options; options.extensionCount = 3; - std::string expectedFileName = System::fileName(options); + const auto expectedFileName = fileName(options); EXPECT_FALSE(expectedFileName.empty()); @@ -47,25 +48,25 @@ TEST_F(SystemTest, FileNameTestWithExtensionCount) options2.extensionRange.min = 1; options2.extensionRange.max = 3; - std::string expectedFileName2 = System::fileName(options2); + const auto expectedFileName2 = fileName(options2); EXPECT_FALSE(expectedFileName2.empty()); } TEST_F(SystemTest, FileExtTestWithMimeType) { - std::string exampleFileExtension = System::fileExtension(); + const auto exampleFileExtension = fileExtension(); EXPECT_FALSE(exampleFileExtension.empty()); } TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) { - auto image = FileType::Image; - auto audio = FileType::Audio; - auto video = FileType::Video; - auto text = FileType::Text; - auto application = FileType::Application; + const auto image = FileType::Image; + const auto audio = FileType::Audio; + const auto video = FileType::Video; + const auto text = FileType::Text; + const auto application = FileType::Application; std::vector imageExtensions; @@ -84,7 +85,9 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) for (const auto& mimeType : mimeTypes) { size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == fileTypeToStringMapping.at(audio)) { audioExtensions.push_back(mimeType.substr(pos + 1)); @@ -96,7 +99,9 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) for (const auto& mimeType : mimeTypes) { size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == fileTypeToStringMapping.at(video)) { videoExtensions.push_back(mimeType.substr(pos + 1)); @@ -107,7 +112,9 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) for (const auto& mimeType : mimeTypes) { size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == fileTypeToStringMapping.at(text)) { textExtensions.push_back(mimeType.substr(pos + 1)); @@ -119,18 +126,20 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) for (const auto& mimeType : mimeTypes) { size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == fileTypeToStringMapping.at(application)) { applicationExtensions.push_back(mimeType.substr(pos + 1)); } } - auto imageExt = System::fileExtension(image); - auto audioExt = System::fileExtension(audio); - auto videoExt = System::fileExtension(video); - auto textExt = System::fileExtension(text); - auto applicationExt = System::fileExtension(application); + const auto imageExt = fileExtension(image); + const auto audioExt = fileExtension(audio); + const auto videoExt = fileExtension(video); + const auto textExt = fileExtension(text); + const auto applicationExt = fileExtension(application); EXPECT_TRUE(std::ranges::find(imageExtensions, imageExt) != imageExtensions.end()); EXPECT_TRUE(std::ranges::find(audioExtensions, audioExt) != audioExtensions.end()); @@ -141,95 +150,100 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) TEST_F(SystemTest, CommonFileNameWithEmptyExtensionTest) { - std::string actualFileName = System::commonFileName(); + const auto actualFileName = commonFileName(); - std::string actualExtension = actualFileName.substr(actualFileName.find_last_of('.') + 1); + const auto actualExtension = actualFileName.substr(actualFileName.find_last_of('.') + 1); EXPECT_FALSE(actualExtension.empty()); - std::string fileNameWithParam = System::commonFileName("txt"); + const auto fileNameWithParam = commonFileName("txt"); - std::string extensionWithParam = fileNameWithParam.substr(fileNameWithParam.find_last_of('.') + 1); + const auto extensionWithParam = fileNameWithParam.substr(fileNameWithParam.find_last_of('.') + 1); EXPECT_EQ(extensionWithParam, "txt"); } TEST_F(SystemTest, MimeTypeTest) { - std::string mimeTypeResult = System::mimeType(); + const auto mimeTypeResult = mimeType(); bool isValidMimeType = std::ranges::find(mimeTypes, mimeTypeResult) != mimeTypes.end(); - EXPECT_TRUE(isValidMimeType); -} -TEST_F(SystemTest, CommonFileTypeTest) -{ - std::string commonFileTypeResult = System::commonFileType(); - - bool isValidCommonFileType = std::ranges::find(commonFileTypes, commonFileTypeResult) != commonFileTypes.end(); - EXPECT_TRUE(isValidCommonFileType); + EXPECT_TRUE(isValidMimeType); } TEST_F(SystemTest, FileTypeTest) { - std::set typeSet; + std::set typeSet; + for (const auto& entry : mimeTypes) { - const std::string& m = std::string(entry); - size_t pos = m.find('/'); + size_t pos = entry.find('/'); + if (pos != std::string::npos) { - std::string type = m.substr(0, pos); + auto type = entry.substr(0, pos); + typeSet.insert(type); } } + std::vector expectedTypes(typeSet.begin(), typeSet.end()); - std::string fileTypeResult = System::fileType(); + const auto fileTypeResult = fileType(); + + std::cout << fileTypeResult << std::endl; bool isValidFileType = std::ranges::find(expectedTypes, fileTypeResult) != expectedTypes.end(); + EXPECT_TRUE(isValidFileType); } TEST_F(SystemTest, FilePathTest) { - std::string filePath = System::filePath(); + const auto generatedFilePath = filePath(); - EXPECT_FALSE(filePath.empty()); + EXPECT_FALSE(generatedFilePath.empty()); } TEST_F(SystemTest, SemverTest) { - std::string semverResult = System::semver(); + const auto semverResult = semver(); EXPECT_TRUE(std::regex_match(semverResult, std::regex("\\d+\\.\\d+\\.\\d+"))); } TEST_F(SystemTest, NetworkInterfaceMethodTest) { + const auto result1 = networkInterface(std::nullopt); - std::string result1 = System::networkInterface(std::nullopt); ASSERT_TRUE(!result1.empty()); NetworkInterfaceOptions options2; options2.interfaceType = "wl"; - std::string result2 = System::networkInterface(options2); + + const auto result2 = networkInterface(options2); + ASSERT_TRUE(!result2.empty()); NetworkInterfaceOptions options3; options3.interfaceSchema = "mac"; - std::string result3 = System::networkInterface(options3); + + const auto result3 = networkInterface(options3); + ASSERT_EQ(result3.size(), 15); NetworkInterfaceOptions options4; options4.interfaceType = "en"; options4.interfaceSchema = "pci"; - std::string result4 = System::networkInterface(options4); + + const auto result4 = networkInterface(options4); + ASSERT_TRUE(!result4.empty()); } TEST_F(SystemTest, ValidCronExpression) { - std::string cronExpr = System::cron(); + std::string cronExpr = cron(); EXPECT_TRUE(isValidCronExpression(cronExpr)); } @@ -237,7 +251,9 @@ TEST_F(SystemTest, IncludeYearOption) { CronOptions options; options.includeYear = true; - std::string cronExpr = System::cron(options); + + const auto cronExpr = cron(options); + EXPECT_TRUE(isValidCronExpression(cronExpr)); int yearValue = -1; @@ -246,6 +262,7 @@ TEST_F(SystemTest, IncludeYearOption) { yearValue = std::stoi(match.str()); } + EXPECT_GE(yearValue, 1970); EXPECT_LE(yearValue, 2099); } @@ -254,7 +271,8 @@ TEST_F(SystemTest, IncludeNonStandardOption) { CronOptions options; options.includeNonStandard = true; - std::string cronExpr = System::cron(options); + + const auto cronExpr = cron(options); std::vector nonStandardExpressions = {"@annually", "@daily", "@hourly", "@monthly", "@reboot", "@weekly", "@yearly"};