Skip to content

Commit

Permalink
refactor: change lorem class to namespace (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
krupalbhat authored Jun 25, 2024
1 parent 87b1f28 commit bcdb101
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
20 changes: 8 additions & 12 deletions include/faker-cxx/Lorem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

#include <string>

namespace faker
namespace faker::lorem
{
class Lorem
{
public:
/**
* @brief Returns a random lorem word.
*
Expand All @@ -16,7 +13,7 @@ class Lorem
* Lorem::word() // "temporibus"
* @endcode
*/
static std::string_view word();
std::string_view word();

/**
* @brief Returns a random lorem words.
Expand All @@ -29,7 +26,7 @@ class Lorem
* Lorem::words() // "qui praesentium pariatur"
* @endcode
*/
static std::string words(unsigned numberOfWords = 3);
std::string words(unsigned numberOfWords = 3);

/**
* @brief Returns a random lorem sentence.
Expand All @@ -43,7 +40,7 @@ class Lorem
* Lorem::sentence() // "Laborum voluptatem officiis est et."
* @endcode
*/
static std::string sentence(unsigned minNumberOfWords = 3, unsigned maxNumberOfWords = 10);
std::string sentence(unsigned minNumberOfWords = 3, unsigned maxNumberOfWords = 10);

/**
* @brief Returns a random lorem sentences.
Expand All @@ -57,7 +54,7 @@ class Lorem
* Lorem::sentences(2, 2) // "Maxime vel numquam quibusdam. Dignissimos ex molestias quam nihil occaecati maiores."
* @endcode
*/
static std::string sentences(unsigned minNumberOfSentences = 2, unsigned maxNumberOfSentences = 6);
std::string sentences(unsigned minNumberOfSentences = 2, unsigned maxNumberOfSentences = 6);

/**
* @brief Generates a slugified text consisting of the given number of hyphen separated words.
Expand All @@ -70,7 +67,7 @@ class Lorem
* Lorem::slug(5) // "delectus-totam-iusto-itaque-placeat"
* @endcode
*/
static std::string slug(unsigned numberOfWords = 3);
std::string slug(unsigned numberOfWords = 3);

/**
* @brief Returns a random lorem paragraph.
Expand All @@ -84,7 +81,7 @@ class Lorem
* Lorem::paragraph() // "Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse."
* @endcode
*/
static std::string paragraph(unsigned minNumberOfSentences = 2, unsigned maxNumberOfSentences = 6);
std::string paragraph(unsigned minNumberOfSentences = 2, unsigned maxNumberOfSentences = 6);

/**
* @brief Returns a random lorem paragraphs.
Expand All @@ -101,6 +98,5 @@ class Lorem
* // Sapiente deleniti et. Ducimus maiores eum. Rem dolorem itaque aliquam."
* @endcode
*/
static std::string paragraphs(unsigned minNumberOfParagraphs = 2, unsigned maxNumberOfParagraphs = 4);
};
std::string paragraphs(unsigned minNumberOfParagraphs = 2, unsigned maxNumberOfParagraphs = 4);
}
16 changes: 8 additions & 8 deletions src/modules/lorem/Lorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#include "faker-cxx/Number.h"
#include "LoremData.h"

namespace faker
namespace faker::lorem
{
std::string_view Lorem::word()
std::string_view word()
{
return Helper::arrayElement(loremWords);
}

std::string Lorem::words(unsigned numberOfWords)
std::string words(unsigned numberOfWords)
{
std::vector<std::string_view> words;
words.reserve(numberOfWords);
Expand All @@ -31,7 +31,7 @@ std::string Lorem::words(unsigned numberOfWords)
return StringHelper::join(words, " ");
}

std::string Lorem::sentence(unsigned minNumberOfWords, unsigned maxNumberOfWords)
std::string sentence(unsigned minNumberOfWords, unsigned maxNumberOfWords)
{
const std::integral auto numberOfWords = number::integer(minNumberOfWords, maxNumberOfWords);

Expand All @@ -40,7 +40,7 @@ std::string Lorem::sentence(unsigned minNumberOfWords, unsigned maxNumberOfWords
return FormatHelper::format("{}{}.", static_cast<char>(std::toupper(sentenceWords[0])), sentenceWords.substr(1));
}

std::string Lorem::sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentences)
std::string sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentences)
{
const std::integral auto numberOfSentences = number::integer(minNumberOfSentences, maxNumberOfSentences);

Expand All @@ -55,7 +55,7 @@ std::string Lorem::sentences(unsigned minNumberOfSentences, unsigned maxNumberOf
return StringHelper::joinString(sentences, " ");
}

std::string Lorem::slug(unsigned int numberOfWords)
std::string slug(unsigned int numberOfWords)
{
std::vector<std::string> words;
words.reserve(numberOfWords);
Expand All @@ -68,12 +68,12 @@ std::string Lorem::slug(unsigned int numberOfWords)
return StringHelper::joinString(words, "-");
}

std::string Lorem::paragraph(unsigned int minNumberOfSentences, unsigned int maxNumberOfSentences)
std::string paragraph(unsigned int minNumberOfSentences, unsigned int maxNumberOfSentences)
{
return sentences(minNumberOfSentences, maxNumberOfSentences);
}

std::string Lorem::paragraphs(unsigned int minNumberOfParagraphs, unsigned int maxNumberOfParagraphs)
std::string paragraphs(unsigned int minNumberOfParagraphs, unsigned int maxNumberOfParagraphs)
{
const std::integral auto numberOfParagraphs = number::integer(minNumberOfParagraphs, maxNumberOfParagraphs);

Expand Down
2 changes: 1 addition & 1 deletion src/modules/lorem/LoremData.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::lorem
{
const auto loremWords = std::to_array<std::string_view>({
"alias",
Expand Down
45 changes: 23 additions & 22 deletions tests/modules/lorem/LoremTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "lorem/LoremData.h"

using namespace ::testing;
using namespace faker;
using namespace faker::lorem;

class LoremTest : public Test
{
Expand All @@ -19,7 +19,7 @@ class LoremTest : public Test

TEST_F(LoremTest, shouldGenerateWord)
{
const auto generatedWord = Lorem::word();
const auto generatedWord = word();

ASSERT_TRUE(std::ranges::any_of(loremWords,
[generatedWord](const std::string_view& word) { return word == generatedWord; }));
Expand All @@ -29,9 +29,10 @@ TEST_F(LoremTest, shouldGenerateWords)
{
const auto numberOfWords = 5;

const auto generatedWords = Lorem::words(numberOfWords);
const auto generatedWords = words(numberOfWords);

const auto separatedWords = StringHelper::split(generatedWords, " ");
const auto separatedWords = faker::StringHelper::split(generatedWords, " ");


ASSERT_EQ(separatedWords.size(), numberOfWords);
ASSERT_TRUE(std::ranges::all_of(
Expand All @@ -46,14 +47,14 @@ TEST_F(LoremTest, shouldGenerateWords)

TEST_F(LoremTest, shouldGenerateSentence)
{
const auto sentence = Lorem::sentence();
const auto generatedSentence = sentence();

const auto sentenceWithoutEndingDot = sentence.substr(0, sentence.size() - 1);
const auto sentenceWithoutEndingDot = generatedSentence.substr(0, generatedSentence.size() - 1);

const auto sentenceWords = StringHelper::split(sentenceWithoutEndingDot, " ");
const auto sentenceWords = faker::StringHelper::split(sentenceWithoutEndingDot, " ");

ASSERT_TRUE(std::isupper(sentence[0]));
ASSERT_TRUE(sentence.ends_with("."));
ASSERT_TRUE(std::isupper(generatedSentence[0]));
ASSERT_TRUE(generatedSentence.ends_with("."));
ASSERT_TRUE(sentenceWords.size() >= 3 && sentenceWords.size() <= 10);
ASSERT_TRUE(std::ranges::all_of(
sentenceWords,
Expand All @@ -67,17 +68,17 @@ TEST_F(LoremTest, shouldGenerateSentence)

TEST_F(LoremTest, shouldGenerateSentences)
{
const auto sentences = Lorem::sentences();
const auto generatedSentences = sentences();

const auto separatedSentences = StringHelper::split(sentences, ". ");
const auto separatedSentences = faker::StringHelper::split(generatedSentences, ". ");

for (auto sentence : separatedSentences)
{
sentence.erase(std::remove(sentence.begin(), sentence.end(), '.'), sentence.end());

ASSERT_TRUE(std::isupper(sentence[0]));

const auto sentenceWords = StringHelper::split(sentence, " ");
const auto sentenceWords = faker::StringHelper::split(sentence, " ");

ASSERT_TRUE(sentenceWords.size() >= 3 && sentenceWords.size() <= 10);

Expand All @@ -94,9 +95,9 @@ TEST_F(LoremTest, shouldGenerateSentences)

TEST_F(LoremTest, shouldGenerateSlug)
{
const auto generatedSlug = Lorem::slug(3);
const auto generatedSlug = slug(3);

const auto separatedWords = StringHelper::split(generatedSlug, "-");
const auto separatedWords = faker::StringHelper::split(generatedSlug, "-");

ASSERT_EQ(separatedWords.size(), 3);
ASSERT_TRUE(std::ranges::all_of(
Expand All @@ -111,17 +112,17 @@ TEST_F(LoremTest, shouldGenerateSlug)

TEST_F(LoremTest, shouldGenerateParagraph)
{
const auto paragraph = Lorem::paragraph();
const auto generatedParagraph = paragraph();

const auto separatedSentences = StringHelper::split(paragraph, ". ");
const auto separatedSentences = faker::StringHelper::split(generatedParagraph, ". ");

for (auto sentence : separatedSentences)
{
sentence.erase(std::remove(sentence.begin(), sentence.end(), '.'), sentence.end());

ASSERT_TRUE(std::isupper(sentence[0]));

const auto sentenceWords = StringHelper::split(sentence, " ");
const auto sentenceWords = faker::StringHelper::split(sentence, " ");

ASSERT_TRUE(sentenceWords.size() >= 3 && sentenceWords.size() <= 10);

Expand All @@ -138,21 +139,21 @@ TEST_F(LoremTest, shouldGenerateParagraph)

TEST_F(LoremTest, shouldGenerateParagraphs)
{
const auto paragraphs = Lorem::paragraphs();
const auto generatedParagraphs = paragraphs();

const auto separatedParagraphs = StringHelper::split(paragraphs, "\n");
const auto separatedParagraphs = faker::StringHelper::split(generatedParagraphs, "\n");

for (const auto& paragraph : separatedParagraphs)
for (const auto& generatedParagraph : separatedParagraphs)
{
const auto separatedSentences = StringHelper::split(paragraph, ". ");
const auto separatedSentences = faker::StringHelper::split(generatedParagraph, ". ");

for (auto sentence : separatedSentences)
{
sentence.erase(std::remove(sentence.begin(), sentence.end(), '.'), sentence.end());

ASSERT_TRUE(std::isupper(sentence[0]));

const auto sentenceWords = StringHelper::split(sentence, " ");
const auto sentenceWords = faker::StringHelper::split(sentence, " ");

ASSERT_TRUE(sentenceWords.size() >= 3 && sentenceWords.size() <= 10);

Expand Down

0 comments on commit bcdb101

Please sign in to comment.