diff --git a/include/faker-cxx/Git.h b/include/faker-cxx/Git.h index eb81eba0..40f8e358 100644 --- a/include/faker-cxx/Git.h +++ b/include/faker-cxx/Git.h @@ -4,7 +4,6 @@ #include #include "faker-cxx/types/Country.h" -#include "faker-cxx/types/Language.h" namespace faker { diff --git a/include/faker-cxx/Image.h b/include/faker-cxx/Image.h index e0a12794..a9617f75 100644 --- a/include/faker-cxx/Image.h +++ b/include/faker-cxx/Image.h @@ -6,18 +6,6 @@ #include "types/ImageCategory.h" -namespace -{ -std::map imageCategoryString = { - {faker::ImageCategory::animals, "animals"}, {faker::ImageCategory::business, "business"}, - {faker::ImageCategory::cats, "cats"}, {faker::ImageCategory::city, "city"}, - {faker::ImageCategory::food, "food"}, {faker::ImageCategory::nightlife, "nightlife"}, - {faker::ImageCategory::fashion, "fashion"}, {faker::ImageCategory::people, "people"}, - {faker::ImageCategory::nature, "nature"}, {faker::ImageCategory::sports, "sports"}, - {faker::ImageCategory::technics, "technics"}, {faker::ImageCategory::transport, "transport"}, -}; -} - namespace faker { class Image @@ -63,4 +51,4 @@ class Image */ static std::string dimensions(); }; -} \ No newline at end of file +} diff --git a/include/faker-cxx/Person.h b/include/faker-cxx/Person.h index 1f1dcd5d..914d462f 100644 --- a/include/faker-cxx/Person.h +++ b/include/faker-cxx/Person.h @@ -4,8 +4,7 @@ #include #include "faker-cxx/types/Country.h" -#include "types/Language.h" -#include "types/Sex.h" +#include "faker-cxx/types/Sex.h" namespace faker { diff --git a/include/faker-cxx/String.h b/include/faker-cxx/String.h index ce47528c..301f8281 100644 --- a/include/faker-cxx/String.h +++ b/include/faker-cxx/String.h @@ -6,7 +6,7 @@ #include #include "types/Hex.h" -#include "types/RandomGenerator.hpp" +#include "types/RandomGenerator.h" namespace faker { diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index fb0a87d0..5ebe0b2a 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -1,8 +1,8 @@ #pragma once #include "types/CronOptions.h" -#include "types/DirectoryPath.h" #include "types/FileOptions.h" +#include "types/FileType.h" #include "types/MimeTypes.h" #include "types/NetworkInterfaceOptions.h" diff --git a/include/faker-cxx/types/FileType.h b/include/faker-cxx/types/FileType.h new file mode 100644 index 00000000..d47db004 --- /dev/null +++ b/include/faker-cxx/types/FileType.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +namespace faker +{ +enum class FileType +{ + Video, + Audio, + Image, + Text, + Application +}; + +inline std::string toString(FileType type) +{ + std::map enumToStringMapping{{FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}}; + return enumToStringMapping.at(type); +} +} diff --git a/include/faker-cxx/types/ImageCategory.h b/include/faker-cxx/types/ImageCategory.h index db425c35..cef99335 100644 --- a/include/faker-cxx/types/ImageCategory.h +++ b/include/faker-cxx/types/ImageCategory.h @@ -4,17 +4,17 @@ namespace faker { enum class ImageCategory { - animals, - business, - cats, - city, - food, - nightlife, - fashion, - people, - nature, - sports, - technics, - transport + Animals, + Business, + Cats, + City, + Food, + Nightlife, + Fashion, + People, + Nature, + Sports, + Technics, + Transport }; -} \ No newline at end of file +} diff --git a/include/faker-cxx/types/Language.h b/include/faker-cxx/types/Language.h deleted file mode 100644 index da898161..00000000 --- a/include/faker-cxx/types/Language.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -enum class Language -{ - English, - Polish, - Italian, - French, - German, - Russian, - Romanian, - Hindi, - Finnish, - Nepali, - Spanish, - Turkish, - Czech, - Slovak, - Ukrainian, - Danish, - Swedish -}; - -const std::vector languages{ - Language::English, Language::Polish, Language::Italian, Language::French, Language::German, Language::Russian, - Language::Romanian, Language::Hindi, Language::Finnish, Language::Nepali, Language::Spanish, Language::Turkish, - Language::Czech, Language::Slovak, Language::Ukrainian, Language::Danish, Language::Swedish}; - -inline std::string toString(Language language) -{ - std::map languageToStringMapping{ - {Language::English, "English"}, {Language::Polish, "Polish"}, {Language::Italian, "Italian"}, - {Language::French, "French"}, {Language::German, "German"}, {Language::Russian, "Russian"}, - {Language::Romanian, "Romanian"}, {Language::Hindi, "Hindi"}, {Language::Finnish, "Finnish"}, - {Language::Nepali, "Nepali"}, {Language::Spanish, "Spanish"}, {Language::Turkish, "Turkish"}, - {Language::Czech, "Czech"}, {Language::Slovak, "Slovak"}, {Language::Ukrainian, "Ukrainian"}, - {Language::Danish, "Danish"}, {Language::Swedish, "Swedish"}}; - - return languageToStringMapping.at(language); -} - -inline std::ostream& operator<<(std::ostream& os, Language language) -{ - return os << toString(language); -} - -} diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index f32540dd..d77d339f 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -6,141 +6,5 @@ namespace faker { -const std::vector mimeTypes = {"application/atom+xml", - "application/font-woff", - "application/gzip", - "application/java-archive", - "application/javascript", - "application/json", - "application/ld+json", - "application/msword", - "application/octet-stream", - "application/ogg", - "application/pdf", - "application/rdf+xml", - "application/rtf", - "application/vnd.apple.mpegurl", - "application/vnd.ms-excel", - "application/vnd.ms-fontobject", - "application/vnd.openxmlformats-officedocument.presentationml.presentation", - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "application/x-7z-compressed", - "application/x-font-ttf", - "application/x-javascript", - "application/x-mpegURL", - "application/x-rar-compressed", - "application/x-shockwave-flash", - "application/x-tar", - "application/x-www-form-urlencoded", - "application/xhtml+xml", - "application/xml", - "application/zip", - "audio/aac", - "audio/midi", - "audio/ogg", - "audio/wav", - "audio/webm", - "audio/mpeg", - "font/woff", - "font/woff2", - "image/apng", - "image/bmp", - "image/gif", - "image/jpeg", - "image/png", - "image/svg+xml", - "image/tiff", - "image/webp", - "image/x-icon", - "multipart/form-data", - "text/calendar", - "text/css", - "text/csv", - "text/html", - "text/javascript", - "text/plain", - "text/xml", - "video/mp4", - "video/3gpp", - "video/3gpp2", - "video/mp2t", - "video/mpeg", - "video/ogg", - "video/webm", - "video/x-msvideo", - "video/x-flv"}; -// Only contains non obvious extensions. -const std::map mimeTypesExtensions{ - {"application/atom+xml", "xml"}, - {"application/font-woff", "woff"}, - {"application/gzip", "gz"}, - {"application/java-archive", "jar"}, - {"application/javascript", "js"}, - {"application/ld+json", "jsonld"}, - {"application/msword", "doc"}, - {"application/octet-stream", "bin"}, - {"application/ogg", "ogx"}, - {"application/vnd.ms-excel", "xls"}, - {"application/vnd.ms-fontobject", "eot"}, - {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, - {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, - {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, - {"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"}, - {"video/ogg", "ogv"}, - {"video/x-msvideo", "avi"}}; - -const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", - "image/png", "image/jpeg", "image/gif", - "video/mp4", "video/mpeg", "text/html"}; - -const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; - -enum class FileType -{ - Video, - Audio, - Image, - Text, - Application -}; -inline std::string toString(FileType type) -{ - std::map enumToStringMapping{{FileType::Video, "video"}, - {FileType::Audio, "audio"}, - {FileType::Image, "image"}, - {FileType::Text, "text"}, - {FileType::Application, "application"}}; - return enumToStringMapping.at(type); -} -inline std::string extension(const std::string& 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; - } -} } diff --git a/include/faker-cxx/types/RandomGenerator.hpp b/include/faker-cxx/types/RandomGenerator.h similarity index 96% rename from include/faker-cxx/types/RandomGenerator.hpp rename to include/faker-cxx/types/RandomGenerator.h index fec65331..a1e806fe 100644 --- a/include/faker-cxx/types/RandomGenerator.hpp +++ b/include/faker-cxx/types/RandomGenerator.h @@ -1,6 +1,9 @@ #pragma once + #include +namespace faker +{ template requires std::uniform_random_bit_generator class RandomGenerator @@ -22,3 +25,4 @@ class RandomGenerator private: T generator_; }; +} diff --git a/src/modules/git/Git.cpp b/src/modules/git/Git.cpp index 2544f626..b6d0ed65 100644 --- a/src/modules/git/Git.cpp +++ b/src/modules/git/Git.cpp @@ -9,7 +9,6 @@ #include "faker-cxx/Number.h" #include "faker-cxx/Person.h" #include "faker-cxx/String.h" -#include "faker-cxx/types/Language.h" #include "faker-cxx/Word.h" #include "fmt/format.h" @@ -62,6 +61,7 @@ std::string Git::commitDate(unsigned years) } timeZoneString += std::to_string(timeZone * 100); + if (!timeZone) { timeZoneString += "00"; diff --git a/src/modules/git/GitTest.cpp b/src/modules/git/GitTest.cpp index ff9e4901..187bb697 100644 --- a/src/modules/git/GitTest.cpp +++ b/src/modules/git/GitTest.cpp @@ -8,14 +8,7 @@ #include "gtest/gtest.h" #include "../../common/StringHelper.h" -#include "../date/data/MonthNames.h" -#include "faker-cxx/Date.h" -#include "faker-cxx/Internet.h" #include "faker-cxx/Number.h" -#include "faker-cxx/Person.h" -#include "faker-cxx/String.h" -#include "faker-cxx/types/Language.h" -#include "faker-cxx/Word.h" using namespace ::testing; using namespace faker; @@ -54,7 +47,7 @@ TEST_F(GitTest, shouldGenerateBranch) TEST_F(GitTest, branchIssueNumTest) { - unsigned testValue = unsigned(faker::Number::integer(2, 100)); + auto testValue = unsigned(faker::Number::integer(2, 100)); std::vector branch = faker::StringHelper::split(Git::branch(testValue), "-"); bool numberAtFront = false; int number; diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index 2383d8d8..42c2a4b5 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -5,11 +5,22 @@ namespace faker { +namespace +{ +std::map imageCategoryToLoremFlickrStringMapping = { + {faker::ImageCategory::Animals, "animals"}, {faker::ImageCategory::Business, "business"}, + {faker::ImageCategory::Cats, "cats"}, {faker::ImageCategory::City, "city"}, + {faker::ImageCategory::Food, "food"}, {faker::ImageCategory::Nightlife, "nightlife"}, + {faker::ImageCategory::Fashion, "fashion"}, {faker::ImageCategory::People, "people"}, + {faker::ImageCategory::Nature, "nature"}, {faker::ImageCategory::Sports, "sports"}, + {faker::ImageCategory::Technics, "technics"}, {faker::ImageCategory::Transport, "transport"}, +}; +} std::string Image::imageUrl(unsigned int width, unsigned int height, std::optional category) { const std::string image_category = - category.has_value() ? fmt::format("/{}", imageCategoryString.at(category.value())) : ""; + category.has_value() ? fmt::format("/{}", imageCategoryToLoremFlickrStringMapping.at(category.value())) : ""; return fmt::format("https://loremflickr.com/{}/{}{}", width, height, image_category); } diff --git a/src/modules/image/ImageTest.cpp b/src/modules/image/ImageTest.cpp index 5442736d..86bf9bf6 100644 --- a/src/modules/image/ImageTest.cpp +++ b/src/modules/image/ImageTest.cpp @@ -35,7 +35,7 @@ TEST_F(ImageTest, shouldGenerateImageUrlCategory) { const auto width = 800; const auto height = 600; - const ImageCategory category = ImageCategory::fashion; + const ImageCategory category = ImageCategory::Fashion; const auto imageUrl = Image::imageUrl(width, height, category); @@ -65,4 +65,4 @@ TEST_F(ImageTest, shouldGenerateDimensions) auto height_dimension = std::stoi(split_dimensions[1]); ASSERT_TRUE(height_dimension >= 1 && height_dimension <= 17280); -} \ No newline at end of file +} diff --git a/src/modules/person/Person.cpp b/src/modules/person/Person.cpp index e315d06b..5a1778a3 100644 --- a/src/modules/person/Person.cpp +++ b/src/modules/person/Person.cpp @@ -17,6 +17,7 @@ #include "data/india/IndianPeopleNames.h" #include "data/italy/ItalianPeopleNames.h" #include "data/JobTitles.h" +#include "data/Languages.h" #include "data/Nationalities.h" #include "data/nepal/NepalesePeopleNames.h" #include "data/norway/NorwegianPeopleNames.h" @@ -288,7 +289,7 @@ std::string Person::hobby() std::string Person::language() { - return toString(Helper::arrayElement(languages)); + return Helper::arrayElement(languages); } std::string Person::nationality() diff --git a/src/modules/person/PersonTest.cpp b/src/modules/person/PersonTest.cpp index ffac752a..5f317217 100644 --- a/src/modules/person/PersonTest.cpp +++ b/src/modules/person/PersonTest.cpp @@ -16,6 +16,7 @@ #include "data/india/IndianPeopleNames.h" #include "data/italy/ItalianPeopleNames.h" #include "data/JobTitles.h" +#include "data/Languages.h" #include "data/Nationalities.h" #include "data/nepal/NepalesePeopleNames.h" #include "data/norway/NorwegianPeopleNames.h" @@ -387,8 +388,8 @@ TEST_F(PersonTest, shouldGenerateLanguage) { const auto generatedLanguage = Person::language(); - ASSERT_TRUE(std::ranges::any_of(languages, [generatedLanguage](Language language) - { return generatedLanguage == toString(language); })); + ASSERT_TRUE(std::ranges::any_of(languages, [generatedLanguage](const std::string& language) + { return generatedLanguage == language; })); } TEST_F(PersonTest, shouldGenerateNationality) diff --git a/src/modules/person/data/Languages.h b/src/modules/person/data/Languages.h new file mode 100644 index 00000000..596b5a81 --- /dev/null +++ b/src/modules/person/data/Languages.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace faker +{ +const std::vector languages = {"English", "Polish", "Italian", "French", "German", "Russian", + "Romanian", "Hindi", "Finnish", "Nepali", "Spanish", "Turkish", + "Czech", "Slovak", "Ukrainian", "Danish", "Swedish"}; + +} diff --git a/src/modules/string/StringTest.cpp b/src/modules/string/StringTest.cpp index d33f35a2..069c2c91 100644 --- a/src/modules/string/StringTest.cpp +++ b/src/modules/string/StringTest.cpp @@ -6,7 +6,7 @@ #include "gtest/gtest.h" #include "data/Characters.h" -#include "faker-cxx/types/RandomGenerator.hpp" +#include "faker-cxx/types/RandomGenerator.h" using namespace ::testing; using namespace faker; diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index ab5c3e5e..5e2534d2 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -7,15 +7,34 @@ #include #include "../src/common/StringHelper.h" +#include "data/CommonInterface.h" +#include "data/CronDayOfWeek.h" +#include "data/DirectoryPath.h" +#include "data/MimeTypes.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Internet.h" #include "faker-cxx/String.h" -#include "faker-cxx/types/CommonInterface.h" -#include "faker-cxx/types/CronDayOfWeek.h" #include "faker-cxx/Word.h" namespace faker { +namespace +{ +std::string extension(const std::string& 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; + } +} +} + std::string System::fileName(const FileOptions& options) { std::string baseName = Word::words(); @@ -85,8 +104,11 @@ std::string System::fileExtension(const std::optional& mimeType) std::string System::commonFileName(const std::optional& ext) { FileOptions options; + options.extensionCount = 0; + std::string str = fileName(options); + if (ext.has_value() && !ext.value().empty()) { return str + "." + ext.value(); @@ -99,13 +121,15 @@ std::string System::commonFileName(const std::optional& ext) std::string System::commonFileExtension() { - std::string mimeType = Helper::arrayElement(commonMimeTypes); + auto mimeType = Helper::arrayElement(commonMimeTypes); + return extension(mimeType); } std::string System::mimeType() { std::vector mimeTypeKeys; + for (const auto& entry : mimeTypes) { mimeTypeKeys.push_back(entry); @@ -122,12 +146,15 @@ std::string System::commonFileType() std::string System::fileType() { std::set typeSet; + const auto& localMimeTypes = mimeTypes; for (const auto& entry : localMimeTypes) { const std::string& m = entry; + size_t pos = m.find('/'); + if (pos != std::string::npos) { std::string type = m.substr(0, pos); @@ -136,14 +163,15 @@ std::string System::fileType() } std::vector types(typeSet.begin(), typeSet.end()); + return Helper::arrayElement(types); } std::string System::directoryPath() { - const std::vector paths = directoryPaths; - return Helper::arrayElement(paths); + return Helper::arrayElement(directoryPaths); } + std::string System::filePath() { return directoryPath() + fileName(); @@ -156,7 +184,9 @@ std::string System::semver() int patch = Number::integer(9); std::stringstream ss; + ss << major << '.' << minor << '.' << patch; + return ss.str(); } @@ -208,6 +238,7 @@ std::string System::networkInterface(const std::optional +#include + +namespace faker +{ +const std::vector mimeTypes = {"application/atom+xml", + "application/font-woff", + "application/gzip", + "application/java-archive", + "application/javascript", + "application/json", + "application/ld+json", + "application/msword", + "application/octet-stream", + "application/ogg", + "application/pdf", + "application/rdf+xml", + "application/rtf", + "application/vnd.apple.mpegurl", + "application/vnd.ms-excel", + "application/vnd.ms-fontobject", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/x-7z-compressed", + "application/x-font-ttf", + "application/x-javascript", + "application/x-mpegURL", + "application/x-rar-compressed", + "application/x-shockwave-flash", + "application/x-tar", + "application/x-www-form-urlencoded", + "application/xhtml+xml", + "application/xml", + "application/zip", + "audio/aac", + "audio/midi", + "audio/ogg", + "audio/wav", + "audio/webm", + "audio/mpeg", + "font/woff", + "font/woff2", + "image/apng", + "image/bmp", + "image/gif", + "image/jpeg", + "image/png", + "image/svg+xml", + "image/tiff", + "image/webp", + "image/x-icon", + "multipart/form-data", + "text/calendar", + "text/css", + "text/csv", + "text/html", + "text/javascript", + "text/plain", + "text/xml", + "video/mp4", + "video/3gpp", + "video/3gpp2", + "video/mp2t", + "video/mpeg", + "video/ogg", + "video/webm", + "video/x-msvideo", + "video/x-flv"}; + +// Only contains non obvious extensions. +const std::map mimeTypesExtensions{ + {"application/atom+xml", "xml"}, + {"application/font-woff", "woff"}, + {"application/gzip", "gz"}, + {"application/java-archive", "jar"}, + {"application/javascript", "js"}, + {"application/ld+json", "jsonld"}, + {"application/msword", "doc"}, + {"application/octet-stream", "bin"}, + {"application/ogg", "ogx"}, + {"application/vnd.ms-excel", "xls"}, + {"application/vnd.ms-fontobject", "eot"}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, + {"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"}, + {"video/ogg", "ogv"}, + {"video/x-msvideo", "avi"}}; + +const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", + "image/png", "image/jpeg", "image/gif", + "video/mp4", "video/mpeg", "text/html"}; + +const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; + +}