Skip to content

Commit

Permalink
feat: replace passport country enum with locale enum in passport func…
Browse files Browse the repository at this point in the history
…tion (#862)
  • Loading branch information
cieslarmichal authored Aug 20, 2024
1 parent b0e8e87 commit 4dd9981
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 153 deletions.
16 changes: 5 additions & 11 deletions include/faker-cxx/person.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "faker-cxx/export.h"
#include "types/country.h"
#include "types/locale.h"

namespace faker::person
{
enum class PassportCountry;
enum class Sex;
enum class SsnCountry;
enum class Language;
Expand Down Expand Up @@ -240,21 +240,15 @@ FAKER_CXX_EXPORT std::string_view chineseZodiac();
/**
* @brief Returns a random passport number from a given country
*
* @param locale The optional locale to use. Defaults to `Locale::en_US`.
*
* @returns Passport
*
* @code
* faker::person::passport(PassportCountry::Romania) // "12345678"
* faker::person::passport(Locale::pl_PL) // "12345678"
* @endcode
*/
FAKER_CXX_EXPORT std::string passport(std::optional<PassportCountry> country = std::nullopt);

enum class PassportCountry
{
France,
Poland,
Romania,
Usa,
};
FAKER_CXX_EXPORT std::string passport(Locale locale = Locale::en_US);

enum class Sex
{
Expand Down
255 changes: 126 additions & 129 deletions include/faker-cxx/types/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,133 +2,130 @@

enum class Locale
{
af_ZA, // Afrikaans, South Africa
ar_AE, // Arabic, United Arab Emirates
ar_BH, // Arabic, Bahrain
ar_DZ, // Arabic, Algeria
ar_EG, // Arabic, Egypt
ar_IQ, // Arabic, Iraq
ar_JO, // Arabic, Jordan
ar_KW, // Arabic, Kuwait
ar_LY, // Arabic, Libya
ar_MA, // Arabic, Morocco
ar_OM, // Arabic, Oman
ar_QA, // Arabic, Qatar
ar_SA, // Arabic, Saudi Arabia
ar_TN, // Arabic, Tunisia
ar_YE, // Arabic, Yemen
as_IN, // Assamese, India
az_AZ, // Azerbaijani, Azerbaijan
be_BY, // Belarusian, Belarus
bg_BG, // Bulgarian, Bulgaria
bn_IN, // Bengali, India
bs_BA, // Bosnian, Bosnia and Herzegovina
ca_ES, // Catalan, Spain
cs_CZ, // Czech, Czech Republic
da_DK, // Danish, Denmark
de_AT, // German, Austria
de_BE, // German, Belgium
de_CH, // German, Switzerland
de_DE, // German, Germany
de_LI, // German, Liechtenstein
de_LU, // German, Luxembourg
el_CY, // Greek, Cyprus
el_GR, // Greek, Greece
en_AU, // English, Australia
en_BW, // English, Botswana
en_CA, // English, Canada
en_GB, // English, United Kingdom
en_HK, // English, Hong Kong SAR China
en_IE, // English, Ireland
en_IN, // English, India
en_MT, // English, Malta
en_NZ, // English, New Zealand
en_PH, // English, Philippines
en_SG, // English, Singapore
en_US, // English, U.S.A.
en_ZW, // English, Zimbabwe
es_AR, // Spanish, Argentina
es_BO, // Spanish, Bolivia
es_CL, // Spanish, Chile
es_CO, // Spanish, Colombia
es_CR, // Spanish, Costa Rica
es_DO, // Spanish, Dominican Republic
es_EC, // Spanish, Ecuador
es_ES, // Spanish, Spain
es_GT, // Spanish, Guatemala
es_HN, // Spanish, Honduras
es_MX, // Spanish, Mexico
es_NI, // Spanish, Nicaragua
es_PA, // Spanish, Panama
es_PE, // Spanish, Peru
es_PR, // Spanish, Puerto Rico
es_PY, // Spanish, Paraguay
es_SV, // Spanish, El Salvador
es_US, // Spanish, U.S.A.
es_UY, // Spanish, Uruguay
es_VE, // Spanish, Venezuela
et_EE, // Estonian, Estonia
fi_FI, // Finnish, Finland
fr_BE, // French, Belgium
fr_CA, // French, Canada
fr_CH, // French, Switzerland
fr_FR, // French, France
fr_LU, // French, Luxembourg
gu_IN, // Gujarati, India
he_IL, // Hebrew, Israel
hi_IN, // Hindi, India
hr_HR, // Croatian, Croatia
hu_HU, // Hungarian, Hungary
hy_AM, // Armenian, Armenia
id_ID, // Indonesian, Indonesia
is_IS, // Icelandic, Iceland
it_CH, // Italian, Switzerland
it_IT, // Italian, Italy
ja_JP, // Japanese, Japan
ka_GE, // Georgian, Georgia
kk_KZ, // Kazakh, Kazakhstan
kn_IN, // Kannada, India
ko_KR, // Korean, Korea
ks_IN, // Kashmiri, India
ku_TR, // Kurdish, Turkey
ku_TR_sorani, // Kurdish (Sorani), Turkey
ky_KG, // Kirghiz, Kyrgyzstan
lt_LT, // Lithuanian, Lithuania
lv_LV, // Latvian, Latvia
mk_MK, // Macedonian, Macedonia
ml_IN, // Malayalam, India
mr_IN, // Marathi, India
ms_MY, // Malay, Malaysia
mt_MT, // Maltese, Malta
nb_NO, // Bokmal, Norway
nl_BE, // Dutch, Belgium
nl_NL, // Dutch, Netherlands
nn_NO, // Nynorsk, Norway
or_IN, // Oriya, India
pa_IN, // Punjabi, India
pl_PL, // Polish, Poland
pt_BR, // Portuguese, Brazil
pt_PT, // Portuguese, Portugal
ro_RO, // Romanian, Romania
ru_RU, // Russian, Russia
ru_UA, // Russian, Ukraine
sa_IN, // Sanskrit, India
sk_SK, // Slovak, Slovakia
sl_SI, // Slovenian, Slovenia
sq_AL, // Albanian, Albania
sr_ME, // Serbian, Montenegro
sr_ME_latin, // Serbian, Montenegro (Latin)
sr_RS, // Serbian, Serbia
sr_RS_latin, // Serbian, Serbia (Latin)
sv_SE, // Swedish, Sweden
ta_IN, // Tamil, India
te_IN, // Telugu, India
th_TH, // Thai, Thailand
tr_TR, // Turkish, Turkey
uk_UA, // Ukrainian, Ukraine
vi_VN, // Vietnamese, Vietnam
zh_CN, // Simplified Chinese, China
zh_HK, // Traditional Chinese, Hong Kong SAR China
zh_SG, // Chinese, Singapore
zh_TW // Traditional Chinese, Taiwan
af_ZA, // Afrikaans, South Africa
ar_AE, // Arabic, United Arab Emirates
ar_BH, // Arabic, Bahrain
ar_DZ, // Arabic, Algeria
ar_EG, // Arabic, Egypt
ar_IQ, // Arabic, Iraq
ar_JO, // Arabic, Jordan
ar_KW, // Arabic, Kuwait
ar_LY, // Arabic, Libya
ar_MA, // Arabic, Morocco
ar_OM, // Arabic, Oman
ar_QA, // Arabic, Qatar
ar_SA, // Arabic, Saudi Arabia
ar_TN, // Arabic, Tunisia
ar_YE, // Arabic, Yemen
as_IN, // Assamese, India
az_AZ, // Azerbaijani, Azerbaijan
be_BY, // Belarusian, Belarus
bg_BG, // Bulgarian, Bulgaria
bn_IN, // Bengali, India
bs_BA, // Bosnian, Bosnia and Herzegovina
ca_ES, // Catalan, Spain
cs_CZ, // Czech, Czech Republic
da_DK, // Danish, Denmark
de_AT, // German, Austria
de_BE, // German, Belgium
de_CH, // German, Switzerland
de_DE, // German, Germany
de_LI, // German, Liechtenstein
de_LU, // German, Luxembourg
el_CY, // Greek, Cyprus
el_GR, // Greek, Greece
en_AU, // English, Australia
en_BW, // English, Botswana
en_CA, // English, Canada
en_GB, // English, United Kingdom
en_HK, // English, Hong Kong SAR China
en_IE, // English, Ireland
en_IN, // English, India
en_MT, // English, Malta
en_NZ, // English, New Zealand
en_PH, // English, Philippines
en_SG, // English, Singapore
en_US, // English, U.S.A.
en_ZW, // English, Zimbabwe
es_AR, // Spanish, Argentina
es_BO, // Spanish, Bolivia
es_CL, // Spanish, Chile
es_CO, // Spanish, Colombia
es_CR, // Spanish, Costa Rica
es_DO, // Spanish, Dominican Republic
es_EC, // Spanish, Ecuador
es_ES, // Spanish, Spain
es_GT, // Spanish, Guatemala
es_HN, // Spanish, Honduras
es_MX, // Spanish, Mexico
es_NI, // Spanish, Nicaragua
es_PA, // Spanish, Panama
es_PE, // Spanish, Peru
es_PR, // Spanish, Puerto Rico
es_PY, // Spanish, Paraguay
es_SV, // Spanish, El Salvador
es_US, // Spanish, U.S.A.
es_UY, // Spanish, Uruguay
es_VE, // Spanish, Venezuela
et_EE, // Estonian, Estonia
fi_FI, // Finnish, Finland
fr_BE, // French, Belgium
fr_CA, // French, Canada
fr_CH, // French, Switzerland
fr_FR, // French, France
fr_LU, // French, Luxembourg
gu_IN, // Gujarati, India
he_IL, // Hebrew, Israel
hi_IN, // Hindi, India
hr_HR, // Croatian, Croatia
hu_HU, // Hungarian, Hungary
hy_AM, // Armenian, Armenia
id_ID, // Indonesian, Indonesia
is_IS, // Icelandic, Iceland
it_CH, // Italian, Switzerland
it_IT, // Italian, Italy
ja_JP, // Japanese, Japan
ka_GE, // Georgian, Georgia
kk_KZ, // Kazakh, Kazakhstan
kn_IN, // Kannada, India
ko_KR, // Korean, Korea
ks_IN, // Kashmiri, India
ku_TR, // Kurdish, Turkey
ky_KG, // Kirghiz, Kyrgyzstan
lt_LT, // Lithuanian, Lithuania
lv_LV, // Latvian, Latvia
mk_MK, // Macedonian, Macedonia
ml_IN, // Malayalam, India
mr_IN, // Marathi, India
ms_MY, // Malay, Malaysia
mt_MT, // Maltese, Malta
nb_NO, // Bokmal, Norway
nl_BE, // Dutch, Belgium
nl_NL, // Dutch, Netherlands
nn_NO, // Nynorsk, Norway
or_IN, // Oriya, India
pa_IN, // Punjabi, India
pl_PL, // Polish, Poland
pt_BR, // Portuguese, Brazil
pt_PT, // Portuguese, Portugal
ro_RO, // Romanian, Romania
ru_RU, // Russian, Russia
ru_UA, // Russian, Ukraine
sa_IN, // Sanskrit, India
sk_SK, // Slovak, Slovakia
sl_SI, // Slovenian, Slovenia
sq_AL, // Albanian, Albania
sr_ME, // Serbian, Montenegro
sr_RS, // Serbian, Serbia
sv_SE, // Swedish, Sweden
ta_IN, // Tamil, India
te_IN, // Telugu, India
th_TH, // Thai, Thailand
tr_TR, // Turkish, Turkey
uk_UA, // Ukrainian, Ukraine
vi_VN, // Vietnamese, Vietnam
zh_CN, // Simplified Chinese, China
zh_HK, // Traditional Chinese, Hong Kong SAR China
zh_SG, // Chinese, Singapore
zh_TW // Traditional Chinese, Taiwan
};
15 changes: 6 additions & 9 deletions src/modules/person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
#include "faker-cxx/number.h"
#include "faker-cxx/string.h"
#include "faker-cxx/types/country.h"
#include "faker-cxx/types/locale.h"
#include "faker-cxx/word.h"
#include "person_data.h"

namespace faker::person
{
namespace
{
const std::unordered_map<PassportCountry, std::string_view> passportFormats{
{PassportCountry::Usa, "AA0000000"},
{PassportCountry::Poland, "AA0000000"},
{PassportCountry::France, "00AA00000"},
{PassportCountry::Romania, "00000000"},
const std::unordered_map<Locale, std::string_view> passportFormats{
{Locale::es_US, "AA0000000"}, {Locale::en_US, "AA0000000"}, {Locale::pl_PL, "AA0000000"},
{Locale::fr_FR, "00AA00000"}, {Locale::ro_RO, "00000000"},
};

const struct PeopleNames& getPeopleNamesByCountry(const Country& country)
Expand Down Expand Up @@ -438,11 +437,9 @@ std::string_view chineseZodiac()
return helper::randomElement(chineseZodiacs);
}

std::string passport(std::optional<PassportCountry> country)
std::string passport(Locale locale)
{
const auto countryStr = country ? *country : PassportCountry::Usa;

const auto& passportFormat = passportFormats.at(countryStr);
const auto& passportFormat = passportFormats.at(locale);

std::string passportNumber;

Expand Down
9 changes: 5 additions & 4 deletions tests/modules/person_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "faker-cxx/internet.h"
#include "faker-cxx/person.h"
#include "faker-cxx/types/country.h"
#include "faker-cxx/types/locale.h"
#include "person_data.h"
#include "string_helper.h"
#include "word_data.h"
Expand Down Expand Up @@ -657,7 +658,7 @@ class PersonPassportTest : public Test

TEST_F(PersonPassportTest, shouldGenerateUsaPassport)
{
const auto passportNumber = passport(PassportCountry::Usa);
const auto passportNumber = passport(Locale::en_US);

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isalpha(passportNumber[0]));
Expand All @@ -673,7 +674,7 @@ TEST_F(PersonPassportTest, shouldGenerateUsaPassport)

TEST_F(PersonPassportTest, shouldGeneratePolandPassport)
{
const auto passportNumber = passport(PassportCountry::Poland);
const auto passportNumber = passport(Locale::pl_PL);

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isalpha(passportNumber[0]));
Expand All @@ -689,7 +690,7 @@ TEST_F(PersonPassportTest, shouldGeneratePolandPassport)

TEST_F(PersonPassportTest, shouldGenerateFrenchPassport)
{
const auto passportNumber = passport(PassportCountry::France);
const auto passportNumber = passport(Locale::fr_FR);

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isdigit(passportNumber[0]));
Expand All @@ -705,7 +706,7 @@ TEST_F(PersonPassportTest, shouldGenerateFrenchPassport)

TEST_F(PersonPassportTest, shouldGenerateRomanianPassport)
{
const auto passportNumber = passport(PassportCountry::Romania);
const auto passportNumber = passport(Locale::ro_RO);
ASSERT_EQ(passportNumber.size(), 8);
ASSERT_TRUE(std::isdigit(passportNumber[0]));
ASSERT_TRUE(std::isdigit(passportNumber[1]));
Expand Down

0 comments on commit 4dd9981

Please sign in to comment.