Skip to content

Commit

Permalink
add address country enum (#279)
Browse files Browse the repository at this point in the history
* add address country enum

* add address country enum 2
  • Loading branch information
cieslarmichal authored Nov 18, 2023
1 parent 70cde2a commit fc84291
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 50 deletions.
1 change: 1 addition & 0 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <array>
#include <optional>
#include <string>

Expand Down
14 changes: 7 additions & 7 deletions include/faker-cxx/Location.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#include "types/Country.h"
#include "faker-cxx/types/AddressCountry.h"
#include "types/Precision.h"

namespace faker
Expand Down Expand Up @@ -54,7 +54,7 @@ class Location
* Location::city() // "Boston"
* @endcode
*/
static std::string city(Country country = Country::Usa);
static std::string city(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random zip code for given country.
Expand All @@ -68,7 +68,7 @@ class Location
* Location::zipCode(Country::Poland) // "31-881"
* @endcode
*/
static std::string zipCode(Country country = Country::Usa);
static std::string zipCode(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random street address for given country.
Expand All @@ -81,7 +81,7 @@ class Location
* Location::streetAddress() // "34830 Erdman Hollow"
* @endcode
*/
static std::string streetAddress(Country country = Country::Usa);
static std::string streetAddress(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random street for given country.
Expand All @@ -94,7 +94,7 @@ class Location
* Location::street() // "Schroeder Isle"
* @endcode
*/
static std::string street(Country country = Country::Usa);
static std::string street(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random building number for given country.
Expand All @@ -107,7 +107,7 @@ class Location
* Location::buildingNumber() // "505"
* @endcode
*/
static std::string buildingNumber(Country country = Country::Usa);
static std::string buildingNumber(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random secondary address number for given country.
Expand All @@ -121,7 +121,7 @@ class Location
* Location::secondaryAddress() // "Apt. 861"
* @endcode
*/
static std::string secondaryAddress(Country country = Country::Usa);
static std::string secondaryAddress(AddressCountry country = AddressCountry::Usa);

/**
* @brief Generates a random latitude.
Expand Down
41 changes: 41 additions & 0 deletions include/faker-cxx/types/AddressCountry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <map>
#include <string>
#include <vector>

namespace faker
{
// TODO: remove when AddressCountry enum values will be same as Country values
enum class AddressCountry
{
Usa,
Poland,
France,
Russia,
};

const std::vector<AddressCountry> addressCountries{
AddressCountry::Usa,
AddressCountry::Poland,
AddressCountry::France,
AddressCountry::Russia,
};

inline std::string toString(AddressCountry country)
{
std::map<AddressCountry, std::string> countryToStringMapping{
{AddressCountry::Usa, "Usa"},
{AddressCountry::Poland, "Poland"},
{AddressCountry::France, "France"},
{AddressCountry::Russia, "Russia"},
};

return countryToStringMapping.at(country);
}

inline std::ostream& operator<<(std::ostream& os, AddressCountry country)
{
return os << toString(country);
}
}
1 change: 1 addition & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "faker-cxx/Internet.h"

#include <array>
#include <map>
#include <utility>

Expand Down
1 change: 1 addition & 0 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "faker-cxx/Internet.h"

#include <algorithm>
#include <array>
#include <charconv>
#include <optional>

Expand Down
33 changes: 20 additions & 13 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ namespace faker
{
namespace
{
const std::map<Country, CountryAddresses> countryToCountryAddressesMapping{
{Country::Usa, usaAddresses},
{Country::Poland, polandAddresses},
{Country::Russia, russiaAddresses},
{Country::France, franceAddresses},
const std::map<AddressCountry, CountryAddresses> countryToCountryAddressesMapping{
{AddressCountry::Usa, usaAddresses},
{AddressCountry::Poland, polandAddresses},
{AddressCountry::Russia, russiaAddresses},
{AddressCountry::France, franceAddresses},
};

const std::map<AddressCountry, Country> countryAddressToCountryMapping{
{AddressCountry::Usa, Country::Usa},
{AddressCountry::Poland, Country::Poland},
{AddressCountry::Russia, Country::Russia},
{AddressCountry::France, Country::France},
};
}

Expand All @@ -43,21 +50,21 @@ std::string Location::state()
return Helper::arrayElement<std::string>(states);
}

std::string Location::city(Country country)
std::string Location::city(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

return Helper::arrayElement<std::string>(countryAddresses.cities);
}

std::string Location::zipCode(Country country)
std::string Location::zipCode(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

return Helper::replaceSymbolWithNumber(countryAddresses.zipCodeFormat);
}

std::string Location::streetAddress(Country country)
std::string Location::streetAddress(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

Expand All @@ -71,15 +78,15 @@ std::string Location::streetAddress(Country country)
return FormatHelper::fillTokenValues(addressFormat, dataGeneratorsMapping);
}

std::string Location::street(Country country)
std::string Location::street(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

const auto streetFormat = Helper::arrayElement<std::string>(countryAddresses.streetFormats);

const auto dataGeneratorsMapping = std::map<std::string, std::function<std::string()>>{
{"firstName", [&country]() { return Person::firstName(country); }},
{"lastName", [&country]() { return Person::lastName(country); }},
{"firstName", [&country]() { return Person::firstName(countryAddressToCountryMapping.at(country)); }},
{"lastName", [&country]() { return Person::lastName(countryAddressToCountryMapping.at(country)); }},
{"streetName",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.streetNames); }},
{"streetPrefix",
Expand All @@ -90,7 +97,7 @@ std::string Location::street(Country country)
return FormatHelper::fillTokenValues(streetFormat, dataGeneratorsMapping);
}

std::string Location::buildingNumber(Country country)
std::string Location::buildingNumber(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

Expand All @@ -99,7 +106,7 @@ std::string Location::buildingNumber(Country country)
return Helper::replaceSymbolWithNumber(buildingNumberFormat);
}

std::string Location::secondaryAddress(Country country)
std::string Location::secondaryAddress(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

Expand Down
56 changes: 26 additions & 30 deletions src/modules/location/LocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,22 @@ using namespace faker;

namespace
{
const std::map<Country, CountryAddresses> countryToCountryAddressesMapping{
{Country::Usa, usaAddresses},
{Country::Poland, polandAddresses},
{Country::Russia, russiaAddresses},
{Country::France, franceAddresses},
const std::map<AddressCountry, CountryAddresses> countryToCountryAddressesMapping{
{AddressCountry::Usa, usaAddresses},
{AddressCountry::Poland, polandAddresses},
{AddressCountry::Russia, russiaAddresses},
{AddressCountry::France, franceAddresses},
};

// TODO: replace with countries from Country enum file
const std::vector<Country> tempCountries{
Country::Poland,
Country::France,
Country::Usa,
Country::Russia,
};

const std::map<Country, std::string> generatedTestName{
{Country::Usa, "shouldGenerateAmericanAddress"},
{Country::France, "shouldGenerateFrenchAddress"},
{Country::Poland, "shouldGeneratePolishAddress"},
{Country::Russia, "shouldGenerateRussianAddress"},
const std::map<AddressCountry, std::string> generatedTestName{
{AddressCountry::Usa, "shouldGenerateAmericanAddress"},
{AddressCountry::France, "shouldGenerateFrenchAddress"},
{AddressCountry::Poland, "shouldGeneratePolishAddress"},
{AddressCountry::Russia, "shouldGenerateRussianAddress"},
};
}

class LocationTest : public TestWithParam<Country>
class LocationTest : public TestWithParam<AddressCountry>
{
public:
static bool checkIfZipCode(const std::string& zipCode)
Expand Down Expand Up @@ -142,8 +134,8 @@ TEST_P(LocationTest, shouldGenerateSecondaryAddress)
}));
}

INSTANTIATE_TEST_SUITE_P(TestLocationByCountries, LocationTest, ValuesIn(tempCountries),
[](const TestParamInfo<Country>& info) { return generatedTestName.at(info.param); });
INSTANTIATE_TEST_SUITE_P(TestLocationByCountries, LocationTest, ValuesIn(addressCountries),
[](const TestParamInfo<AddressCountry>& info) { return generatedTestName.at(info.param); });

TEST_F(LocationTest, shouldGenerateUsaStreet)
{
Expand Down Expand Up @@ -192,7 +184,7 @@ TEST_F(LocationTest, shouldGenerateUsaStreetAddress)

TEST_F(LocationTest, shouldGeneratePolandStreet)
{
const auto generatedStreet = Location::street(Country::Poland);
const auto generatedStreet = Location::street(AddressCountry::Poland);

const auto generatedStreetElements = StringHelper::split(generatedStreet, " ");

Expand All @@ -208,7 +200,7 @@ TEST_F(LocationTest, shouldGeneratePolandStreet)

TEST_F(LocationTest, shouldGeneratePolandStreetAddress)
{
const auto generatedStreetAddress = Location::streetAddress(Country::Poland);
const auto generatedStreetAddress = Location::streetAddress(AddressCountry::Poland);

ASSERT_TRUE(std::ranges::any_of(polandStreetPrefixes, [&generatedStreetAddress](const std::string& prefix)
{ return generatedStreetAddress.find(prefix) != std::string::npos; }));
Expand All @@ -218,7 +210,7 @@ TEST_F(LocationTest, shouldGeneratePolandStreetAddress)

TEST_F(LocationTest, shouldGenerateRussiaStreet)
{
const auto generatedStreet = Location::street(Country::Russia);
const auto generatedStreet = Location::street(AddressCountry::Russia);

const auto generatedStreetElements = StringHelper::split(generatedStreet, " ");

Expand All @@ -244,7 +236,7 @@ TEST_F(LocationTest, shouldGenerateRussiaStreet)

TEST_F(LocationTest, shouldGenerateRussiaStreetAddress)
{
const auto generatedStreetAddress = Location::streetAddress(Country::Russia);
const auto generatedStreetAddress = Location::streetAddress(AddressCountry::Russia);

std::vector<std::string> firstNames{russianMalesFirstNames};
firstNames.insert(firstNames.end(), russianFemalesFirstNames.begin(), russianFemalesFirstNames.end());
Expand All @@ -255,16 +247,20 @@ TEST_F(LocationTest, shouldGenerateRussiaStreetAddress)
ASSERT_TRUE(std::ranges::any_of(russiaStreetPrefixes, [&generatedStreetAddress](const std::string& prefix)
{ return generatedStreetAddress.find(prefix) != std::string::npos; }));
ASSERT_TRUE(std::ranges::any_of(firstNames, [&generatedStreetAddress](const std::string& firstName)
{ return generatedStreetAddress.find(firstName) != std::string::npos; }) ||
{ return generatedStreetAddress.find(firstName) != std::string::npos; }) ||
std::ranges::any_of(lastNames, [&generatedStreetAddress](const std::string& lastName)
{ return generatedStreetAddress.find(lastName) != std::string::npos; }) ||
std::ranges::any_of(russiaStreetNames, [&generatedStreetAddress](const std::string& streetName)
{ return generatedStreetAddress.find(streetName) != std::string::npos;; }));
std::ranges::any_of(russiaStreetNames,
[&generatedStreetAddress](const std::string& streetName)
{
return generatedStreetAddress.find(streetName) != std::string::npos;
;
}));
}

TEST_F(LocationTest, shouldGenerateFranceStreet)
{
const auto generatedStreet = Location::street(Country::France);
const auto generatedStreet = Location::street(AddressCountry::France);

const auto generatedStreetElements = StringHelper::split(generatedStreet, " ");

Expand All @@ -281,7 +277,7 @@ TEST_F(LocationTest, shouldGenerateFranceStreet)

TEST_F(LocationTest, shouldGenerateFranceStreetAddress)
{
const auto generatedStreetAddress = Location::streetAddress(Country::France);
const auto generatedStreetAddress = Location::streetAddress(AddressCountry::France);

const auto generatedStreetAddressElements = StringHelper::split(generatedStreetAddress, " ");

Expand Down

0 comments on commit fc84291

Please sign in to comment.