diff --git a/src/modules/hacker/Hacker.cpp b/src/modules/hacker/Hacker.cpp index 3c143fe4d..5dfdfe0a8 100644 --- a/src/modules/hacker/Hacker.cpp +++ b/src/modules/hacker/Hacker.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/Hacker.h" #include +#include #include "data/Abbreviations.h" #include "data/Adjectives.h" @@ -9,28 +10,65 @@ #include "data/Phrases.h" #include "data/Verbs.h" -namespace faker { -std::string Hacker::abbreviation() { +namespace faker +{ +std::string Hacker::abbreviation() +{ return faker::Helper::arrayElement(faker::abbreviations); } -std::string Hacker::adjective() { +std::string Hacker::adjective() +{ return faker::Helper::arrayElement(faker::adjectives); } -std::string Hacker::noun() { +std::string Hacker::noun() +{ return faker::Helper::arrayElement(faker::nouns); } -std::string Hacker::verb() { +std::string Hacker::verb() +{ return faker::Helper::arrayElement(faker::verbs); } -std::string Hacker::ingverb() { +std::string Hacker::ingverb() +{ return faker::Helper::arrayElement(faker::ingverbs); } -std::string Hacker::phrase() { - return faker::Helper::arrayElement(faker::phrases); +std::string Hacker::phrase() +{ + auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement(faker::phrases)); + std::string ret; + + for (auto& word : splitRandomPhrase) + { + word = Helper::removePunctuation(word); + if (word == "{abbreviation}") + { + word = abbreviation(); + } + else if (word == "{adjective}") + { + word = adjective(); + } + else if (word == "{noun}") + { + word = noun(); + } + else if (word == "{verb}") + { + word = verb(); + } + else if (word == "{ingverb}") + { + word = ingverb(); + } + + ret += word + " "; + } + + return ret; } } diff --git a/src/modules/hacker/HackerTest.cpp b/src/modules/hacker/HackerTest.cpp index 0a594b1b8..f6f4aac53 100644 --- a/src/modules/hacker/HackerTest.cpp +++ b/src/modules/hacker/HackerTest.cpp @@ -1,71 +1,109 @@ -#include "gtest/gtest.h" #include "faker-cxx/Hacker.h" #include #include -#include + +#include "gtest/gtest.h" #include "data/Abbreviations.h" #include "data/Adjectives.h" #include "data/Ingverbs.h" #include "data/Nouns.h" -#include "data/Verbs.h" #include "data/Phrases.h" - -// testing Hacker module, test like Person module tests -// testing phrases will be different, test that each phrase will have a random word from each word list +#include "data/Verbs.h" +#include "faker-cxx/Helper.h" using namespace ::testing; using namespace faker; -class HackerTest : public Test { +class HackerTest : public Test +{ public: }; -TEST_F(HackerTest, shouldGenerateAbbreviation) { +TEST_F(HackerTest, shouldGenerateAbbreviation) +{ std::string generatedAbbreviation = Hacker::abbreviation(); - ASSERT_TRUE(std::ranges::any_of(abbreviations, [generatedAbbreviation](const std::string& abbreviation) { return abbreviation == generatedAbbreviation; })); + ASSERT_TRUE(std::ranges::any_of(abbreviations, [generatedAbbreviation](const std::string& abbreviation) + { return abbreviation == generatedAbbreviation; })); } -TEST_F(HackerTest, shouldGenerateAdjective) { +TEST_F(HackerTest, shouldGenerateAdjective) +{ std::string generatedAdjective = Hacker::adjective(); - ASSERT_TRUE(std::ranges::any_of(adjectives, [generatedAdjective](const std::string& adjective) { return adjective == generatedAdjective; })); + ASSERT_TRUE(std::ranges::any_of(adjectives, [generatedAdjective](const std::string& adjective) + { return adjective == generatedAdjective; })); } -TEST_F(HackerTest, shouldGenerateNoun) { +TEST_F(HackerTest, shouldGenerateNoun) +{ std::string generatedNoun = Hacker::noun(); ASSERT_TRUE(std::ranges::any_of(nouns, [generatedNoun](const std::string& noun) { return noun == generatedNoun; })); } -TEST_F(HackerTest, shouldGenerateVerb) { +TEST_F(HackerTest, shouldGenerateVerb) +{ std::string generatedVerb = Hacker::verb(); ASSERT_TRUE(std::ranges::any_of(verbs, [generatedVerb](const std::string& verb) { return verb == generatedVerb; })); } -TEST_F(HackerTest, shouldGenerateIngverb) { +TEST_F(HackerTest, shouldGenerateIngverb) +{ std::string generatedIngverb = Hacker::ingverb(); - ASSERT_TRUE(std::ranges::any_of(ingverbs, [generatedIngverb](const std::string& ingverb) { return ingverb == generatedIngverb; })); + ASSERT_TRUE(std::ranges::any_of(ingverbs, [generatedIngverb](const std::string& ingverb) + { return ingverb == generatedIngverb; })); } -TEST_F(HackerTest, shouldGeneratePhrase) { +TEST_F(HackerTest, shouldGeneratePhrase) +{ std::string generatedPhrase = Hacker::phrase(); - size_t limit = 1000000; - size_t attempt = 0; - - while (attempt <= limit) { - std::string newGeneratedPhrase = Hacker::phrase(); - if (newGeneratedPhrase == generatedPhrase) { - std::cout << generatedPhrase << "\n"; - std::cout << newGeneratedPhrase << "\n"; - ASSERT_TRUE(true); + bool hasAdjective, hasNoun, hasVerb, hasAbbreviation; + hasAdjective = hasNoun = hasVerb = hasAbbreviation = false; + + // Check for adjectives + for (const std::string& adj : adjectives) + { + if (generatedPhrase.find(adj) != std::string::npos) + { + hasAdjective = true; break; } + } - attempt++; + // Check for nouns + for (const std::string& noun : nouns) + { + if (generatedPhrase.find(noun) != std::string::npos) + { + hasNoun = true; + break; + } } -} \ No newline at end of file + + // Check for verbs + for (const std::string& verb : verbs) + { + if (generatedPhrase.find(verb) != std::string::npos) + { + hasVerb = true; + break; + } + } + + // Check for abbreviations + for (const std::string& abbreviation : abbreviations) + { + if (generatedPhrase.find(abbreviation) != std::string::npos) + { + hasAbbreviation = true; + break; + } + } + + ASSERT_TRUE((hasAdjective && hasNoun && hasVerb && hasAbbreviation)); +} diff --git a/src/modules/hacker/data/Abbreviations.h b/src/modules/hacker/data/Abbreviations.h index ad625b8d6..a048b9278 100644 --- a/src/modules/hacker/data/Abbreviations.h +++ b/src/modules/hacker/data/Abbreviations.h @@ -3,23 +3,10 @@ #include #include -namespace faker { - const std::vector abbreviations = { - "e.g.", - "i.e.", - "etc.", - "Mr.", - "Mrs.", - "Ms.", - "Dr.", - "Prof.", - "Ave.", - "St.", - "A.M.", - "P.M.", - "USA", - "UK", - "CEO", - "CFO", - }; -} \ No newline at end of file +namespace faker +{ +const std::vector abbreviations = { + "e.g.", "i.e.", "etc.", "Mr.", "Mrs.", "Ms.", "Dr.", "Prof.", + "Ave.", "St.", "A.M.", "P.M.", "USA", "UK", "CEO", "CFO", +}; +} diff --git a/src/modules/hacker/data/Adjectives.h b/src/modules/hacker/data/Adjectives.h index 52ce59dd5..6cacc6a6c 100644 --- a/src/modules/hacker/data/Adjectives.h +++ b/src/modules/hacker/data/Adjectives.h @@ -3,23 +3,10 @@ #include #include -namespace faker { - const std::vector adjectives = { - "auxiliary", - "primary", - "back-end", - "digital", - "open-source", - "virtual", - "cross-platform", - "redundant", - "online", - "haptic", - "multi-byte", - "bluetooth", - "wireless", - "1080p", - "neural", - "optical", - }; -} \ No newline at end of file +namespace faker +{ +const std::vector adjectives = { + "auxiliary", "primary", "back-end", "digital", "open-source", "virtual", "cross-platform", "redundant", + "online", "haptic", "multi-byte", "bluetooth", "wireless", "1080p", "neural", "optical", +}; +} diff --git a/src/modules/hacker/data/Ingverbs.h b/src/modules/hacker/data/Ingverbs.h index fc8c9e54c..8afe2c55b 100644 --- a/src/modules/hacker/data/Ingverbs.h +++ b/src/modules/hacker/data/Ingverbs.h @@ -3,23 +3,10 @@ #include #include -namespace faker { - const std::vector ingverbs = { - "backing up", - "bypassing", - "hacking", - "overriding", - "compressing", - "copying", - "navigating", - "indexing", - "connecting", - "generating", - "quantifying", - "calculating", - "synthesizing", - "transmitting", - "programming", - "parsing", - }; -} \ No newline at end of file +namespace faker +{ +const std::vector ingverbs = { + "backing up", "bypassing", "hacking", "overriding", "compressing", "copying", "navigating", "indexing", + "connecting", "generating", "quantifying", "calculating", "synthesizing", "transmitting", "programming", "parsing", +}; +} diff --git a/src/modules/hacker/data/Nouns.h b/src/modules/hacker/data/Nouns.h index 2f3ec37b5..e446dc9b2 100644 --- a/src/modules/hacker/data/Nouns.h +++ b/src/modules/hacker/data/Nouns.h @@ -3,23 +3,10 @@ #include #include -namespace faker { - const std::vector nouns = { - "driver", - "protocol", - "bandwidth", - "panel", - "microchip", - "program", - "port", - "card", - "array", - "interface", - "system", - "sensor", - "firewall", - "hard drive", - "pixel", - "alarm", - }; -} \ No newline at end of file +namespace faker +{ +const std::vector nouns = { + "driver", "protocol", "bandwidth", "panel", "microchip", "program", "port", "card", + "array", "interface", "system", "sensor", "firewall", "hard drive", "pixel", "alarm", +}; +} diff --git a/src/modules/hacker/data/Phrases.h b/src/modules/hacker/data/Phrases.h index f5d3c14bc..36c86d639 100644 --- a/src/modules/hacker/data/Phrases.h +++ b/src/modules/hacker/data/Phrases.h @@ -1,32 +1,28 @@ #pragma once -#include "faker-cxx/Helper.h" -#include "fmt/format.h" - #include #include #include "Abbreviations.h" +#include "Adjectives.h" +#include "faker-cxx/Helper.h" +#include "fmt/format.h" #include "Ingverbs.h" #include "Nouns.h" #include "Verbs.h" -#include "Adjectives.h" - - -namespace faker { - // use fmt::format() to format each phrase w/ random word - - const std::vector phrases = { - fmt::format("If we {} the {}, we can get to the {} {} through the {} {} {}!", Helper::arrayElement(verbs), Helper::arrayElement(nouns), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns), Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - fmt::format("We need to {} the {} {} {}!", Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - fmt::format("Try to {} the {} {}, maybe it will {} the {} {}!", Helper::arrayElement(verbs), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns), Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(nouns)), - fmt::format("You can't {} the {} without {} the {} {} {}!", Helper::arrayElement(verbs), Helper::arrayElement(nouns), Helper::arrayElement(ingverbs), Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - fmt::format("Use the {} {} {}, then you can {} the {} {}!", Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns), Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(nouns)), - fmt::format("The {} {} is down, {} the {} {} so we can {} the {} {}!", Helper::arrayElement(abbreviations), Helper::arrayElement(nouns), Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(nouns), Helper::arrayElement(verbs), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - fmt::format("{} the {} won't do anything, we need to {} the {} {} {}!", Helper::arrayElement(ingverbs), Helper::arrayElement(nouns), Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - fmt::format("I'll {} the {} {} {}, that should {} the {} {}!", Helper::arrayElement(verbs), Helper::arrayElement(adjectives), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns), Helper::arrayElement(nouns), Helper::arrayElement(abbreviations), Helper::arrayElement(nouns)), - }; - - //const std::vector phrases = {fmt::format("{0}", Helper::arrayElement(faker::abbreviations))}; -} \ No newline at end of file +namespace faker +{ +// use fmt::format() to format each phrase w/ random word + +const std::vector phrases = { + "If we {verb} the {noun}, we can get to the {abbreviation} {noun} through the {adjective} {abbreviation} {noun}!", + "We need to {verb} the {adjective} {abbreviation} {noun}!", + "Try to {verb} the {abbreviation} {noun}, maybe it will {verb} the {adjective} {noun}!", + "You can't {verb} the {noun} without {ingverb} the {adjective} {abbreviation} {noun}!", + "Use the {adjective} {abbreviation} {noun}, then you can {verb} the {adjective} {noun}!", + "The {abbreviation} {noun} is down, {verb} the {adjective} {noun} so we can {verb} the {abbreviation} {noun}!", + "{ingverb} the {noun} won't do anything, we need to {verb} the {adjective} {abbreviation} {noun}!", + "I'll {verb} the {adjective} {abbreviation} {noun}, that should {noun} the {abbreviation} {noun}!", +}; +} diff --git a/src/modules/hacker/data/Verbs.h b/src/modules/hacker/data/Verbs.h index bffd4681b..f62bde23f 100644 --- a/src/modules/hacker/data/Verbs.h +++ b/src/modules/hacker/data/Verbs.h @@ -3,23 +3,10 @@ #include #include -namespace faker { - const std::vector verbs = { - "back up", - "bypass", - "hack", - "override", - "compress", - "copy", - "navigate", - "index", - "connect", - "generate", - "quantify", - "calculate", - "synthesize", - "transmit", - "program", - "parse", - }; -} \ No newline at end of file +namespace faker +{ +const std::vector verbs = { + "back up", "bypass", "hack", "override", "compress", "copy", "navigate", "index", + "connect", "generate", "quantify", "calculate", "synthesize", "transmit", "program", "parse", +}; +} diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index 463f4fa51..a76864768 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -1,9 +1,12 @@ #include "faker-cxx/Helper.h" #include +#include #include #include +#include #include +#include #include "../src/common/LuhnCheck.h" #include "../src/common/StringHelper.h" @@ -104,4 +107,26 @@ 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 b58053a1c..e851719e9 100644 --- a/src/modules/helper/HelperTest.cpp +++ b/src/modules/helper/HelperTest.cpp @@ -107,4 +107,35 @@ 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!"); +} }