From b67ccb976e5856807a0afa6f9dcffaebf3bce3d0 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 2 Jul 2024 21:29:36 +0200 Subject: [PATCH] refactor: luhncheck module - luhncheck module migration from class to functions within common namespace - modified reference to functions and variables from luhncheck module in all dependent modules Signed-off-by: Guru Mehar Rachaputi --- src/common/LuhnCheck.cpp | 8 ++++---- src/common/LuhnCheck.h | 12 ++++-------- src/modules/helper/Helper.cpp | 2 +- tests/common/LuhnCheckTest.cpp | 5 +++-- tests/modules/finance/FinanceTest.cpp | 10 +++++----- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/common/LuhnCheck.cpp b/src/common/LuhnCheck.cpp index 7bf6063ec..46d6d7a6e 100644 --- a/src/common/LuhnCheck.cpp +++ b/src/common/LuhnCheck.cpp @@ -4,9 +4,9 @@ #include #include -namespace faker +namespace faker::common { -int LuhnCheck::luhnCheckSum(const std::string& inputString) +int luhnCheckSum(const std::string& inputString) { std::string modifiedStr = inputString; modifiedStr.erase( @@ -36,12 +36,12 @@ int LuhnCheck::luhnCheckSum(const std::string& inputString) return sum % 10; } -bool LuhnCheck::luhnCheck(const std::string& inputString) +bool luhnCheck(const std::string& inputString) { return luhnCheckSum(inputString) == 0; } -int LuhnCheck::luhnCheckValue(const std::string& inputString) +int luhnCheckValue(const std::string& inputString) { int checksum = luhnCheckSum(inputString.substr(0, inputString.length() - 1) + '0'); diff --git a/src/common/LuhnCheck.h b/src/common/LuhnCheck.h index b1a7b7bb9..d9892964a 100644 --- a/src/common/LuhnCheck.h +++ b/src/common/LuhnCheck.h @@ -3,11 +3,8 @@ #include #include "faker-cxx/Export.h" -namespace faker +namespace faker::common { -class LuhnCheck -{ -public: /** * @brief Returns luhn checksum value for the given value. * @@ -16,7 +13,7 @@ class LuhnCheck * @return The luhn checksum value for the given value. * */ - FAKER_CXX_EXPORT static int luhnCheckSum(const std::string& inputString); + FAKER_CXX_EXPORT int luhnCheckSum(const std::string& inputString); /** * @brief Checks that the given string passes the luhn algorithm. @@ -26,7 +23,7 @@ class LuhnCheck * @return Is the string pass the check or not. * */ - FAKER_CXX_EXPORT static bool luhnCheck(const std::string& inputString); + FAKER_CXX_EXPORT bool luhnCheck(const std::string& inputString); /** * @brief Returns the luhn check value for the given string. @@ -37,6 +34,5 @@ class LuhnCheck * @return the luhn check value for the given string. * */ - FAKER_CXX_EXPORT static int luhnCheckValue(const std::string& inputString); -}; + FAKER_CXX_EXPORT int luhnCheckValue(const std::string& inputString); } diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index cabe73d01..d7071badf 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -52,7 +52,7 @@ std::string replaceCreditCardSymbols(const std::string& inputString, char symbol modifiedString = replaceSymbolWithNumber(modifiedString, symbol); // Calculate the luhnCheckValue and replace 'L' with the checkNum - int checkNum = LuhnCheck::luhnCheckValue(modifiedString); + int checkNum = common::luhnCheckValue(modifiedString); size_t pos = modifiedString.find('L'); diff --git a/tests/common/LuhnCheckTest.cpp b/tests/common/LuhnCheckTest.cpp index 37e451ba9..27fc8718f 100644 --- a/tests/common/LuhnCheckTest.cpp +++ b/tests/common/LuhnCheckTest.cpp @@ -5,17 +5,18 @@ #include "gtest/gtest.h" using namespace faker; +using namespace faker::common; TEST(LuhnCheckTest, BasicTest) { std::string input = "6453-4876-8626-8995-3771"; - bool result = LuhnCheck::luhnCheck(input); + bool result = luhnCheck(input); EXPECT_TRUE(result); } TEST(LuhnCheckValueTest, BasicTest) { std::string input = "6453-4876-8626-8995-377L"; - int result = LuhnCheck::luhnCheckValue(input); + int result = luhnCheckValue(input); EXPECT_EQ(result, 1); } diff --git a/tests/modules/finance/FinanceTest.cpp b/tests/modules/finance/FinanceTest.cpp index f55af8d17..7d685cf24 100644 --- a/tests/modules/finance/FinanceTest.cpp +++ b/tests/modules/finance/FinanceTest.cpp @@ -290,7 +290,7 @@ TEST_F(FinanceTest, shouldGenerateCreditCardNumber) const auto generatedCreditCardNumber = creditCardNumber(); ASSERT_TRUE(checkIfAllCharactersAreCreditCardCharacters(generatedCreditCardNumber)); - ASSERT_TRUE(LuhnCheck::luhnCheck(generatedCreditCardNumber)); + ASSERT_TRUE(common::luhnCheck(generatedCreditCardNumber)); } TEST_F(FinanceTest, shouldGenerateAmericanExpressCreditCardNumber) @@ -299,7 +299,7 @@ TEST_F(FinanceTest, shouldGenerateAmericanExpressCreditCardNumber) ASSERT_TRUE(generatedCreditCardNumber.starts_with("34") || generatedCreditCardNumber.starts_with("37")); ASSERT_TRUE(checkIfAllCharactersAreCreditCardCharacters(generatedCreditCardNumber)); - ASSERT_TRUE(LuhnCheck::luhnCheck(generatedCreditCardNumber)); + ASSERT_TRUE(common::luhnCheck(generatedCreditCardNumber)); } TEST_F(FinanceTest, shouldGenerateDiscoverCreditCardNumber) @@ -312,7 +312,7 @@ TEST_F(FinanceTest, shouldGenerateDiscoverCreditCardNumber) generatedCreditCardNumber.starts_with("648") || generatedCreditCardNumber.starts_with("649") || generatedCreditCardNumber.starts_with("6011-62")); ASSERT_TRUE(checkIfAllCharactersAreCreditCardCharacters(generatedCreditCardNumber)); - ASSERT_TRUE(LuhnCheck::luhnCheck(generatedCreditCardNumber)); + ASSERT_TRUE(common::luhnCheck(generatedCreditCardNumber)); } TEST_F(FinanceTest, shouldGenerateMasterCardCreditCardNumber) @@ -323,7 +323,7 @@ TEST_F(FinanceTest, shouldGenerateMasterCardCreditCardNumber) generatedCreditCardNumber.starts_with("53") || generatedCreditCardNumber.starts_with("54") || generatedCreditCardNumber.starts_with("55") || generatedCreditCardNumber.starts_with("6771-89")); ASSERT_TRUE(checkIfAllCharactersAreCreditCardCharacters(generatedCreditCardNumber)); - ASSERT_TRUE(LuhnCheck::luhnCheck(generatedCreditCardNumber)); + ASSERT_TRUE(common::luhnCheck(generatedCreditCardNumber)); } TEST_F(FinanceTest, shouldGenerateVisaCreditCardNumber) @@ -332,7 +332,7 @@ TEST_F(FinanceTest, shouldGenerateVisaCreditCardNumber) ASSERT_TRUE(generatedCreditCardNumber.starts_with("4")); ASSERT_TRUE(checkIfAllCharactersAreCreditCardCharacters(generatedCreditCardNumber)); - ASSERT_TRUE(LuhnCheck::luhnCheck(generatedCreditCardNumber)); + ASSERT_TRUE(common::luhnCheck(generatedCreditCardNumber)); } TEST_F(FinanceTest, shouldGenerateCreditCardCvv)