diff --git a/include/faker-cxx/Internet.h b/include/faker-cxx/Internet.h index c9edb119b..f9209df8a 100644 --- a/include/faker-cxx/Internet.h +++ b/include/faker-cxx/Internet.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include diff --git a/include/faker-cxx/Location.h b/include/faker-cxx/Location.h index 5c765bf3b..84ed13d27 100644 --- a/include/faker-cxx/Location.h +++ b/include/faker-cxx/Location.h @@ -2,7 +2,7 @@ #include -#include "types/Country.h" +#include "faker-cxx/types/AddressCountry.h" #include "types/Precision.h" namespace faker @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/include/faker-cxx/types/AddressCountry.h b/include/faker-cxx/types/AddressCountry.h new file mode 100644 index 000000000..e75558b47 --- /dev/null +++ b/include/faker-cxx/types/AddressCountry.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include + +namespace faker +{ +// TODO: remove when AddressCountry enum values will be same as Country values +enum class AddressCountry +{ + Usa, + Poland, + France, + Russia, +}; + +const std::vector addressCountries{ + AddressCountry::Usa, + AddressCountry::Poland, + AddressCountry::France, + AddressCountry::Russia, +}; + +inline std::string toString(AddressCountry country) +{ + std::map 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); +} +} diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index ede89d0b5..ec1033be7 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -1,5 +1,6 @@ #include "faker-cxx/Internet.h" +#include #include #include diff --git a/src/modules/internet/InternetTest.cpp b/src/modules/internet/InternetTest.cpp index 311915b72..305bf0132 100644 --- a/src/modules/internet/InternetTest.cpp +++ b/src/modules/internet/InternetTest.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/Internet.h" #include +#include #include #include diff --git a/src/modules/location/Location.cpp b/src/modules/location/Location.cpp index 821857428..8f0b3124c 100644 --- a/src/modules/location/Location.cpp +++ b/src/modules/location/Location.cpp @@ -20,11 +20,18 @@ namespace faker { namespace { -const std::map countryToCountryAddressesMapping{ - {Country::Usa, usaAddresses}, - {Country::Poland, polandAddresses}, - {Country::Russia, russiaAddresses}, - {Country::France, franceAddresses}, +const std::map countryToCountryAddressesMapping{ + {AddressCountry::Usa, usaAddresses}, + {AddressCountry::Poland, polandAddresses}, + {AddressCountry::Russia, russiaAddresses}, + {AddressCountry::France, franceAddresses}, +}; + +const std::map countryAddressToCountryMapping{ + {AddressCountry::Usa, Country::Usa}, + {AddressCountry::Poland, Country::Poland}, + {AddressCountry::Russia, Country::Russia}, + {AddressCountry::France, Country::France}, }; } @@ -43,21 +50,21 @@ std::string Location::state() return Helper::arrayElement(states); } -std::string Location::city(Country country) +std::string Location::city(AddressCountry country) { const auto& countryAddresses = countryToCountryAddressesMapping.at(country); return Helper::arrayElement(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); @@ -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(countryAddresses.streetFormats); const auto dataGeneratorsMapping = std::map>{ - {"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(countryAddresses.streetNames); }}, {"streetPrefix", @@ -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); @@ -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); diff --git a/src/modules/location/LocationTest.cpp b/src/modules/location/LocationTest.cpp index 4f86c5f9c..0b9101517 100644 --- a/src/modules/location/LocationTest.cpp +++ b/src/modules/location/LocationTest.cpp @@ -25,30 +25,22 @@ using namespace faker; namespace { -const std::map countryToCountryAddressesMapping{ - {Country::Usa, usaAddresses}, - {Country::Poland, polandAddresses}, - {Country::Russia, russiaAddresses}, - {Country::France, franceAddresses}, +const std::map countryToCountryAddressesMapping{ + {AddressCountry::Usa, usaAddresses}, + {AddressCountry::Poland, polandAddresses}, + {AddressCountry::Russia, russiaAddresses}, + {AddressCountry::France, franceAddresses}, }; -// TODO: replace with countries from Country enum file -const std::vector tempCountries{ - Country::Poland, - Country::France, - Country::Usa, - Country::Russia, -}; - -const std::map generatedTestName{ - {Country::Usa, "shouldGenerateAmericanAddress"}, - {Country::France, "shouldGenerateFrenchAddress"}, - {Country::Poland, "shouldGeneratePolishAddress"}, - {Country::Russia, "shouldGenerateRussianAddress"}, +const std::map generatedTestName{ + {AddressCountry::Usa, "shouldGenerateAmericanAddress"}, + {AddressCountry::France, "shouldGenerateFrenchAddress"}, + {AddressCountry::Poland, "shouldGeneratePolishAddress"}, + {AddressCountry::Russia, "shouldGenerateRussianAddress"}, }; } -class LocationTest : public TestWithParam +class LocationTest : public TestWithParam { public: static bool checkIfZipCode(const std::string& zipCode) @@ -142,8 +134,8 @@ TEST_P(LocationTest, shouldGenerateSecondaryAddress) })); } -INSTANTIATE_TEST_SUITE_P(TestLocationByCountries, LocationTest, ValuesIn(tempCountries), - [](const TestParamInfo& info) { return generatedTestName.at(info.param); }); +INSTANTIATE_TEST_SUITE_P(TestLocationByCountries, LocationTest, ValuesIn(addressCountries), + [](const TestParamInfo& info) { return generatedTestName.at(info.param); }); TEST_F(LocationTest, shouldGenerateUsaStreet) { @@ -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, " "); @@ -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; })); @@ -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, " "); @@ -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 firstNames{russianMalesFirstNames}; firstNames.insert(firstNames.end(), russianFemalesFirstNames.begin(), russianFemalesFirstNames.end()); @@ -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, " "); @@ -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, " ");