From e1a775767bbd13e1ace765c8686dc08a69ebac19 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Wed, 11 Sep 2024 00:18:53 +0200 Subject: [PATCH 1/4] chore: added weather descriptions * swedish weather descriptions added to weather module Signed-off-by: Guru Mehar Rachaputi --- src/modules/weather_data.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/modules/weather_data.h b/src/modules/weather_data.h index eff48e7ef..6ac26c33c 100644 --- a/src/modules/weather_data.h +++ b/src/modules/weather_data.h @@ -24,4 +24,22 @@ const auto weatherDescriptions = std::to_array({ "windy", }); +const auto svSEWeatherDescriptions = std::to_array({ + "dimma", // fog + "regn", // rain + "snö", // snow + "sol", // sun + "vind", // wind + "is", // ice + "storm", // storm + "vinter", // winter + "kallt", // cold + "snöflinga", // snow flake + "snöstorm", // blizzard + "sommar", // summer + "vår", // spring + "skurar", // showers + "höst", // fall/autumn +}); + } From 5a81638de5971aa93fa0d93be100b5999d72780e Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Thu, 12 Sep 2024 23:37:13 +0200 Subject: [PATCH 2/4] refactor: locale to weather module * added locale to weather module and changed the weather function definition Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/weather.h | 7 ++++-- src/modules/weather.cpp | 40 ++++++++++++++++++++++++++++++-- src/modules/weather_data.h | 2 +- tests/modules/weather_test.cpp | 42 ++++++++++++++++++++++++++++++++-- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/include/faker-cxx/weather.h b/include/faker-cxx/weather.h index da2dc1203..e483db86e 100644 --- a/include/faker-cxx/weather.h +++ b/include/faker-cxx/weather.h @@ -3,17 +3,20 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::weather { /** * @brief Generated a random weather description + * + * * @param locale The locale. Defaults to `Locale::en_US`. * * @return A random weather description * * @code - * faker::weather::weatherDescription(); // "Sunny" + * faker::weather::weatherDescription(Locale::en_GB); // "Sunny" * @endcode */ -FAKER_CXX_EXPORT std::string_view weatherDescription(); +FAKER_CXX_EXPORT std::string_view weatherDescription(Locale locale = Locale::en_US); } diff --git a/src/modules/weather.cpp b/src/modules/weather.cpp index 9cb360f51..f26f1610d 100644 --- a/src/modules/weather.cpp +++ b/src/modules/weather.cpp @@ -7,8 +7,44 @@ namespace faker::weather { -std::string_view weatherDescription() +namespace { - return helper::randomElement(weatherDescriptions); +const auto& getWeatherDefinition(Locale locale) +{ + switch (locale) + { + case Locale::sv_SE: + return svSEWeatherDescriptions; + case Locale::en_029: + case Locale::en_AU: + case Locale::en_BW: + case Locale::en_BZ: + case Locale::en_CA: + case Locale::en_cb: + case Locale::en_GB: + case Locale::en_GH: + case Locale::en_HK: + case Locale::en_IE: + case Locale::en_IN: + case Locale::en_JM: + case Locale::en_MT: + case Locale::en_MY: + case Locale::en_NZ: + case Locale::en_PH: + case Locale::en_SG: + case Locale::en_TT: + case Locale::en_US: + case Locale::en_ZA: + case Locale::en_ZW: + default: + return enUSWeatherDescriptions; + } +} +} +std::string_view weatherDescription(Locale locale) +{ + const auto& weatherDefinition = getWeatherDefinition(locale); + + return helper::randomElement(weatherDefinition); } } diff --git a/src/modules/weather_data.h b/src/modules/weather_data.h index 6ac26c33c..d036c52af 100644 --- a/src/modules/weather_data.h +++ b/src/modules/weather_data.h @@ -5,7 +5,7 @@ namespace faker::weather { -const auto weatherDescriptions = std::to_array({ +const auto enUSWeatherDescriptions = std::to_array({ "broken clouds", "clear sky", "cloudy", diff --git a/tests/modules/weather_test.cpp b/tests/modules/weather_test.cpp index 6b0dcb37d..971af5312 100644 --- a/tests/modules/weather_test.cpp +++ b/tests/modules/weather_test.cpp @@ -3,12 +3,48 @@ #include "gtest/gtest.h" +#include "faker-cxx/types/locale.h" #include "faker-cxx/weather.h" #include "weather_data.h" using namespace ::testing; using namespace faker::weather; +namespace +{ +const auto& getWeatherDefinition(Locale locale) +{ + switch (locale) + { + case Locale::sv_SE: + return svSEWeatherDescriptions; + case Locale::en_029: + case Locale::en_AU: + case Locale::en_BW: + case Locale::en_BZ: + case Locale::en_CA: + case Locale::en_cb: + case Locale::en_GB: + case Locale::en_GH: + case Locale::en_HK: + case Locale::en_IE: + case Locale::en_IN: + case Locale::en_JM: + case Locale::en_MT: + case Locale::en_MY: + case Locale::en_NZ: + case Locale::en_PH: + case Locale::en_SG: + case Locale::en_TT: + case Locale::en_US: + case Locale::en_ZA: + case Locale::en_ZW: + default: + return enUSWeatherDescriptions; + } +} +} + class WeatherTest : public Test { public: @@ -16,9 +52,11 @@ class WeatherTest : public Test TEST_F(WeatherTest, shouldGenerateWeatherDescription) { - const std::string_view generatedWeatherDescription = weatherDescription(); + const auto& weatherDefinition = getWeatherDefinition(Locale::en_US); + + const std::string_view generatedWeatherDescription = weatherDescription(Locale::en_US); - ASSERT_TRUE(std::ranges::any_of(weatherDescriptions, + ASSERT_TRUE(std::ranges::any_of(weatherDefinition, [generatedWeatherDescription](const std::string_view& weatherDescription) { return weatherDescription == generatedWeatherDescription; })); } From 91e8726259f45e86996462874e6d62f34f77d0ca Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Fri, 27 Sep 2024 14:56:44 +0200 Subject: [PATCH 3/4] test: parameterized test added * added parameterized test to weather module Signed-off-by: Guru Mehar Rachaputi --- tests/modules/weather_test.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/modules/weather_test.cpp b/tests/modules/weather_test.cpp index 971af5312..bd4080657 100644 --- a/tests/modules/weather_test.cpp +++ b/tests/modules/weather_test.cpp @@ -45,18 +45,24 @@ const auto& getWeatherDefinition(Locale locale) } } -class WeatherTest : public Test +class WeatherTest : public TestWithParam { public: }; TEST_F(WeatherTest, shouldGenerateWeatherDescription) { - const auto& weatherDefinition = getWeatherDefinition(Locale::en_US); - const std::string_view generatedWeatherDescription = weatherDescription(Locale::en_US); + const auto locale = GetParam(); + + const auto& weatherDefinition = getWeatherDefinition(locale); + + const std::string_view generatedWeatherDescription = weatherDescription(locale); ASSERT_TRUE(std::ranges::any_of(weatherDefinition, [generatedWeatherDescription](const std::string_view& weatherDescription) { return weatherDescription == generatedWeatherDescription; })); } + +INSTANTIATE_TEST_SUITE_P(TestWeatherByLocale, WeatherTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); From 42ff86181aa26b2c2e3efe22c77d286ba26e677d Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Fri, 27 Sep 2024 17:03:52 +0200 Subject: [PATCH 4/4] test: weather module * weather module test case fixed Signed-off-by: Guru Mehar Rachaputi --- src/modules/weather.cpp | 24 ++---------------------- tests/modules/weather_test.cpp | 27 ++++----------------------- 2 files changed, 6 insertions(+), 45 deletions(-) diff --git a/src/modules/weather.cpp b/src/modules/weather.cpp index f26f1610d..49ecd921f 100644 --- a/src/modules/weather.cpp +++ b/src/modules/weather.cpp @@ -4,38 +4,18 @@ #include "faker-cxx/helper.h" #include "weather_data.h" +#include namespace faker::weather { namespace { -const auto& getWeatherDefinition(Locale locale) +const std::span getWeatherDefinition(Locale locale) { switch (locale) { case Locale::sv_SE: return svSEWeatherDescriptions; - case Locale::en_029: - case Locale::en_AU: - case Locale::en_BW: - case Locale::en_BZ: - case Locale::en_CA: - case Locale::en_cb: - case Locale::en_GB: - case Locale::en_GH: - case Locale::en_HK: - case Locale::en_IE: - case Locale::en_IN: - case Locale::en_JM: - case Locale::en_MT: - case Locale::en_MY: - case Locale::en_NZ: - case Locale::en_PH: - case Locale::en_SG: - case Locale::en_TT: - case Locale::en_US: - case Locale::en_ZA: - case Locale::en_ZW: default: return enUSWeatherDescriptions; } diff --git a/tests/modules/weather_test.cpp b/tests/modules/weather_test.cpp index bd4080657..071be51d5 100644 --- a/tests/modules/weather_test.cpp +++ b/tests/modules/weather_test.cpp @@ -6,39 +6,20 @@ #include "faker-cxx/types/locale.h" #include "faker-cxx/weather.h" #include "weather_data.h" +#include using namespace ::testing; +using namespace faker; using namespace faker::weather; namespace { -const auto& getWeatherDefinition(Locale locale) +const std::span getWeatherDefinition(Locale locale) { switch (locale) { case Locale::sv_SE: return svSEWeatherDescriptions; - case Locale::en_029: - case Locale::en_AU: - case Locale::en_BW: - case Locale::en_BZ: - case Locale::en_CA: - case Locale::en_cb: - case Locale::en_GB: - case Locale::en_GH: - case Locale::en_HK: - case Locale::en_IE: - case Locale::en_IN: - case Locale::en_JM: - case Locale::en_MT: - case Locale::en_MY: - case Locale::en_NZ: - case Locale::en_PH: - case Locale::en_SG: - case Locale::en_TT: - case Locale::en_US: - case Locale::en_ZA: - case Locale::en_ZW: default: return enUSWeatherDescriptions; } @@ -50,7 +31,7 @@ class WeatherTest : public TestWithParam public: }; -TEST_F(WeatherTest, shouldGenerateWeatherDescription) +TEST_P(WeatherTest, shouldGenerateWeatherDescription) { const auto locale = GetParam();