Skip to content

Commit

Permalink
test: git testcase added (#793)
Browse files Browse the repository at this point in the history
* test: git testcase added

- testcase added for git module for code coverage

- modified commit date function to just return ISO date format

Signed-off-by: Guru Mehar Rachaputi <gurumeharrachaputi@gmail.com>

* refactor: git module

- branch function in git module is modified for codecoverage

Signed-off-by: Guru Mehar Rachaputi <gurumeharrachaputi@gmail.com>

---------

Signed-off-by: Guru Mehar Rachaputi <gurumeharrachaputi@gmail.com>
  • Loading branch information
00thirdeye00 committed Jul 8, 2024
1 parent e1cbca0 commit f36227a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 56 deletions.
63 changes: 9 additions & 54 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ namespace faker::git

std::string branch(unsigned maxIssueNum)
{
switch (number::integer(1, 3))
switch (number::integer(1, 2))
{
case 1:
return common::format("{}-{}", word::verb(), word::noun());
case 2:
return common::format("{}-{}-{}", word::verb(), word::adjective(), word::noun());
default:
return common::format("{}-{}-{}-{}", number::integer(unsigned(1), maxIssueNum), word::verb(),
word::adjective(), word::noun());
Expand All @@ -34,57 +32,18 @@ std::string branch(unsigned maxIssueNum)

std::string commitDate(unsigned years)
{
const auto date = faker::date::pastDate(int(years));
const std::string pastDate = faker::date::pastDate(int(years));

const auto dateSplit = common::split(date, "-");

const auto& year = dateSplit[0];
const auto& month = dateSplit[1];
const auto& rest = dateSplit[2];

const auto restSplit = common::split(rest, "T");

const auto& day = restSplit[0];

const auto time = common::split(restSplit[1], "Z")[0];

int timeZone = number::integer(0, 12);

std::string timeZoneString;

if (number::integer(0, 1))
{
timeZoneString += "-";
}
else
{
timeZoneString += "+";
}

if (timeZone <= 9)
{
timeZoneString += "0";
}

timeZoneString += std::to_string(timeZone * 100);

if (!timeZone)
{
timeZoneString += "00";
}

return common::format("{} {} {} {} {} {}", faker::date::weekdayAbbreviatedName(),
faker::date::monthAbbreviatedNames[size_t(std::stoi(month) - 1)], day, time, year,
timeZoneString);
return pastDate;
}

std::string commitEntry(std::optional<unsigned> dateYears, std::optional<unsigned> shaLength, Country country)
{
std::string entry = "commit ";

if (shaLength)
if (shaLength.value_or(0))
{
entry += commitSha(shaLength.emplace());
entry += commitSha(shaLength.value_or(0));
}
else
{
Expand All @@ -96,9 +55,9 @@ std::string commitEntry(std::optional<unsigned> dateYears, std::optional<unsigne

entry += "\nAuthor: " + firstName + " " + lastName + " " + internet::email(firstName, lastName) + "\nDate: ";

if (dateYears)
if (dateYears.value_or(0))
{
entry += commitDate(dateYears.emplace());
entry += commitDate(dateYears.value_or(0));
}
else
{
Expand All @@ -112,16 +71,12 @@ std::string commitEntry(std::optional<unsigned> dateYears, std::optional<unsigne

std::string commitMessage()
{
switch (number::integer(1, 4))
switch (number::integer(1, 2))
{
case 1:
return common::format("{} {}", word::verb(), word::noun());
case 2:
return common::format("{} {} {}", word::verb(), word::adjective(), word::noun());
case 3:
return common::format("{} {} {}", word::verb(), word::noun(), word::adverb());
default:
return common::format("{} {} {} {}", word::verb(), word::adjective(), word::noun(), word::adverb());
return common::format("{} {} {}", word::verb(), word::adjective(), word::noun());
}
}

Expand Down
17 changes: 15 additions & 2 deletions tests/modules/git/GitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class GitTest : public Test
{
public:
inline static const std::string DATE_REGEX =
"[A-Z][a-z]{2} [A-Z][a-z]{2,3} (([0-2][0-9])|(3[0-1])) (([0-1][0-9])|(2[0-4])):[0-5][0-9]:[0-5][0-9] "
"[1-2][0-9]{3} (-|\\+)((0[0-9])|(1[0-2]))00";
"[0-9]{4}-((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|(0[469]|11)-(0[1-9]|[12][0-9]|30)|(02)-(0[1-9]|[12][0-9]))"
"T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])Z";

inline static const std::string MESSAGE_REGEX =
R"([a-zA-Z]+(\-[a-zA-Z]+)* ([a-zA-Z\-]+(\-[a-zA-Z]+)*\s)*[a-zA-Z\-]+(\-[a-zA-Z]+)*)";
inline static const std::string EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
Expand Down Expand Up @@ -90,6 +91,18 @@ TEST_F(GitTest, shouldGenerateCommitEntry)
ASSERT_TRUE(std::regex_match(commitEntry(), entryRegex));
}

TEST_F(GitTest, shouldGenerateCommitEntryWithGivenArguments)
{
const unsigned years = 20;
const unsigned length = 50;

const std::regex entryRegex("^commit " + GitTest::generateShaRegex(length) +
"\nAuthor: [A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+ .+@[0-9a-zA-Z]+\\.[0-9a-zA-Z]+\nDate: " +
GitTest::DATE_REGEX + "\n\n\t" + GitTest::MESSAGE_REGEX + "$");

ASSERT_TRUE(std::regex_match(commitEntry(years, length, Country::India), entryRegex));
}

TEST_F(GitTest, shouldGenerateCommitMessage)
{
const std::regex messageRegex("^" + GitTest::MESSAGE_REGEX + "$");
Expand Down

0 comments on commit f36227a

Please sign in to comment.