Skip to content

Commit

Permalink
refactor: change git class to namespace (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Jun 24, 2024
1 parent da55f85 commit a941440
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 89 deletions.
139 changes: 67 additions & 72 deletions include/faker-cxx/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,79 @@

#include "faker-cxx/types/Country.h"

namespace faker
namespace faker::git
{
class Git
struct Author
{
public:
struct Author
{
std::string name;
std::string email;
};
std::string name;
std::string email;
};

/**
* @brief Returns a random branch name.
*
* @param maxIssueNum The maximum issue number in branch name. Defaults to `100`.
* @returns Branch name.
*
* @code
* Git::branch() // "capitalize-bus"
* @endcode
*/
static std::string branch(unsigned maxIssueNum = 100);
/**
* @brief Returns a random branch name.
*
* @param maxIssueNum The maximum issue number in branch name. Defaults to `100`.
* @returns Branch name.
*
* @code
* git::branch() // "capitalize-bus"
* @endcode
*/
std::string branch(unsigned maxIssueNum = 100);

/**
* @brief Generates a random date in form of string.
*
* @param years The range of years the date may be in the past. Defaults to `15`.
* @returns Commit date.
*
* @code
* Git::commitDate() // "Mon Jan 17 15:05:53 2022 +1100"
* @endcode
*/
static std::string commitDate(unsigned years = 15);
/**
* @brief Generates a random date in form of string.
*
* @param years The range of years the date may be in the past. Defaults to `15`.
* @returns Commit date.
*
* @code
* git::commitDate() // "Mon Jan 17 15:05:53 2022 +1100"
* @endcode
*/
std::string commitDate(unsigned years = 15);

/**
* @brief Generates a random commit entry in form of string.
*
* @param dateYears The range of years the date may be in the past. Defaults to `15`.
* @param shaLength The length of output SHA hash. Defaults to `40`.
* @param country The country set for name generating. Defaults to `England` (could be random, if there was a
random language generator).
* @returns Commit entry.
*
* @code
* Git::commitEntry() // "commit 9cbc41bb8ce0438c8de9cb25a1c6ad33441d8aca
Author: Rachel McLaughlin [email protected]
Date: Mon Jan 17 15:05:53 2022 +1100
/**
* @brief Generates a random commit entry in form of string.
*
* @param dateYears The range of years the date may be in the past. Defaults to `15`.
* @param shaLength The length of output SHA hash. Defaults to `40`.
* @param country The country set for name generating. Defaults to `England` (could be random, if there was a
random language generator).
* @returns Commit entry.
*
* @code
* git::commitEntry() // "commit 9cbc41bb8ce0438c8de9cb25a1c6ad33441d8aca
Author: Rachel McLaughlin [email protected]
Date: Mon Jan 17 15:05:53 2022 +1100
spawn polyp"
* @endcode
*/
static std::string commitEntry(std::optional<unsigned> dateYears = std::nullopt,
std::optional<unsigned> shaLength = std::nullopt,
Country country = Country::England);
spawn polyp"
* @endcode
*/
std::string commitEntry(std::optional<unsigned> dateYears = std::nullopt,
std::optional<unsigned> shaLength = std::nullopt, Country country = Country::England);

/**
* @brief Generates a random commit message.
*
* @returns Commit message.
*
* @code
* Git::commitMessage() // "spawn polyp"
* @endcode
*/
static std::string commitMessage();
/**
* @brief Generates a random commit message.
*
* @returns Commit message.
*
* @code
* git::commitMessage() // "spawn polyp"
* @endcode
*/
std::string commitMessage();

/**
* @brief Returns a random SHA hash.
*
* @param length The length of output SHA hash. Defaults to `40`.
*
* @returns SHA hash.
*
* @code
* Git::commitSha() // "9cbc41bb8ce0438c8de9cb25a1c6ad33441d8aca"
* @endcode
*/
static std::string commitSha(unsigned length = 40);
};
/**
* @brief Returns a random SHA hash.
*
* @param length The length of output SHA hash. Defaults to `40`.
*
* @returns SHA hash.
*
* @code
* git::commitSha() // "9cbc41bb8ce0438c8de9cb25a1c6ad33441d8aca"
* @endcode
*/
std::string commitSha(unsigned length = 40);
}
15 changes: 8 additions & 7 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include "faker-cxx/types/Hex.h"
#include "faker-cxx/Word.h"

namespace faker
namespace faker::git
{

std::string Git::branch(unsigned maxIssueNum)
std::string branch(unsigned maxIssueNum)
{
switch (Number::integer(1, 3))
{
Expand All @@ -32,7 +32,7 @@ std::string Git::branch(unsigned maxIssueNum)
}
}

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

Expand Down Expand Up @@ -74,10 +74,11 @@ std::string Git::commitDate(unsigned years)
}

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

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

Expand Down Expand Up @@ -109,7 +110,7 @@ std::string Git::commitEntry(std::optional<unsigned> dateYears, std::optional<un
return entry;
}

std::string Git::commitMessage()
std::string commitMessage()
{
switch (Number::integer(1, 4))
{
Expand All @@ -124,7 +125,7 @@ std::string Git::commitMessage()
}
}

std::string Git::commitSha(unsigned length)
std::string commitSha(unsigned length)
{
return faker::String::hexadecimal(length, HexCasing::Lower, HexPrefix::None);
}
Expand Down
22 changes: 12 additions & 10 deletions tests/modules/git/GitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using namespace ::testing;
using namespace faker;
using namespace git;

class GitTest : public Test
{
Expand Down Expand Up @@ -39,28 +40,29 @@ std::string GitTest::generateShaRegex()

TEST_F(GitTest, shouldGenerateBranch)
{
const auto branch = Git::branch();
const auto branchSplit = faker::StringHelper::split(branch, "-").size();
const auto generatedBranch = branch();
const auto branchSplit = StringHelper::split(generatedBranch, "-").size();

ASSERT_TRUE(2 <= branchSplit && branchSplit <= 7);
}

TEST_F(GitTest, branchIssueNumTest)
{
auto testValue = unsigned(faker::Number::integer(2, 100));
auto testValue = unsigned(Number::integer(2, 100));

std::vector<std::string> branch = faker::StringHelper::split(Git::branch(testValue), "-");
std::vector<std::string> branchElements = StringHelper::split(branch(testValue), "-");

bool numberAtFront = false;

int number;

while (!numberAtFront)
{
branch = faker::StringHelper::split(Git::branch(testValue), "-");
branchElements = StringHelper::split(branch(testValue), "-");

try
{
number = std::stoi(branch[0]);
number = std::stoi(branchElements[0]);
numberAtFront = true;
}
catch (...)
Expand All @@ -76,7 +78,7 @@ TEST_F(GitTest, shouldGenerateCommitDate)
{
const std::regex dateRegex("^" + GitTest::DATE_REGEX + "$");

ASSERT_TRUE(std::regex_match(Git::commitDate(), dateRegex));
ASSERT_TRUE(std::regex_match(commitDate(), dateRegex));
}

TEST_F(GitTest, shouldGenerateCommitEntry)
Expand All @@ -85,14 +87,14 @@ TEST_F(GitTest, shouldGenerateCommitEntry)
"\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(Git::commitEntry(), entryRegex));
ASSERT_TRUE(std::regex_match(commitEntry(), entryRegex));
}

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

const auto temp = Git::commitMessage();
const auto temp = commitMessage();

ASSERT_TRUE(std::regex_match(temp, messageRegex));
}
Expand All @@ -103,5 +105,5 @@ TEST_F(GitTest, shouldGenerateCommitSha)

const std::regex shaRegex("^" + GitTest::generateShaRegex(length) + "$");

ASSERT_TRUE(std::regex_match(Git::commitSha(length), shaRegex));
ASSERT_TRUE(std::regex_match(commitSha(length), shaRegex));
}

0 comments on commit a941440

Please sign in to comment.