From 64cd118f94414e28951ea7f0cacf57b1c81261d0 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Tue, 30 Jul 2024 00:45:39 +0200 Subject: [PATCH] test: company module testcase added * 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 --- CHANGELOG.md | 2 ++ include/faker-cxx/company.h | 14 +++++++-- src/modules/company.cpp | 23 +++++++------- tests/modules/company_test.cpp | 55 ++++++++++++++++++++-------------- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 730a2bf29..f6a3814de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/include/faker-cxx/company.h b/include/faker-cxx/company.h index f464ccf77..5fd825cd5 100644 --- a/include/faker-cxx/company.h +++ b/include/faker-cxx/company.h @@ -1,20 +1,23 @@ #pragma once +#include #include #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 format = std::nullopt); /** * @brief Returns a random company type. @@ -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, +}; } diff --git a/src/modules/company.cpp b/src/modules/company.cpp index 97f17f8a5..26c718ce6 100644 --- a/src/modules/company.cpp +++ b/src/modules/company.cpp @@ -11,25 +11,24 @@ namespace faker::company { -std::string name() +std::string companyName(std::optional format) { - std::string companyName; + CompanyNameFormat nameFormat = format ? *format : CompanyNameFormat::LastNameSuffix; - switch (number::integer(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; } diff --git a/tests/modules/company_test.cpp b/tests/modules/company_test.cpp index 7664efb5d..4ebd39dcf 100644 --- a/tests/modules/company_test.cpp +++ b/tests/modules/company_test.cpp @@ -21,44 +21,56 @@ 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 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; })); @@ -66,8 +78,7 @@ TEST_F(CompanyTest, shouldGenerateCompanyName) { 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; })); } }