From 0d1f6ec64df52d24196544dcabd6f032feef78f0 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Sun, 26 May 2024 20:24:03 +0200 Subject: [PATCH] Refactor Sport module (#589) * use string_view instead of string Signed-off-by: Uilian Ries * use array instead of vector for sports * fix sport name when testing Signed-off-by: Uilian Ries --------- Signed-off-by: Uilian Ries --- CMakeLists.txt | 1 + include/faker-cxx/Sport.h | 14 ++--- src/modules/sport/Sport.cpp | 28 +++++----- src/modules/sport/SportData.cpp | 70 +++++++++++++++++++++++++ src/modules/sport/SportData.h | 14 +++++ src/modules/sport/data/FemaleAthletes.h | 12 ----- src/modules/sport/data/MaleAthletes.h | 12 ----- src/modules/sport/data/SoccerTeams.h | 12 ----- src/modules/sport/data/SportEvents.h | 20 ------- src/modules/sport/data/SportNames.h | 12 ----- tests/modules/sport/SportTest.cpp | 30 +++++------ 11 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 src/modules/sport/SportData.cpp create mode 100644 src/modules/sport/SportData.h delete mode 100644 src/modules/sport/data/FemaleAthletes.h delete mode 100644 src/modules/sport/data/MaleAthletes.h delete mode 100644 src/modules/sport/data/SoccerTeams.h delete mode 100644 src/modules/sport/data/SportEvents.h delete mode 100644 src/modules/sport/data/SportNames.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 78fafc5d8..7316a98e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/faker-cxx/Sport.h b/include/faker-cxx/Sport.h index a9f0bfeb7..4d2a1a7a4 100644 --- a/include/faker-cxx/Sport.h +++ b/include/faker-cxx/Sport.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace faker { @@ -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. @@ -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. @@ -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. @@ -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. @@ -60,6 +60,6 @@ class Sport * Sport::sportEvent() // "Super Bowl" * @endcode */ - static std::string sportEvent(); + static std::string_view sportEvent(); }; -} \ No newline at end of file +} diff --git a/src/modules/sport/Sport.cpp b/src/modules/sport/Sport.cpp index c7deb7ae4..0b671ff86 100644 --- a/src/modules/sport/Sport.cpp +++ b/src/modules/sport/Sport.cpp @@ -1,38 +1,34 @@ #include "faker-cxx/Sport.h" -#include +#include -#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(sportNames); + return Helper::arrayElement(sport::sportNames); } -std::string Sport::soccerTeam() +std::string_view Sport::soccerTeam() { - return Helper::arrayElement(soccerTeams); + return Helper::arrayElement(sport::soccerTeams); } -std::string Sport::maleAthlete() +std::string_view Sport::maleAthlete() { - return Helper::arrayElement(maleAthletes); + return Helper::arrayElement(sport::maleAthletes); } -std::string Sport::femaleAthlete() +std::string_view Sport::femaleAthlete() { - return Helper::arrayElement(femaleAthletes); + return Helper::arrayElement(sport::femaleAthletes); } -std::string Sport::sportEvent() +std::string_view Sport::sportEvent() { - return Helper::arrayElement(sportEvents); + return Helper::arrayElement(sport::sportEvents); } } diff --git a/src/modules/sport/SportData.cpp b/src/modules/sport/SportData.cpp new file mode 100644 index 000000000..3b77c98d9 --- /dev/null +++ b/src/modules/sport/SportData.cpp @@ -0,0 +1,70 @@ +#include "SportData.h" + +namespace faker::sport +{ +const std::array 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 maleAthletes = { + "Cristiano Ronaldo", + "Kobe Bryant", + "LeBron James", + "Lionel Messi", + "Michael Jordan", + "Michael Phelps", + "Muhammad Ali", + "Roger Federer", + "Tom Brady", + "Usain Bolt" +}; + +const std::array 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 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 sportNames = { + "American Football", + "Baseball", + "Basketball", + "Cricket", + "Golf", + "Rugby", + "Soccer", + "Table Tennis", + "Tennis", + "Volleyball" +}; + +} diff --git a/src/modules/sport/SportData.h b/src/modules/sport/SportData.h new file mode 100644 index 000000000..947227870 --- /dev/null +++ b/src/modules/sport/SportData.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace faker::sport { + +extern const std::array femaleAthletes; +extern const std::array maleAthletes; +extern const std::array soccerTeams; +extern const std::array sportEvents; +extern const std::array sportNames; + +} diff --git a/src/modules/sport/data/FemaleAthletes.h b/src/modules/sport/data/FemaleAthletes.h deleted file mode 100644 index 4fa90fc90..000000000 --- a/src/modules/sport/data/FemaleAthletes.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector femaleAthletes = { - "Serena Williams", "Simone Biles", "Mia Hamm", "Ronda Rousey", "Lindsey Vonn", "Alex Morgan", - "Martina Navratilova", "Jackie Joyner-Kersee", "Nadia Comăneci", "Steffi Graf", -}; -} diff --git a/src/modules/sport/data/MaleAthletes.h b/src/modules/sport/data/MaleAthletes.h deleted file mode 100644 index d25143564..000000000 --- a/src/modules/sport/data/MaleAthletes.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector maleAthletes = { - "Lionel Messi", "Cristiano Ronaldo", "LeBron James", "Usain Bolt", "Michael Phelps", - "Roger Federer", "Kobe Bryant", "Tom Brady", "Muhammad Ali", "Michael Jordan", -}; -} diff --git a/src/modules/sport/data/SoccerTeams.h b/src/modules/sport/data/SoccerTeams.h deleted file mode 100644 index c817b3184..000000000 --- a/src/modules/sport/data/SoccerTeams.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector soccerTeams{ - "FC Barcelona", "Real Madrid CF", "Manchester United FC", "Liverpool FC", "FC Bayern Munich", - "AC Milan", "Inter Milan", "Juventus FC", "Paris Saint-Germain FC", "Chelsea FC", -}; -} diff --git a/src/modules/sport/data/SportEvents.h b/src/modules/sport/data/SportEvents.h deleted file mode 100644 index f273b3da1..000000000 --- a/src/modules/sport/data/SportEvents.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector sportEvents = { - "World Cup", - "Olympics", - "Super Bowl", - "World Athletics Championship", - "UEFA Champions League", - "NBA Finals", - "Wimbledon", - "ICC Cricket World Cup", - "The Masters", - "Rugby World Cup", -}; -} diff --git a/src/modules/sport/data/SportNames.h b/src/modules/sport/data/SportNames.h deleted file mode 100644 index b3ffe1ec0..000000000 --- a/src/modules/sport/data/SportNames.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector sportNames = { - "Soccer", "Cricket", "Basketball", "Tennis", "Volleyball", "Table Tennis", - "Golf", "Baseball", "American Football", "Rugby", -}; -} diff --git a/tests/modules/sport/SportTest.cpp b/tests/modules/sport/SportTest.cpp index b2c29fe17..f55fcfe69 100644 --- a/tests/modules/sport/SportTest.cpp +++ b/tests/modules/sport/SportTest.cpp @@ -1,15 +1,11 @@ #include "faker-cxx/Sport.h" #include -#include +#include #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; @@ -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; })); }