Skip to content

Commit

Permalink
refactor date module data
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed May 28, 2024
1 parent 16f1e8c commit 5fa3349
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 72 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions include/faker-cxx/Date.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -153,7 +153,7 @@ class Date
* Date::monthAbbreviatedName() // "Feb"
* @endcode
*/
static std::string monthAbbreviatedName();
static std::string_view monthAbbreviatedName();

/**
* @brief Returns random year.
Expand Down Expand Up @@ -252,6 +252,6 @@ class Date
* Date::timezone() // PT
* @endcode
*/
static std::string timezone();
static std::string_view timezone();
};
}
24 changes: 11 additions & 13 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <sstream>

#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"

Expand Down Expand Up @@ -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<std::string>(weekdayNames);
return Helper::arrayElement(weekdayNames);
}

std::string Date::weekdayAbbreviatedName()
std::string_view Date::weekdayAbbreviatedName()
{
return Helper::arrayElement<std::string>(weekdayAbbreviatedNames);
return Helper::arrayElement(weekdayAbbreviatedNames);
}

std::string Date::monthName()
std::string_view Date::monthName()
{
return Helper::arrayElement<std::string>(monthNames);
return Helper::arrayElement(monthNames);
}

std::string Date::monthAbbreviatedName()
std::string_view Date::monthAbbreviatedName()
{
return Helper::arrayElement<std::string>(monthAbbreviatedNames);
return Helper::arrayElement(monthAbbreviatedNames);
}

unsigned int Date::year()
Expand Down Expand Up @@ -195,9 +193,9 @@ unsigned Date::dayOfWeek()
return Number::integer<unsigned>(1, 7);
}

std::string Date::timezone()
std::string_view Date::timezone()
{
return Helper::arrayElement<std::string>(faker::timezonesAbbreviatedNames);
return Helper::arrayElement(timezonesAbbreviatedNames);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#pragma once

#include <string>
#include <vector>
#include "DateData.h"

namespace faker
{
const std::vector<std::string> timezonesAbbreviatedNames = {
const std::array<std::string_view, 12> monthNames = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
};

const std::array<std::string_view, 12> monthAbbreviatedNames = {"Jan", "Feb", "Mar", "Apr", "May", "June",
"July", "Aug", "Sept", "Oct", "Nov", "Dec"};

const std::array<std::string_view, 7> weekdayNames = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};

const std::array<std::string_view, 7> weekdayAbbreviatedNames = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

const std::array<std::string_view, 209> 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",
Expand Down
13 changes: 13 additions & 0 deletions src/modules/date/DateData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <array>
#include <string_view>

namespace faker
{
extern const std::array<std::string_view, 12> monthNames;
extern const std::array<std::string_view, 12> monthAbbreviatedNames;
extern const std::array<std::string_view, 7> weekdayNames;
extern const std::array<std::string_view, 7> weekdayAbbreviatedNames;
extern const std::array<std::string_view, 209> timezonesAbbreviatedNames;
}
16 changes: 0 additions & 16 deletions src/modules/date/data/MonthNames.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/modules/date/data/WeekdayNames.h

This file was deleted.

25 changes: 13 additions & 12 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "faker-cxx/Git.h"

#include <string>

#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"
Expand All @@ -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<std::string> 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<std::string> 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 += "-";
Expand Down
14 changes: 6 additions & 8 deletions tests/modules/date/DateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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; }));
}

Expand All @@ -241,15 +239,15 @@ 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; }));
}

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; }));
}

Expand All @@ -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; }));
}

Expand Down Expand Up @@ -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; }));
}

Expand Down

0 comments on commit 5fa3349

Please sign in to comment.