Skip to content

Commit

Permalink
feat: add parametrized sex tests (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Aug 28, 2024
1 parent 8e0c53e commit ce65b61
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 77 deletions.
1 change: 1 addition & 0 deletions include/faker-cxx/color.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <string_view>

#include "faker-cxx/export.h"
#include "types/hex.h"
Expand Down
53 changes: 6 additions & 47 deletions include/faker-cxx/person.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

namespace faker::person
{
enum class Sex;
enum class Language;
enum class Sex
{
Female,
Male,
};

/**
* @brief Returns a random first name.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
};
}
13 changes: 6 additions & 7 deletions src/modules/person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ std::string fullName(Locale locale, std::optional<Sex> sex)
weightedElements.reserve(peopleNames.nameFormats.size());

std::transform(peopleNames.nameFormats.begin(), peopleNames.nameFormats.end(), std::back_inserter(weightedElements),
[](const NameFormat& nameFormat)
{ return helper::WeightedElement<std::string_view>{nameFormat.weight, nameFormat.format}; });
[](const NameFormat& nameFormat) {
return helper::WeightedElement<std::string_view>{nameFormat.weight, nameFormat.format};
});

const auto nameFormat = static_cast<std::string>(helper::weightedRandomElement<std::string_view>(weightedElements));

Expand Down Expand Up @@ -337,11 +338,9 @@ std::string bio()

std::string_view sex(Locale locale)
{
const std::vector<std::string> sexes{"Male", "Female"};
const std::vector<Sex> 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);

Expand All @@ -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()
Expand Down
32 changes: 9 additions & 23 deletions tests/modules/person_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ using namespace faker::person;

namespace
{
const std::vector<std::string> sexes{"Male", "Female"};

const struct PeopleNames& getPeopleNamesByLocale(Locale locale)
{
switch (locale)
Expand Down Expand Up @@ -419,33 +417,21 @@ TEST_P(PersonTest, shouldGenerateFemaleSuffix)
generatedSuffix.empty());
}

INSTANTIATE_TEST_SUITE_P(TestPersonNamesByLocale, PersonTest, ValuesIn(locales),
[](const TestParamInfo<Locale>& 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<Locale>& paramInfo) { return toString(paramInfo.param); });

TEST_F(PersonTest, shouldGenerateGender)
{
const auto generatedGender = gender();
Expand Down

0 comments on commit ce65b61

Please sign in to comment.