From 738ce3ec9868390066eb6bc5c1e8a306a83c502f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Mon, 30 Sep 2024 23:40:45 +0200 Subject: [PATCH] feat: add locales to color module (#926) * feat: add locales to color module * fix: fix build issues --- CHANGELOG.md | 1 + include/faker-cxx/color.h | 5 ++++- src/modules/color.cpp | 20 +++++++++++++++++++- src/modules/color_data.h | 9 ++++++++- tests/modules/color_test.cpp | 35 +++++++++++++++++++++++++++++------ 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9cad359..3d36a751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/include/faker-cxx/color.h b/include/faker-cxx/color.h index 9d345419..b2884231 100644 --- a/include/faker-cxx/color.h +++ b/include/faker-cxx/color.h @@ -4,6 +4,7 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" #include "types/hex.h" namespace faker::color @@ -11,13 +12,15 @@ 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. diff --git a/src/modules/color.cpp b/src/modules/color.cpp index 4df4acf4..14d8834b 100644 --- a/src/modules/color.cpp +++ b/src/modules/color.cpp @@ -1,5 +1,6 @@ #include "faker-cxx/color.h" +#include #include #include @@ -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 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); } diff --git a/src/modules/color_data.h b/src/modules/color_data.h index a83f1fad..db8f16fb 100644 --- a/src/modules/color_data.h +++ b/src/modules/color_data.h @@ -5,11 +5,18 @@ namespace faker::color { -const auto colors = std::to_array({ +const auto englishColors = std::to_array({ "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({ + "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", +}); + } diff --git a/tests/modules/color_test.cpp b/tests/modules/color_test.cpp index a3880cb6..c454ceb1 100644 --- a/tests/modules/color_test.cpp +++ b/tests/modules/color_test.cpp @@ -1,7 +1,7 @@ #include #include +#include #include -#include #include "gtest/gtest.h" @@ -13,21 +13,44 @@ using namespace ::testing; using namespace faker; -using namespace color; +using namespace faker::color; -class ColorTest : public Test +std::span getColors(Locale locale) +{ + switch (locale) + { + case Locale::pl_PL: + return polishColors; + default: + return englishColors; + } +} + +class ColorNameTest : public TestWithParam { 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& paramInfo) { return toString(paramInfo.param); }); + +class ColorTest : public Test +{ +public: +}; + TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha) { const auto generatedRgbColor = rgb();