Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add language function to person module #210

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set(FAKER_SOURCES
src/common/StringHelper.cpp
src/modules/phone/Phone.cpp
src/common/LuhnCheck.cpp
src/common/mappers/PrecisionMapper.cpp
src/common/mappers/precisionMapper/PrecisionMapper.cpp
src/modules/system/System.cpp
src/modules/database/Database.cpp
src/modules/music/Music.cpp
Expand All @@ -46,7 +46,7 @@ set(FAKER_SOURCES
src/modules/sport/Sport.cpp
src/modules/videoGame/VideoGame.cpp
src/modules/medicine/Medicine.cpp
)
)

set(FAKER_UT_SOURCES
src/modules/animal/AnimalTest.cpp
Expand All @@ -68,7 +68,7 @@ set(FAKER_UT_SOURCES
src/modules/phone/PhoneTest.cpp
src/modules/helper/HelperTest.cpp
src/common/LuhnCheckTest.cpp
src/common/mappers/PrecisionMapperTest.cpp
src/common/mappers/precisionMapper/PrecisionMapperTest.cpp
src/modules/system/SystemTest.cpp
src/modules/database/DatabaseTest.cpp
src/modules/music/MusicTest.cpp
Expand All @@ -78,7 +78,7 @@ set(FAKER_UT_SOURCES
src/modules/sport/SportTest.cpp
src/modules/videoGame/VideoGameTest.cpp
src/modules/medicine/MedicineTest.cpp
)
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

Expand Down
8 changes: 8 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class Helper
return data[index];
}

template <class T>
static T arrayElement(const std::vector<T>& data)
{
const auto index = Number::integer<size_t>(data.size() - 1);

return data[index];
}

/**
* @brief Returns shuffled STL container.
*
Expand Down
12 changes: 11 additions & 1 deletion include/faker-cxx/Person.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,17 @@ class Person
* Person::hobby() // "Gaming"
* @endcode
*/

static std::string hobby();

/**
* @brief Returns a random language.
*
* @returns Language.
*
* @code
* Person::language() // "Polish"
* @endcode
*/
static std::string language();
};
}
6 changes: 6 additions & 0 deletions include/faker-cxx/types/Language.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <map>
#include <vector>

namespace faker
{
Expand All @@ -18,6 +19,11 @@ enum class Language
Nepali,
};

const std::vector<Language> languages{
Language::English, Language::Polish, Language::Italian, Language::French, Language::German,
Language::Russian, Language::Romanian, Language::Hindi, Language::Finnish, Language::Nepali,
};

inline std::string toString(Language language)
{
std::map<Language, std::string> languageToStringMapping{
Expand Down
32 changes: 0 additions & 32 deletions src/common/mappers/PrecisionMapperTest.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace faker
{

const std::map<Precision, unsigned> PrecisionMapper::precisionToDecimalPlacesMapping{
{Precision::ZeroDp, 0}, {Precision::OneDp, 1}, {Precision::TwoDp, 2}, {Precision::ThreeDp, 3},
{Precision::FourDp, 4}, {Precision::FiveDp, 5}, {Precision::SixDp, 6}, {Precision::SevenDp, 7}};

unsigned PrecisionMapper::toDecimalPlaces(Precision precision)
unsigned PrecisionMapper::mapToDecimalPlaces(Precision precision)
{
return precisionToDecimalPlacesMapping.at(precision);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace faker
class PrecisionMapper
{
public:
static unsigned toDecimalPlaces(Precision precision);
static unsigned mapToDecimalPlaces(Precision precision);

private:
static const std::map<Precision, unsigned> precisionToDecimalPlacesMapping;
Expand Down
32 changes: 32 additions & 0 deletions src/common/mappers/precisionMapper/PrecisionMapperTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "PrecisionMapper.h"

#include "gtest/gtest.h"

using namespace ::testing;
using namespace faker;

class PrecisionMapperTest : public Test
{
public:
};

TEST_F(PrecisionMapperTest, mapsPrecisionToDecimalPlaces)
{
const auto zeroDp = PrecisionMapper::mapToDecimalPlaces(Precision::ZeroDp);
const auto oneDp = PrecisionMapper::mapToDecimalPlaces(Precision::OneDp);
const auto twoDp = PrecisionMapper::mapToDecimalPlaces(Precision::TwoDp);
const auto threeDp = PrecisionMapper::mapToDecimalPlaces(Precision::ThreeDp);
const auto fourDp = PrecisionMapper::mapToDecimalPlaces(Precision::FourDp);
const auto fiveDp = PrecisionMapper::mapToDecimalPlaces(Precision::FiveDp);
const auto sixDp = PrecisionMapper::mapToDecimalPlaces(Precision::SixDp);
const auto sevenDp = PrecisionMapper::mapToDecimalPlaces(Precision::SevenDp);

ASSERT_EQ(zeroDp, 0);
ASSERT_EQ(oneDp, 1);
ASSERT_EQ(twoDp, 2);
ASSERT_EQ(threeDp, 3);
ASSERT_EQ(fourDp, 4);
ASSERT_EQ(fiveDp, 5);
ASSERT_EQ(sixDp, 6);
ASSERT_EQ(sevenDp, 7);
}
6 changes: 2 additions & 4 deletions src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "faker-cxx/Finance.h"

#include "../../common/mappers/PrecisionMapper.h"
#include "../../common/mappers/precisionMapper/PrecisionMapper.h"
#include "data/AccountTypes.h"
#include "data/BankIndentifiersCodes.h"
#include "data/CreditCardsFormats.h"
Expand Down Expand Up @@ -59,7 +59,7 @@ std::string Finance::amount(double min, double max, Precision precision, const s

ss << std::fixed;

ss.precision(PrecisionMapper::toDecimalPlaces(precision));
ss.precision(PrecisionMapper::mapToDecimalPlaces(precision));

ss << generatedNumber;

Expand All @@ -70,7 +70,6 @@ std::string Finance::iban(std::optional<IbanCountry> country)
{
const auto ibanCountry = country ? *country : Helper::arrayElement<IbanCountry>(supportedIbanCountries);

// TODO: error handling
const auto& ibanFormat = ibanFormats.at(ibanCountry);

const auto& countryCode = ibanFormat[0];
Expand Down Expand Up @@ -105,7 +104,6 @@ std::string Finance::bic(std::optional<BicCountry> country)
{
const auto bicCountry = country ? *country : Helper::arrayElement<BicCountry>(supportedBicCountries);

// TODO: error handling
return Helper::arrayElement<std::string>(bankIdentifiersCodesMapping.at(bicCountry));
}

Expand Down
1 change: 0 additions & 1 deletion src/modules/finance/FinanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ TEST_F(FinanceTest, shouldGenerateIban)
{
const auto iban = Finance::iban();

// TODO: implement more detailed checks for iban with default argument
ASSERT_TRUE(iban.starts_with("AT") || iban.starts_with("BE") || iban.starts_with("BG") || iban.starts_with("HR") ||
iban.starts_with("CY") || iban.starts_with("CZ") || iban.starts_with("DK") || iban.starts_with("EE") ||
iban.starts_with("FI") || iban.starts_with("FR") || iban.starts_with("DE") || iban.starts_with("GR") ||
Expand Down
6 changes: 3 additions & 3 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <map>

#include "../../common/mappers/PrecisionMapper.h"
#include "../../common/mappers/precisionMapper/PrecisionMapper.h"
#include "../../common/StringHelper.h"
#include "data/Countries.h"
#include "data/Directions.h"
Expand Down Expand Up @@ -194,7 +194,7 @@ std::string Location::latitude(Precision precision)

ss << std::fixed;

ss.precision(PrecisionMapper::toDecimalPlaces(precision));
ss.precision(PrecisionMapper::mapToDecimalPlaces(precision));

ss << latitude;

Expand All @@ -209,7 +209,7 @@ std::string Location::longitude(Precision precision)

ss << std::fixed;

ss.precision(PrecisionMapper::toDecimalPlaces(precision));
ss.precision(PrecisionMapper::mapToDecimalPlaces(precision));

ss << longitude;

Expand Down
5 changes: 5 additions & 0 deletions src/modules/person/Person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,9 @@ std::string Person::hobby()
{
return Helper::arrayElement<std::string>(hobbies);
}

std::string Person::language()
{
return toString(Helper::arrayElement<Language>(languages));
}
}
13 changes: 8 additions & 5 deletions src/modules/person/PersonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ const auto malePrefix{"Mr."};
const std::vector<std::string> femalePrefixes{"Ms.", "Miss"};
const std::vector<std::string> allPrefixes{"Mr.", "Ms.", "Miss"};

const std::vector<Language> languages{
Language::English, Language::French, Language::German, Language::Italian, Language::Polish,
Language::Russian, Language::Romanian, Language::Hindi, Language::Finnish, Language::Nepali,
};

const std::map<Language, std::map<Sex, std::vector<std::string>>> languageToFirstNamesMapping{
{Language::English, {{Sex::Male, englishFirstNamesMales}, {Sex::Female, englishFirstNamesFemales}}},
{Language::French, {{Sex::Male, frenchFirstNamesMales}, {Sex::Female, frenchFirstNamesFemales}}},
Expand Down Expand Up @@ -332,3 +327,11 @@ TEST_F(PersonTest, shouldGenerateHobby)
ASSERT_TRUE(
std::ranges::any_of(hobbies, [generatedHobby](const std::string& hobby) { return hobby == generatedHobby; }));
}

TEST_F(PersonTest, shouldGenerateLanguage)
{
const auto generatedLanguage = Person::language();

ASSERT_TRUE(std::ranges::any_of(languages, [generatedLanguage](Language language)
{ return generatedLanguage == toString(language); }));
}