From 4dd99811223988bf83d063b637e30e554fce7429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Tue, 20 Aug 2024 23:12:55 +0200 Subject: [PATCH] feat: replace passport country enum with locale enum in passport function (#862) --- include/faker-cxx/person.h | 16 +- include/faker-cxx/types/locale.h | 255 +++++++++++++++---------------- src/modules/person.cpp | 15 +- tests/modules/person_test.cpp | 9 +- 4 files changed, 142 insertions(+), 153 deletions(-) diff --git a/include/faker-cxx/person.h b/include/faker-cxx/person.h index acfb4594..e51d36d6 100644 --- a/include/faker-cxx/person.h +++ b/include/faker-cxx/person.h @@ -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; @@ -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 country = std::nullopt); - -enum class PassportCountry -{ - France, - Poland, - Romania, - Usa, -}; +FAKER_CXX_EXPORT std::string passport(Locale locale = Locale::en_US); enum class Sex { diff --git a/include/faker-cxx/types/locale.h b/include/faker-cxx/types/locale.h index 9f970664..a5f9e190 100644 --- a/include/faker-cxx/types/locale.h +++ b/include/faker-cxx/types/locale.h @@ -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 }; diff --git a/src/modules/person.cpp b/src/modules/person.cpp index e469f8e2..d3e892b2 100644 --- a/src/modules/person.cpp +++ b/src/modules/person.cpp @@ -12,6 +12,7 @@ #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" @@ -19,11 +20,9 @@ namespace faker::person { namespace { -const std::unordered_map passportFormats{ - {PassportCountry::Usa, "AA0000000"}, - {PassportCountry::Poland, "AA0000000"}, - {PassportCountry::France, "00AA00000"}, - {PassportCountry::Romania, "00000000"}, +const std::unordered_map 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) @@ -438,11 +437,9 @@ std::string_view chineseZodiac() return helper::randomElement(chineseZodiacs); } -std::string passport(std::optional 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; diff --git a/tests/modules/person_test.cpp b/tests/modules/person_test.cpp index 6d9637fa..a012fe77 100644 --- a/tests/modules/person_test.cpp +++ b/tests/modules/person_test.cpp @@ -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" @@ -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])); @@ -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])); @@ -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])); @@ -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]));