Skip to content

Commit

Permalink
test: company module testcase added (#834)
Browse files Browse the repository at this point in the history
* test case added to company module for code coverage
* function name changed from name to companyName
* changed companyName definition to take optional enum parameter

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 authored Jul 30, 2024
1 parent 3d0357a commit dbd3487
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file
* renamed all include files from `CamelCase` to `snake_case`
* changed function name from `number` (by format) to `phoneNumberByFormat` in phone module
* changed function name from `number` (by country) to `phoneNumberByCountry` in phone module
* changed function name from `name` to `companyName` in company module
* changed function `companyName` to take optional enum parameter in company module

## v2.0.0 (27.06.2024)

Expand Down
14 changes: 12 additions & 2 deletions include/faker-cxx/company.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#pragma once

#include <optional>
#include <string_view>
#include "faker-cxx/export.h"

namespace faker::company
{
enum class CompanyNameFormat;
/**
* @brief Returns a random company name.
*
* @returns Company name.
*
* @code
* faker::company::name() // "Adams Inc"
* faker::company::companyName() // "Peterson Inc"
* faker::company::companyName(CompanyNameFormat::FirstNameLastNameSuffix) // "Adams Peterson Inc"
* @endcode
*/
FAKER_CXX_EXPORT std::string name();
FAKER_CXX_EXPORT std::string companyName(std::optional<CompanyNameFormat> format = std::nullopt);

/**
* @brief Returns a random company type.
Expand Down Expand Up @@ -125,4 +128,11 @@ FAKER_CXX_EXPORT std::string_view catchPhraseDescriptor();
* @endcode
*/
FAKER_CXX_EXPORT std::string_view catchPhraseNoun();

enum class CompanyNameFormat
{
LastNameSuffix,
FirstNameLastNameSuffix,
FirstNameLastNameJobAreaSuffix,
};
}
23 changes: 11 additions & 12 deletions src/modules/company.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@

namespace faker::company
{
std::string name()
std::string companyName(std::optional<CompanyNameFormat> format)
{
std::string companyName;
CompanyNameFormat nameFormat = format ? *format : CompanyNameFormat::LastNameSuffix;

switch (number::integer<int>(3))
std::string companyName = "";

switch (nameFormat)
{
case 0:
case CompanyNameFormat::LastNameSuffix:
companyName = common::format("{} {}", person::lastName(), helper::arrayElement(companySuffixes));
break;
case 1:
companyName = common::format("{} {} {}", person::firstName(), person::lastName(), person::jobArea());
case CompanyNameFormat::FirstNameLastNameSuffix:
companyName = common::format("{} {} {}", person::firstName(), person::lastName(), helper::arrayElement(companySuffixes));
break;
case 2:
case CompanyNameFormat::FirstNameLastNameJobAreaSuffix:
companyName =
common::format("{} {} {} Services", person::firstName(), person::lastName(), person::jobArea());
break;
case 3:
companyName = common::format("{} {} {} {}", person::firstName(), person::lastName(), person::jobArea(),
helper::arrayElement(companySuffixes));
common::format("{} {} {} {}", person::firstName(), person::lastName(), person::jobArea(),
helper::arrayElement(companySuffixes));
break;
}

Expand Down
55 changes: 33 additions & 22 deletions tests/modules/company_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,64 @@ class CompanyTest : public Test

TEST_F(CompanyTest, shouldGenerateCompanyName)
{
const auto companyName = name();
const auto generatedCompanyName = companyName();
const auto generatedCompanyFirstLastNames =
companyName(CompanyNameFormat::FirstNameLastNameSuffix);
const auto generatedCompanyFirstLastNamesJobArea =
companyName(CompanyNameFormat::FirstNameLastNameJobAreaSuffix);

const auto companyNameElements = common::split(companyName, " ");
const auto companyNameElements = common::split(generatedCompanyName, " ");
const auto companyNameElements1 = common::split(generatedCompanyFirstLastNames, " ");
const auto companyNameElements2 = common::split(generatedCompanyFirstLastNamesJobArea, " ");

std::vector<std::string_view> expectedFirstNames(person::englishMaleFirstNames.begin(),
person::englishMaleFirstNames.end());
expectedFirstNames.insert(expectedFirstNames.end(), person::englishFemaleFirstNames.begin(),
person::englishFemaleFirstNames.end());

if (companyNameElements.size() == 2)
{
EXPECT_TRUE(companyNameElements.size() == 2);
EXPECT_TRUE(companyNameElements1.size() == 3);
EXPECT_TRUE(companyNameElements2.size() == 4);

if (companyNameElements.size() == 2){

const auto& generatedLastName = companyNameElements[0];
const auto& generatedCompanySuffix = companyNameElements[1];
const auto& lastElement = companyNameElements[1];

ASSERT_TRUE(std::ranges::any_of(person::englishLastNames, [generatedLastName](const std::string_view& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::ranges::any_of(companySuffixes, [generatedCompanySuffix](const std::string_view& companySuffix)
{ return companySuffix == generatedCompanySuffix; }));
ASSERT_TRUE(std::ranges::any_of(companySuffixes, [lastElement](const std::string_view& companySuffix)
{ return companySuffix == lastElement; }));
}
else if (companyNameElements.size() == 3)
{
const auto& generatedFirstName = companyNameElements[0];
const auto& generatedLastName = companyNameElements[1];
const auto& generatedJobArea = companyNameElements[2];

if (companyNameElements1.size() == 3){

const auto& generatedFirstName = companyNameElements1[0];
const auto& generatedLastName = companyNameElements1[1];
const auto& lastElement = companyNameElements1[2];

ASSERT_TRUE(std::ranges::any_of(expectedFirstNames, [generatedFirstName](const std::string_view& firstName)
{ return firstName == generatedFirstName; }));
ASSERT_TRUE(std::ranges::any_of(person::englishLastNames, [generatedLastName](const std::string_view& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::ranges::any_of(person::jobAreas, [generatedJobArea](const std::string_view& jobArea)
{ return jobArea == generatedJobArea; }));
ASSERT_TRUE(std::ranges::any_of(companySuffixes, [lastElement](const std::string_view& companySuffix)
{ return companySuffix == lastElement; }));
}
else if (companyNameElements.size() == 4)
{
const auto& generatedFirstName = companyNameElements[0];
const auto& generatedLastName = companyNameElements[1];
const auto& generatedJobArea = companyNameElements[2];
const auto& lastElement = companyNameElements[3];

if (companyNameElements2.size() == 4){

const auto& generatedFirstName = companyNameElements2[0];
const auto& generatedLastName = companyNameElements2[1];
const auto& generatedJobArea = companyNameElements2[2];
const auto& lastElement = companyNameElements2[3];

ASSERT_TRUE(std::ranges::any_of(expectedFirstNames, [generatedFirstName](const std::string_view& firstName)
{ return firstName == generatedFirstName; }));
ASSERT_TRUE(std::ranges::any_of(person::englishLastNames, [generatedLastName](const std::string_view& lastName)
{ return lastName == generatedLastName; }));
ASSERT_TRUE(std::ranges::any_of(person::jobAreas, [generatedJobArea](const std::string_view& jobArea)
{ return jobArea == generatedJobArea; }));
ASSERT_TRUE(lastElement == "Services" ||
std::ranges::any_of(companySuffixes, [lastElement](const std::string_view& companySuffix)
ASSERT_TRUE(std::ranges::any_of(companySuffixes, [lastElement](const std::string_view& companySuffix)
{ return companySuffix == lastElement; }));
}
}
Expand Down

0 comments on commit dbd3487

Please sign in to comment.