Skip to content

Commit

Permalink
refactor: luhncheck module
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
00thirdeye00 committed Jul 2, 2024
1 parent ea580c0 commit b67ccb9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/common/LuhnCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <cctype>
#include <string>

namespace faker
namespace faker::common
{
int LuhnCheck::luhnCheckSum(const std::string& inputString)
int luhnCheckSum(const std::string& inputString)
{
std::string modifiedStr = inputString;
modifiedStr.erase(
Expand Down Expand Up @@ -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');

Expand Down
12 changes: 4 additions & 8 deletions src/common/LuhnCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#include <string>
#include "faker-cxx/Export.h"

namespace faker
namespace faker::common
{
class LuhnCheck
{
public:
/**
* @brief Returns luhn checksum value for the given value.
*
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
5 changes: 3 additions & 2 deletions tests/common/LuhnCheckTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 5 additions & 5 deletions tests/modules/finance/FinanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b67ccb9

Please sign in to comment.