Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds Date module functions: year/month/dayOfMonth/dayOfWeek #510

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions include/faker-cxx/Date.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,56 @@ class Date
* @endcode
*/
static std::string monthAbbreviatedName();

/**
* @brief Returns a random year.
*
* @param minYear The minimum year to generate a year. Defaults to `1920`.
* @param maxYear The maximum year to generate a year. Defaults to `2000`.
*
* @returns unsigned int.
*
* @code
* Date::year() // "1996"
* @endcode
*/
static unsigned int year(int minYear=1920, int maxYear=2000);

/**
* @brief Returns a month.
*
*
* @returns unsigned int 1-12.
*
* @code
* Date::month() // "1"
* @endcode
*/
static unsigned int month();

/**
* @brief Returns a random day of month.
*
*
* @returns unsigned int 1-31.
*
* @code
* Date::dayOfMonth() // "1"
* @endcode
*/
static unsigned int dayOfMonth();

/**
* @brief Returns a random day of the week.
*
*
* @returns unsigned int 1-7.
*
* @code
* Date::dayOfWeek() // "1"
* @endcode
*/
static unsigned int dayOfWeek();

};
}
31 changes: 31 additions & 0 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono>
#include <ctime>
#include <random>
#include <iomanip>

#include "../../common/FormatHelper.h"
Expand Down Expand Up @@ -150,4 +151,34 @@ std::string Date::monthAbbreviatedName()
return Helper::arrayElement<std::string>(monthAbbreviatedNames);
}

unsigned int Date::year(int minYear, int maxYear)
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<unsigned int> distr(minYear, maxYear);

return distr(eng);
}

unsigned int Date::month()
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<unsigned int> distr(1, 12);
}

unsigned int Date::dayOfMonth()
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<unsigned int> distr(1, 31);
}

unsigned int Date::dayOfWeek()
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<unsigned int> distr(1, 7);
}

KashuvY marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 27 additions & 0 deletions src/modules/date/DateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,30 @@ TEST_F(DateTest, shouldGenerateMonthAbbreviatedName)
[generatedMonthAbbreviatedName](const std::string& monthAbbreviatedName)
{ return monthAbbreviatedName == generatedMonthAbbreviatedName; }));
}

TEST_F(DateTest, shouldGenerateYearWithinRange) {
int minYear = 1900;
int maxYear = 2100;
auto generatedYear = Date::year(minYear, maxYear);

ASSERT_TRUE(generatedYear >= minYear && generatedYear <= maxYear);
}

TEST_F(DateTest, shouldGenerateMonthWithinRange) {
auto generatedMonth = Date::month();

ASSERT_TRUE(generatedMonth >= 1 && generatedMonth <= 12);
}

TEST_F(DateTest, shouldGenerateDayOfMonthWithinRange) {
auto generatedDay = Date::dayOfMonth();

ASSERT_TRUE(generatedDay >= 1 && generatedDay <= 31);
}

TEST_F(DateTest, shouldGenerateDayOfWeekWithinRange) {
auto generatedDayOfWeek = Date::dayOfWeek();

ASSERT_TRUE(generatedDayOfWeek >= 1 && generatedDayOfWeek <= 7);
}

Loading