Skip to content

Commit

Permalink
Feature/sport (#198)
Browse files Browse the repository at this point in the history
* Removed uneeded functions and moved some to StringHelper

* Added Sport Module
  • Loading branch information
eric-bodhi authored Oct 10, 2023
1 parent d6e26e1 commit 51ba70d
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 100 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
41 changes: 0 additions & 41 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <functional>
#include <span>
#include <string>
#include <vector>

#include "Datatype.h"
#include "Number.h"
Expand Down Expand Up @@ -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<std::string> 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;
Expand Down
65 changes: 65 additions & 0 deletions include/faker-cxx/Sport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <string>

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();
};
}
14 changes: 14 additions & 0 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/common/StringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ class StringHelper
static std::string join(const std::vector<std::string>& 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);
};
}
22 changes: 22 additions & 0 deletions src/common/StringHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
5 changes: 3 additions & 2 deletions src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "faker-cxx/Hacker.h"
#include "../../common/StringHelper.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -39,12 +40,12 @@ std::string Hacker::ingverb()

std::string Hacker::phrase()
{
auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement<std::string>(faker::phrases));
auto splitRandomPhrase = StringHelper::split(faker::Helper::arrayElement<std::string>(faker::phrases));
std::string ret;

for (auto& word : splitRandomPhrase)
{
word = Helper::removePunctuation(word);
word = StringHelper::removePunctuation(word);
if (word == "{abbreviation}")
{
word = abbreviation();
Expand Down
2 changes: 0 additions & 2 deletions src/modules/hacker/HackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "faker-cxx/Helper.h"

#include <chrono>
#include <iterator>
#include <random>
#include <regex>
#include <string>
#include <string_view>
#include <vector>

#include "../src/common/LuhnCheck.h"
Expand Down Expand Up @@ -107,26 +105,4 @@ std::string Helper::regexpStyleStringParse(const std::string& input)

return string;
}

// helper functions for phrases
std::vector<std::string> Helper::splitIntoWords(const std::string& input)
{
std::istringstream iss(input);
std::vector<std::string> words(std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>());
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;
}
}
31 changes: 0 additions & 31 deletions src/modules/helper/HelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,4 @@ TEST_F(HelperTest, MaybeDouble)
result = Helper::maybe<double>([]() { 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<std::string> result = Helper::splitIntoWords(input);
EXPECT_EQ(result.size(), 2);
EXPECT_EQ(result[0], "Hello");
EXPECT_EQ(result[1], "World!");
}
}
39 changes: 39 additions & 0 deletions src/modules/sport/Sport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "faker-cxx/Sport.h"
#include "faker-cxx/Helper.h"


#include <string>

#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<std::string>(sportNames);
}

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

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

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

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

#include <algorithm>
#include <string>

#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; }));
}
Loading

0 comments on commit 51ba70d

Please sign in to comment.