Skip to content

Commit

Permalink
feat: add missing tests in git module (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Oct 12, 2024
1 parent 5e7cd8e commit ef580fa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
7 changes: 4 additions & 3 deletions include/faker-cxx/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct FAKER_CXX_EXPORT Author
std::string email;
};

enum class BranchIssueNum
enum class BranchIssueType
{
WithoutIssueNumber,
WithIssueNumber,
Expand All @@ -23,7 +23,7 @@ enum class BranchIssueNum
/**
* @brief Returns a random branch name.
*
* @param issueNum The optional issue number in branch name.
* @param issueType The optional branch issue type.
* @param maxIssueNum The maximum issue number in branch name. Defaults to `100`.
*
* @returns Branch name.
Expand All @@ -32,7 +32,8 @@ enum class BranchIssueNum
* faker::git::branch() // "capitalize-bus"
* @endcode
*/
FAKER_CXX_EXPORT std::string branch(std::optional<BranchIssueNum> issueNum = std::nullopt, unsigned maxIssueNum = 100);
FAKER_CXX_EXPORT std::string branch(std::optional<BranchIssueType> issueType = std::nullopt,
unsigned maxIssueNum = 100);

/**
* @brief Generates a random date in form of string.
Expand Down
10 changes: 5 additions & 5 deletions src/modules/git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

namespace faker::git
{
std::string branch(std::optional<BranchIssueNum> issueNum, unsigned maxIssueNum)
std::string branch(std::optional<BranchIssueType> issueType, unsigned maxIssueNum)
{
const auto issue = issueNum ? *issueNum : BranchIssueNum::WithIssueNumber;
const auto targetIssueType = issueType ? *issueType : BranchIssueType::WithIssueNumber;

std::string generatedBranch;

switch (issue)
switch (targetIssueType)
{
case BranchIssueNum::WithoutIssueNumber:
case BranchIssueType::WithoutIssueNumber:
generatedBranch = common::format("{}-{}", word::verb(), word::noun());
break;
case BranchIssueNum::WithIssueNumber:
case BranchIssueType::WithIssueNumber:
generatedBranch = common::format("{}-{}-{}-{}", number::integer(unsigned(1), maxIssueNum), word::verb(),
word::adjective(), word::noun());
break;
Expand Down
27 changes: 6 additions & 21 deletions tests/modules/git_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,15 @@ TEST_F(GitTest, shouldGenerateBranch)

TEST_F(GitTest, branchWithIssueNumTest)
{
auto testValue = unsigned(number::integer(2, 100));
const auto testValue = unsigned(number::integer(2, 100));

std::vector<std::string> branchElements;

bool numberAtFront = false;

int number;
const auto randomBranch = branch(BranchIssueType::WithIssueNumber, testValue);

while (!numberAtFront)
{
branchElements = common::split(branch(BranchIssueNum::WithIssueNumber, testValue), "-");
const auto branchElements = common::split(randomBranch, "-");

try
{
number = std::stoi(branchElements[0]);
numberAtFront = true;
}
catch (...)
{
continue;
}
}
const auto issueNumber = std::stoi(branchElements[0]);

ASSERT_TRUE(1 <= number && number <= int(testValue));
ASSERT_TRUE(1 <= issueNumber && issueNumber <= int(testValue));
}

TEST_F(GitTest, branchWithoutIssueNumTest)
Expand All @@ -82,7 +67,7 @@ TEST_F(GitTest, branchWithoutIssueNumTest)

std::vector<std::string> branchElements;

branchElements = common::split(branch(BranchIssueNum::WithoutIssueNumber, testValue), "-");
branchElements = common::split(branch(BranchIssueType::WithoutIssueNumber, testValue), "-");

const auto& generatedVerb = branchElements[0];
const auto& generatedNoun = branchElements[1];
Expand Down

0 comments on commit ef580fa

Please sign in to comment.