Skip to content

Commit

Permalink
add spain location, add county for countryaddresses, and fix bugs in …
Browse files Browse the repository at this point in the history
…location test
  • Loading branch information
joshhn committed Jan 12, 2024
1 parent 847aea1 commit 02d970d
Show file tree
Hide file tree
Showing 24 changed files with 571 additions and 97 deletions.
26 changes: 13 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(BUILD_FAKER_TESTS DEFAULT ON)

if (MSVC)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++20 /permissive- /bigobj")
else ()
else()
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wconversion -Wformat -Werror"
)
endif ()
endif()

set(LIBRARY_NAME faker-cxx)

Expand Down Expand Up @@ -113,13 +113,13 @@ target_include_directories(
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include"
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include")

if (APPLE)
if(APPLE)
add_subdirectory(externals/fmt)
set(FMT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/fmt/include")
target_link_libraries(${LIBRARY_NAME} PRIVATE fmt)
endif ()
endif()

if (BUILD_FAKER_TESTS)
if(BUILD_FAKER_TESTS)
add_subdirectory(externals/googletest)

set(GTEST_INCLUDE_DIR
Expand All @@ -138,24 +138,24 @@ if (BUILD_FAKER_TESTS)
add_executable(${LIBRARY_NAME}-UT ${FAKER_UT_SOURCES})

target_link_libraries(${LIBRARY_NAME}-UT PRIVATE gtest_main gmock_main
faker-cxx)
faker-cxx)

if (APPLE)
if(APPLE)
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
else ()
${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
else()
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})
endif ()
${CMAKE_CURRENT_LIST_DIR})
endif()

add_test(
NAME ${LIBRARY_NAME}-UT
COMMAND ${LIBRARY_NAME}-UT
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

target_code_coverage(${LIBRARY_NAME}-UT ALL)
endif ()
endif()
15 changes: 14 additions & 1 deletion include/faker-cxx/Location.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Location
static std::string countryCode();

/**
* @brief Returns a random state for a given country..
* @brief Returns a random state for a given country.
*
* @param country The country to generate state from. Defaults to `Country::Usa`.
*
Expand All @@ -45,6 +45,19 @@ class Location
*/
static std::string state(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random county for a given country.
*
* @param country The country to generate county from. Defaults to `Country::Usa`.
*
* @returns County.
*
* @code
* Location::county() // "Adams County"
* @endcode
*/
static std::string county(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random city for given country.
*
Expand Down
4 changes: 3 additions & 1 deletion include/faker-cxx/types/AddressCountry.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ namespace faker
Australia,
India,
Denmark,
Spain,
};

const std::vector<AddressCountry> addressCountries{
AddressCountry::Usa, AddressCountry::Poland, AddressCountry::France,
AddressCountry::Russia, AddressCountry::Ukraine, AddressCountry::Italy,
AddressCountry::Germany, AddressCountry::Czech, AddressCountry::India,
AddressCountry::Denmark,
AddressCountry::Denmark, AddressCountry::Australia, AddressCountry::Spain,
};

inline std::string toString(AddressCountry country)
Expand All @@ -36,6 +37,7 @@ namespace faker
{AddressCountry::Russia, "Russia"}, {AddressCountry::Ukraine, "Ukraine"}, {AddressCountry::Italy, "Italy"},
{AddressCountry::Germany, "Germany"}, {AddressCountry::Czech, "Czech"}, {AddressCountry::Australia, "Australia"},
{AddressCountry::India, "India"}, {AddressCountry::Denmark, "Denmark"},
{AddressCountry::Spain, "Spain"},
};

return countryToStringMapping.at(country);
Expand Down
21 changes: 18 additions & 3 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include "data/poland/PolandAddresses.h"
#include "data/italy/ItalyAddresses.h"
#include "data/russia/RussiaAddresses.h"
#include "data/States.h"
#include "data/TimeZones.h"
#include "data/ukraine/UkraineAddresses.h"
#include "data/germany/GermanyAddresses.h"
#include "data/usa/UsaAddresses.h"
#include "data/spain/SpainAddresses.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
#include "faker-cxx/String.h"
Expand All @@ -33,7 +33,7 @@ namespace faker
{AddressCountry::Ukraine, ukraineAddresses}, {AddressCountry::Italy, italyAddresses},
{AddressCountry::Germany, germanyAddresses}, {AddressCountry::Czech, czechAddresses},
{AddressCountry::Australia, australiaAddresses}, {AddressCountry::India, indiaAddresses},
{AddressCountry::Denmark, denmarkAddresses}
{AddressCountry::Denmark, denmarkAddresses}, {AddressCountry::Spain, spainAddresses},
};

const std::map<AddressCountry, Country> countryAddressToCountryMapping{
Expand All @@ -42,7 +42,7 @@ namespace faker
{AddressCountry::Ukraine, Country::Ukraine}, {AddressCountry::Italy, Country::Italy},
{AddressCountry::Germany, Country::Germany}, {AddressCountry::Czech, Country::Czech},
{AddressCountry::Australia, Country::Australia}, {AddressCountry::India, Country::India},
{AddressCountry::Denmark, Country::Denmark},
{AddressCountry::Denmark, Country::Denmark}, {AddressCountry::Spain, Country::Spain},
};
}

Expand All @@ -56,6 +56,16 @@ namespace faker
return Helper::arrayElement<std::string>(countryCodes);
}

std::string Location::county(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
if(countryAddresses.counties.empty())
{
return "";
}
return Helper::arrayElement<std::string>(countryAddresses.counties);
}

std::string Location::state(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
Expand Down Expand Up @@ -122,6 +132,11 @@ namespace faker
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

if (countryAddresses.secondaryAddressFormats.empty())
{
return "";
}

const auto secondaryAddressFormat = Helper::arrayElement<std::string>(countryAddresses.secondaryAddressFormats);

return Helper::replaceSymbolWithNumber(secondaryAddressFormat);
Expand Down
84 changes: 74 additions & 10 deletions src/modules/location/LocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "../../common/StringHelper.h"
#include "../person/data/england/EnglishFirstNames.h"
#include "../person/data/england/EnglishLastNames.h"
#include "../person/data/spain/SpanishFirstNames.h"
#include "../person/data/spain/SpanishLastNames.h"
#include "../person/data/russia/RussianFirstNames.h"
#include "../person/data/russia/RussianLastNames.h"
#include "../person/data/ukraine/UkrainianFirstNames.h"
Expand All @@ -29,6 +31,7 @@
#include "data/usa/UsaAddresses.h"
#include "data/india/IndiaAddresses.h"
#include "data/denmark/DenmarkAddresses.h"
#include "data/spain/SpainAddresses.h"

using namespace ::testing;
using namespace faker;
Expand All @@ -41,7 +44,7 @@ namespace
{AddressCountry::Ukraine, ukraineAddresses}, {AddressCountry::Italy, italyAddresses},
{AddressCountry::Germany, germanyAddresses}, {AddressCountry::Czech, czechAddresses},
{AddressCountry::Australia, australiaAddresses}, {AddressCountry::India, indiaAddresses},
{AddressCountry::Denmark, denmarkAddresses},
{AddressCountry::Denmark, denmarkAddresses}, {AddressCountry::Spain, spainAddresses},
};

const std::map<AddressCountry, std::string> generatedTestName{
Expand All @@ -56,6 +59,7 @@ namespace
{AddressCountry::Australia, "shouldGenerateAustraliaAddress"},
{AddressCountry::India, "shouldGenerateIndiaAddress"},
{AddressCountry::Denmark, "shouldGenerateDenmarkAddress"},
{AddressCountry::Spain, "shouldGenerateSpainAddress"},
};
}

Expand Down Expand Up @@ -87,6 +91,19 @@ class LocationTest : public TestWithParam<AddressCountry>
}
};

TEST_P(LocationTest, shouldGenerateCounty)
{
const auto country = GetParam();

const auto& countryAddresses = countryToCountryAddressesMapping.at(country);

const auto generatedCounty = Location::county(country);

ASSERT_TRUE(std::ranges::any_of(countryAddresses.counties,
[&generatedCounty](const std::string& county) { return county == generatedCounty; }) ||
(countryAddresses.counties.empty() && generatedCounty.empty()));
}

TEST_P(LocationTest, shouldGenerateState)
{
const auto country = GetParam();
Expand Down Expand Up @@ -137,8 +154,10 @@ TEST_P(LocationTest, shouldGenerateBuildingNumber)
ASSERT_TRUE(std::ranges::any_of(countryAddresses.buildingNumberFormats,
[&generatedBuildingNumber](const std::string& buildingNumberFormat)
{ return buildingNumberFormat.size() == generatedBuildingNumber.size(); }));

ASSERT_TRUE(checkIfAllCharactersAreNumeric(generatedBuildingNumber) ||
checkIfAllCharactersAreNumeric(generatedBuildingNumberExceptLastCharacter));
checkIfAllCharactersAreNumeric(generatedBuildingNumberExceptLastCharacter) ||
generatedBuildingNumber == spainAddresses.buildingNumberFormats[0]);
}

TEST_P(LocationTest, shouldGenerateSecondaryAddress)
Expand All @@ -149,14 +168,17 @@ TEST_P(LocationTest, shouldGenerateSecondaryAddress)

const auto generatedSecondaryAddress = Location::secondaryAddress(country);

ASSERT_TRUE(std::ranges::any_of(
ASSERT_TRUE((countryAddresses.secondaryAddressFormats.empty() && generatedSecondaryAddress.empty()) ||
std::ranges::any_of(
countryAddresses.secondaryAddressFormats,
[&generatedSecondaryAddress, &country](const std::string& secondaryAddressFormat)
{
const auto secondaryAddressElements = StringHelper::split(secondaryAddressFormat, " ");

const auto& secondaryAddressPrefix = secondaryAddressElements[0];

const auto& secondaryAddressNumber = secondaryAddressElements[1];

const auto generatedSecondaryAddressElements = StringHelper::split(generatedSecondaryAddress, " ");

const auto& generatedSecondaryAddressPrefix = generatedSecondaryAddressElements[0];
Expand All @@ -165,18 +187,18 @@ TEST_P(LocationTest, shouldGenerateSecondaryAddress)

if (country == faker::AddressCountry::Denmark)
{
if (!checkIfAllCharactersAreNumeric(generatedSecondaryAddressPrefix))
{
return generatedSecondaryAddressNumber == generatedSecondaryAddressNumber &&
generatedSecondaryAddress.size() == secondaryAddressFormat.size();
}
const auto generatedSecondaryAddressNumberPart = generatedSecondaryAddressPrefix.substr(
0, generatedSecondaryAddressPrefix.size() - 1);
return generatedSecondaryAddressNumber == secondaryAddressNumber &&
generatedSecondaryAddress.size() == secondaryAddressFormat.size() &&
checkIfAllCharactersAreNumeric(generatedSecondaryAddressNumberPart);
}
else if (country == faker::AddressCountry::Germany)
{
if (checkIfAllCharactersAreNumeric(generatedSecondaryAddressPrefix))
{
return generatedSecondaryAddressNumber == generatedSecondaryAddressNumber &&
generatedSecondaryAddress.size() == secondaryAddressFormat.size();
return generatedSecondaryAddressNumber == secondaryAddressNumber &&
generatedSecondaryAddress.size() == secondaryAddressFormat.size();
}
}

Expand Down Expand Up @@ -640,3 +662,45 @@ TEST_F(LocationTest, shouldGenerateDenmarkStreetAddress)
ASSERT_TRUE(std::ranges::any_of(denmarkStreetNames, [&generatedStreetAddress](const std::string& streetName)
{ return generatedStreetAddress.find(streetName) != std::string::npos; }));
}

TEST_F(LocationTest, shouldGenerateSpainStreet)
{
const auto generatedStreet = Location::street(AddressCountry::Spain);

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

const auto& generatedStreetPrefix = generatedStreetElements[0];
const auto& generatedStreetSuffix =
StringHelper::join({generatedStreetElements.begin() + 1, generatedStreetElements.end()});

ASSERT_TRUE(std::ranges::any_of(spainStreetSuffixes, [&generatedStreetPrefix](const std::string& streetSuffix)
{ return streetSuffix == generatedStreetPrefix; }));

std::vector<std::string> firstNames{spanishMalesFirstNames};
firstNames.insert(firstNames.end(), spanishFemalesFirstNames.begin(), spanishFemalesFirstNames.end());

std::vector<std::string> lastNames{spanishLastNames};

ASSERT_TRUE(std::ranges::any_of(firstNames, [&generatedStreetSuffix](const std::string& firstName)
{ return generatedStreetSuffix.find(firstName) != std::string::npos; }) ||
std::ranges::any_of(lastNames, [&generatedStreetSuffix](const std::string& lastName)
{ return generatedStreetSuffix.find(lastName) != std::string::npos; }));
}

TEST_F(LocationTest, shouldGenerateSpainStreetAddress)
{
const auto generatedStreetAddress = Location::streetAddress(AddressCountry::Spain);

ASSERT_TRUE(std::ranges::any_of(spainStreetSuffixes, [&generatedStreetAddress](const std::string& suffix)
{ return generatedStreetAddress.find(suffix) != std::string::npos; }));

std::vector<std::string> firstNames{spanishMalesFirstNames};
firstNames.insert(firstNames.end(), spanishFemalesFirstNames.begin(), spanishFemalesFirstNames.end());

std::vector<std::string> lastNames{spanishLastNames};

ASSERT_TRUE(std::ranges::any_of(firstNames, [&generatedStreetAddress](const std::string& firstName)
{ return generatedStreetAddress.find(firstName) != std::string::npos; }) ||
std::ranges::any_of(lastNames, [&generatedStreetAddress](const std::string& lastName)
{ return generatedStreetAddress.find(lastName) != std::string::npos; }));
}
1 change: 1 addition & 0 deletions src/modules/location/data/CountryAddresses.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ struct CountryAddresses
std::vector<std::string> streetSuffixes;
std::vector<std::string> buildingNumberFormats;
std::vector<std::string> states;
std::vector<std::string> counties;
};
}
17 changes: 0 additions & 17 deletions src/modules/location/data/States.h

This file was deleted.

20 changes: 10 additions & 10 deletions src/modules/location/data/australia/AustraliaAddresses.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const std::vector<std::string> australiaBuildingNumberFormats{"####", "###", "##
const std::vector<std::string> australiaStreetFormats{"{firstName} {streetSuffix}", "{lastName} {streetSuffix}"};



const CountryAddresses australiaAddresses{australiaCities,
australiaZipCodeFormat,
australiaAddressFormats,
{},
australiaStreetFormats,
{},
{},
australiaStreetSuffixes,
australiaBuildingNumberFormats,
australiaStates };
australiaZipCodeFormat,
australiaAddressFormats,
{},
australiaStreetFormats,
{},
{},
australiaStreetSuffixes,
australiaBuildingNumberFormats,
australiaStates,
{}};
}
Loading

0 comments on commit 02d970d

Please sign in to comment.