From 4fd32d88f7ba9c0f3265598b4bd0e0c52fdbc322 Mon Sep 17 00:00:00 2001 From: Eric Floyd <101071809+eric-bodhi@users.noreply.github.com> Date: Sun, 21 Jan 2024 16:31:02 -0500 Subject: [PATCH] added credit card expiration function to Finance (#487) --- include/faker-cxx/Finance.h | 11 +++++++++++ src/modules/finance/Finance.cpp | 6 ++++++ src/modules/finance/FinanceTest.cpp | 14 ++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/faker-cxx/Finance.h b/include/faker-cxx/Finance.h index bf8431fcc..ce7435602 100644 --- a/include/faker-cxx/Finance.h +++ b/include/faker-cxx/Finance.h @@ -220,5 +220,16 @@ class Finance * @endcode */ static std::string ethereumAddress(); + + /** + * Generates a random expiration date. + * + * @returns std::string date. + * + * @code + * Finance::creditCardExpirationDate() // "03/26" + * @endcode + */ + static std::string creditCardExpirationDate(); }; } diff --git a/src/modules/finance/Finance.cpp b/src/modules/finance/Finance.cpp index 0a37f37fe..5577f8302 100644 --- a/src/modules/finance/Finance.cpp +++ b/src/modules/finance/Finance.cpp @@ -7,6 +7,7 @@ #include "data/CreditCardsFormats.h" #include "data/Currencies.h" #include "data/IbanFormats.h" +#include "faker-cxx/Date.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "faker-cxx/String.h" @@ -166,4 +167,9 @@ std::string Finance::ethereumAddress() return String::hexadecimal(40, HexCasing::Lower); } +std::string Finance::creditCardExpirationDate() +{ + const auto expirationDate = Date::futureDate(3); + return expirationDate.substr(5, 2) + "/" + expirationDate.substr(2, 2); +} } diff --git a/src/modules/finance/FinanceTest.cpp b/src/modules/finance/FinanceTest.cpp index 8b03c5b7e..1c8d6079e 100644 --- a/src/modules/finance/FinanceTest.cpp +++ b/src/modules/finance/FinanceTest.cpp @@ -386,6 +386,13 @@ TEST_F(FinanceTest, shouldGenerateEthereumAddress) { return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; })); } +TEST_F(FinanceTest, shouldGenerateExpirationDate) +{ + const auto expirationDate = Finance::creditCardExpirationDate(); + int tenthPlaceYear = std::stoi(expirationDate.substr(3, 2)); + std::cout << expirationDate << " " << tenthPlaceYear << "\n"; + ASSERT_TRUE(tenthPlaceYear >= 24); +} class FinanceBicTest : public TestWithParam { @@ -399,13 +406,12 @@ TEST_P(FinanceBicTest, CheckBicGenerator) const auto& bankIdentifiersCodes = bankIdentifiersCodesMapping.at(country); - ASSERT_TRUE(std::ranges::any_of(bankIdentifiersCodes, [bic](const std::string& bankIdentifierCode) { - return bic == bankIdentifierCode; - })); + ASSERT_TRUE(std::ranges::any_of(bankIdentifiersCodes, [bic](const std::string& bankIdentifierCode) + { return bic == bankIdentifierCode; })); } INSTANTIATE_TEST_SUITE_P(TestBicGenerator, FinanceBicTest, Values(BicCountry::Poland, BicCountry::United_States, BicCountry::United_Kingdom, BicCountry::Germany, BicCountry::Romania, BicCountry::France, BicCountry::Italy, BicCountry::Spain, BicCountry::Netherlands, BicCountry::India), - [](const TestParamInfo& info) { return generatedBicTestName.at(info.param); }); \ No newline at end of file + [](const TestParamInfo& info) { return generatedBicTestName.at(info.param); });