Skip to content

Commit

Permalink
Refactor Sport module (#589)
Browse files Browse the repository at this point in the history
* use string_view instead of string

Signed-off-by: Uilian Ries <[email protected]>

* use array instead of vector for sports

* fix sport name when testing

Signed-off-by: Uilian Ries <[email protected]>

---------

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries authored May 26, 2024
1 parent e4fc0cc commit 0d1f6ec
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 108 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(FAKER_SOURCES
src/modules/phone/PhoneData.cpp
src/modules/science/Science.cpp
src/modules/sport/Sport.cpp
src/modules/sport/SportData.cpp
src/modules/string/String.cpp
src/modules/system/System.cpp
src/modules/vehicle/Vehicle.cpp
Expand Down
14 changes: 7 additions & 7 deletions include/faker-cxx/Sport.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 All @@ -16,7 +16,7 @@ class Sport
* Sport::sport() // "football"
* @endcode
*/
static std::string sport();
static std::string_view sport();

/**
* @brief Returns a random soccer team.
Expand All @@ -27,7 +27,7 @@ class Sport
* Sport::soccerTeam() // "Manchester United"
* @endcode
*/
static std::string soccerTeam();
static std::string_view soccerTeam();

/**
* @brief Returns a random male athlete.
Expand All @@ -38,7 +38,7 @@ class Sport
* Sport::maleAthlete() // "Cristiano Ronaldo"
* @endcode
*/
static std::string maleAthlete();
static std::string_view maleAthlete();

/**
* @brief Returns a random female athlete.
Expand All @@ -49,7 +49,7 @@ class Sport
* Sport::femaleAthlete() // "Serena Williams"
* @endcode
*/
static std::string femaleAthlete();
static std::string_view femaleAthlete();

/**
* @brief Returns a random Sport Event.
Expand All @@ -60,6 +60,6 @@ class Sport
* Sport::sportEvent() // "Super Bowl"
* @endcode
*/
static std::string sportEvent();
static std::string_view sportEvent();
};
}
}
28 changes: 12 additions & 16 deletions src/modules/sport/Sport.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
#include "faker-cxx/Sport.h"

#include <string>
#include <string_view>

#include "data/FemaleAthletes.h"
#include "data/MaleAthletes.h"
#include "data/SoccerTeams.h"
#include "data/SportEvents.h"
#include "data/SportNames.h"
#include "SportData.h"
#include "faker-cxx/Helper.h"

namespace faker
{
std::string Sport::sport()
std::string_view Sport::sport()
{
return Helper::arrayElement<std::string>(sportNames);
return Helper::arrayElement(sport::sportNames);
}

std::string Sport::soccerTeam()
std::string_view Sport::soccerTeam()
{
return Helper::arrayElement<std::string>(soccerTeams);
return Helper::arrayElement(sport::soccerTeams);
}

std::string Sport::maleAthlete()
std::string_view Sport::maleAthlete()
{
return Helper::arrayElement<std::string>(maleAthletes);
return Helper::arrayElement(sport::maleAthletes);
}

std::string Sport::femaleAthlete()
std::string_view Sport::femaleAthlete()
{
return Helper::arrayElement<std::string>(femaleAthletes);
return Helper::arrayElement(sport::femaleAthletes);
}

std::string Sport::sportEvent()
std::string_view Sport::sportEvent()
{
return Helper::arrayElement<std::string>(sportEvents);
return Helper::arrayElement(sport::sportEvents);
}
}
70 changes: 70 additions & 0 deletions src/modules/sport/SportData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "SportData.h"

namespace faker::sport
{
const std::array<std::string_view, 10> femaleAthletes = {
"Alex Morgan",
"Jackie Joyner-Kersee",
"Lindsey Vonn",
"Martina Navratilova",
"Mia Hamm",
"Nadia Comăneci",
"Ronda Rousey",
"Serena Williams",
"Simone Biles",
"Steffi Graf"
};

const std::array<std::string_view, 10> maleAthletes = {
"Cristiano Ronaldo",
"Kobe Bryant",
"LeBron James",
"Lionel Messi",
"Michael Jordan",
"Michael Phelps",
"Muhammad Ali",
"Roger Federer",
"Tom Brady",
"Usain Bolt"
};

const std::array<std::string_view, 10> soccerTeams = {
"AC Milan",
"Chelsea FC",
"FC Barcelona",
"FC Bayern Munich",
"Inter Milan",
"Juventus FC",
"Liverpool FC",
"Manchester United FC",
"Paris Saint-Germain FC",
"Real Madrid CF"
};

const std::array<std::string_view, 10> sportEvents = {
"ICC Cricket World Cup",
"NBA Finals",
"Olympics",
"Rugby World Cup",
"Super Bowl",
"The Masters",
"UEFA Champions League",
"Wimbledon",
"World Athletics Championship",
"World Cup"
};

const std::array<std::string_view, 10> sportNames = {
"American Football",
"Baseball",
"Basketball",
"Cricket",
"Golf",
"Rugby",
"Soccer",
"Table Tennis",
"Tennis",
"Volleyball"
};

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

#include <string>
#include <array>

namespace faker::sport {

extern const std::array<std::string_view, 10> femaleAthletes;
extern const std::array<std::string_view, 10> maleAthletes;
extern const std::array<std::string_view, 10> soccerTeams;
extern const std::array<std::string_view, 10> sportEvents;
extern const std::array<std::string_view, 10> sportNames;

}
12 changes: 0 additions & 12 deletions src/modules/sport/data/FemaleAthletes.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/modules/sport/data/MaleAthletes.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/modules/sport/data/SoccerTeams.h

This file was deleted.

20 changes: 0 additions & 20 deletions src/modules/sport/data/SportEvents.h

This file was deleted.

12 changes: 0 additions & 12 deletions src/modules/sport/data/SportNames.h

This file was deleted.

30 changes: 13 additions & 17 deletions tests/modules/sport/SportTest.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "faker-cxx/Sport.h"

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

#include "gtest/gtest.h"

#include "sport/data/FemaleAthletes.h"
#include "sport/data/MaleAthletes.h"
#include "sport/data/SoccerTeams.h"
#include "sport/data/SportEvents.h"
#include "sport/data/SportNames.h"
#include "sport/SportData.h"

using namespace ::testing;
using namespace faker;
Expand All @@ -21,40 +17,40 @@ class SportTest : public Test

TEST_F(SportTest, shouldGenerateSport)
{
std::string generatedSport = Sport::sport();
const auto generatedSport = Sport::sport();

ASSERT_TRUE(std::ranges::any_of(sportNames,
[generatedSport](const std::string& sport) { return sport == generatedSport; }));
ASSERT_TRUE(std::ranges::any_of(sport::sportNames,
[generatedSport](const std::string_view& sport) { return sport == generatedSport; }));
}

TEST_F(SportTest, shouldGenerateSoccerTeam)
{
std::string generatedSoccerTeam = Sport::soccerTeam();
const auto generatedSoccerTeam = Sport::soccerTeam();

ASSERT_TRUE(std::ranges::any_of(soccerTeams, [generatedSoccerTeam](const std::string& soccerTeam)
ASSERT_TRUE(std::ranges::any_of(sport::soccerTeams, [generatedSoccerTeam](const std::string_view& soccerTeam)
{ return soccerTeam == generatedSoccerTeam; }));
}

TEST_F(SportTest, shouldGenerateSportEvent)
{
std::string generatedSportEvent = Sport::sportEvent();
const auto generatedSportEvent = Sport::sportEvent();

ASSERT_TRUE(std::ranges::any_of(sportEvents, [generatedSportEvent](const std::string& sportEvent)
ASSERT_TRUE(std::ranges::any_of(sport::sportEvents, [generatedSportEvent](const std::string_view& sportEvent)
{ return sportEvent == generatedSportEvent; }));
}

TEST_F(SportTest, shouldGenerateMaleAthlete)
{
std::string generatedMaleAthlete = Sport::maleAthlete();
const auto generatedMaleAthlete = Sport::maleAthlete();

ASSERT_TRUE(std::ranges::any_of(maleAthletes, [generatedMaleAthlete](const std::string& maleAthlete)
ASSERT_TRUE(std::ranges::any_of(sport::maleAthletes, [generatedMaleAthlete](const std::string_view& maleAthlete)
{ return maleAthlete == generatedMaleAthlete; }));
}

TEST_F(SportTest, shouldGenerateFemaleAthlete)
{
std::string generatedFemaleAthlete = Sport::femaleAthlete();
const auto generatedFemaleAthlete = Sport::femaleAthlete();

ASSERT_TRUE(std::ranges::any_of(femaleAthletes, [generatedFemaleAthlete](const std::string& femaleAthlete)
ASSERT_TRUE(std::ranges::any_of(sport::femaleAthletes, [generatedFemaleAthlete](const std::string_view& femaleAthlete)
{ return femaleAthlete == generatedFemaleAthlete; }));
}

0 comments on commit 0d1f6ec

Please sign in to comment.