From a90b9739ce2246ee6b73de258f32f74ce214d96d Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 25 Jun 2024 13:49:09 +0200 Subject: [PATCH 1/2] refactor: music module migrate (#731) - music module migration from class to functions within music namespace Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/Music.h | 18 +++++++----------- src/modules/music/Music.cpp | 14 +++++++------- tests/modules/music/MusicTest.cpp | 13 +++++++------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/include/faker-cxx/Music.h b/include/faker-cxx/Music.h index 151b52e87..313b79c04 100644 --- a/include/faker-cxx/Music.h +++ b/include/faker-cxx/Music.h @@ -2,21 +2,18 @@ #include -namespace faker +namespace faker::music { -class Music -{ -public: /** * @brief Returns a random artist. * * @returns Artist. * * @code - * Music::artist() // "Nirvana" + * music::artist() // "Nirvana" * @endcode */ - static std::string_view artist(); + std::string_view artist(); /** * @brief Returns a random music genre. @@ -24,10 +21,10 @@ class Music * @returns Music genre. * * @code - * Music::genre() // "Rock" + * music::genre() // "Rock" * @endcode */ - static std::string_view genre(); + std::string_view genre(); /** * @brief Returns a random song name. @@ -35,9 +32,8 @@ class Music * @returns Song name. * * @code - * Music::songName() // "Light My Fire" + * music::songName() // "Light My Fire" * @endcode */ - static std::string_view songName(); -}; + std::string_view songName(); } diff --git a/src/modules/music/Music.cpp b/src/modules/music/Music.cpp index 2c0ce584c..d68e11075 100644 --- a/src/modules/music/Music.cpp +++ b/src/modules/music/Music.cpp @@ -5,20 +5,20 @@ #include "faker-cxx/Helper.h" #include "MusicData.h" -namespace faker +namespace faker::music { -std::string_view Music::artist() +std::string_view artist() { - return Helper::arrayElement(music::artists); + return Helper::arrayElement(artists); } -std::string_view Music::genre() +std::string_view genre() { - return Helper::arrayElement(music::musicGenres); + return Helper::arrayElement(musicGenres); } -std::string_view Music::songName() +std::string_view songName() { - return Helper::arrayElement(music::songNames); + return Helper::arrayElement(songNames); } } diff --git a/tests/modules/music/MusicTest.cpp b/tests/modules/music/MusicTest.cpp index c2af12f7d..7bee72132 100644 --- a/tests/modules/music/MusicTest.cpp +++ b/tests/modules/music/MusicTest.cpp @@ -9,6 +9,7 @@ using namespace ::testing; using namespace faker; +using namespace faker::music; class MusicTest : public Test { @@ -17,24 +18,24 @@ class MusicTest : public Test TEST_F(MusicTest, shouldGenerateArtist) { - const auto generatedArtist = Music::artist(); + const auto generatedArtist = artist(); - ASSERT_TRUE(std::ranges::any_of(music::artists, [generatedArtist](const std::string_view& artist) + ASSERT_TRUE(std::ranges::any_of(artists, [generatedArtist](const std::string_view& artist) { return generatedArtist == artist; })); } TEST_F(MusicTest, shouldGenerateGenre) { - const auto generatedGenre = Music::genre(); + const auto generatedGenre = genre(); - ASSERT_TRUE(std::ranges::any_of(music::musicGenres, [generatedGenre](const std::string_view& genre) + ASSERT_TRUE(std::ranges::any_of(musicGenres, [generatedGenre](const std::string_view& genre) { return generatedGenre == genre; })); } TEST_F(MusicTest, shouldGenerateSongName) { - const auto generatedSongName = Music::songName(); + const auto generatedSongName = songName(); - ASSERT_TRUE(std::ranges::any_of(music::songNames, [generatedSongName](const std::string_view& songName) + ASSERT_TRUE(std::ranges::any_of(songNames, [generatedSongName](const std::string_view& songName) { return generatedSongName == songName; })); } From 02fa3e780bfd6f3f413ee85814d1c7c68aaf99de Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 25 Jun 2024 13:49:35 +0200 Subject: [PATCH 2/2] refactor: phone module migrate (#729) - phone module migration from class to functions within phone namespace Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/Phone.h | 43 +++++++++++++------------------ src/modules/phone/Phone.cpp | 22 ++++++++-------- tests/modules/phone/PhoneTest.cpp | 43 ++++++++++++++++--------------- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/include/faker-cxx/Phone.h b/include/faker-cxx/Phone.h index 767d4181e..5e37b21f1 100644 --- a/include/faker-cxx/Phone.h +++ b/include/faker-cxx/Phone.h @@ -5,13 +5,9 @@ #include #include -namespace faker +namespace faker::phone { enum class PhoneNumberCountryFormat; - -class Phone -{ -public: /** * @brief Returns a random phone number. * @@ -20,12 +16,12 @@ class Phone * @returns Random phone number. * * @code - * Phone::number() // "961-770-7727" - * Phone::number("501-###-###") // "501-039-841" - * Phone::number("+48 91 ### ## ##") // "+48 91 463 61 70" + * phone::number() // "961-770-7727" + * phone::number("501-###-###") // "501-039-841" + * phone::number("+48 91 ### ## ##") // "+48 91 463 61 70" * @endcode */ - static std::string number(std::optional = std::nullopt); + std::string number(std::optional = std::nullopt); /** * @brief Returns a random phone platform. @@ -33,10 +29,10 @@ class Phone * @returns Random phone platform. * * @code - * Phone::platform() // "iOS" + * phone::platform() // "iOS" * @endcode */ - static std::string_view platform(); + std::string_view platform(); /** * @brief Returns a random phone model. @@ -44,10 +40,10 @@ class Phone * @returns Random phone model. * * @code - * Phone::modelName() // "Samsung Galaxy S22" + * phone::modelName() // "Samsung Galaxy S22" * @endcode */ - static std::string_view modelName(); + std::string_view modelName(); /** * @brief Returns a random phone manufacturer. @@ -55,10 +51,10 @@ class Phone * @returns Random phone manufacturer. * * @code - * Phone::manufacturer() // "Sony" + * phone::manufacturer() // "Sony" * @endcode */ - static std::string_view manufacturer(); + std::string_view manufacturer(); /** * @brief Returns a random phone number based on country phone number template. @@ -68,10 +64,10 @@ class Phone * @returns Random phone number based on country phone number template. * * @code - * Phone::number(PhoneNumberCountryFormat::Usa) // "+1 (395) 714-1494" + * phone::number(PhoneNumberCountryFormat::Usa) // "+1 (395) 714-1494" * @endcode */ - static std::string number(PhoneNumberCountryFormat format); + std::string number(PhoneNumberCountryFormat format); /** * @brief Returns IMEI number. @@ -79,10 +75,10 @@ class Phone * @returns IMEI number. * * @code - * Phone::imei() // "13-850175-913761-7" + * phone::imei() // "13-850175-913761-7" * @endcode */ - static std::string imei(); + std::string imei(); /** * @brief returns a random country area code @@ -90,15 +86,12 @@ class Phone * @returns Random country area code * * @code - * Phone::areaCode() // "+1" + * phone::areaCode() // "+1" * @endcode */ - static std::string_view areaCode(); + std::string_view areaCode(); -private: - static std::unordered_map createPhoneNumberFormatMap(); - static std::unordered_map phoneNumberFormatMap; -}; + std::unordered_map createPhoneNumberFormatMap(); enum class PhoneNumberCountryFormat { diff --git a/src/modules/phone/Phone.cpp b/src/modules/phone/Phone.cpp index 07aef8a1b..d3e3aa117 100644 --- a/src/modules/phone/Phone.cpp +++ b/src/modules/phone/Phone.cpp @@ -8,12 +8,12 @@ #include "faker-cxx/Helper.h" #include "PhoneData.h" -namespace faker +namespace faker::phone { -std::unordered_map Phone::phoneNumberFormatMap = - Phone::createPhoneNumberFormatMap(); +std::unordered_map phoneNumberFormatMap = + createPhoneNumberFormatMap(); -std::string Phone::number(std::optional format) +std::string number(std::optional format) { std::string selectedFormat; @@ -29,7 +29,7 @@ std::string Phone::number(std::optional format) return Helper::replaceSymbolWithNumber(selectedFormat); } -std::string Phone::number(PhoneNumberCountryFormat format) +std::string number(PhoneNumberCountryFormat format) { std::string countryFormat = phoneNumberFormatMap.at(format); @@ -41,27 +41,27 @@ std::string Phone::number(PhoneNumberCountryFormat format) return Helper::replaceSymbolWithNumber(countryFormat); } -std::string Phone::imei() +std::string imei() { return Helper::replaceCreditCardSymbols("##-######-######-L", '#'); } -std::string_view Phone::platform() +std::string_view platform() { return Helper::arrayElement(phone::PhonePlatforms); } -std::string_view Phone::modelName() +std::string_view modelName() { return Helper::arrayElement(phone::PhoneModelNames); } -std::string_view Phone::manufacturer() +std::string_view manufacturer() { return Helper::arrayElement(phone::PhoneManufacturers); } -std::unordered_map Phone::createPhoneNumberFormatMap() +std::unordered_map createPhoneNumberFormatMap() { std::unordered_map formatMap; @@ -77,7 +77,7 @@ std::unordered_map Phone::createPhoneNumb return formatMap; } -std::string_view Phone::areaCode() +std::string_view areaCode() { return Helper::arrayElement(phone::areaCodes); } diff --git a/tests/modules/phone/PhoneTest.cpp b/tests/modules/phone/PhoneTest.cpp index b2288d4c5..eb3f22e77 100644 --- a/tests/modules/phone/PhoneTest.cpp +++ b/tests/modules/phone/PhoneTest.cpp @@ -11,6 +11,7 @@ using namespace ::testing; using namespace faker; +using namespace faker::phone; class PhoneTest : public Test { @@ -24,7 +25,7 @@ class PhoneTest : public Test TEST_F(PhoneTest, NumberWithNoFormat) { - const auto phoneNumber = Phone::number(); + const auto phoneNumber = number(); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); } @@ -32,71 +33,71 @@ TEST_F(PhoneTest, NumberWithNoFormat) TEST_F(PhoneTest, NumberWithFormat) { auto format = "501-###-###"; - auto phoneNumber = Phone::number(format); + auto phoneNumber = number(format); ASSERT_NE(phoneNumber, format); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); format = "+48 91 ### ## ##"; - phoneNumber = Phone::number(format); + phoneNumber = number(format); ASSERT_NE(phoneNumber, format); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); format = "+376 (###) ###-####"; - phoneNumber = Phone::number(format); + phoneNumber = number(format); ASSERT_NE(phoneNumber, format); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); format = "+376 (!!!) !!!-!!!!"; - phoneNumber = Phone::number(format); + phoneNumber = number(format); ASSERT_NE(phoneNumber, format); ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); } TEST_F(PhoneTest, IMEIGeneration) { - auto imei = Phone::imei(); + auto generatedImei = imei(); - imei.erase(std::remove(imei.begin(), imei.end(), '-'), imei.end()); + generatedImei.erase(std::remove(generatedImei.begin(), generatedImei.end(), '-'), generatedImei.end()); - ASSERT_EQ(imei.length(), 15); - ASSERT_TRUE(isStringNumericWithSpecialChars(imei)); + ASSERT_EQ(generatedImei.length(), 15); + ASSERT_TRUE(isStringNumericWithSpecialChars(generatedImei)); } TEST_F(PhoneTest, NumberFormatTest) { - const auto phoneNumber = Phone::number(PhoneNumberCountryFormat::Zimbabwe); + const auto generatedPhoneNumber = number(PhoneNumberCountryFormat::Zimbabwe); - EXPECT_FALSE(phoneNumber.empty()); - ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber)); + EXPECT_FALSE(generatedPhoneNumber.empty()); + ASSERT_TRUE(isStringNumericWithSpecialChars(generatedPhoneNumber)); } TEST_F(PhoneTest, PlatformGeneration) { - const auto generatedPlatform = Phone::platform(); - ASSERT_TRUE(std::ranges::any_of(phone::PhonePlatforms.begin(), phone::PhonePlatforms.end(), + const auto generatedPlatform = platform(); + ASSERT_TRUE(std::ranges::any_of(PhonePlatforms.begin(), PhonePlatforms.end(), [generatedPlatform](const std::string_view& platform) { return platform == generatedPlatform; })); } TEST_F(PhoneTest, ModelNameGeneration) { - const auto generatedModelName = Phone::modelName(); - ASSERT_TRUE(std::ranges::any_of(phone::PhoneModelNames.begin(), phone::PhoneModelNames.end(), + const auto generatedModelName = modelName(); + ASSERT_TRUE(std::ranges::any_of(PhoneModelNames.begin(), PhoneModelNames.end(), [generatedModelName](const std::string_view& modelName) { return modelName == generatedModelName; })); } TEST_F(PhoneTest, ManufacturerGeneration) { - const auto generatedManufacturer = Phone::manufacturer(); - ASSERT_TRUE(std::ranges::any_of(phone::PhoneManufacturers.begin(), phone::PhoneManufacturers.end(), + const auto generatedManufacturer = manufacturer(); + ASSERT_TRUE(std::ranges::any_of(PhoneManufacturers.begin(), PhoneManufacturers.end(), [generatedManufacturer](const std::string_view& manufacturer) { return manufacturer == generatedManufacturer; })); } TEST_F(PhoneTest, AreaCodeGeneration) { - 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; })); + const auto generatedAreaCode = areaCode(); + ASSERT_TRUE(std::ranges::any_of(areaCodes.begin(), areaCodes.end(), + [generatedAreaCode](const std::string_view& code) { return code == generatedAreaCode; })); }