From e4fc0cc75294ba255f167577040bd21f392b70b4 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Sun, 26 May 2024 19:51:08 +0200 Subject: [PATCH] Refactor Phone Module (#592) * change phone to use string_view Signed-off-by: Uilian Ries * add empty phone data Signed-off-by: Uilian Ries * adapt phone tests to use string view Signed-off-by: Uilian Ries * fill phone data with vector files Signed-off-by: Uilian Ries --------- Signed-off-by: Uilian Ries --- CMakeLists.txt | 1 + include/faker-cxx/Phone.h | 9 +- src/modules/phone/Phone.cpp | 24 +- .../{data/PhoneNumbers.h => PhoneData.cpp} | 268 +++++++++++++++++- src/modules/phone/PhoneData.h | 14 + src/modules/phone/data/AreaCodes.h | 29 -- src/modules/phone/data/PhoneData.h | 19 -- tests/modules/phone/PhoneTest.cpp | 39 +-- 8 files changed, 313 insertions(+), 90 deletions(-) rename src/modules/phone/{data/PhoneNumbers.h => PhoneData.cpp} (73%) create mode 100644 src/modules/phone/PhoneData.h delete mode 100644 src/modules/phone/data/AreaCodes.h delete mode 100644 src/modules/phone/data/PhoneData.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 463faf4dc..78fafc5d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ set(FAKER_SOURCES src/modules/number/Number.cpp src/modules/person/Person.cpp src/modules/phone/Phone.cpp + src/modules/phone/PhoneData.cpp src/modules/science/Science.cpp src/modules/sport/Sport.cpp src/modules/string/String.cpp diff --git a/include/faker-cxx/Phone.h b/include/faker-cxx/Phone.h index 5b38d6477..1e5f2381a 100644 --- a/include/faker-cxx/Phone.h +++ b/include/faker-cxx/Phone.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -35,7 +36,7 @@ class Phone * Phone::platform() // "iOS" * @endcode */ - static std::string platform(); + static std::string_view platform(); /** * @brief Returns a random phone model. @@ -46,7 +47,7 @@ class Phone * Phone::modelName() // "Samsung Galaxy S22" * @endcode */ - static std::string modelName(); + static std::string_view modelName(); /** * @brief Returns a random phone manufacturer. @@ -57,7 +58,7 @@ class Phone * Phone::manufacturer() // "Sony" * @endcode */ - static std::string manufacturer(); + static std::string_view manufacturer(); /** * @brief Returns a random phone number based on country phone number template. @@ -92,7 +93,7 @@ class Phone * Phone::areaCode() // "+1" * @endcode */ - static std::string areaCode(); + static std::string_view areaCode(); private: static std::unordered_map createPhoneNumberFormatMap(); diff --git a/src/modules/phone/Phone.cpp b/src/modules/phone/Phone.cpp index 623c936ec..0f408d301 100644 --- a/src/modules/phone/Phone.cpp +++ b/src/modules/phone/Phone.cpp @@ -1,8 +1,6 @@ #include "faker-cxx/Phone.h" -#include "data/AreaCodes.h" -#include "data/PhoneData.h" -#include "data/PhoneNumbers.h" +#include "PhoneData.h" #include "faker-cxx/Helper.h" namespace faker @@ -16,7 +14,7 @@ std::string Phone::number(std::optional format) if (!format.has_value() || format->empty()) { - selectedFormat = Helper::arrayElement(phoneNumbers); + selectedFormat = Helper::arrayElement(phone::phoneNumbers); } else { @@ -43,19 +41,19 @@ std::string Phone::imei() return Helper::replaceCreditCardSymbols("##-######-######-L", '#'); } -std::string Phone::platform() +std::string_view Phone::platform() { - return Helper::arrayElement(faker::data::PhonePlatforms); + return Helper::arrayElement(phone::PhonePlatforms); } -std::string Phone::modelName() +std::string_view Phone::modelName() { - return Helper::arrayElement(faker::data::PhoneModelNames); + return Helper::arrayElement(phone::PhoneModelNames); } -std::string Phone::manufacturer() +std::string_view Phone::manufacturer() { - return Helper::arrayElement(faker::data::PhoneManufacturers); + return Helper::arrayElement(phone::PhoneManufacturers); } std::unordered_map Phone::createPhoneNumberFormatMap() @@ -68,14 +66,14 @@ std::unordered_map Phone::createPhoneNumb { auto formatEnum = static_cast(i); - formatMap[formatEnum] = phoneNumbers[i]; + formatMap[formatEnum] = phone::phoneNumbers[i]; } return formatMap; } -std::string Phone::areaCode() +std::string_view Phone::areaCode() { - return Helper::arrayElement(faker::data::areaCodes); + return Helper::arrayElement(phone::areaCodes); } } diff --git a/src/modules/phone/data/PhoneNumbers.h b/src/modules/phone/PhoneData.cpp similarity index 73% rename from src/modules/phone/data/PhoneNumbers.h rename to src/modules/phone/PhoneData.cpp index ebbf0134c..d3b8d30be 100644 --- a/src/modules/phone/data/PhoneNumbers.h +++ b/src/modules/phone/PhoneData.cpp @@ -1,11 +1,267 @@ -#pragma once +#include "PhoneData.h" -#include -#include - -namespace faker +namespace faker::phone { -const std::vector phoneNumbers = { +const std::array areaCodes = { + "+1", + "+144", + "+20", + "+210", + "+211", + "+212", + "+213", + "+214", + "+215", + "+216", + "+217", + "+218", + "+219", + "+220", + "+221", + "+222", + "+223", + "+224", + "+225", + "+226", + "+227", + "+228", + "+229", + "+230", + "+231", + "+232", + "+233", + "+234", + "+235", + "+236", + "+237", + "+238", + "+239", + "+240", + "+241", + "+242", + "+243", + "+244", + "+245", + "+246", + "+247", + "+248", + "+249", + "+250", + "+251", + "+252", + "+253", + "+254", + "+255", + "+256", + "+257", + "+258", + "+259", + "+260", + "+261", + "+262", + "+263", + "+264", + "+265", + "+266", + "+267", + "+268", + "+269", + "+27", + "+28", + "+290", + "+291", + "+292", + "+293", + "+294", + "+295", + "+296", + "+297", + "+298", + "+299", + "+30", + "+31", + "+32", + "+33", + "+34", + "+350", + "+351", + "+352", + "+353", + "+354", + "+355", + "+356", + "+357", + "+358", + "+359", + "+36", + "+370", + "+371", + "+373", + "+374", + "+375", + "+377", + "+38", + "+380", + "+381", + "+385", + "+387", + "+389", + "+39", + "+40", + "+41", + "+42", + "+43", + "+44", + "+45", + "+46", + "+47", + "+48", + "+49", + "+500", + "+501", + "+502", + "+503", + "+504", + "+505", + "+506", + "+507", + "+508", + "+509", + "+51", + "+52", + "+53", + "+54", + "+55", + "+56", + "+57", + "+58", + "+590", + "+591", + "+592", + "+593", + "+594", + "+595", + "+596", + "+597", + "+598", + "+599", + "+60", + "+61", + "+62", + "+63", + "+64", + "+65", + "+66", + "+670", + "+671", + "+672", + "+673", + "+674", + "+675", + "+676", + "+677", + "+678", + "+679", + "+680", + "+681", + "+682", + "+683", + "+684", + "+685", + "+686", + "+687", + "+688", + "+689", + "+690", + "+691", + "+692", + "+7", + "+808", + "+809", + "+81", + "+82", + "+84", + "+850", + "+852", + "+853", + "+855", + "+856", + "+86", + "+871", + "+872", + "+873", + "+874", + "+880", + "+886", + "+90 ", + "+91", + "+92", + "+93", + "+94", + "+95", + "+960", + "+961", + "+962", + "+963", + "+964", + "+965", + "+966", + "+967", + "+968", + "+969", + "+971", + "+972", + "+973", + "+974", + "+975", + "+976", + "+977", + "+98", + "+993", + "+994", + "+995" +}; + +const std::array PhonePlatforms = { + "Android OS", + "Palm OS", + "Symbian", + "Tizen", + "Windows Phone", + "iOS" +}; + +const std::array PhoneModelNames = { + "Google Pixel 4", + "Google Pixel 6", + "Huawei P50", + "Motorola Edge 20", + "OnePlus 9", + "Oppo Find X3", + "Samsung Galaxy S22", + "Samsung Galaxy S9", + "Sony Xperia 1 III", + "Xiaomi Mi 11", + "iPhone 13", + "iPhone 13", + "iPhone 14", + "iPhone 15", + "iPhone X" +}; + +const std::array PhoneManufacturers = { + "Apple", + "Google", + "Huawei", + "Motorola", + "OnePlus", + "Oppo", + "Samsung", + "Sony", + "Xiaomi" +}; + +const std::array phoneNumbers = { "!##-###-###", // Default "+93 (###) ###-####", // Afghanistan "+358 (###) ###-####", // Aland Islands diff --git a/src/modules/phone/PhoneData.h b/src/modules/phone/PhoneData.h new file mode 100644 index 000000000..3cc572e2b --- /dev/null +++ b/src/modules/phone/PhoneData.h @@ -0,0 +1,14 @@ + +#pragma once + +#include +#include + +namespace faker::phone +{ +extern const std::array areaCodes; +extern const std::array PhonePlatforms; +extern const std::array PhoneModelNames; +extern const std::array PhoneManufacturers; +extern const std::array phoneNumbers; +} diff --git a/src/modules/phone/data/AreaCodes.h b/src/modules/phone/data/AreaCodes.h deleted file mode 100644 index a676cf95b..000000000 --- a/src/modules/phone/data/AreaCodes.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -namespace data -{ -const std::vector areaCodes = { - "+1", "+144", "+20", "+210", "+211", "+212", "+213", "+214", "+215", "+216", "+217", "+218", "+219", "+220", - "+221", "+222", "+223", "+224", "+225", "+226", "+227", "+228", "+229", "+230", "+231", "+232", "+233", "+234", - "+235", "+236", "+237", "+238", "+239", "+240", "+241", "+242", "+243", "+244", "+245", "+246", "+247", "+248", - "+249", "+250", "+251", "+252", "+253", "+254", "+255", "+256", "+257", "+258", "+259", "+260", "+261", "+262", - "+263", "+264", "+265", "+266", "+267", "+268", "+269", "+27", "+28", "+290", "+291", "+292", "+293", "+294", - "+295", "+296", "+297", "+298", "+299", "+30", "+31", "+32", "+33", "+34", "+350", "+351", "+352", "+353", - "+354", "+355", "+356", "+357", "+358", "+359", "+36", "+370", "+371", "+373", "+374", "+375", "+377", "+38", - "+380", "+381", "+385", "+387", "+389", "+39", "+40", "+41", "+42", "+43", "+44", "+45", "+46", "+47", - "+48", "+49", "+500", "+501", "+502", "+503", "+504", "+505", "+506", "+507", "+508", "+509", "+51", "+52", - "+53", "+54", "+55", "+56", "+57", "+58", "+590", "+591", "+592", "+593", "+594", "+595", "+596", "+597", - "+598", "+599", "+60", "+61", "+62", "+63", "+64", "+65", "+66", "+670", "+671", "+672", "+673", "+674", - "+675", "+676", "+677", "+678", "+679", "+680", "+681", "+682", "+683", "+684", "+685", "+686", "+687", "+688", - "+689", "+690", "+691", "+692", "+7", "+808", "+809", "+81", "+82", "+84", "+850", "+852", "+853", "+855", - "+856", "+86", "+871", "+872", "+873", "+874", "+880", "+886", "+90 ", "+91", "+92", "+93", "+94", "+95", - "+960", "+961", "+962", "+963", "+964", "+965", "+966", "+967", "+968", "+969", "+971", "+972", "+973", "+974", - "+975", "+976", "+977", "+98", "+993", "+994", "+995", -}; -} -} diff --git a/src/modules/phone/data/PhoneData.h b/src/modules/phone/data/PhoneData.h deleted file mode 100644 index 92563191b..000000000 --- a/src/modules/phone/data/PhoneData.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include -#include - -namespace faker -{ -namespace data -{ -const std::vector PhonePlatforms = { - "Android OS", "iOS", "Windows Phone", "Symbian", "Palm OS", "Tizen", -}; -const std::vector PhoneModelNames = { - "Samsung Galaxy S9", "iPhone X", "Google Pixel 4", "Samsung Galaxy S22", "iPhone 13", - "iPhone 13", "iPhone 14", "iPhone 15", "Google Pixel 6", "OnePlus 9", - "Xiaomi Mi 11", "Huawei P50", "Oppo Find X3", "Sony Xperia 1 III", "Motorola Edge 20"}; -const std::vector PhoneManufacturers = {"Samsung", "Apple", "Google", "OnePlus", "Xiaomi", - "Huawei", "Oppo", "Sony", "Motorola"}; -} -} diff --git a/tests/modules/phone/PhoneTest.cpp b/tests/modules/phone/PhoneTest.cpp index efbcb49e5..23f376b23 100644 --- a/tests/modules/phone/PhoneTest.cpp +++ b/tests/modules/phone/PhoneTest.cpp @@ -2,11 +2,12 @@ #include #include +#include +#include #include "gtest/gtest.h" -#include "phone/data/AreaCodes.h" -#include "phone/data/PhoneData.h" +#include "phone/PhoneData.h" using namespace ::testing; using namespace faker; @@ -23,15 +24,15 @@ class PhoneTest : public Test TEST_F(PhoneTest, NumberWithNoFormat) { - std::string phoneNumber = Phone::number(); + const auto phoneNumber = Phone::number(); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); } TEST_F(PhoneTest, NumberWithFormat) { - std::string format = "501-###-###"; - std::string phoneNumber = Phone::number(format); + auto format = "501-###-###"; + auto phoneNumber = Phone::number(format); ASSERT_NE(phoneNumber, format); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); @@ -53,7 +54,7 @@ TEST_F(PhoneTest, NumberWithFormat) TEST_F(PhoneTest, IMEIGeneration) { - std::string imei = Phone::imei(); + auto imei = Phone::imei(); imei.erase(std::remove(imei.begin(), imei.end(), '-'), imei.end()); @@ -63,7 +64,7 @@ TEST_F(PhoneTest, IMEIGeneration) TEST_F(PhoneTest, NumberFormatTest) { - std::string phoneNumber = Phone::number(PhoneNumberCountryFormat::Zimbabwe); + const auto phoneNumber = Phone::number(PhoneNumberCountryFormat::Zimbabwe); EXPECT_FALSE(phoneNumber.empty()); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); @@ -71,31 +72,31 @@ TEST_F(PhoneTest, NumberFormatTest) TEST_F(PhoneTest, PlatformGeneration) { - std::string generatedPlatform = Phone::platform(); - ASSERT_TRUE(std::ranges::any_of(faker::data::PhonePlatforms.begin(), faker::data::PhonePlatforms.end(), - [generatedPlatform](const std::string& platform) + const auto generatedPlatform = Phone::platform(); + ASSERT_TRUE(std::ranges::any_of(phone::PhonePlatforms.begin(), phone::PhonePlatforms.end(), + [generatedPlatform](const std::string_view& platform) { return platform == generatedPlatform; })); } TEST_F(PhoneTest, ModelNameGeneration) { - std::string generatedModelName = Phone::modelName(); - ASSERT_TRUE(std::ranges::any_of(faker::data::PhoneModelNames.begin(), faker::data::PhoneModelNames.end(), - [generatedModelName](const std::string& modelName) + const auto generatedModelName = Phone::modelName(); + ASSERT_TRUE(std::ranges::any_of(phone::PhoneModelNames.begin(), phone::PhoneModelNames.end(), + [generatedModelName](const std::string_view& modelName) { return modelName == generatedModelName; })); } TEST_F(PhoneTest, ManufacturerGeneration) { - std::string generatedManufacturer = Phone::manufacturer(); - ASSERT_TRUE(std::ranges::any_of(faker::data::PhoneManufacturers.begin(), faker::data::PhoneManufacturers.end(), - [generatedManufacturer](const std::string& manufacturer) + const auto generatedManufacturer = Phone::manufacturer(); + ASSERT_TRUE(std::ranges::any_of(phone::PhoneManufacturers.begin(), phone::PhoneManufacturers.end(), + [generatedManufacturer](const std::string_view& manufacturer) { return manufacturer == generatedManufacturer; })); } TEST_F(PhoneTest, AreaCodeGeneration) { - std::string areaCode = Phone::areaCode(); - ASSERT_TRUE(std::ranges::any_of(faker::data::areaCodes.begin(), faker::data::areaCodes.end(), - [areaCode](const std::string& code) { return code == areaCode; })); + const auto areaCode = Phone::areaCode(); + ASSERT_TRUE(std::ranges::any_of(phone::areaCodes.begin(), phone::areaCodes.end(), + [areaCode](const std::string_view& code) { return code == areaCode; })); }