From 5fa334999d2e2f2b8b70a19d209feb08e5682f0e Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Tue, 28 May 2024 20:48:35 +0200 Subject: [PATCH] refactor date module data --- CMakeLists.txt | 1 + include/faker-cxx/Date.h | 12 ++++----- src/modules/date/Date.cpp | 24 ++++++++---------- .../date/{data/TimeZones.h => DateData.cpp} | 20 +++++++++++---- src/modules/date/DateData.h | 13 ++++++++++ src/modules/date/data/MonthNames.h | 16 ------------ src/modules/date/data/WeekdayNames.h | 12 --------- src/modules/git/Git.cpp | 25 ++++++++++--------- tests/modules/date/DateTest.cpp | 14 +++++------ 9 files changed, 65 insertions(+), 72 deletions(-) rename src/modules/date/{data/TimeZones.h => DateData.cpp} (71%) create mode 100644 src/modules/date/DateData.h delete mode 100644 src/modules/date/data/MonthNames.h delete mode 100644 src/modules/date/data/WeekdayNames.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b6d8a672..4a8180747 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ set(FAKER_SOURCES src/modules/database/DatabaseData.cpp src/modules/datatype/Datatype.cpp src/modules/date/Date.cpp + src/modules/date/DateData.cpp src/modules/finance/Finance.cpp src/modules/food/Food.cpp src/modules/git/Git.cpp diff --git a/include/faker-cxx/Date.h b/include/faker-cxx/Date.h index c9f655411..ab2cd7afd 100644 --- a/include/faker-cxx/Date.h +++ b/include/faker-cxx/Date.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace faker { @@ -120,7 +120,7 @@ class Date * Date::weekdayName() // "Monday" * @endcode */ - static std::string weekdayName(); + static std::string_view weekdayName(); /** * @brief Returns an abbreviated name of random day of the week. @@ -131,7 +131,7 @@ class Date * Date::weekdayAbbreviatedName() // "Mon" * @endcode */ - static std::string weekdayAbbreviatedName(); + static std::string_view weekdayAbbreviatedName(); /** * @brief Returns a random name of a month. @@ -142,7 +142,7 @@ class Date * Date::monthName() // "October" * @endcode */ - static std::string monthName(); + static std::string_view monthName(); /** * @brief Returns an abbreviated name of random month. @@ -153,7 +153,7 @@ class Date * Date::monthAbbreviatedName() // "Feb" * @endcode */ - static std::string monthAbbreviatedName(); + static std::string_view monthAbbreviatedName(); /** * @brief Returns random year. @@ -252,6 +252,6 @@ class Date * Date::timezone() // PT * @endcode */ - static std::string timezone(); + static std::string_view timezone(); }; } diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index cf399fb2c..7e847658a 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -6,9 +6,7 @@ #include #include "../../common/FormatHelper.h" -#include "data/MonthNames.h" -#include "data/TimeZones.h" -#include "data/WeekdayNames.h" +#include "DateData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" @@ -132,24 +130,24 @@ std::string Date::birthdateByYear(int minYear, int maxYear, Date::DateFormat dat return betweenDate(startDate, endDate, dateFormat); } -std::string Date::weekdayName() +std::string_view Date::weekdayName() { - return Helper::arrayElement(weekdayNames); + return Helper::arrayElement(weekdayNames); } -std::string Date::weekdayAbbreviatedName() +std::string_view Date::weekdayAbbreviatedName() { - return Helper::arrayElement(weekdayAbbreviatedNames); + return Helper::arrayElement(weekdayAbbreviatedNames); } -std::string Date::monthName() +std::string_view Date::monthName() { - return Helper::arrayElement(monthNames); + return Helper::arrayElement(monthNames); } -std::string Date::monthAbbreviatedName() +std::string_view Date::monthAbbreviatedName() { - return Helper::arrayElement(monthAbbreviatedNames); + return Helper::arrayElement(monthAbbreviatedNames); } unsigned int Date::year() @@ -195,9 +193,9 @@ unsigned Date::dayOfWeek() return Number::integer(1, 7); } -std::string Date::timezone() +std::string_view Date::timezone() { - return Helper::arrayElement(faker::timezonesAbbreviatedNames); + return Helper::arrayElement(timezonesAbbreviatedNames); } } diff --git a/src/modules/date/data/TimeZones.h b/src/modules/date/DateData.cpp similarity index 71% rename from src/modules/date/data/TimeZones.h rename to src/modules/date/DateData.cpp index b8249cdcc..0917b42fb 100644 --- a/src/modules/date/data/TimeZones.h +++ b/src/modules/date/DateData.cpp @@ -1,11 +1,21 @@ -#pragma once - -#include -#include +#include "DateData.h" namespace faker { -const std::vector timezonesAbbreviatedNames = { +const std::array monthNames = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December", +}; + +const std::array monthAbbreviatedNames = {"Jan", "Feb", "Mar", "Apr", "May", "June", + "July", "Aug", "Sept", "Oct", "Nov", "Dec"}; + +const std::array weekdayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday", "Sunday"}; + +const std::array weekdayAbbreviatedNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; + +const std::array timezonesAbbreviatedNames = { "ACDT", "ACST", "ACT", "ACT", "ACWST", "ADT", "AEDT", "AEST", "AET (AEST/AEDT)", "AFT", "AKDT", "AKST", "ALMT", "AMST", "AMT", "AMT", "ANAT", "AQTT", "ART", "AST", "AST", "AWST", "AZOST", "AZOT", "AZT", "BNT", "BIOT", diff --git a/src/modules/date/DateData.h b/src/modules/date/DateData.h new file mode 100644 index 000000000..4765d3fe2 --- /dev/null +++ b/src/modules/date/DateData.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +namespace faker +{ +extern const std::array monthNames; +extern const std::array monthAbbreviatedNames; +extern const std::array weekdayNames; +extern const std::array weekdayAbbreviatedNames; +extern const std::array timezonesAbbreviatedNames; +} diff --git a/src/modules/date/data/MonthNames.h b/src/modules/date/data/MonthNames.h deleted file mode 100644 index ab89b320b..000000000 --- a/src/modules/date/data/MonthNames.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector monthNames = { - - "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December", -}; - -const std::vector monthAbbreviatedNames = {"Jan", "Feb", "Mar", "Apr", "May", "June", - "July", "Aug", "Sept", "Oct", "Nov", "Dec"}; -} diff --git a/src/modules/date/data/WeekdayNames.h b/src/modules/date/data/WeekdayNames.h deleted file mode 100644 index 49a40c196..000000000 --- a/src/modules/date/data/WeekdayNames.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector weekdayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", - "Friday", "Saturday", "Sunday"}; - -const std::vector weekdayAbbreviatedNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; -} diff --git a/src/modules/git/Git.cpp b/src/modules/git/Git.cpp index 6336308b4..7e12d9a80 100644 --- a/src/modules/git/Git.cpp +++ b/src/modules/git/Git.cpp @@ -1,10 +1,8 @@ #include "faker-cxx/Git.h" -#include - #include "../../common/FormatHelper.h" #include "../../common/StringHelper.h" -#include "../date/data/MonthNames.h" +#include "../date/DateData.h" #include "faker-cxx/Date.h" #include "faker-cxx/Internet.h" #include "faker-cxx/Number.h" @@ -31,21 +29,24 @@ std::string Git::branch(unsigned maxIssueNum) std::string Git::commitDate(unsigned years) { - std::string date = Date::pastDate(int(years)); - std::string outputDate = Date::weekdayAbbreviatedName(); + const auto date = Date::pastDate(int(years)); + + const auto dateSplit = StringHelper::split(date, "-"); + + const auto& year = dateSplit[0]; + const auto& month = dateSplit[1]; + const auto& rest = dateSplit[2]; - std::vector dateSplit = StringHelper::split(date, "-"); - std::string year = dateSplit[0]; - std::string month = dateSplit[1]; - std::string rest = dateSplit[2]; + const auto restSplit = StringHelper::split(rest, "T"); - std::vector restSplit = StringHelper::split(rest, "T"); - std::string day = restSplit[0]; + const auto& day = restSplit[0]; - std::string time = StringHelper::split(restSplit[1], "Z")[0]; + const auto time = StringHelper::split(restSplit[1], "Z")[0]; int timeZone = Number::integer(0, 12); + std::string timeZoneString; + if (Number::integer(0, 1)) { timeZoneString += "-"; diff --git a/tests/modules/date/DateTest.cpp b/tests/modules/date/DateTest.cpp index 35891e0f4..1346ca76e 100644 --- a/tests/modules/date/DateTest.cpp +++ b/tests/modules/date/DateTest.cpp @@ -6,9 +6,7 @@ #include "gtest/gtest.h" #include "common/StringHelper.h" -#include "date/data/MonthNames.h" -#include "date/data/TimeZones.h" -#include "date/data/WeekdayNames.h" +#include "date/DateData.h" #ifdef _WIN32 #define timegm _mkgmtime @@ -232,7 +230,7 @@ TEST_F(DateTest, shouldGenerateWeekdayName) { const auto generatedWeekdayName = Date::weekdayName(); - ASSERT_TRUE(std::ranges::any_of(weekdayNames, [generatedWeekdayName](const std::string& weekdayName) + ASSERT_TRUE(std::ranges::any_of(weekdayNames, [generatedWeekdayName](const std::string_view& weekdayName) { return weekdayName == generatedWeekdayName; })); } @@ -241,7 +239,7 @@ TEST_F(DateTest, shouldGenerateWeekdayAbbreviatedName) const auto generatedWeekdayAbbreviatedName = Date::weekdayAbbreviatedName(); ASSERT_TRUE(std::ranges::any_of(weekdayAbbreviatedNames, - [generatedWeekdayAbbreviatedName](const std::string& weekdayAbbreviatedName) + [generatedWeekdayAbbreviatedName](const std::string_view& weekdayAbbreviatedName) { return weekdayAbbreviatedName == generatedWeekdayAbbreviatedName; })); } @@ -249,7 +247,7 @@ TEST_F(DateTest, shouldGenerateMonthName) { const auto generatedMonthName = Date::monthName(); - ASSERT_TRUE(std::ranges::any_of(monthNames, [generatedMonthName](const std::string& monthName) + ASSERT_TRUE(std::ranges::any_of(monthNames, [generatedMonthName](const std::string_view& monthName) { return monthName == generatedMonthName; })); } @@ -258,7 +256,7 @@ TEST_F(DateTest, shouldGenerateMonthAbbreviatedName) const auto generatedMonthAbbreviatedName = Date::monthAbbreviatedName(); ASSERT_TRUE(std::ranges::any_of(monthAbbreviatedNames, - [generatedMonthAbbreviatedName](const std::string& monthAbbreviatedName) + [generatedMonthAbbreviatedName](const std::string_view& monthAbbreviatedName) { return monthAbbreviatedName == generatedMonthAbbreviatedName; })); } @@ -322,7 +320,7 @@ TEST_F(DateTest, shouldGenerateRandomTimezone) { const auto generatedTimeZone = Date::timezone(); - ASSERT_TRUE(std::ranges::any_of(timezonesAbbreviatedNames, [generatedTimeZone](const std::string& timezoneName) + ASSERT_TRUE(std::ranges::any_of(timezonesAbbreviatedNames, [generatedTimeZone](const std::string_view& timezoneName) { return timezoneName == generatedTimeZone; })); }