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

Added Weather Module #213

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ set(FAKER_SOURCES
src/modules/sport/Sport.cpp
src/modules/videoGame/VideoGame.cpp
src/modules/medicine/Medicine.cpp
)
src/modules/weather/Weather.cpp
)

set(FAKER_UT_SOURCES
src/modules/animal/AnimalTest.cpp
Expand Down Expand Up @@ -78,7 +79,8 @@ set(FAKER_UT_SOURCES
src/modules/sport/SportTest.cpp
src/modules/videoGame/VideoGameTest.cpp
src/modules/medicine/MedicineTest.cpp
)
src/modules/weather/WeatherTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

Expand Down
118 changes: 118 additions & 0 deletions include/faker-cxx/Weather.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#pragma once

#include <string>

namespace faker
{
class Weather
{
public:
struct Temperature {
int metric, imperial;
};

/**
* @brief Generates a random temperature
*
* @return Temperature object with metric and imperial temperatures
*
* @code
* Weather::temperature(); // Temperature.metric = 10, Temperature.imperial = 50
* @endcode
*/

static Temperature temperature();

struct Pressure {
int metric;
double imperial;
};

/**
* @brief Generated a random pressure
*
* @return Pressure object with metric and imperial pressures
*
* @code
* Weather::pressure(); // Pressure.metric = 1000, Pressure.imperial = 14.5
* @endcode
*/
static Pressure pressure();

struct Visibility {
double metric;
double imperial;
};

/**
* @brief Generated a random visibility
*
* @return Visibility object with metric and imperial visibility
*
* @code
* Weather::visibility(); // Visibility.metric = 10.0, Visibility.imperial = 6.2
* @endcode
*/
static Visibility visibility();

struct WindSpeed {
double metric;
double imperial;
};

/**
* @brief Generated a random wind speed
*
* @return WindSpeed object with metric and imperial wind speed
*
* @code
* Weather::windSpeed(); // WindSpeed.metric = 10.0, WindSpeed.imperial = 6.2
* @endcode
*/
static WindSpeed windSpeed();

/**
* @brief Generated a random uvIndex
*
* @return Int A random uvIndex
*
* @code
* Weather::uvIndex(); // 10
* @endcode
*/
static int uvIndex();

/**
* @brief Generated a random humidity percentage
*
* @return Int A random humidity percentage
*
* @code
* Weather::humidity(); // 10
* @endcode
*/
static int humidity();

/**
* @brief Generated a random weather description
*
* @return String A random weather description
*
* @code
* Weather::description(); // "Sunny"
* @endcode
*/
static std::string weatherDescription();

/**
* @brief Generated a random cloud cover percentage
*
* @return Int A random cloud cover percentage
*
* @code
* Weather::cloudCover(); // 10
* @endcode
*/
static int cloudCover();
};
}
50 changes: 50 additions & 0 deletions src/modules/weather/Weather.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "faker-cxx/Weather.h"

#include <string>

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

namespace faker
{
Weather::Temperature Weather::temperature()
{
return {Number::integer(10, 30), Number::integer(50, 90)};
}

Weather::Pressure Weather::pressure()
{
return {Number::integer(1000, 1100), Number::integer(14, 16) + Number::decimal(0.1, 0.9)};
}

Weather::Visibility Weather::visibility()
{
return {Number::decimal(0.1, 10.0), Number::decimal(0.1, 6.2)};
}

Weather::WindSpeed Weather::windSpeed()
{
return {Number::decimal(0.1, 24.2), Number::decimal(0.1, 15.0)};
}

int Weather::uvIndex()
{
return Number::integer(0, 11);
}

int Weather::humidity()
{
return Number::integer(0, 100);
}

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

int Weather::cloudCover()
{
return Number::integer(0, 100);
}
}
75 changes: 75 additions & 0 deletions src/modules/weather/WeatherTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "faker-cxx/Weather.h"

#include <algorithm>
#include <string>

#include "gtest/gtest.h"
#include "data/WeatherDescription.h"

using namespace ::testing;
using namespace faker;

class WeatherTest : public Test
{
public:
};

TEST_F(WeatherTest, shouldGenerateTemperature)
{
Weather::Temperature generatedTemperature = Weather::temperature();

ASSERT_TRUE(generatedTemperature.metric >= 10 && generatedTemperature.metric <= 30);
ASSERT_TRUE(generatedTemperature.imperial >= 50 && generatedTemperature.imperial <= 90);
}

TEST_F(WeatherTest, shouldGeneratePressure)
{
Weather::Pressure generatedPressure = Weather::pressure();

ASSERT_TRUE(generatedPressure.metric >= 1000 && generatedPressure.metric <= 1100);
ASSERT_TRUE(generatedPressure.imperial >= 14.0 && generatedPressure.imperial <= 16.0);
}

TEST_F(WeatherTest, shouldGenerateVisibility)
{
Weather::Visibility generatedVisibility = Weather::visibility();

ASSERT_TRUE(generatedVisibility.metric >= 0.1 && generatedVisibility.metric <= 10.0);
ASSERT_TRUE(generatedVisibility.imperial >= 0.1 && generatedVisibility.imperial <= 6.2);
}

TEST_F(WeatherTest, shouldGenerateWindSpeed)
{
Weather::WindSpeed generatedWindSpeed = Weather::windSpeed();

ASSERT_TRUE(generatedWindSpeed.metric >= 0.1 && generatedWindSpeed.metric <= 24.2);
ASSERT_TRUE(generatedWindSpeed.imperial >= 0.1 && generatedWindSpeed.imperial <= 15.0);
}

TEST_F(WeatherTest, shouldGenerateUvIndex)
{
int generatedUvIndex = Weather::uvIndex();

ASSERT_TRUE(generatedUvIndex >= 0 && generatedUvIndex <= 11);
}

TEST_F(WeatherTest, shouldGenerateHumidity)
{
int generatedHumidity = Weather::humidity();

ASSERT_TRUE(generatedHumidity >= 0 && generatedHumidity <= 100);
}

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

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

TEST_F(WeatherTest, shouldGenerateWeatherCloudCover) {
int generatedCloudCover = Weather::cloudCover();

ASSERT_TRUE(generatedCloudCover >= 0 && generatedCloudCover <= 100);
}
16 changes: 16 additions & 0 deletions src/modules/weather/data/WeatherDescription.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> weatherDescriptions = {
"clear sky", "few clouds", "scattered clouds",
"broken clouds", "shower rain", "rainy",
"thunderstorm", "snowy", "misty",
"smoky", "haze", "sunny",
"cloudy", "windy", "dark",
"foggy",
};
}
Loading