Skip to content

Commit

Permalink
chore: added weather descriptions (#911)
Browse files Browse the repository at this point in the history
* chore: added weather descriptions

* swedish weather descriptions added to weather module

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

* refactor: locale to weather module

* added locale to weather module and changed the weather function
  definition

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

* test: parameterized test added

* added parameterized test to weather module

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

* test: weather module

* weather module test case fixed

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

---------

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 authored Sep 27, 2024
1 parent 499eadc commit eb1a88f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
7 changes: 5 additions & 2 deletions include/faker-cxx/weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
#include <string_view>

#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);
}
20 changes: 18 additions & 2 deletions src/modules/weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@

#include "faker-cxx/helper.h"
#include "weather_data.h"
#include <span>

namespace faker::weather
{
std::string_view weatherDescription()
namespace
{
return helper::randomElement(weatherDescriptions);
const std::span<const std::string_view> getWeatherDefinition(Locale locale)
{
switch (locale)
{
case Locale::sv_SE:
return svSEWeatherDescriptions;
default:
return enUSWeatherDescriptions;
}
}
}
std::string_view weatherDescription(Locale locale)
{
const auto& weatherDefinition = getWeatherDefinition(locale);

return helper::randomElement(weatherDefinition);
}
}
20 changes: 19 additions & 1 deletion src/modules/weather_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace faker::weather
{
const auto weatherDescriptions = std::to_array<std::string_view>({
const auto enUSWeatherDescriptions = std::to_array<std::string_view>({
"broken clouds",
"clear sky",
"cloudy",
Expand All @@ -24,4 +24,22 @@ const auto weatherDescriptions = std::to_array<std::string_view>({
"windy",
});

const auto svSEWeatherDescriptions = std::to_array<std::string_view>({
"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
});

}
33 changes: 29 additions & 4 deletions tests/modules/weather_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@

#include "gtest/gtest.h"

#include "faker-cxx/types/locale.h"
#include "faker-cxx/weather.h"
#include "weather_data.h"
#include <span>

using namespace ::testing;
using namespace faker;
using namespace faker::weather;

class WeatherTest : public Test
namespace
{
const std::span<const std::string_view> getWeatherDefinition(Locale locale)
{
switch (locale)
{
case Locale::sv_SE:
return svSEWeatherDescriptions;
default:
return enUSWeatherDescriptions;
}
}
}

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

TEST_F(WeatherTest, shouldGenerateWeatherDescription)
TEST_P(WeatherTest, shouldGenerateWeatherDescription)
{
const std::string_view generatedWeatherDescription = weatherDescription();

ASSERT_TRUE(std::ranges::any_of(weatherDescriptions,
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<Locale>& paramInfo) { return toString(paramInfo.param); });

0 comments on commit eb1a88f

Please sign in to comment.