Skip to content

Commit

Permalink
feat: add locales to color module (#926)
Browse files Browse the repository at this point in the history
* feat: add locales to color module

* fix: fix build issues
  • Loading branch information
cieslarmichal authored Sep 30, 2024
1 parent 388e098 commit 738ce3e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file

* added locale to `book` module
* added locale to `weather` module
* added locale to `color` module

## v3.0.0 (28.08.2024)

Expand Down
5 changes: 4 additions & 1 deletion include/faker-cxx/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
#include <string_view>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"
#include "types/hex.h"

namespace faker::color
{
/**
* @brief Returns a random color.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Human readable color name.
*
* @code
* faker::color::name() // "Blue"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view name();
FAKER_CXX_EXPORT std::string_view name(Locale locale = Locale::en_US);

/**
* @brief Returns an RGB color.
Expand Down
20 changes: 19 additions & 1 deletion src/modules/color.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "faker-cxx/color.h"

#include <span>
#include <string>
#include <string_view>

Expand All @@ -8,11 +9,28 @@
#include "faker-cxx/helper.h"
#include "faker-cxx/number.h"
#include "faker-cxx/types/hex.h"
#include "faker-cxx/types/locale.h"

namespace faker::color
{
std::string_view name()
namespace
{
std::span<const std::string_view> getColors(Locale locale)
{
switch (locale)
{
case Locale::pl_PL:
return polishColors;
default:
return englishColors;
}
}
}

std::string_view name(Locale locale)
{
const auto& colors = getColors(locale);

return helper::randomElement(colors);
}

Expand Down
9 changes: 8 additions & 1 deletion src/modules/color_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

namespace faker::color
{
const auto colors = std::to_array<std::string_view>({
const auto englishColors = std::to_array<std::string_view>({
"azure", "black", "blue", "cyan", "fuchsia", "gold", "green", "grey",
"indigo", "ivory", "lavender", "lime", "magenta", "maroon", "mint green", "olive",
"orange", "orchid", "pink", "plum", "purple", "red", "salmon", "silver",
"sky blue", "tan", "teal", "turquoise", "violet", "white", "yellow",
});

const auto polishColors = std::to_array<std::string_view>({
"błękitny", "czarny", "niebieski", "cyjan", "fuksja", "złoty", "zielony", "szary",
"indygo", "kość słoniowa", "lawendowy", "limonkowy", "magenta", "kasztanowy", "miętowy", "oliwkowy",
"pomarańczowy", "orchidea", "różowy", "śliwkowy", "purpurowy", "czerwony", "łososiowy", "srebrny",
"błękit nieba", "jasnobrązowy", "morski", "turkusowy", "fioletowy", "biały", "żółty",
});

}
35 changes: 29 additions & 6 deletions tests/modules/color_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <algorithm>
#include <charconv>
#include <span>
#include <string>
#include <string_view>

#include "gtest/gtest.h"

Expand All @@ -13,21 +13,44 @@

using namespace ::testing;
using namespace faker;
using namespace color;
using namespace faker::color;

class ColorTest : public Test
std::span<const std::string_view> getColors(Locale locale)
{
switch (locale)
{
case Locale::pl_PL:
return polishColors;
default:
return englishColors;
}
}

class ColorNameTest : public TestWithParam<Locale>
{
public:
};

TEST_F(ColorTest, shouldGenerateColorName)
TEST_P(ColorNameTest, shouldGenerateColorName)
{
const auto generatedColorName = name();
const auto locale = GetParam();

const auto generatedColorName = name(locale);

const auto exceptedColors = getColors(locale);

ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string_view& colorName)
ASSERT_TRUE(std::ranges::any_of(exceptedColors, [generatedColorName](const std::string_view& colorName)
{ return colorName == generatedColorName; }));
}

INSTANTIATE_TEST_SUITE_P(TestColorNameByLocale, ColorNameTest, ValuesIn(locales),
[](const TestParamInfo<Locale>& paramInfo) { return toString(paramInfo.param); });

class ColorTest : public Test
{
public:
};

TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha)
{
const auto generatedRgbColor = rgb();
Expand Down

0 comments on commit 738ce3e

Please sign in to comment.