From c7512911c0ef4976946ad76e3672d6ba9c4cd56e Mon Sep 17 00:00:00 2001 From: Krupal J S Bhat Date: Tue, 25 Jun 2024 21:04:34 +0530 Subject: [PATCH] refactor: change lorem class to namespace --- include/faker-cxx/Lorem.h | 20 ++++++-------- src/modules/lorem/Lorem.cpp | 16 +++++------ src/modules/lorem/LoremData.h | 2 +- tests/modules/lorem/LoremTest.cpp | 45 ++++++++++++++++--------------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/include/faker-cxx/Lorem.h b/include/faker-cxx/Lorem.h index d7cd5d440..4986a532d 100644 --- a/include/faker-cxx/Lorem.h +++ b/include/faker-cxx/Lorem.h @@ -2,11 +2,8 @@ #include -namespace faker +namespace faker::lorem { -class Lorem -{ -public: /** * @brief Returns a random lorem word. * @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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); } diff --git a/src/modules/lorem/Lorem.cpp b/src/modules/lorem/Lorem.cpp index 20d6f35cb..178df5236 100644 --- a/src/modules/lorem/Lorem.cpp +++ b/src/modules/lorem/Lorem.cpp @@ -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 words; words.reserve(numberOfWords); @@ -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); @@ -40,7 +40,7 @@ std::string Lorem::sentence(unsigned minNumberOfWords, unsigned maxNumberOfWords return FormatHelper::format("{}{}.", static_cast(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); @@ -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 words; words.reserve(numberOfWords); @@ -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); diff --git a/src/modules/lorem/LoremData.h b/src/modules/lorem/LoremData.h index 48cbe6857..39b08cb2e 100644 --- a/src/modules/lorem/LoremData.h +++ b/src/modules/lorem/LoremData.h @@ -2,7 +2,7 @@ #include #include -namespace faker +namespace faker::lorem { const auto loremWords = std::to_array({ "alias", diff --git a/tests/modules/lorem/LoremTest.cpp b/tests/modules/lorem/LoremTest.cpp index d78cbb0e8..bc858c478 100644 --- a/tests/modules/lorem/LoremTest.cpp +++ b/tests/modules/lorem/LoremTest.cpp @@ -10,7 +10,7 @@ #include "lorem/LoremData.h" using namespace ::testing; -using namespace faker; +using namespace faker::lorem; class LoremTest : public Test { @@ -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; })); @@ -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( @@ -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, @@ -67,9 +68,9 @@ 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) { @@ -77,7 +78,7 @@ TEST_F(LoremTest, shouldGenerateSentences) 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); @@ -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( @@ -111,9 +112,9 @@ 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) { @@ -121,7 +122,7 @@ TEST_F(LoremTest, shouldGenerateParagraph) 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); @@ -138,13 +139,13 @@ 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) { @@ -152,7 +153,7 @@ TEST_F(LoremTest, shouldGenerateParagraphs) 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);