Skip to content

Commit

Permalink
add person name locales (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Aug 14, 2023
1 parent addc165 commit 7212b15
Show file tree
Hide file tree
Showing 23 changed files with 3,390 additions and 98 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- [x] job title
- [x] job type
- [x] prefix
- [ ] support for more countries
- [x] support for more countries

### 📞 Phone:

Expand Down
20 changes: 13 additions & 7 deletions include/faker-cxx/Person.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <optional>
#include <string>

#include "types/Language.h"
#include "types/Sex.h"

namespace faker
Expand All @@ -13,41 +14,46 @@ class Person
/**
* @brief Returns a random first name.
*
* @param language The local name language. Defaults to `Language::English`.
* @param sex The optional sex to use.
*
* @returns First name starting with a capital letter.
*
* @code
* Person::firstName() // "Michael"
* Person::firstName(Sex::Female) // "Emma"
* Person::firstName(Sex::Male) // "Arthur"
* Person::firstName(Language::English, Sex::Female) // "Emma"
* Person::firstName(Language::English, Sex::Male) // "Arthur"
* @endcode
*/
static std::string firstName(std::optional<Sex> = std::nullopt);
static std::string firstName(Language language = Language::English, std::optional<Sex> = std::nullopt);

/**
* @brief Returns a random last name.
*
* @param language The local name language. Defaults to `Language::English`.
*
* @returns Last name starting with a capital letter.
*
* @code
* Person::lastName() // "Peterson"
* @endcode
*/
static std::string lastName();
static std::string lastName(Language language = Language::English);

/**
* @brief Returns a random full name.
*
* @param language The local name language. Defaults to `Language::English`.
*
* @returns Full name starting with first name.
*
* @code
* Person::fullName() // "Marcia Robinson"
* Person::fullName(Sex::Female) // "Jennifer Martin"
* Person::fullName(Sex::Male) // "Samuel Walker"
* Person::fullName(Language::English, Sex::Female) // "Jennifer Martin"
* Person::fullName(Language::English, Sex::Male) // "Samuel Walker"
* @endcode
*/
static std::string fullName(std::optional<Sex> = std::nullopt);
static std::string fullName(Language language = Language::English, std::optional<Sex> = std::nullopt);

/**
* @brief Returns a random name prefix.
Expand Down
32 changes: 32 additions & 0 deletions include/faker-cxx/types/Language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <map>

namespace faker
{
enum class Language
{
English,
Polish,
Italian,
French,
German
};

inline std::string toString(Language language)
{
std::map<Language, std::string> languageToStringMapping{{Language::English, "English"},
{Language::Polish, "Polish"},
{Language::Italian, "Italian"},
{Language::French, "French"},
{Language::German, "German"}};

return languageToStringMapping.at(language);
}

inline std::ostream& operator<<(std::ostream& os, Language language)
{
return os << toString(language);
}

}
17 changes: 9 additions & 8 deletions src/modules/company/CompanyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "gtest/gtest.h"

#include "../../common/StringHelper.h"
#include "../person/data/FirstNamesFemales.h"
#include "../person/data/FirstNamesMales.h"
#include "../person/data/english/EnglishFirstNamesFemales.h"
#include "../person/data/english/EnglishFirstNamesMales.h"
#include "../person/data/english/EnglishLastNames.h"
#include "../person/data/JobTitles.h"
#include "../person/data/LastNames.h"
#include "data/BuzzAdjectives.h"
#include "data/BuzzNouns.h"
#include "data/BuzzVerbs.h"
Expand All @@ -33,16 +33,17 @@ TEST_F(CompanyTest, shouldGenerateCompanyName)

const auto companyNameElements = StringHelper::split(companyName, " ");

std::vector<std::string> expectedFirstNames{firstNamesMales};
std::vector<std::string> expectedFirstNames{englishFirstNamesMales};

expectedFirstNames.insert(expectedFirstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
expectedFirstNames.insert(expectedFirstNames.end(), englishFirstNamesFemales.begin(),
englishFirstNamesFemales.end());

if (companyNameElements.size() == 2)
{
const auto& generatedLastName = companyNameElements[0];
const auto& generatedCompanySuffix = companyNameElements[1];

ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedLastName](const std::string& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::any_of(companySuffixes.begin(), companySuffixes.end(),
Expand All @@ -58,7 +59,7 @@ TEST_F(CompanyTest, shouldGenerateCompanyName)
ASSERT_TRUE(std::any_of(expectedFirstNames.begin(), expectedFirstNames.end(),
[generatedFirstName](const std::string& firstName)
{ return firstName == generatedFirstName; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedLastName](const std::string& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::any_of(jobAreas.begin(), jobAreas.end(),
Expand All @@ -75,7 +76,7 @@ TEST_F(CompanyTest, shouldGenerateCompanyName)
ASSERT_TRUE(std::any_of(expectedFirstNames.begin(), expectedFirstNames.end(),
[generatedFirstName](const std::string& firstName)
{ return firstName == generatedFirstName; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedLastName](const std::string& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::any_of(jobAreas.begin(), jobAreas.end(),
Expand Down
61 changes: 29 additions & 32 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "gtest/gtest.h"

#include "../../common/StringHelper.h"
#include "../person/data/FirstNamesFemales.h"
#include "../person/data/FirstNamesMales.h"
#include "../person/data/LastNames.h"
#include "../person/data/english/EnglishFirstNamesFemales.h"
#include "../person/data/english/EnglishFirstNamesMales.h"
#include "../person/data/english/EnglishLastNames.h"
#include "../string/data/Characters.h"
#include "../word/data/Adjectives.h"
#include "../word/data/Nouns.h"
Expand Down Expand Up @@ -97,16 +97,16 @@ class InternetTest : public Test

TEST_F(InternetTest, shouldGenerateUsername)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto username = Internet::username();

ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[username](const std::string& firstName)
{ return username.find(firstName) != std::string::npos; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[username](const std::string& lastName)
{ return username.find(lastName) != std::string::npos; }));
}
Expand All @@ -118,16 +118,16 @@ TEST_F(InternetTest, shouldGenerateUsernameWithFirstNameProvided)
const auto username = Internet::username(firstName);

ASSERT_TRUE(username.find(firstName) != std::string::npos);
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[username](const std::string& lastName)
{ return username.find(lastName) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGenerateUsernameWithLastNameProvided)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto lastName = "Cieslar";

Expand All @@ -153,9 +153,9 @@ TEST_F(InternetTest, shouldGenerateUsernameWithFullNameProvided)

TEST_F(InternetTest, shouldGenerateEmail)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto email = Internet::email();

Expand All @@ -172,7 +172,7 @@ TEST_F(InternetTest, shouldGenerateEmail)
ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[generatedUsername](const std::string& firstName)
{ return generatedUsername.find(firstName) != std::string::npos; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedUsername](const std::string& lastName)
{ return generatedUsername.find(lastName) != std::string::npos; }));
}
Expand All @@ -194,16 +194,16 @@ TEST_F(InternetTest, shouldGenerateEmailWithFirstName)
[generatedEmailHost](const std::string& emailHost)
{ return generatedEmailHost == emailHost; }));
ASSERT_TRUE(generatedUsername.find(firstName) != std::string::npos);
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedUsername](const std::string& lastName)
{ return generatedUsername.find(lastName) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGenerateEmailWithLastName)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto lastName = "Howard";

Expand All @@ -227,9 +227,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithLastName)

TEST_F(InternetTest, shouldGenerateEmailWithFullName)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto firstName = "Cindy";

Expand All @@ -253,9 +253,9 @@ TEST_F(InternetTest, shouldGenerateEmailWithFullName)

TEST_F(InternetTest, shouldGenerateEmailWithSpecifiedEmailHost)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto emailHost = "example.com";

Expand All @@ -272,16 +272,16 @@ TEST_F(InternetTest, shouldGenerateEmailWithSpecifiedEmailHost)
ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[generatedUsername](const std::string& firstName)
{ return generatedUsername.find(firstName) != std::string::npos; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedUsername](const std::string& lastName)
{ return generatedUsername.find(lastName) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGenerateExampleEmail)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto email = Internet::exampleEmail();

Expand All @@ -298,7 +298,7 @@ TEST_F(InternetTest, shouldGenerateExampleEmail)
ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[generatedUsername](const std::string& firstName)
{ return generatedUsername.find(firstName) != std::string::npos; }));
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedUsername](const std::string& lastName)
{ return generatedUsername.find(lastName) != std::string::npos; }));
}
Expand All @@ -320,16 +320,16 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithFirstName)
[generatedEmailHost](const std::string& emailHost)
{ return generatedEmailHost == emailHost; }));
ASSERT_TRUE(generatedUsername.find(firstName) != std::string::npos);
ASSERT_TRUE(std::any_of(lastNames.begin(), lastNames.end(),
ASSERT_TRUE(std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedUsername](const std::string& lastName)
{ return generatedUsername.find(lastName) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGenerateExampleEmailWithLastName)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto lastName = "Wilkinson";

Expand All @@ -353,9 +353,9 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithLastName)

TEST_F(InternetTest, shouldGenerateExampleEmailWithFullName)
{
std::vector<std::string> firstNames{firstNamesMales};
std::vector<std::string> firstNames{englishFirstNamesMales};

firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

const auto firstName = "Walter";

Expand Down Expand Up @@ -653,10 +653,7 @@ TEST_F(InternetTest, shouldGenerateIpv6)
ASSERT_EQ(generatedIpv6Parts.size(), 8);

ASSERT_TRUE(std::all_of(generatedIpv6Parts.begin(), generatedIpv6Parts.end(),
[](const std::string& generatedIpv6Part)
{
return generatedIpv6Part.size() == 4;
}));
[](const std::string& generatedIpv6Part) { return generatedIpv6Part.size() == 4; }));
ASSERT_TRUE(std::all_of(generatedIpv6Parts.begin(), generatedIpv6Parts.end(),
[](const std::string& generatedIpv6Part)
{
Expand Down
18 changes: 9 additions & 9 deletions src/modules/location/LocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "gtest/gtest.h"

#include "../../common/StringHelper.h"
#include "../person/data/FirstNamesFemales.h"
#include "../person/data/FirstNamesMales.h"
#include "../person/data/LastNames.h"
#include "../person/data/english/EnglishFirstNamesFemales.h"
#include "../person/data/english/EnglishFirstNamesMales.h"
#include "../person/data/english/EnglishLastNames.h"
#include "../string/data/Characters.h"
#include "data/Countries.h"
#include "data/Directions.h"
Expand Down Expand Up @@ -106,14 +106,14 @@ TEST_F(LocationTest, shouldGenerateUsaStreet)
const auto& generatedFirstOrLastName = generatedStreetElements[0];
const auto& generatedStreetSuffix = generatedStreetElements[1];

std::vector<std::string> firstNames{firstNamesMales};
firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
std::vector<std::string> firstNames{englishFirstNamesMales};
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

ASSERT_EQ(generatedStreetElements.size(), 2);
ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[generatedFirstOrLastName](const std::string& firstName)
{ return firstName == generatedFirstOrLastName; }) ||
std::any_of(lastNames.begin(), lastNames.end(),
std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedFirstOrLastName](const std::string& lastName)
{ return lastName == generatedFirstOrLastName; }));
ASSERT_TRUE(std::any_of(usaStreetSuffixes.begin(), usaStreetSuffixes.end(),
Expand All @@ -131,16 +131,16 @@ TEST_F(LocationTest, shouldGenerateUsaStreetAddress)
const auto& generatedFirstOrLastName = generatedStreetAddressElements[1];
const auto& generatedStreetSuffix = generatedStreetAddressElements[2];

std::vector<std::string> firstNames{firstNamesMales};
firstNames.insert(firstNames.end(), firstNamesFemales.begin(), firstNamesFemales.end());
std::vector<std::string> firstNames{englishFirstNamesMales};
firstNames.insert(firstNames.end(), englishFirstNamesFemales.begin(), englishFirstNamesFemales.end());

ASSERT_EQ(generatedStreetAddressElements.size(), 3);
ASSERT_TRUE(generatedBuildingNumber.size() >= 3 && generatedBuildingNumber.size() <= 5);
ASSERT_TRUE(checkIfAllCharactersAreNumeric(generatedBuildingNumber));
ASSERT_TRUE(std::any_of(firstNames.begin(), firstNames.end(),
[generatedFirstOrLastName](const std::string& firstName)
{ return firstName == generatedFirstOrLastName; }) ||
std::any_of(lastNames.begin(), lastNames.end(),
std::any_of(englishLastNames.begin(), englishLastNames.end(),
[generatedFirstOrLastName](const std::string& lastName)
{ return lastName == generatedFirstOrLastName; }));
ASSERT_TRUE(std::any_of(usaStreetSuffixes.begin(), usaStreetSuffixes.end(),
Expand Down
Loading

0 comments on commit 7212b15

Please sign in to comment.