From 53d98e71bc482bee0573590c1432c5bf7885c2c3 Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Wed, 28 Aug 2024 22:13:24 +0200 Subject: [PATCH] feat: add parametrized sex tests --- include/faker-cxx/color.h | 1 + include/faker-cxx/person.h | 53 ++++------------------------------- src/modules/person.cpp | 13 ++++----- tests/modules/person_test.cpp | 32 ++++++--------------- 4 files changed, 22 insertions(+), 77 deletions(-) diff --git a/include/faker-cxx/color.h b/include/faker-cxx/color.h index 6862a279..9d345419 100644 --- a/include/faker-cxx/color.h +++ b/include/faker-cxx/color.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "faker-cxx/export.h" #include "types/hex.h" diff --git a/include/faker-cxx/person.h b/include/faker-cxx/person.h index bcbaf341..aa6ac26b 100644 --- a/include/faker-cxx/person.h +++ b/include/faker-cxx/person.h @@ -9,8 +9,11 @@ namespace faker::person { -enum class Sex; -enum class Language; +enum class Sex +{ + Female, + Male, +}; /** * @brief Returns a random first name. @@ -101,7 +104,7 @@ FAKER_CXX_EXPORT std::string bio(); /** * @brief Returns a random sex of the locale passed. - * + * * @param locale The locale. Defaults to `Locale::en_US`. * * @returns Sex. @@ -249,48 +252,4 @@ FAKER_CXX_EXPORT std::string_view chineseZodiac(); * @endcode */ FAKER_CXX_EXPORT std::string passport(Locale locale = Locale::en_US); - -enum class Sex -{ - Female, - Male, -}; - -enum class Language -{ - Albanian, - Belarusian, - Croatian, - Czech, - Danish, - Dutch, - English, - Estonian, - Finnish, - French, - German, - Greek, - Hindi, - Hungarian, - Irish, - Italian, - Japanese, - Korean, - Latvian, - Macedonian, - Mandarin, - Nepali, - Norwegian, - Polish, - Portuguese, - Romanian, - Russian, - Serbian, - Slovak, - Slovene, - Spanish, - Swedish, - Turkish, - Ukrainian, -}; } diff --git a/src/modules/person.cpp b/src/modules/person.cpp index 1a2da360..72628acd 100644 --- a/src/modules/person.cpp +++ b/src/modules/person.cpp @@ -238,8 +238,9 @@ std::string fullName(Locale locale, std::optional sex) weightedElements.reserve(peopleNames.nameFormats.size()); std::transform(peopleNames.nameFormats.begin(), peopleNames.nameFormats.end(), std::back_inserter(weightedElements), - [](const NameFormat& nameFormat) - { return helper::WeightedElement{nameFormat.weight, nameFormat.format}; }); + [](const NameFormat& nameFormat) { + return helper::WeightedElement{nameFormat.weight, nameFormat.format}; + }); const auto nameFormat = static_cast(helper::weightedRandomElement(weightedElements)); @@ -337,11 +338,9 @@ std::string bio() std::string_view sex(Locale locale) { - const std::vector sexes{"Male", "Female"}; + const std::vector sexes{Sex::Male, Sex::Female}; - const auto chosenSex = helper::randomElement(sexes); - - const auto sexEnum = chosenSex == "Male" ? Sex::Male : Sex::Female; + const auto sex = helper::randomElement(sexes); auto sexTranslation = sexTranslations.find(locale); @@ -350,7 +349,7 @@ std::string_view sex(Locale locale) sexTranslation = sexTranslations.find(Locale::en_US); } - return sexTranslation->second.at(sexEnum); + return sexTranslation->second.at(sex); } std::string_view gender() diff --git a/tests/modules/person_test.cpp b/tests/modules/person_test.cpp index c72d5bd4..83fded0a 100644 --- a/tests/modules/person_test.cpp +++ b/tests/modules/person_test.cpp @@ -21,8 +21,6 @@ using namespace faker::person; namespace { -const std::vector sexes{"Male", "Female"}; - const struct PeopleNames& getPeopleNamesByLocale(Locale locale) { switch (locale) @@ -419,33 +417,21 @@ TEST_P(PersonTest, shouldGenerateFemaleSuffix) generatedSuffix.empty()); } -INSTANTIATE_TEST_SUITE_P(TestPersonNamesByLocale, PersonTest, ValuesIn(locales), - [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); - -TEST_F(PersonTest, shouldGenerateSex) +TEST_P(PersonTest, shouldGenerateSex) { - const auto generatedSex = sex(); - - const auto sexTranslation = sexTranslations.find(Locale::en_US); - - ASSERT_TRUE(generatedSex == sexTranslation->second.at(Sex::Male) || - generatedSex == sexTranslation->second.at(Sex::Female)); -} + const auto locale = GetParam(); -TEST_F(PersonTest, shouldGenerateLocaleSex) -{ - auto generatedSex = sex(Locale::fr_FR); - auto sexTranslation = sexTranslations.find(Locale::fr_FR); + const auto generatedSex = sex(locale); - if(sexTranslation == sexTranslations.end()){ - generatedSex = sex(Locale::en_US); - sexTranslation = sexTranslations.find(Locale::en_US); - } + const auto sexTranslation = + sexTranslations.contains(locale) ? sexTranslations.at(locale) : sexTranslations.at(Locale::en_US); - ASSERT_TRUE(generatedSex == sexTranslation->second.at(Sex::Male) || - generatedSex == sexTranslation->second.at(Sex::Female)); + ASSERT_TRUE(generatedSex == sexTranslation.at(Sex::Male) || generatedSex == sexTranslation.at(Sex::Female)); } +INSTANTIATE_TEST_SUITE_P(TestPersonNamesByLocale, PersonTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); + TEST_F(PersonTest, shouldGenerateGender) { const auto generatedGender = gender();