Skip to content

Commit

Permalink
Author added to Git Module (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
srivastava-yash authored Jan 26, 2024
1 parent 3a773b5 commit 59d5337
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/faker-cxx/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace faker
class Git
{
public:
struct Author
{
std::string name, email;
};

/**
* @brief Returns a random branch name.
*
Expand Down Expand Up @@ -78,5 +83,19 @@ class Git
* @endcode
*/
static std::string commitSha(unsigned length = 40);


/**
* @brief Returns a random author name and email.
*
* @param
*
* @returns Author.
*
* @code
* Git::author // {Author.name = "Rachel McLaughlin", Author.email = "[email protected]"}
* @endcode
*/
static Author author();
};
}
12 changes: 12 additions & 0 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,16 @@ std::string Git::commitSha(unsigned length)
{
return faker::String::hexadecimal(length, HexCasing::Lower, HexPrefix::None);
}

Git::Author Git::author()
{
const std::string firstName = Person::firstName();
const std::string lastName = Person::lastName();

const std::string name = firstName + " " + lastName;
const std::string email = Internet::email(firstName, lastName);

return {name, email};
}

}
13 changes: 13 additions & 0 deletions src/modules/git/GitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class GitTest : public Test
"[1-2][0-9]{3} (-|\\+)((0[0-9])|(1[0-2]))00";
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,}$";
inline static const std::string NAME_REGEX =
"^[A-Z][a-zA-Z]+\\s[A-Z][a-zA-Z]+$";

static std::string generateShaRegex(unsigned length);
static std::string generateShaRegex();
Expand Down Expand Up @@ -94,3 +98,12 @@ TEST_F(GitTest, shouldGenerateCommitSha)
const std::regex shaRegex("^" + GitTest::generateShaRegex(length) + "$");
ASSERT_TRUE(std::regex_match(Git::commitSha(length), shaRegex));
}

TEST_F(GitTest, shouldGenerateAuthor)
{
Git::Author generatedAuthor = Git::author();
const std::regex nameRegex(NAME_REGEX);
const std::regex emailRegex(EMAIL_REGEX);
ASSERT_TRUE(std::regex_match(generatedAuthor.name, nameRegex));
ASSERT_TRUE(std::regex_match(generatedAuthor.email, emailRegex));
}

0 comments on commit 59d5337

Please sign in to comment.