Skip to content

Commit

Permalink
refactor weather data (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
krupalbhat authored May 31, 2024
1 parent c752aaa commit d1ccb72
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
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

0 comments on commit d1ccb72

Please sign in to comment.