From 51ba70d8d02a13ffd8f8d06462bbca4505f944df Mon Sep 17 00:00:00 2001 From: Eric Floyd <101071809+eric-bodhi@users.noreply.github.com> Date: Tue, 10 Oct 2023 04:42:42 -0400 Subject: [PATCH] Feature/sport (#198) * Removed uneeded functions and moved some to StringHelper * Added Sport Module --- CMakeLists.txt | 2 + include/faker-cxx/Helper.h | 41 ---------------- include/faker-cxx/Sport.h | 65 +++++++++++++++++++++++++ src/common/StringHelper.cpp | 14 ++++++ src/common/StringHelper.h | 2 + src/common/StringHelperTest.cpp | 22 +++++++++ src/modules/hacker/Hacker.cpp | 5 +- src/modules/hacker/HackerTest.cpp | 2 - src/modules/helper/Helper.cpp | 24 --------- src/modules/helper/HelperTest.cpp | 31 ------------ src/modules/sport/Sport.cpp | 39 +++++++++++++++ src/modules/sport/SportTest.cpp | 60 +++++++++++++++++++++++ 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 +++++ 17 files changed, 275 insertions(+), 100 deletions(-) create mode 100644 include/faker-cxx/Sport.h create mode 100644 src/modules/sport/Sport.cpp create mode 100644 src/modules/sport/SportTest.cpp create mode 100644 src/modules/sport/data/FemaleAthletes.h create mode 100644 src/modules/sport/data/MaleAthletes.h create mode 100644 src/modules/sport/data/SoccerTeams.h create mode 100644 src/modules/sport/data/SportEvents.h create mode 100644 src/modules/sport/data/SportNames.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 04750b088..857e7799a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ set(FAKER_SOURCES src/modules/music/Music.cpp src/modules/movie/Movie.cpp src/modules/hacker/Hacker.cpp + src/modules/sport/Sport.cpp ) set(FAKER_UT_SOURCES @@ -70,6 +71,7 @@ set(FAKER_UT_SOURCES src/modules/music/MusicTest.cpp src/modules/movie/MovieTest.cpp src/modules/hacker/HackerTest.cpp + src/modules/sport/SportTest.cpp ) add_library(${LIBRARY_NAME} ${FAKER_SOURCES}) diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 794069347..f1f20056b 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "Datatype.h" #include "Number.h" @@ -179,46 +178,6 @@ class Helper return TResult(); } - /* - * @brief Returns a split string into words. - * - * @param input The string to split. - * - * @return A vector of words. - * - * @code - * Helper::splitIntoWords("Hello World!") // {"Hello", "World!"} - * @endcode - */ - static std::vector splitIntoWords(const std::string& input); - - /* - * @brief Returns true if the given character is punctuation. - * - * @param c The character to check. - * - * @return True if the given character is punctuation. - * - * @code - * Helper::isPunctuation('!') // true - * Helper::isPunctuation('a') // false - * @endcode - */ - static bool isPunctuation(char c); - - /* - * @brief Returns the given word without punctuation. - * - * @param word The word to remove punctuation. - * - * @return The given word without punctuation. - * - * @code - * Helper::removePunctuation("Hello!") // "Hello" - * @endcode - */ - static std::string removePunctuation(const std::string& word); - private: static std::random_device randomDevice; static std::mt19937 pseudoRandomGenerator; diff --git a/include/faker-cxx/Sport.h b/include/faker-cxx/Sport.h new file mode 100644 index 000000000..a9f0bfeb7 --- /dev/null +++ b/include/faker-cxx/Sport.h @@ -0,0 +1,65 @@ +#pragma once + +#include + +namespace faker +{ +class Sport +{ +public: + /** + * @brief Returns a random sport. + * + * @returns sport. + * + * @code + * Sport::sport() // "football" + * @endcode + */ + static std::string sport(); + + /** + * @brief Returns a random soccer team. + * + * @returns soccer team. + * + * @code + * Sport::soccerTeam() // "Manchester United" + * @endcode + */ + static std::string soccerTeam(); + + /** + * @brief Returns a random male athlete. + * + * @returns male athlete. + * + * @code + * Sport::maleAthlete() // "Cristiano Ronaldo" + * @endcode + */ + static std::string maleAthlete(); + + /** + * @brief Returns a random female athlete. + * + * @returns female athlete. + * + * @code + * Sport::femaleAthlete() // "Serena Williams" + * @endcode + */ + static std::string femaleAthlete(); + + /** + * @brief Returns a random Sport Event. + * + * @returns Sport Event. + * + * @code + * Sport::sportEvent() // "Super Bowl" + * @endcode + */ + static std::string sportEvent(); +}; +} \ No newline at end of file diff --git a/src/common/StringHelper.cpp b/src/common/StringHelper.cpp index 3b4c87f30..173c56c10 100644 --- a/src/common/StringHelper.cpp +++ b/src/common/StringHelper.cpp @@ -63,4 +63,18 @@ std::string StringHelper::toLower(const std::string& data) return lowerData; } + +// Function to check if a character is punctuation +bool StringHelper::isPunctuation(char c) +{ + return (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':'); +} + +// Function to remove punctuation from a word +std::string StringHelper::removePunctuation(const std::string& word) +{ + std::string result = word; + result.erase(std::remove_if(result.begin(), result.end(), isPunctuation), result.end()); + return result; +} } diff --git a/src/common/StringHelper.h b/src/common/StringHelper.h index 3c22433b3..163bcacad 100644 --- a/src/common/StringHelper.h +++ b/src/common/StringHelper.h @@ -12,5 +12,7 @@ class StringHelper static std::string join(const std::vector& data, const std::string& separator = " "); static std::string repeat(const std::string& data, int repetition); static std::string toLower(const std::string& data); + static bool isPunctuation(char c); + static std::string removePunctuation(const std::string& word); }; } diff --git a/src/common/StringHelperTest.cpp b/src/common/StringHelperTest.cpp index f905b2ccc..ec58c75bb 100644 --- a/src/common/StringHelperTest.cpp +++ b/src/common/StringHelperTest.cpp @@ -70,3 +70,25 @@ TEST_F(StringHelperTest, toLower) ASSERT_EQ(result, "hello!"); } + +TEST_F(StringHelperTest, IsPunctuation) +{ + std::string punctuation = ".,;:!?"; + for (char c : punctuation) + { + EXPECT_TRUE(StringHelper::isPunctuation(c)); + } + + std::string notPunctuation = "abc123"; + for (char c : notPunctuation) + { + EXPECT_FALSE(StringHelper::isPunctuation(c)); + } +} + +TEST_F(StringHelperTest, RemovePunctuation) +{ + std::string input = "Hello, World!"; + std::string result = StringHelper::removePunctuation(input); + EXPECT_EQ(result, "Hello World"); +} diff --git a/src/modules/hacker/Hacker.cpp b/src/modules/hacker/Hacker.cpp index 5dfdfe0a8..b61eb059d 100644 --- a/src/modules/hacker/Hacker.cpp +++ b/src/modules/hacker/Hacker.cpp @@ -1,4 +1,5 @@ #include "faker-cxx/Hacker.h" +#include "../../common/StringHelper.h" #include #include @@ -39,12 +40,12 @@ std::string Hacker::ingverb() std::string Hacker::phrase() { - auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement(faker::phrases)); + auto splitRandomPhrase = StringHelper::split(faker::Helper::arrayElement(faker::phrases)); std::string ret; for (auto& word : splitRandomPhrase) { - word = Helper::removePunctuation(word); + word = StringHelper::removePunctuation(word); if (word == "{abbreviation}") { word = abbreviation(); diff --git a/src/modules/hacker/HackerTest.cpp b/src/modules/hacker/HackerTest.cpp index f6f4aac53..1b1a5df52 100644 --- a/src/modules/hacker/HackerTest.cpp +++ b/src/modules/hacker/HackerTest.cpp @@ -9,9 +9,7 @@ #include "data/Adjectives.h" #include "data/Ingverbs.h" #include "data/Nouns.h" -#include "data/Phrases.h" #include "data/Verbs.h" -#include "faker-cxx/Helper.h" using namespace ::testing; using namespace faker; diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index a76864768..74b400e42 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -1,11 +1,9 @@ #include "faker-cxx/Helper.h" #include -#include #include #include #include -#include #include #include "../src/common/LuhnCheck.h" @@ -107,26 +105,4 @@ std::string Helper::regexpStyleStringParse(const std::string& input) return string; } - -// helper functions for phrases -std::vector Helper::splitIntoWords(const std::string& input) -{ - std::istringstream iss(input); - std::vector words(std::istream_iterator{iss}, std::istream_iterator()); - return words; -} - -// Function to check if a character is punctuation -bool Helper::isPunctuation(char c) -{ - return (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':'); -} - -// Function to remove punctuation from a word -std::string Helper::removePunctuation(const std::string& word) -{ - std::string result = word; - result.erase(std::remove_if(result.begin(), result.end(), isPunctuation), result.end()); - return result; -} } diff --git a/src/modules/helper/HelperTest.cpp b/src/modules/helper/HelperTest.cpp index e851719e9..b58053a1c 100644 --- a/src/modules/helper/HelperTest.cpp +++ b/src/modules/helper/HelperTest.cpp @@ -107,35 +107,4 @@ TEST_F(HelperTest, MaybeDouble) result = Helper::maybe([]() { return 3.14; }, lowProbability); EXPECT_EQ(result, 0.0); } - -TEST_F(HelperTest, IsPunctuation) -{ - std::string punctuation = ".,;:!?"; - for (char c : punctuation) - { - EXPECT_TRUE(Helper::isPunctuation(c)); - } - - std::string notPunctuation = "abc123"; - for (char c : notPunctuation) - { - EXPECT_FALSE(Helper::isPunctuation(c)); - } -} - -TEST_F(HelperTest, RemovePunctuation) -{ - std::string input = "Hello, World!"; - std::string result = Helper::removePunctuation(input); - EXPECT_EQ(result, "Hello World"); -} - -TEST_F(HelperTest, SplitIntoWords) -{ - std::string input = "Hello World!"; - std::vector result = Helper::splitIntoWords(input); - EXPECT_EQ(result.size(), 2); - EXPECT_EQ(result[0], "Hello"); - EXPECT_EQ(result[1], "World!"); -} } diff --git a/src/modules/sport/Sport.cpp b/src/modules/sport/Sport.cpp new file mode 100644 index 000000000..253eb7d1c --- /dev/null +++ b/src/modules/sport/Sport.cpp @@ -0,0 +1,39 @@ +#include "faker-cxx/Sport.h" +#include "faker-cxx/Helper.h" + + +#include + +#include "data/FemaleAthletes.h" +#include "data/MaleAthletes.h" +#include "data/SoccerTeams.h" +#include "data/SportEvents.h" +#include "data/SportNames.h" + +namespace faker +{ +std::string Sport::sport() +{ + return Helper::arrayElement(sportNames); +} + +std::string Sport::soccerTeam() +{ + return Helper::arrayElement(soccerTeams); +} + +std::string Sport::maleAthlete() +{ + return Helper::arrayElement(maleAthletes); +} + +std::string Sport::femaleAthlete() +{ + return Helper::arrayElement(femaleAthletes); +} + +std::string Sport::sportEvent() +{ + return Helper::arrayElement(sportEvents); +} +} diff --git a/src/modules/sport/SportTest.cpp b/src/modules/sport/SportTest.cpp new file mode 100644 index 000000000..634cbfed1 --- /dev/null +++ b/src/modules/sport/SportTest.cpp @@ -0,0 +1,60 @@ +#include "faker-cxx/Sport.h" + +#include +#include + +#include "gtest/gtest.h" + +#include "data/FemaleAthletes.h" +#include "data/MaleAthletes.h" +#include "data/SoccerTeams.h" +#include "data/SportEvents.h" +#include "data/SportNames.h" + +using namespace ::testing; +using namespace faker; + +class SportTest : public Test +{ +public: +}; + +TEST_F(SportTest, shouldGenerateSport) +{ + std::string generatedSport = Sport::sport(); + + ASSERT_TRUE(std::ranges::any_of(sportNames, + [generatedSport](const std::string& sport) { return sport == generatedSport; })); +} + +TEST_F(SportTest, shouldGenerateSoccerTeam) +{ + std::string generatedSoccerTeam = Sport::soccerTeam(); + + ASSERT_TRUE(std::ranges::any_of(soccerTeams, [generatedSoccerTeam](const std::string& soccerTeam) + { return soccerTeam == generatedSoccerTeam; })); +} + +TEST_F(SportTest, shouldGenerateSportEvent) +{ + std::string generatedSportEvent = Sport::sportEvent(); + + ASSERT_TRUE(std::ranges::any_of(sportEvents, [generatedSportEvent](const std::string& sportEvent) + { return sportEvent == generatedSportEvent; })); +} + +TEST_F(SportTest, shouldGenerateMaleAthlete) +{ + std::string generatedMaleAthlete = Sport::maleAthlete(); + + ASSERT_TRUE(std::ranges::any_of(maleAthletes, [generatedMaleAthlete](const std::string& maleAthlete) + { return maleAthlete == generatedMaleAthlete; })); +} + +TEST_F(SportTest, shouldGenerateFemaleAthlete) +{ + std::string generatedFemaleAthlete = Sport::femaleAthlete(); + + ASSERT_TRUE(std::ranges::any_of(femaleAthletes, [generatedFemaleAthlete](const std::string& femaleAthlete) + { return femaleAthlete == generatedFemaleAthlete; })); +} diff --git a/src/modules/sport/data/FemaleAthletes.h b/src/modules/sport/data/FemaleAthletes.h new file mode 100644 index 000000000..4fa90fc90 --- /dev/null +++ b/src/modules/sport/data/FemaleAthletes.h @@ -0,0 +1,12 @@ +#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 new file mode 100644 index 000000000..d25143564 --- /dev/null +++ b/src/modules/sport/data/MaleAthletes.h @@ -0,0 +1,12 @@ +#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 new file mode 100644 index 000000000..c817b3184 --- /dev/null +++ b/src/modules/sport/data/SoccerTeams.h @@ -0,0 +1,12 @@ +#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 new file mode 100644 index 000000000..f273b3da1 --- /dev/null +++ b/src/modules/sport/data/SportEvents.h @@ -0,0 +1,20 @@ +#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 new file mode 100644 index 000000000..b3ffe1ec0 --- /dev/null +++ b/src/modules/sport/data/SportNames.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace faker +{ +const std::vector sportNames = { + "Soccer", "Cricket", "Basketball", "Tennis", "Volleyball", "Table Tennis", + "Golf", "Baseball", "American Football", "Rugby", +}; +}