Skip to content

Commit

Permalink
Feature/anonymous username (#518)
Browse files Browse the repository at this point in the history
* Added anonymousUsername generator

Added a new method `anonymousUsername` that generates the username of length
[6,20] characters. It uses `Words` module to get the adjective for the first
half of the username and then uses the nouns collection to generate the
next half.

* Min length of adjective must be 3

* Getting rid of while loop

* Cleaning up commented code
  • Loading branch information
sdorbala authored Mar 28, 2024
1 parent fec324d commit 0bfa912
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ build-linux-clang
.cache/
#vim swap files
*.sw?

#kdevelop temp files
*.kdev4
.kdev4
13 changes: 13 additions & 0 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
19 changes: 19 additions & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,23 @@ std::string Internet::domainSuffix()
return Helper::arrayElement<std::string>(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<unsigned>(3, 1 + maxLength/2);
unsigned nounLength = maxLength - adjectiveLength;
std::stringstream usernameBuilder;

usernameBuilder << Word::adjective(adjectiveLength) << Word::noun(nounLength);

return usernameBuilder.str();
}

}
28 changes: 28 additions & 0 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "faker-cxx/Internet.h"
#include "faker-cxx/Number.h"

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -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<unsigned>(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);
}

0 comments on commit 0bfa912

Please sign in to comment.