Skip to content

Commit

Permalink
added passport to person (#524)
Browse files Browse the repository at this point in the history
* added passport to person

* separated tests and fixed documentation
  • Loading branch information
eric-bodhi committed Apr 5, 2024
1 parent ee834b6 commit 8d37101
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 9 deletions.
12 changes: 12 additions & 0 deletions include/faker-cxx/Person.h
Expand Up @@ -4,6 +4,7 @@
#include <string>

#include "faker-cxx/types/Country.h"
#include "faker-cxx/types/PassportType.h"
#include "faker-cxx/types/Sex.h"
#include "faker-cxx/types/SsnCountry.h"

Expand Down Expand Up @@ -242,5 +243,16 @@ class Person
* @endcode
*/
static std::string chineseZodiac();

/**
* @brief Returns a random passport number from a given country
*
* @returns Passport
*
* @code
* Person::passport(PassportCountry::Romania) // "12345678"
* @endcode
*/
static std::string passport(PassportCountry country = PassportCountry::Usa);
};
}
24 changes: 24 additions & 0 deletions include/faker-cxx/types/PassportType.h
@@ -0,0 +1,24 @@
#pragma once

#include <map>
#include <string>

namespace faker
{

enum class PassportCountry
{
Usa,
Poland,
France,
Romania,
};

const std::map<PassportCountry, std::string> passportFormats{
{PassportCountry::Usa, "AA0000000"},
{PassportCountry::Poland, "AA0000000"},
{PassportCountry::France, "00AA00000"},
{PassportCountry::Romania, "00000000"},
};

}
22 changes: 22 additions & 0 deletions src/modules/person/Person.cpp
Expand Up @@ -77,6 +77,7 @@
#include "faker-cxx/Helper.h"
#include "faker-cxx/Internet.h"
#include "faker-cxx/String.h"
#include "faker-cxx/types/PassportType.h"
#include "faker-cxx/Word.h"

namespace faker
Expand Down Expand Up @@ -455,6 +456,27 @@ std::string Person::chineseZodiac()
return Helper::arrayElement<std::string>(chineseZodiacs);
}

std::string Person::passport(PassportCountry country)
{
std::string passportFormat = passportFormats.at(country);
std::string passportNumber;

for (const char& c : passportFormat)
{
if (c == 'A')
{
passportNumber += String::alpha(1, StringCasing::Upper);
}

else if (c == '0')
{
passportNumber += String::numeric(1);
}
}

return passportNumber;
}

namespace
{
std::string middleNameForCountry(Country country, std::optional<Sex> sex)
Expand Down
87 changes: 78 additions & 9 deletions src/modules/person/PersonTest.cpp
Expand Up @@ -76,6 +76,7 @@
#include "data/vietnam/VietnamesePeopleNames.h"
#include "data/ZodiacSigns.h"
#include "faker-cxx/Internet.h"
#include "faker-cxx/types/PassportType.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -646,6 +647,73 @@ INSTANTIATE_TEST_SUITE_P(TestPersonSsn, PersonSsnSuite, testing::ValuesIn(suppor
[](const testing::TestParamInfo<PersonSsnSuite::ParamType>& info)
{ return "shouldGenerate" + toString(info.param) + "Ssn"; });

class PersonPassportTest : public Test
{
public:
};

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

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isalpha(passportNumber[0]));
ASSERT_TRUE(std::isalpha(passportNumber[1]));
ASSERT_TRUE(std::isdigit(passportNumber[2]));
ASSERT_TRUE(std::isdigit(passportNumber[3]));
ASSERT_TRUE(std::isdigit(passportNumber[4]));
ASSERT_TRUE(std::isdigit(passportNumber[5]));
ASSERT_TRUE(std::isdigit(passportNumber[6]));
ASSERT_TRUE(std::isdigit(passportNumber[7]));
ASSERT_TRUE(std::isdigit(passportNumber[8]));
};

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

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isalpha(passportNumber[0]));
ASSERT_TRUE(std::isalpha(passportNumber[1]));
ASSERT_TRUE(std::isdigit(passportNumber[2]));
ASSERT_TRUE(std::isdigit(passportNumber[3]));
ASSERT_TRUE(std::isdigit(passportNumber[4]));
ASSERT_TRUE(std::isdigit(passportNumber[5]));
ASSERT_TRUE(std::isdigit(passportNumber[6]));
ASSERT_TRUE(std::isdigit(passportNumber[7]));
ASSERT_TRUE(std::isdigit(passportNumber[8]));
};

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

ASSERT_EQ(passportNumber.size(), 9);
ASSERT_TRUE(std::isdigit(passportNumber[0]));
ASSERT_TRUE(std::isdigit(passportNumber[1]));
ASSERT_TRUE(std::isalpha(passportNumber[2]));
ASSERT_TRUE(std::isalpha(passportNumber[3]));
ASSERT_TRUE(std::isdigit(passportNumber[4]));
ASSERT_TRUE(std::isdigit(passportNumber[5]));
ASSERT_TRUE(std::isdigit(passportNumber[6]));
ASSERT_TRUE(std::isdigit(passportNumber[7]));
ASSERT_TRUE(std::isdigit(passportNumber[8]));
};

TEST_F(PersonPassportTest, shouldGenerateRomanianPassport)
{
const auto passportNumber = Person::passport(PassportCountry::Romania);
ASSERT_EQ(passportNumber.size(), 8);
ASSERT_TRUE(std::isdigit(passportNumber[0]));
ASSERT_TRUE(std::isdigit(passportNumber[1]));
ASSERT_TRUE(std::isdigit(passportNumber[2]));
ASSERT_TRUE(std::isdigit(passportNumber[3]));
ASSERT_TRUE(std::isdigit(passportNumber[4]));
ASSERT_TRUE(std::isdigit(passportNumber[5]));
ASSERT_TRUE(std::isdigit(passportNumber[6]));
ASSERT_TRUE(std::isdigit(passportNumber[7]));
};

bool checkTokenFormat(const std::string& bio)
{
const std::regex firstRegex{R"(^(\w+\s?\w+)$)"};
Expand All @@ -661,7 +729,8 @@ bool checkTokenFormat(const std::string& bio)
//
if (std::regex_match(bio, matches, firstRegex))
{
// In this case the bio is in the format {bio_part} so check that the value is present in the bio_part vector.
// In this case the bio is in the format {bio_part} so check that the value is present in the bio_part
// vector.
if (std::find(bioPart.begin(), bioPart.end(), matches[0]) != bioPart.end())
return true;
}
Expand All @@ -677,8 +746,8 @@ bool checkTokenFormat(const std::string& bio)

if (std::regex_match(bio, matches, thirdRegex))
{
// In this case the bio is in the format {bio_part}, {bio_part}, {bio_part} so check that the value is present
// in the bio_part vector.
// In this case the bio is in the format {bio_part}, {bio_part}, {bio_part} so check that the value is
// present in the bio_part vector.
if (std::find(bioPart.begin(), bioPart.end(), matches[1]) != bioPart.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[2]) != bioPart.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[3]) != bioPart.end())
Expand All @@ -687,8 +756,8 @@ bool checkTokenFormat(const std::string& bio)

if (std::regex_match(bio, matches, fourthRegex))
{
// In this case the bio is in the format {bio_part}, {bio_part}, {bio_part}, {emoji} so check that the value is
// present in the bio_part vector.
// In this case the bio is in the format {bio_part}, {bio_part}, {bio_part}, {emoji} so check that the value
// is present in the bio_part vector.
if (std::find(bioPart.begin(), bioPart.end(), matches[1]) != bioPart.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[2]) != bioPart.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[3]) != bioPart.end() &&
Expand Down Expand Up @@ -717,8 +786,8 @@ bool checkTokenFormat(const std::string& bio)

if (std::regex_match(bio, matches, seventhRegex))
{
// In this case the bio is in the format {noun} {bio_supporter}, {bio_part} so check that the value is present
// in the bio_part vector.
// In this case the bio is in the format {noun} {bio_supporter}, {bio_part} so check that the value is
// present in the bio_part vector.
if (std::find(nouns.begin(), nouns.end(), matches[1]) != nouns.end() &&
std::find(bioSupporter.begin(), bioSupporter.end(), matches[2]) != bioSupporter.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[3]) != bioPart.end())
Expand All @@ -727,8 +796,8 @@ bool checkTokenFormat(const std::string& bio)

if (std::regex_match(bio, matches, eigthRegex))
{
// In this case the bio is in the format {noun} {bio_supporter}, {bio_part} {emoji} so check that the value is
// present in the bio_part vector.
// In this case the bio is in the format {noun} {bio_supporter}, {bio_part} {emoji} so check that the value
// is present in the bio_part vector.
if (std::find(nouns.begin(), nouns.end(), matches[1]) != nouns.end() &&
std::find(bioSupporter.begin(), bioSupporter.end(), matches[2]) != bioSupporter.end() &&
std::find(bioPart.begin(), bioPart.end(), matches[3]) != bioPart.end() &&
Expand Down

0 comments on commit 8d37101

Please sign in to comment.