Skip to content

Commit

Permalink
refactor: phone module migrate
Browse files Browse the repository at this point in the history
- phone module migration from class to functions within phone namespace

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 committed Jun 25, 2024
1 parent a941440 commit 0fbf73d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 57 deletions.
43 changes: 18 additions & 25 deletions include/faker-cxx/Phone.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
#include <string_view>
#include <unordered_map>

namespace faker
namespace faker::phone
{
enum class PhoneNumberCountryFormat;

class Phone
{
public:
/**
* @brief Returns a random phone number.
*
Expand All @@ -20,45 +16,45 @@ class Phone
* @returns Random phone number.
*
* @code
* Phone::number() // "961-770-7727"
* Phone::number("501-###-###") // "501-039-841"
* Phone::number("+48 91 ### ## ##") // "+48 91 463 61 70"
* phone::number() // "961-770-7727"
* phone::number("501-###-###") // "501-039-841"
* phone::number("+48 91 ### ## ##") // "+48 91 463 61 70"
* @endcode
*/
static std::string number(std::optional<std::string> = std::nullopt);
std::string number(std::optional<std::string> = std::nullopt);

/**
* @brief Returns a random phone platform.
*
* @returns Random phone platform.
*
* @code
* Phone::platform() // "iOS"
* phone::platform() // "iOS"
* @endcode
*/
static std::string_view platform();
std::string_view platform();

/**
* @brief Returns a random phone model.
*
* @returns Random phone model.
*
* @code
* Phone::modelName() // "Samsung Galaxy S22"
* phone::modelName() // "Samsung Galaxy S22"
* @endcode
*/
static std::string_view modelName();
std::string_view modelName();

/**
* @brief Returns a random phone manufacturer.
*
* @returns Random phone manufacturer.
*
* @code
* Phone::manufacturer() // "Sony"
* phone::manufacturer() // "Sony"
* @endcode
*/
static std::string_view manufacturer();
std::string_view manufacturer();

/**
* @brief Returns a random phone number based on country phone number template.
Expand All @@ -68,37 +64,34 @@ class Phone
* @returns Random phone number based on country phone number template.
*
* @code
* Phone::number(PhoneNumberCountryFormat::Usa) // "+1 (395) 714-1494"
* phone::number(PhoneNumberCountryFormat::Usa) // "+1 (395) 714-1494"
* @endcode
*/
static std::string number(PhoneNumberCountryFormat format);
std::string number(PhoneNumberCountryFormat format);

/**
* @brief Returns IMEI number.
*
* @returns IMEI number.
*
* @code
* Phone::imei() // "13-850175-913761-7"
* phone::imei() // "13-850175-913761-7"
* @endcode
*/
static std::string imei();
std::string imei();

/**
* @brief returns a random country area code
*
* @returns Random country area code
*
* @code
* Phone::areaCode() // "+1"
* phone::areaCode() // "+1"
* @endcode
*/
static std::string_view areaCode();
std::string_view areaCode();

private:
static std::unordered_map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
static std::unordered_map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap;
};
std::unordered_map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();

enum class PhoneNumberCountryFormat
{
Expand Down
22 changes: 11 additions & 11 deletions src/modules/phone/Phone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include "faker-cxx/Helper.h"
#include "PhoneData.h"

namespace faker
namespace faker::phone
{
std::unordered_map<PhoneNumberCountryFormat, std::string> Phone::phoneNumberFormatMap =
Phone::createPhoneNumberFormatMap();
std::unordered_map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap =
createPhoneNumberFormatMap();

std::string Phone::number(std::optional<std::string> format)
std::string number(std::optional<std::string> format)
{
std::string selectedFormat;

Expand All @@ -29,7 +29,7 @@ std::string Phone::number(std::optional<std::string> format)
return Helper::replaceSymbolWithNumber(selectedFormat);
}

std::string Phone::number(PhoneNumberCountryFormat format)
std::string number(PhoneNumberCountryFormat format)
{
std::string countryFormat = phoneNumberFormatMap.at(format);

Expand All @@ -41,27 +41,27 @@ std::string Phone::number(PhoneNumberCountryFormat format)
return Helper::replaceSymbolWithNumber(countryFormat);
}

std::string Phone::imei()
std::string imei()
{
return Helper::replaceCreditCardSymbols("##-######-######-L", '#');
}

std::string_view Phone::platform()
std::string_view platform()
{
return Helper::arrayElement(phone::PhonePlatforms);
}

std::string_view Phone::modelName()
std::string_view modelName()
{
return Helper::arrayElement(phone::PhoneModelNames);
}

std::string_view Phone::manufacturer()
std::string_view manufacturer()
{
return Helper::arrayElement(phone::PhoneManufacturers);
}

std::unordered_map<PhoneNumberCountryFormat, std::string> Phone::createPhoneNumberFormatMap()
std::unordered_map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap()
{
std::unordered_map<PhoneNumberCountryFormat, std::string> formatMap;

Expand All @@ -77,7 +77,7 @@ std::unordered_map<PhoneNumberCountryFormat, std::string> Phone::createPhoneNumb
return formatMap;
}

std::string_view Phone::areaCode()
std::string_view areaCode()
{
return Helper::arrayElement(phone::areaCodes);
}
Expand Down
43 changes: 22 additions & 21 deletions tests/modules/phone/PhoneTest.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 faker::phone;

class PhoneTest : public Test
{
Expand All @@ -24,79 +25,79 @@ class PhoneTest : public Test

TEST_F(PhoneTest, NumberWithNoFormat)
{
const auto phoneNumber = Phone::number();
const auto phoneNumber = number();

ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));
}

TEST_F(PhoneTest, NumberWithFormat)
{
auto format = "501-###-###";
auto phoneNumber = Phone::number(format);
auto phoneNumber = number(format);
ASSERT_NE(phoneNumber, format);
ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));

format = "+48 91 ### ## ##";
phoneNumber = Phone::number(format);
phoneNumber = number(format);
ASSERT_NE(phoneNumber, format);
ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));

format = "+376 (###) ###-####";
phoneNumber = Phone::number(format);
phoneNumber = number(format);
ASSERT_NE(phoneNumber, format);
ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));

format = "+376 (!!!) !!!-!!!!";
phoneNumber = Phone::number(format);
phoneNumber = number(format);
ASSERT_NE(phoneNumber, format);
ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));
}

TEST_F(PhoneTest, IMEIGeneration)
{
auto imei = Phone::imei();
auto generatedImei = imei();

imei.erase(std::remove(imei.begin(), imei.end(), '-'), imei.end());
generatedImei.erase(std::remove(generatedImei.begin(), generatedImei.end(), '-'), generatedImei.end());

ASSERT_EQ(imei.length(), 15);
ASSERT_TRUE(isStringNumericWithSpecialChars(imei));
ASSERT_EQ(generatedImei.length(), 15);
ASSERT_TRUE(isStringNumericWithSpecialChars(generatedImei));
}

TEST_F(PhoneTest, NumberFormatTest)
{
const auto phoneNumber = Phone::number(PhoneNumberCountryFormat::Zimbabwe);
const auto generatedPhoneNumber = number(PhoneNumberCountryFormat::Zimbabwe);

EXPECT_FALSE(phoneNumber.empty());
ASSERT_TRUE(isStringNumericWithSpecialChars(phoneNumber));
EXPECT_FALSE(generatedPhoneNumber.empty());
ASSERT_TRUE(isStringNumericWithSpecialChars(generatedPhoneNumber));
}

TEST_F(PhoneTest, PlatformGeneration)
{
const auto generatedPlatform = Phone::platform();
ASSERT_TRUE(std::ranges::any_of(phone::PhonePlatforms.begin(), phone::PhonePlatforms.end(),
const auto generatedPlatform = platform();
ASSERT_TRUE(std::ranges::any_of(PhonePlatforms.begin(), PhonePlatforms.end(),
[generatedPlatform](const std::string_view& platform)
{ return platform == generatedPlatform; }));
}

TEST_F(PhoneTest, ModelNameGeneration)
{
const auto generatedModelName = Phone::modelName();
ASSERT_TRUE(std::ranges::any_of(phone::PhoneModelNames.begin(), phone::PhoneModelNames.end(),
const auto generatedModelName = modelName();
ASSERT_TRUE(std::ranges::any_of(PhoneModelNames.begin(), PhoneModelNames.end(),
[generatedModelName](const std::string_view& modelName)
{ return modelName == generatedModelName; }));
}

TEST_F(PhoneTest, ManufacturerGeneration)
{
const auto generatedManufacturer = Phone::manufacturer();
ASSERT_TRUE(std::ranges::any_of(phone::PhoneManufacturers.begin(), phone::PhoneManufacturers.end(),
const auto generatedManufacturer = manufacturer();
ASSERT_TRUE(std::ranges::any_of(PhoneManufacturers.begin(), PhoneManufacturers.end(),
[generatedManufacturer](const std::string_view& manufacturer)
{ return manufacturer == generatedManufacturer; }));
}

TEST_F(PhoneTest, AreaCodeGeneration)
{
const auto areaCode = Phone::areaCode();
ASSERT_TRUE(std::ranges::any_of(phone::areaCodes.begin(), phone::areaCodes.end(),
[areaCode](const std::string_view& code) { return code == areaCode; }));
const auto generatedAreaCode = areaCode();
ASSERT_TRUE(std::ranges::any_of(areaCodes.begin(), areaCodes.end(),
[generatedAreaCode](const std::string_view& code) { return code == generatedAreaCode; }));
}

0 comments on commit 0fbf73d

Please sign in to comment.