From 0e11550f7fd8e1af1d79e870fb39ec91aac7af16 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 25 Jun 2024 19:24:30 +0200 Subject: [PATCH 1/2] refactor: internet module - internet module migration from class to functions within internet namespace - modified reference to functions from internet module in all dependent modules Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/Internet.h | 118 ++++++++--------- src/modules/git/Git.cpp | 2 +- src/modules/internet/Internet.cpp | 68 +++++----- src/modules/internet/InternetData.h | 2 +- src/modules/person/Person.cpp | 2 +- src/modules/system/System.cpp | 2 +- tests/modules/internet/InternetTest.cpp | 167 ++++++++++++------------ tests/modules/person/PersonTest.cpp | 6 +- 8 files changed, 182 insertions(+), 185 deletions(-) diff --git a/include/faker-cxx/Internet.h b/include/faker-cxx/Internet.h index ba96ad5d8..3223e1e1b 100644 --- a/include/faker-cxx/Internet.h +++ b/include/faker-cxx/Internet.h @@ -7,7 +7,7 @@ #include "types/Country.h" -namespace faker +namespace faker::internet { enum class HttpResponseType { @@ -39,9 +39,6 @@ struct PasswordOptions bool symbols = true; }; -class Internet -{ -public: /** * @brief Generates a username using the given person's name as base. * @@ -51,13 +48,13 @@ class Internet * @returns Username including neither, one or both of the provided names. * * @code - * Internet::username() // "Richardson.Jeffrey1997" - * Internet::username("Michael") // "Michael_Allen29" - * Internet::username(std::nullopt, "Cieslar") // "Phillip_Cieslar20" - * Internet::username("Andrew", "Cieslar") // "Andrew.Cieslar" + * internet::username() // "Richardson.Jeffrey1997" + * internet::username("Michael") // "Michael_Allen29" + * internet::username(std::nullopt, "Cieslar") // "Phillip_Cieslar20" + * internet::username("Andrew", "Cieslar") // "Andrew.Cieslar" * @endcode */ - static std::string username(std::optional firstName = std::nullopt, + std::string username(std::optional firstName = std::nullopt, std::optional lastName = std::nullopt, Country country = Country::Usa); /** @@ -70,14 +67,14 @@ class Internet * @returns Email including neither, one or both of the provided names, with random/provided email host. * * @code - * Internet::email() // "Jimenez.Clyde@gmail.com" - * Internet::email("Tom") // "TomRichardson79@outlook.com" - * Internet::email(std::nullopt, "Howard") // "FreddieHoward@hotmail.com" - * Internet::email("Cindy", "Young") // "Young_Cindy@gmail.com" - * Internet::email(std::nullopt, std::nullopt, "example.com") // "Wright.Edna1973@code.com" + * internet::email() // "Jimenez.Clyde@gmail.com" + * internet::email("Tom") // "TomRichardson79@outlook.com" + * internet::email(std::nullopt, "Howard") // "FreddieHoward@hotmail.com" + * internet::email("Cindy", "Young") // "Young_Cindy@gmail.com" + * internet::email(std::nullopt, std::nullopt, "example.com") // "Wright.Edna1973@code.com" * @endcode */ - static std::string email(std::optional firstName = std::nullopt, + std::string email(std::optional firstName = std::nullopt, std::optional lastName = std::nullopt, std::optional emailHost = std::nullopt); @@ -90,10 +87,10 @@ class Internet * @returns Email including neither, one or both of the provided names, with example email host. * * @code - * Internet::exampleEmail() // "Jimenez.Clyde@example.com" + * internet::exampleEmail() // "Jimenez.Clyde@example.com" * @endcode */ - static std::string exampleEmail(std::optional firstName = std::nullopt, + std::string exampleEmail(std::optional firstName = std::nullopt, std::optional lastName = std::nullopt); /** @@ -105,11 +102,11 @@ class Internet * @returns Random password-like string. * * @code - * Internet::password() // "gXGpe9pKfFcKy9R" - * Internet::password(25) // "xv8vDu*wM!Rg0$zd0kH%8p!WY" + * internet::password() // "gXGpe9pKfFcKy9R" + * internet::password(25) // "xv8vDu*wM!Rg0$zd0kH%8p!WY" * @endcode */ - static std::string password(int length = 15, const PasswordOptions& options = {}); + std::string password(int length = 15, const PasswordOptions& options = {}); enum class EmojiType { @@ -133,11 +130,11 @@ class Internet * @returns Emoji. * * @code - * Internet::emoji() // "👑" - * Internet::emoji(EmojiType::Food) // "🍕" + * internet::emoji() // "👑" + * internet::emoji(EmojiType::Food) // "🍕" * @endcode */ - static std::string_view emoji(std::optional type = std::nullopt); + std::string_view emoji(std::optional type = std::nullopt); /** * @brief Verify that a given emoji is valid. @@ -147,10 +144,10 @@ class Internet * @returns true if emojiToCheck is found in one of the vectors, false otherwise. * * @code - * Internet::checkIfEmojiIsValid("👑") // true + * internet::checkIfEmojiIsValid("👑") // true * @endcode */ - static bool checkIfEmojiIsValid(const std::string& emojiToCheck); + bool checkIfEmojiIsValid(const std::string& emojiToCheck); /** * @brief Returns a random web protocol. Either `http` or `https`. @@ -158,10 +155,10 @@ class Internet * @returns Web protocol. * * @code - * Internet::protocol() // "https" + * internet::protocol() // "https" * @endcode */ - static std::string_view protocol(); + std::string_view protocol(); /** * @brief Generates a random http method name. @@ -169,10 +166,10 @@ class Internet * @returns Http method name. * * @code - * Internet::httpMethod() // "POST" + * internet::httpMethod() // "POST" * @endcode */ - static std::string_view httpMethod(); + std::string_view httpMethod(); /** * @brief Returns a random http status code. @@ -182,11 +179,11 @@ class Internet * @returns Http status code. * * @code - * Internet::httpStatusCode() // 500 - * Internet::httpStatusCode(HttpStatusCodeType::success) // 201 + * internet::httpStatusCode() // 500 + * internet::httpStatusCode(HttpStatusCodeType::success) // 201 * @endcode */ - static unsigned httpStatusCode(std::optional responseType = std::nullopt); + unsigned httpStatusCode(std::optional responseType = std::nullopt); /** * @brief Generates a random http request header. @@ -194,10 +191,10 @@ class Internet * @returns Http request header. * * @code - * Internet::httpRequestHeader() // "Authorization" + * internet::httpRequestHeader() // "Authorization" * @endcode */ - static std::string_view httpRequestHeader(); + std::string_view httpRequestHeader(); /** * @brief Generates a random http response header. @@ -205,10 +202,10 @@ class Internet * @returns Http response header. * * @code - * Internet::httpResponseHeader() // "Location" + * internet::httpResponseHeader() // "Location" * @endcode */ - static std::string_view httpResponseHeader(); + std::string_view httpResponseHeader(); /** * @brief Generates a random http media type. @@ -216,10 +213,10 @@ class Internet * @returns Http media type. * * @code - * Internet::httpMediaType() // "application/json" + * internet::httpMediaType() // "application/json" * @endcode */ - static std::string_view httpMediaType(); + std::string_view httpMediaType(); /** * @brief Returns a string containing randomized ipv4 address of the given class. @@ -229,11 +226,11 @@ class Internet * @return String representation of the ipv4 address. * * @code - * Internet::ipv4() // "192.168.0.1" - * Internet::ipv4(IPv4Class::classA) // "10.0.0.1" + * internet::ipv4() // "192.168.0.1" + * internet::ipv4(IPv4Class::classA) // "10.0.0.1" * @endcode */ - static std::string ipv4(const IPv4Class& ipv4class = IPv4Class::C); + std::string ipv4(const IPv4Class& ipv4class = IPv4Class::C); /** * @brief Returns a string containing randomized ipv4 address based on given base address and mask. @@ -248,11 +245,11 @@ class Internet * @return std::string representation of the ipv4 address. * * @code - * Internet::ipv4({255.0.0.0}, {10.100.100.100}) // "10.128.17.1" - * Internet::ipv4({255.255.128.0}, {129.168.255.0}) // "192.168.128.10" + * internet::ipv4({255.0.0.0}, {10.100.100.100}) // "10.128.17.1" + * internet::ipv4({255.255.128.0}, {129.168.255.0}) // "192.168.128.10" * @endcode */ - static std::string ipv4(const std::array& baseIpv4Address, + std::string ipv4(const std::array& baseIpv4Address, const std::array& generationMask); /** @@ -261,10 +258,10 @@ class Internet * @return String representation of the ipv6 address. * * @code - * Internet::ipv6() // "269f:1230:73e3:318d:842b:daab:326d:897b" + * internet::ipv6() // "269f:1230:73e3:318d:842b:daab:326d:897b" * @endcode */ - static std::string ipv6(); + std::string ipv6(); /** * @brief Returns a generated random mac address. @@ -274,10 +271,10 @@ class Internet * @return A generated random mac address. * * @code - * Internet::mac() // "2d:10:34:2f:ac:ac" + * internet::mac() // "2d:10:34:2f:ac:ac" * @endcode */ - static std::string mac(const std::string& sep = ":"); + std::string mac(const std::string& sep = ":"); /** * @brief Generates a random port. @@ -285,10 +282,10 @@ class Internet * @return Port. * * @code - * Internet::port() // 5432 + * internet::port() // 5432 * @endcode */ - static unsigned port(); + unsigned port(); /** * @brief Generates a random url. @@ -298,10 +295,10 @@ class Internet * @return Http(s) url. * * @code - * Internet::url() // "https://slow-timer.info" + * internet::url() // "https://slow-timer.info" * @endcode */ - static std::string url(const WebProtocol& webProtocol = WebProtocol::Https); + std::string url(const WebProtocol& webProtocol = WebProtocol::Https); /** * @brief Generates a random domain name. @@ -309,10 +306,10 @@ class Internet * @return Domain name. * * @code - * Internet::domainName() // "slow-timer.info" + * internet::domainName() // "slow-timer.info" * @endcode */ - static std::string domainName(); + std::string domainName(); /** * @brief Generates a random domain word. @@ -320,10 +317,10 @@ class Internet * @return Domain word. * * @code - * Internet::domainWord() // "close-reality" + * internet::domainWord() // "close-reality" * @endcode */ - static std::string domainWord(); + std::string domainWord(); /** * @brief Generates a random domain suffix. @@ -331,10 +328,10 @@ class Internet * @return Domain suffix. * * @code - * Internet::domainSuffix() // "com" + * internet::domainSuffix() // "com" * @endcode */ - static std::string_view domainSuffix(); + std::string_view domainSuffix(); /** * @brief Generates a random username. @@ -344,9 +341,8 @@ class Internet * @return Username. * * @code - * Internet::anonymousUsername() // "profusebrother", "richad", "powerfuldifferential" + * internet::anonymousUsername() // "profusebrother", "richad", "powerfuldifferential" * @endcode */ - static std::string anonymousUsername(unsigned maxLength); -}; + std::string anonymousUsername(unsigned maxLength); } diff --git a/src/modules/git/Git.cpp b/src/modules/git/Git.cpp index 9676a2069..c90a94eed 100644 --- a/src/modules/git/Git.cpp +++ b/src/modules/git/Git.cpp @@ -94,7 +94,7 @@ std::string commitEntry(std::optional dateYears, std::optional(Person::firstName(country)); const auto lastName = static_cast(Person::lastName(country)); - entry += "\nAuthor: " + firstName + " " + lastName + " " + Internet::email(firstName, lastName) + "\nDate: "; + entry += "\nAuthor: " + firstName + " " + lastName + " " + internet::email(firstName, lastName) + "\nDate: "; if (dateYears) { diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index 117916cec..46c79ae63 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -22,7 +22,7 @@ #include "InternetData.h" #include "modules/string/StringData.h" -namespace faker +namespace faker::internet { namespace { @@ -51,17 +51,17 @@ constexpr unsigned int ipv4ClassBSecondSectorLowerBound = 16u; constexpr unsigned int ipv4ClassBSecondSectorUpperBound = 31u; constexpr unsigned int ipv4SectorUpperBound = 255u; -const std::map> emojiTypeToEmojisMapping = { - {Internet::EmojiType::Smiley, Helper::toVector(smileyEmojis)}, - {Internet::EmojiType::Body, Helper::toVector(bodyEmojis)}, - {Internet::EmojiType::Person, Helper::toVector(personEmojis)}, - {Internet::EmojiType::Nature, Helper::toVector(natureEmojis)}, - {Internet::EmojiType::Food, Helper::toVector(foodEmojis)}, - {Internet::EmojiType::Travel, Helper::toVector(travelEmojis)}, - {Internet::EmojiType::Activity, Helper::toVector(activityEmojis)}, - {Internet::EmojiType::Object, Helper::toVector(objectEmojis)}, - {Internet::EmojiType::Symbol, Helper::toVector(symbolEmojis)}, - {Internet::EmojiType::Flag, Helper::toVector(flagEmojis)}, +const std::map> emojiTypeToEmojisMapping = { + {EmojiType::Smiley, Helper::toVector(smileyEmojis)}, + {EmojiType::Body, Helper::toVector(bodyEmojis)}, + {EmojiType::Person, Helper::toVector(personEmojis)}, + {EmojiType::Nature, Helper::toVector(natureEmojis)}, + {EmojiType::Food, Helper::toVector(foodEmojis)}, + {EmojiType::Travel, Helper::toVector(travelEmojis)}, + {EmojiType::Activity, Helper::toVector(activityEmojis)}, + {EmojiType::Object, Helper::toVector(objectEmojis)}, + {EmojiType::Symbol, Helper::toVector(symbolEmojis)}, + {EmojiType::Flag, Helper::toVector(flagEmojis)}, }; } @@ -85,7 +85,7 @@ std::vector getAllEmojis() return emojis; } -std::string Internet::username(std::optional firstNameInit, std::optional lastNameInit, +std::string username(std::optional firstNameInit, std::optional lastNameInit, Country country) { const auto firstName = firstNameInit ? *firstNameInit : Person::firstName(country); @@ -112,20 +112,20 @@ std::string Internet::username(std::optional firstNameInit, std::op return username; } -std::string Internet::email(std::optional firstName, std::optional lastName, +std::string email(std::optional firstName, std::optional lastName, std::optional emailHost) { return FormatHelper::format("{}@{}", username(std::move(firstName), std::move(lastName)), emailHost ? *emailHost : Helper::arrayElement(emailHosts)); } -std::string Internet::exampleEmail(std::optional firstName, std::optional lastName) +std::string exampleEmail(std::optional firstName, std::optional lastName) { return FormatHelper::format("{}@{}", username(std::move(firstName), std::move(lastName)), Helper::arrayElement(emailExampleHosts)); } -std::string Internet::password(int length, const PasswordOptions& options) +std::string password(int length, const PasswordOptions& options) { std::string characters; @@ -159,7 +159,7 @@ std::string Internet::password(int length, const PasswordOptions& options) return password; } -std::string_view Internet::emoji(std::optional type) +std::string_view emoji(std::optional type) { if (type) { @@ -172,23 +172,23 @@ std::string_view Internet::emoji(std::optional type) return Helper::arrayElement(emojis); } -bool Internet::checkIfEmojiIsValid(const std::string& emojiToCheck) +bool checkIfEmojiIsValid(const std::string& emojiToCheck) { const auto emojis = getAllEmojis(); return std::find(emojis.begin(), emojis.end(), emojiToCheck) != emojis.end(); } -std::string_view Internet::protocol() +std::string_view protocol() { return Helper::arrayElement(webProtocols); } -std::string_view Internet::httpMethod() +std::string_view httpMethod() { return Helper::arrayElement(httpMethodNames); } -unsigned Internet::httpStatusCode(std::optional responseType) +unsigned httpStatusCode(std::optional responseType) { if (responseType) { @@ -211,22 +211,22 @@ unsigned Internet::httpStatusCode(std::optional responseType) return Helper::arrayElement(statusCodes); } -std::string_view Internet::httpRequestHeader() +std::string_view httpRequestHeader() { return Helper::arrayElement(httpRequestHeaders); } -std::string_view Internet::httpResponseHeader() +std::string_view httpResponseHeader() { return Helper::arrayElement(httpResponseHeaders); } -std::string_view Internet::httpMediaType() +std::string_view httpMediaType() { return Helper::arrayElement(httpMediaTypes); } -std::string Internet::ipv4(const IPv4Class& ipv4class) +std::string ipv4(const IPv4Class& ipv4class) { std::array sectors{}; @@ -257,7 +257,7 @@ std::string Internet::ipv4(const IPv4Class& ipv4class) return FormatHelper::format("{}.{}.{}.{}", sectors[0], sectors[1], sectors[2], sectors[3]); } -std::string Internet::ipv4(const std::array& baseIpv4Address, +std::string ipv4(const std::array& baseIpv4Address, const std::array& generationMask) { std::array sectors{}; @@ -271,7 +271,7 @@ std::string Internet::ipv4(const std::array& baseIpv4Address, return FormatHelper::format("{}.{}.{}.{}", sectors[0], sectors[1], sectors[2], sectors[3]); } -std::string Internet::ipv6() +std::string ipv6() { std::vector ipv6Parts; @@ -285,7 +285,7 @@ std::string Internet::ipv6() return StringHelper::joinString(ipv6Parts, ":"); } -std::string Internet::mac(const std::string& sep) +std::string mac(const std::string& sep) { std::string mac; std::string currentSep = sep; @@ -309,34 +309,34 @@ std::string Internet::mac(const std::string& sep) return mac; } -unsigned Internet::port() +unsigned port() { return number::integer(65535u); } -std::string Internet::url(const WebProtocol& webProtocol) +std::string url(const WebProtocol& webProtocol) { const auto protocol = webProtocol == WebProtocol::Https ? "https" : "http"; return FormatHelper::format("{}://{}", protocol, domainName()); } -std::string Internet::domainName() +std::string domainName() { return FormatHelper::format("{}.{}", domainWord(), domainSuffix()); } -std::string Internet::domainWord() +std::string domainWord() { return StringHelper::toLower(FormatHelper::format("{}-{}", word::adjective(), word::noun())); } -std::string_view Internet::domainSuffix() +std::string_view domainSuffix() { return Helper::arrayElement(domainSuffixes); } -std::string Internet::anonymousUsername(unsigned maxLength) +std::string anonymousUsername(unsigned maxLength) { unsigned defaultMin = 6; unsigned defaultMax = 20; diff --git a/src/modules/internet/InternetData.h b/src/modules/internet/InternetData.h index aa19978d6..551040fcf 100644 --- a/src/modules/internet/InternetData.h +++ b/src/modules/internet/InternetData.h @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::internet { const auto domainSuffixes = std::to_array({ "biz", "com", "info", "name", "net", "org", diff --git a/src/modules/person/Person.cpp b/src/modules/person/Person.cpp index 488dcba22..212c44b9b 100644 --- a/src/modules/person/Person.cpp +++ b/src/modules/person/Person.cpp @@ -328,7 +328,7 @@ std::string Person::bio() {"bio_part", []() { return Helper::arrayElement(bioParts); }}, {"bio_supporter", []() { return Helper::arrayElement(bioSupporters); }}, {"noun", []() { return word::noun(); }}, - {"emoji", []() { return Internet::emoji(); }}}; + {"emoji", []() { return internet::emoji(); }}}; return FormatHelper::fillTokenValues(randomBioFormat, dataGeneratorsMapping); } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index b5c5aab36..b2f198f65 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -216,7 +216,7 @@ std::string networkInterface(const std::optional& optio } else if (interfaceSchema == "mac") { - suffix = Internet::mac(""); + suffix = internet::mac(""); } else if (interfaceSchema == "pci") { diff --git a/tests/modules/internet/InternetTest.cpp b/tests/modules/internet/InternetTest.cpp index 10c0841ce..88d368aa3 100644 --- a/tests/modules/internet/InternetTest.cpp +++ b/tests/modules/internet/InternetTest.cpp @@ -21,6 +21,7 @@ using namespace ::testing; using namespace faker; +using namespace faker::internet; namespace { @@ -102,23 +103,23 @@ TEST_F(InternetTest, shouldGenerateUsername) std::vector firstNames(englishMaleFirstNames.begin(), englishMaleFirstNames.end()); firstNames.insert(firstNames.end(), englishFemaleFirstNames.begin(), englishFemaleFirstNames.end()); - const auto username = Internet::username(); + const auto generatedUsername = username(); - ASSERT_TRUE(std::ranges::any_of(firstNames, [username](const std::string_view& firstName) - { return username.find(firstName) != std::string::npos; })); - ASSERT_TRUE(std::ranges::any_of(englishLastNames, [username](const std::string_view& lastName) - { return username.find(lastName) != std::string::npos; })); + ASSERT_TRUE(std::ranges::any_of(firstNames, [generatedUsername](const std::string_view& firstName) + { return generatedUsername.find(firstName) != std::string::npos; })); + ASSERT_TRUE(std::ranges::any_of(englishLastNames, [generatedUsername](const std::string_view& lastName) + { return generatedUsername.find(lastName) != std::string::npos; })); } TEST_F(InternetTest, shouldGenerateUsernameWithFirstNameProvided) { const auto firstName = "Michael"; - const auto username = Internet::username(firstName); + const auto generatedUsername = username(firstName); - ASSERT_TRUE(username.find(firstName) != std::string::npos); - ASSERT_TRUE(std::ranges::any_of(englishLastNames, [username](const std::string_view& lastName) - { return username.find(lastName) != std::string::npos; })); + ASSERT_TRUE(generatedUsername.find(firstName) != std::string::npos); + ASSERT_TRUE(std::ranges::any_of(englishLastNames, [generatedUsername](const std::string_view& lastName) + { return generatedUsername.find(lastName) != std::string::npos; })); } TEST_F(InternetTest, shouldGenerateUsernameWithLastNameProvided) @@ -128,11 +129,11 @@ TEST_F(InternetTest, shouldGenerateUsernameWithLastNameProvided) const auto lastName = "Cieslar"; - const auto username = Internet::username(std::nullopt, lastName); + const auto generatedUsername = username(std::nullopt, lastName); - ASSERT_TRUE(std::ranges::any_of(firstNames, [username](const std::string_view& firstName) - { return username.find(firstName) != std::string::npos; })); - ASSERT_TRUE(username.find(lastName) != std::string::npos); + ASSERT_TRUE(std::ranges::any_of(firstNames, [generatedUsername](const std::string_view& firstName) + { return generatedUsername.find(firstName) != std::string::npos; })); + ASSERT_TRUE(generatedUsername.find(lastName) != std::string::npos); } TEST_F(InternetTest, shouldGenerateUsernameWithFullNameProvided) @@ -141,10 +142,10 @@ TEST_F(InternetTest, shouldGenerateUsernameWithFullNameProvided) const auto lastName = "Cieslar"; - const auto username = Internet::username(firstName, lastName); + const auto generatedUsername = username(firstName, lastName); - ASSERT_TRUE(username.find(firstName) != std::string::npos); - ASSERT_TRUE(username.find(lastName) != std::string::npos); + ASSERT_TRUE(generatedUsername.find(firstName) != std::string::npos); + ASSERT_TRUE(generatedUsername.find(lastName) != std::string::npos); } TEST_F(InternetTest, shouldGenerateInternationalUsernames) @@ -152,13 +153,13 @@ TEST_F(InternetTest, shouldGenerateInternationalUsernames) std::vector firstNames(polishMaleFirstNames.begin(), polishMaleFirstNames.end()); firstNames.insert(firstNames.end(), polishFemaleFirstNames.begin(), polishFemaleFirstNames.end()); - const auto username = Internet::username(std::nullopt, std::nullopt, Country::Poland); + const auto generatedUsername = username(std::nullopt, std::nullopt, Country::Poland); - ASSERT_TRUE(std::ranges::any_of(firstNames, [username](const std::string_view& firstName) - { return username.find(firstName) != std::string::npos; })); + ASSERT_TRUE(std::ranges::any_of(firstNames, [generatedUsername](const std::string_view& firstName) + { return generatedUsername.find(firstName) != std::string::npos; })); - ASSERT_TRUE(std::ranges::any_of(polishLastNames, [username](const std::string_view& lastName) - { return username.find(lastName) != std::string::npos; })); + ASSERT_TRUE(std::ranges::any_of(polishLastNames, [generatedUsername](const std::string_view& lastName) + { return generatedUsername.find(lastName) != std::string::npos; })); } TEST_F(InternetTest, shouldGenerateEmail) @@ -166,9 +167,9 @@ TEST_F(InternetTest, shouldGenerateEmail) std::vector firstNames(englishMaleFirstNames.begin(), englishMaleFirstNames.end()); firstNames.insert(firstNames.end(), englishFemaleFirstNames.begin(), englishFemaleFirstNames.end()); - const auto email = Internet::email(); + const auto generatedEmail = email(); - const auto emailParts = StringHelper::split(email, "@"); + const auto emailParts = StringHelper::split(generatedEmail, "@"); ASSERT_EQ(emailParts.size(), 2); @@ -187,9 +188,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithFirstName) { const auto firstName = "Tom"; - const auto email = Internet::email(firstName); + const auto generatedEmail = email(firstName); - const auto emailParts = StringHelper::split(email, "@"); + const auto emailParts = StringHelper::split(generatedEmail, "@"); ASSERT_EQ(emailParts.size(), 2); @@ -210,9 +211,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithLastName) const auto lastName = "Howard"; - const auto email = Internet::email(std::nullopt, lastName); + const auto generatedEmail = email(std::nullopt, lastName); - const auto emailParts = StringHelper::split(email, "@"); + const auto emailParts = StringHelper::split(generatedEmail, "@"); ASSERT_EQ(emailParts.size(), 2); @@ -232,9 +233,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithFullName) const auto lastName = "Young"; - const auto email = Internet::email(firstName, lastName); + const auto generatedEmail = email(firstName, lastName); - const auto emailParts = StringHelper::split(email, "@"); + const auto emailParts = StringHelper::split(generatedEmail, "@"); ASSERT_EQ(emailParts.size(), 2); @@ -254,9 +255,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithSpecifiedEmailHost) const auto emailHost = "example.com"; - const auto email = Internet::email(std::nullopt, std::nullopt, emailHost); + const auto generatedEmail = email(std::nullopt, std::nullopt, emailHost); - const auto emailParts = StringHelper::split(email, "@"); + const auto emailParts = StringHelper::split(generatedEmail, "@"); ASSERT_EQ(emailParts.size(), 2); @@ -275,7 +276,7 @@ TEST_F(InternetTest, shouldGenerateExampleEmail) std::vector firstNames(englishMaleFirstNames.begin(), englishMaleFirstNames.end()); firstNames.insert(firstNames.end(), englishFemaleFirstNames.begin(), englishFemaleFirstNames.end()); - const auto email = Internet::exampleEmail(); + const auto email = exampleEmail(); const auto emailParts = StringHelper::split(email, "@"); @@ -296,7 +297,7 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithFirstName) { const auto firstName = "Barry"; - const auto email = Internet::exampleEmail(firstName); + const auto email = exampleEmail(firstName); const auto emailParts = StringHelper::split(email, "@"); @@ -319,7 +320,7 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithLastName) const auto lastName = "Wilkinson"; - const auto email = Internet::exampleEmail(std::nullopt, lastName); + const auto email = exampleEmail(std::nullopt, lastName); const auto emailParts = StringHelper::split(email, "@"); @@ -341,7 +342,7 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithFullName) const auto lastName = "Brown"; - const auto email = Internet::exampleEmail(firstName, lastName); + const auto email = exampleEmail(firstName, lastName); const auto emailParts = StringHelper::split(email, "@"); @@ -358,10 +359,10 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithFullName) TEST_F(InternetTest, shouldGeneratePassword) { - const auto password = Internet::password(); + const auto generatedPassword = password(); - ASSERT_EQ(password.size(), 15); - ASSERT_TRUE(std::ranges::all_of(password, [&](char passwordCharacter) + ASSERT_EQ(generatedPassword.size(), 15); + ASSERT_TRUE(std::ranges::all_of(generatedPassword, [&](char passwordCharacter) { return passwordCharacters.find(passwordCharacter) != std::string::npos; })); } @@ -369,16 +370,16 @@ TEST_F(InternetTest, shouldGeneratePasswordWithSpecifiedLength) { const auto passwordLength = 25; - const auto password = Internet::password(passwordLength); + const auto generatedPassword = password(passwordLength); - ASSERT_EQ(password.size(), passwordLength); - ASSERT_TRUE(std::ranges::all_of(password, [&](char passwordCharacter) + ASSERT_EQ(generatedPassword.size(), passwordLength); + ASSERT_TRUE(std::ranges::all_of(generatedPassword, [&](char passwordCharacter) { return passwordCharacters.find(passwordCharacter) != std::string::npos; })); } TEST_F(InternetTest, shouldGenerateEmoji) { - const auto generatedEmoji = Internet::emoji(); + const auto generatedEmoji = emoji(); std::vector emojis; emojis.reserve(smileyEmojis.size() + bodyEmojis.size() + personEmojis.size() + natureEmojis.size() + @@ -402,7 +403,7 @@ TEST_F(InternetTest, shouldGenerateEmoji) TEST_F(InternetTest, shouldGenerateSmileyEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Smiley); + const auto generatedEmoji = emoji(EmojiType::Smiley); ASSERT_TRUE(std::ranges::any_of(smileyEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -410,7 +411,7 @@ TEST_F(InternetTest, shouldGenerateSmileyEmoji) TEST_F(InternetTest, shouldGenerateBodyEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Body); + const auto generatedEmoji = emoji(EmojiType::Body); ASSERT_TRUE(std::ranges::any_of(bodyEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -418,7 +419,7 @@ TEST_F(InternetTest, shouldGenerateBodyEmoji) TEST_F(InternetTest, shouldGeneratePersonEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Person); + const auto generatedEmoji = emoji(EmojiType::Person); ASSERT_TRUE(std::ranges::any_of(personEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -426,7 +427,7 @@ TEST_F(InternetTest, shouldGeneratePersonEmoji) TEST_F(InternetTest, shouldGenerateNatureEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Nature); + const auto generatedEmoji = emoji(EmojiType::Nature); ASSERT_TRUE(std::ranges::any_of(natureEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -434,7 +435,7 @@ TEST_F(InternetTest, shouldGenerateNatureEmoji) TEST_F(InternetTest, shouldGenerateFoodEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Food); + const auto generatedEmoji = emoji(EmojiType::Food); ASSERT_TRUE(std::ranges::any_of(foodEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -442,7 +443,7 @@ TEST_F(InternetTest, shouldGenerateFoodEmoji) TEST_F(InternetTest, shouldGenerateTravelEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Travel); + const auto generatedEmoji = emoji(EmojiType::Travel); ASSERT_TRUE(std::ranges::any_of(travelEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -450,7 +451,7 @@ TEST_F(InternetTest, shouldGenerateTravelEmoji) TEST_F(InternetTest, shouldGenerateActivityEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Activity); + const auto generatedEmoji = emoji(EmojiType::Activity); ASSERT_TRUE(std::ranges::any_of(activityEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -458,7 +459,7 @@ TEST_F(InternetTest, shouldGenerateActivityEmoji) TEST_F(InternetTest, shouldGenerateObjectEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Object); + const auto generatedEmoji = emoji(EmojiType::Object); ASSERT_TRUE(std::ranges::any_of(objectEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -466,7 +467,7 @@ TEST_F(InternetTest, shouldGenerateObjectEmoji) TEST_F(InternetTest, shouldGenerateSymbolEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Symbol); + const auto generatedEmoji = emoji(EmojiType::Symbol); ASSERT_TRUE(std::ranges::any_of(symbolEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -474,7 +475,7 @@ TEST_F(InternetTest, shouldGenerateSymbolEmoji) TEST_F(InternetTest, shouldGenerateFlagEmoji) { - const auto generatedEmoji = Internet::emoji(Internet::EmojiType::Flag); + const auto generatedEmoji = emoji(EmojiType::Flag); ASSERT_TRUE(std::ranges::any_of(flagEmojis, [generatedEmoji](const std::string_view& emoji) { return generatedEmoji == emoji; })); @@ -482,7 +483,7 @@ TEST_F(InternetTest, shouldGenerateFlagEmoji) TEST_F(InternetTest, shouldGenerateProtocol) { - const auto webProtocol = Internet::protocol(); + const auto webProtocol = protocol(); ASSERT_TRUE(std::ranges::any_of(webProtocols, [webProtocol](const std::string_view& protocol) { return webProtocol == protocol; })); @@ -490,7 +491,7 @@ TEST_F(InternetTest, shouldGenerateProtocol) TEST_F(InternetTest, shouldGenerateHttpMethod) { - const auto generatedHttpMethod = Internet::httpMethod(); + const auto generatedHttpMethod = httpMethod(); ASSERT_TRUE(std::ranges::any_of(httpMethodNames, [generatedHttpMethod](const std::string_view& httpMethod) { return generatedHttpMethod == httpMethod; })); @@ -498,7 +499,7 @@ TEST_F(InternetTest, shouldGenerateHttpMethod) TEST_F(InternetTest, shouldGenerateHttpStatusCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(); + const auto generatedHttpStatusCode = httpStatusCode(); std::vector statusCodes; statusCodes.insert(statusCodes.end(), httpStatusInformationalCodes.begin(), httpStatusInformationalCodes.end()); @@ -513,7 +514,7 @@ TEST_F(InternetTest, shouldGenerateHttpStatusCode) TEST_F(InternetTest, shouldGenerateHttpRequestHeader) { - const auto generatedHttpRequestHeader = Internet::httpRequestHeader(); + const auto generatedHttpRequestHeader = httpRequestHeader(); ASSERT_TRUE(std::ranges::any_of(httpRequestHeaders, [generatedHttpRequestHeader](const std::string_view& httpHeader) { return generatedHttpRequestHeader == httpHeader; })); @@ -521,7 +522,7 @@ TEST_F(InternetTest, shouldGenerateHttpRequestHeader) TEST_F(InternetTest, shouldGenerateHttpResponseHeader) { - const auto generatedHttpResponseHeader = Internet::httpResponseHeader(); + const auto generatedHttpResponseHeader = httpResponseHeader(); ASSERT_TRUE(std::ranges::any_of(httpResponseHeaders, [generatedHttpResponseHeader](const std::string_view& httpHeader) @@ -530,7 +531,7 @@ TEST_F(InternetTest, shouldGenerateHttpResponseHeader) TEST_F(InternetTest, shouldGenerateHttpMediaType) { - const auto generatedHttpMediaType = Internet::httpMediaType(); + const auto generatedHttpMediaType = httpMediaType(); ASSERT_TRUE(std::ranges::any_of(httpMediaTypes, [generatedHttpMediaType](const std::string_view& httpMediaType) { return generatedHttpMediaType == httpMediaType; })); @@ -538,7 +539,7 @@ TEST_F(InternetTest, shouldGenerateHttpMediaType) TEST_F(InternetTest, shouldGenerateHttpInformationalSuccessCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::Informational); + const auto generatedHttpStatusCode = httpStatusCode(HttpResponseType::Informational); ASSERT_TRUE(std::ranges::any_of(httpStatusInformationalCodes, [generatedHttpStatusCode](unsigned statusCode) { return generatedHttpStatusCode == statusCode; })); @@ -546,7 +547,7 @@ TEST_F(InternetTest, shouldGenerateHttpInformationalSuccessCode) TEST_F(InternetTest, shouldGenerateHttpStatusSuccessCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::Success); + const auto generatedHttpStatusCode = httpStatusCode(HttpResponseType::Success); ASSERT_TRUE(std::ranges::any_of(httpStatusSuccessCodes, [generatedHttpStatusCode](unsigned statusCode) { return generatedHttpStatusCode == statusCode; })); @@ -554,7 +555,7 @@ TEST_F(InternetTest, shouldGenerateHttpStatusSuccessCode) TEST_F(InternetTest, shouldGenerateHttpStatusRedirectionCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::Redirection); + const auto generatedHttpStatusCode = httpStatusCode(HttpResponseType::Redirection); ASSERT_TRUE(std::ranges::any_of(httpStatusRedirectionCodes, [generatedHttpStatusCode](unsigned statusCode) { return generatedHttpStatusCode == statusCode; })); @@ -562,7 +563,7 @@ TEST_F(InternetTest, shouldGenerateHttpStatusRedirectionCode) TEST_F(InternetTest, shouldGenerateHttpStatusClientErrorCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::ClientError); + const auto generatedHttpStatusCode = httpStatusCode(HttpResponseType::ClientError); ASSERT_TRUE(std::ranges::any_of(httpStatusClientErrorCodes, [generatedHttpStatusCode](unsigned statusCode) { return generatedHttpStatusCode == statusCode; })); @@ -570,7 +571,7 @@ TEST_F(InternetTest, shouldGenerateHttpStatusClientErrorCode) TEST_F(InternetTest, shouldGenerateHttpStatusServerErrorCode) { - const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::ServerError); + const auto generatedHttpStatusCode = httpStatusCode(HttpResponseType::ServerError); ASSERT_TRUE(std::ranges::any_of(httpStatusServerErrorCodes, [generatedHttpStatusCode](unsigned statusCode) { return generatedHttpStatusCode == statusCode; })); @@ -578,7 +579,7 @@ TEST_F(InternetTest, shouldGenerateHttpStatusServerErrorCode) TEST_F(InternetTest, shouldGenerateIpv4WithPrivateClassAAddress) { - const auto generatedIpv4 = Internet::ipv4(IPv4Class::A); + const auto generatedIpv4 = ipv4(IPv4Class::A); const auto addressSectors = deconstructIpv4String(generatedIpv4); ASSERT_EQ(addressSectors[0], classAFirstSection); @@ -586,7 +587,7 @@ TEST_F(InternetTest, shouldGenerateIpv4WithPrivateClassAAddress) TEST_F(InternetTest, shouldGenerateIpv4WithPrivateClassBAddress) { - const auto generatedIpv4 = Internet::ipv4(IPv4Class::B); + const auto generatedIpv4 = ipv4(IPv4Class::B); const auto addressSectors = deconstructIpv4String(generatedIpv4); @@ -597,7 +598,7 @@ TEST_F(InternetTest, shouldGenerateIpv4WithPrivateClassBAddress) TEST_F(InternetTest, shouldGenerateIpv4WithPrivateClassCAddress) { - const auto generatedIpv4 = Internet::ipv4(IPv4Class::C); + const auto generatedIpv4 = ipv4(IPv4Class::C); const auto addressSectors = deconstructIpv4String(generatedIpv4); ASSERT_EQ(addressSectors[0], classCFirstSection); @@ -609,7 +610,7 @@ TEST_F(InternetTest, shouldGenerateIpv4KeepingTheMaskedPart) const std::array sampleAddress = {192, 168, 10, 12}; const std::array generationMask = {255, 128, 0, 0}; - const auto generatedAddress = deconstructIpv4String(Internet::ipv4(sampleAddress, generationMask)); + const auto generatedAddress = deconstructIpv4String(ipv4(sampleAddress, generationMask)); constexpr unsigned int expectedSecondSectorMaskedValue = 0x00000080; @@ -619,7 +620,7 @@ TEST_F(InternetTest, shouldGenerateIpv4KeepingTheMaskedPart) TEST_F(InternetTest, shouldGenerateIpv6) { - const auto generatedIpv6 = Internet::ipv6(); + const auto generatedIpv6 = ipv6(); const auto generatedIpv6Parts = StringHelper::split(generatedIpv6, ":"); @@ -638,24 +639,24 @@ TEST_F(InternetTest, shouldGenerateIpv6) TEST_F(InternetTest, MacDefaultSeparator) { - const auto mac = Internet::mac(); + const auto generatedMac = mac(); - ASSERT_EQ(mac.size(), 17); + ASSERT_EQ(generatedMac.size(), 17); - for (size_t i = 0; i < mac.size(); i += 3) + for (size_t i = 0; i < generatedMac.size(); i += 3) { - ASSERT_TRUE(isxdigit(mac[i])); + ASSERT_TRUE(isxdigit(generatedMac[i])); } - for (size_t i = 2; i < mac.size(); i += 3) + for (size_t i = 2; i < generatedMac.size(); i += 3) { - ASSERT_EQ(mac[i], ':'); + ASSERT_EQ(generatedMac[i], ':'); } } TEST_F(InternetTest, shouldGenerateDomainSuffix) { - const auto generatedDomainSuffix = Internet::domainSuffix(); + const auto generatedDomainSuffix = domainSuffix(); ASSERT_TRUE(std::ranges::any_of(domainSuffixes, [generatedDomainSuffix](const std::string_view& domainSuffix) { return generatedDomainSuffix == domainSuffix; })); @@ -663,14 +664,14 @@ TEST_F(InternetTest, shouldGenerateDomainSuffix) TEST_F(InternetTest, shouldGenerateDomainWord) { - const auto generatedDomainWord = Internet::domainWord(); + const auto generatedDomainWord = domainWord(); assertDomainWord(generatedDomainWord); } TEST_F(InternetTest, shouldGenerateDomainName) { - const auto generatedDomainName = Internet::domainName(); + const auto generatedDomainName = domainName(); const auto generatedDomainNameParts = StringHelper::split(generatedDomainName, "."); @@ -684,7 +685,7 @@ TEST_F(InternetTest, shouldGenerateDomainName) TEST_F(InternetTest, shouldGenerateHttpsUrl) { - const auto generatedUrl = Internet::url(); + const auto generatedUrl = url(); const auto generatedUrlParts = StringHelper::split(generatedUrl, "://"); @@ -704,7 +705,7 @@ TEST_F(InternetTest, shouldGenerateHttpsUrl) TEST_F(InternetTest, shouldGenerateHttpUrl) { - const auto generatedUrl = Internet::url(WebProtocol::Http); + const auto generatedUrl = url(WebProtocol::Http); const auto generatedUrlParts = StringHelper::split(generatedUrl, "://"); @@ -724,7 +725,7 @@ TEST_F(InternetTest, shouldGenerateHttpUrl) TEST_F(InternetTest, shouldGeneratePort) { - const auto generatedPort = Internet::port(); + const auto generatedPort = port(); ASSERT_GE(generatedPort, 0); ASSERT_LE(generatedPort, 65535); @@ -735,7 +736,7 @@ TEST_F(InternetTest, shouldGenerateAnonymousUsername) for (int i = 0; i < 100; i++) { const std::integral auto maxLength = number::integer(6, 20); - const auto generatedUsername = Internet::anonymousUsername(maxLength); + const auto generatedUsername = anonymousUsername(maxLength); ASSERT_EQ(generatedUsername.length(), maxLength); } @@ -744,7 +745,7 @@ TEST_F(InternetTest, shouldGenerateAnonymousUsername) TEST_F(InternetTest, shouldGenerateAnonymousUsernameWithMinLength) { const auto maxLength = 5; - const auto generatedUsername = Internet::anonymousUsername(maxLength); + const auto generatedUsername = anonymousUsername(maxLength); ASSERT_EQ(generatedUsername.length(), 6); } @@ -752,7 +753,7 @@ TEST_F(InternetTest, shouldGenerateAnonymousUsernameWithMinLength) TEST_F(InternetTest, shouldGenerateAnonymousUsernameWithMaxLength) { const auto maxLength = 21; - const auto generatedUsername = Internet::anonymousUsername(maxLength); + const auto generatedUsername = anonymousUsername(maxLength); ASSERT_EQ(generatedUsername.length(), 20); } diff --git a/tests/modules/person/PersonTest.cpp b/tests/modules/person/PersonTest.cpp index 8d9f253be..f3073e407 100644 --- a/tests/modules/person/PersonTest.cpp +++ b/tests/modules/person/PersonTest.cpp @@ -768,7 +768,7 @@ bool checkTokenFormat(const std::string& bio) if (std::find(bioParts.begin(), bioParts.end(), std::string{matches[1]}) != bioParts.end() && std::find(bioParts.begin(), bioParts.end(), std::string{matches[2]}) != bioParts.end() && std::find(bioParts.begin(), bioParts.end(), std::string{matches[3]}) != bioParts.end() && - Internet::checkIfEmojiIsValid(std::string{matches[4]})) + internet::checkIfEmojiIsValid(std::string{matches[4]})) return true; } @@ -787,7 +787,7 @@ bool checkTokenFormat(const std::string& bio) // in the bio_part vector. if (std::find(word::nouns.begin(), word::nouns.end(), std::string{matches[1]}) != word::nouns.end() && std::find(bioSupporters.begin(), bioSupporters.end(), std::string{matches[2]}) != bioSupporters.end() && - Internet::checkIfEmojiIsValid(std::string{matches[3]})) + internet::checkIfEmojiIsValid(std::string{matches[3]})) return true; } @@ -808,7 +808,7 @@ bool checkTokenFormat(const std::string& bio) if (std::find(word::nouns.begin(), word::nouns.end(), std::string{matches[1]}) != word::nouns.end() && std::find(bioSupporters.begin(), bioSupporters.end(), std::string{matches[2]}) != bioSupporters.end() && std::find(bioParts.begin(), bioParts.end(), std::string{matches[3]}) != bioParts.end() && - Internet::checkIfEmojiIsValid(std::string{matches[4]})) + internet::checkIfEmojiIsValid(std::string{matches[4]})) return true; } From 1f933b0b534dd3b40a04099a4dd3fc5d48f217c7 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 25 Jun 2024 19:29:27 +0200 Subject: [PATCH 2/2] modified internet module reference in example Signed-off-by: Guru Mehar Rachaputi --- examples/basic/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/basic/main.cpp b/examples/basic/main.cpp index f809246b7..ee4608b96 100644 --- a/examples/basic/main.cpp +++ b/examples/basic/main.cpp @@ -9,8 +9,8 @@ int main() { const auto id = faker::String::uuid(); - const auto email = faker::Internet::email(); - const auto password = faker::Internet::password(); + const auto email = faker::internet::email(); + const auto password = faker::internet::password(); const auto city = faker::Location::city(); const auto streetAddress = faker::Location::streetAddress();