diff --git a/.gitignore b/.gitignore index 7c8da29c1..e256dca60 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,7 @@ build-linux-clang .cache/ #vim swap files *.sw? + +#kdevelop temp files +*.kdev4 +.kdev4 diff --git a/include/faker-cxx/Internet.h b/include/faker-cxx/Internet.h index af90ca453..5366d1eef 100644 --- a/include/faker-cxx/Internet.h +++ b/include/faker-cxx/Internet.h @@ -321,5 +321,18 @@ class Internet * @endcode */ static std::string domainSuffix(); + + /** + * @brief Generates a random username. + * + * @param maxLength maxLength of the generated username. + * + * @return Username. + * + * @code + * Internet::anonymousUsername() // "profusebrother", "richad", "powerfuldifferential" + * @endcode + */ + static std::string anonymousUsername(unsigned maxLength); }; } diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index f336810fd..490e4d206 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -320,4 +320,23 @@ std::string Internet::domainSuffix() return Helper::arrayElement(domainSuffixes); } +std::string Internet::anonymousUsername(unsigned maxLength) +{ + unsigned defaultMin = 6; + unsigned defaultMax = 20; + + if (maxLength < defaultMin) + maxLength = defaultMin; + else if (maxLength > defaultMax) + maxLength = defaultMax; + + unsigned adjectiveLength = Number::integer(3, 1 + maxLength/2); + unsigned nounLength = maxLength - adjectiveLength; + std::stringstream usernameBuilder; + + usernameBuilder << Word::adjective(adjectiveLength) << Word::noun(nounLength); + + return usernameBuilder.str(); +} + } diff --git a/src/modules/internet/InternetTest.cpp b/src/modules/internet/InternetTest.cpp index 556f3819b..f5608bb98 100644 --- a/src/modules/internet/InternetTest.cpp +++ b/src/modules/internet/InternetTest.cpp @@ -1,4 +1,5 @@ #include "faker-cxx/Internet.h" +#include "faker-cxx/Number.h" #include #include @@ -745,3 +746,30 @@ TEST_F(InternetTest, shouldGeneratePort) ASSERT_GE(generatedPort, 0); ASSERT_LE(generatedPort, 65535); } + +TEST_F(InternetTest, shouldGenerateAnonymousUsername) +{ + for (int i = 0; i < 100; i++) + { + const auto maxLength = Number::integer(6, 20); + const auto generatedUsername = Internet::anonymousUsername(maxLength); + + ASSERT_EQ(generatedUsername.length(), maxLength); + } +} + +TEST_F(InternetTest, shouldGenerateAnonymousUsernameWithMinLength) +{ + const auto maxLength = 5; + const auto generatedUsername = Internet::anonymousUsername(maxLength); + + ASSERT_EQ(generatedUsername.length(), 6); +} + +TEST_F(InternetTest, shouldGenerateAnonymousUsernameWithMaxLength) +{ + const auto maxLength = 21; + const auto generatedUsername = Internet::anonymousUsername(maxLength); + + ASSERT_EQ(generatedUsername.length(), 20); +}