Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor weather data #612

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ set(FAKER_SOURCES
src/modules/videoGame/VideoGame.cpp
src/modules/videoGame/VideoGameData.cpp
src/modules/weather/Weather.cpp
src/modules/weather/WeatherData.cpp
src/modules/word/Word.cpp
src/common/FormatHelper.cpp
src/common/LuhnCheck.cpp
Expand Down
6 changes: 3 additions & 3 deletions include/faker-cxx/Weather.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand Down Expand Up @@ -99,13 +99,13 @@ class Weather
/**
* @brief Generated a random weather description
*
* @return String A random weather description
* @return String_View A random weather description
*
* @code
* Weather::description(); // "Sunny"
* @endcode
*/
static std::string weatherDescription();
static std::string_view weatherDescription();

/**
* @brief Generated a random cloud cover percentage
Expand Down
8 changes: 4 additions & 4 deletions src/modules/weather/Weather.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "faker-cxx/Weather.h"

#include <string>
#include <string_view>

#include "data/WeatherDescription.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "WeatherData.h"

namespace faker
{
Expand Down Expand Up @@ -51,9 +51,9 @@ int Weather::humidity()
return Number::integer(0, 100);
}

std::string Weather::weatherDescription()
std::string_view Weather::weatherDescription()
{
return Helper::arrayElement<std::string>(weatherDescriptions);
return Helper::arrayElement(weatherDescriptions);
}

int Weather::cloudCover()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#pragma once

#include <string>
#include <vector>
#include "WeatherData.h"

namespace faker
{
const std::vector<std::string> weatherDescriptions = {

const std::array<std::string_view, 16> weatherDescriptions = {
"clear sky", "few clouds", "scattered clouds",
"broken clouds", "shower rain", "rainy",
"thunderstorm", "snowy", "misty",
"smoky", "haze", "sunny",
"cloudy", "windy", "dark",
"foggy",
};

}
11 changes: 11 additions & 0 deletions src/modules/weather/WeatherData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <array>
#include <string_view>

namespace faker
{
extern const std::array<std::string_view, 16> weatherDescriptions;

}

8 changes: 4 additions & 4 deletions tests/modules/weather/WeatherTest.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "faker-cxx/Weather.h"

#include <algorithm>
#include <string>
#include <string_view>

#include "gtest/gtest.h"

#include "weather/data/WeatherDescription.h"
#include "weather/WeatherData.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -63,10 +63,10 @@ TEST_F(WeatherTest, shouldGenerateHumidity)

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

ASSERT_TRUE(std::ranges::any_of(weatherDescriptions,
[generatedWeatherDescription](const std::string& weatherDescription)
[generatedWeatherDescription](const std::string_view& weatherDescription)
{ return weatherDescription == generatedWeatherDescription; }));
}

Expand Down
Loading