Skip to content

Commit

Permalink
Refined Hacker::Phrases and created better Tests + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-bodhi committed Oct 8, 2023
1 parent 2e9078c commit f3f2902
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 157 deletions.
54 changes: 46 additions & 8 deletions src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "faker-cxx/Hacker.h"

#include <string>
#include <vector>

#include "data/Abbreviations.h"
#include "data/Adjectives.h"
Expand All @@ -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<std::string>(faker::abbreviations);
}

std::string Hacker::adjective() {
std::string Hacker::adjective()
{
return faker::Helper::arrayElement<std::string>(faker::adjectives);
}

std::string Hacker::noun() {
std::string Hacker::noun()
{
return faker::Helper::arrayElement<std::string>(faker::nouns);
}

std::string Hacker::verb() {
std::string Hacker::verb()
{
return faker::Helper::arrayElement<std::string>(faker::verbs);
}

std::string Hacker::ingverb() {
std::string Hacker::ingverb()
{
return faker::Helper::arrayElement<std::string>(faker::ingverbs);
}

std::string Hacker::phrase() {
return faker::Helper::arrayElement<std::string>(faker::phrases);
std::string Hacker::phrase()
{
auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement<std::string>(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;
}
}
92 changes: 65 additions & 27 deletions src/modules/hacker/HackerTest.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,109 @@
#include "gtest/gtest.h"
#include "faker-cxx/Hacker.h"

#include <algorithm>
#include <string>
#include <iostream>

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

// 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));
}
27 changes: 7 additions & 20 deletions src/modules/hacker/data/Abbreviations.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@
#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> abbreviations = {
"e.g.",
"i.e.",
"etc.",
"Mr.",
"Mrs.",
"Ms.",
"Dr.",
"Prof.",
"Ave.",
"St.",
"A.M.",
"P.M.",
"USA",
"UK",
"CEO",
"CFO",
};
}
namespace faker
{
const std::vector<std::string> abbreviations = {
"e.g.", "i.e.", "etc.", "Mr.", "Mrs.", "Ms.", "Dr.", "Prof.",
"Ave.", "St.", "A.M.", "P.M.", "USA", "UK", "CEO", "CFO",
};
}
27 changes: 7 additions & 20 deletions src/modules/hacker/data/Adjectives.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@
#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> adjectives = {
"auxiliary",
"primary",
"back-end",
"digital",
"open-source",
"virtual",
"cross-platform",
"redundant",
"online",
"haptic",
"multi-byte",
"bluetooth",
"wireless",
"1080p",
"neural",
"optical",
};
}
namespace faker
{
const std::vector<std::string> adjectives = {
"auxiliary", "primary", "back-end", "digital", "open-source", "virtual", "cross-platform", "redundant",
"online", "haptic", "multi-byte", "bluetooth", "wireless", "1080p", "neural", "optical",
};
}
27 changes: 7 additions & 20 deletions src/modules/hacker/data/Ingverbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@
#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> ingverbs = {
"backing up",
"bypassing",
"hacking",
"overriding",
"compressing",
"copying",
"navigating",
"indexing",
"connecting",
"generating",
"quantifying",
"calculating",
"synthesizing",
"transmitting",
"programming",
"parsing",
};
}
namespace faker
{
const std::vector<std::string> ingverbs = {
"backing up", "bypassing", "hacking", "overriding", "compressing", "copying", "navigating", "indexing",
"connecting", "generating", "quantifying", "calculating", "synthesizing", "transmitting", "programming", "parsing",
};
}
27 changes: 7 additions & 20 deletions src/modules/hacker/data/Nouns.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@
#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> nouns = {
"driver",
"protocol",
"bandwidth",
"panel",
"microchip",
"program",
"port",
"card",
"array",
"interface",
"system",
"sensor",
"firewall",
"hard drive",
"pixel",
"alarm",
};
}
namespace faker
{
const std::vector<std::string> nouns = {
"driver", "protocol", "bandwidth", "panel", "microchip", "program", "port", "card",
"array", "interface", "system", "sensor", "firewall", "hard drive", "pixel", "alarm",
};
}
40 changes: 18 additions & 22 deletions src/modules/hacker/data/Phrases.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#pragma once

#include "faker-cxx/Helper.h"
#include "fmt/format.h"

#include <string>
#include <vector>

#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<std::string> phrases = {
fmt::format("If we {} the {}, we can get to the {} {} through the {} {} {}!", Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
fmt::format("We need to {} the {} {} {}!", Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
fmt::format("Try to {} the {} {}, maybe it will {} the {} {}!", Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(nouns)),
fmt::format("You can't {} the {} without {} the {} {} {}!", Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(ingverbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
fmt::format("Use the {} {} {}, then you can {} the {} {}!", Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(nouns)),
fmt::format("The {} {} is down, {} the {} {} so we can {} the {} {}!", Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
fmt::format("{} the {} won't do anything, we need to {} the {} {} {}!", Helper::arrayElement<std::string>(ingverbs), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
fmt::format("I'll {} the {} {} {}, that should {} the {} {}!", Helper::arrayElement<std::string>(verbs), Helper::arrayElement<std::string>(adjectives), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(nouns), Helper::arrayElement<std::string>(abbreviations), Helper::arrayElement<std::string>(nouns)),
};


//const std::vector<std::string> phrases = {fmt::format("{0}", Helper::arrayElement<std::string>(faker::abbreviations))};
}
namespace faker
{
// use fmt::format() to format each phrase w/ random word

const std::vector<std::string> 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}!",
};
}
Loading

0 comments on commit f3f2902

Please sign in to comment.