From af7f3e367cf92c3092f51645386d110bfd4c50fb Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Tue, 1 Oct 2024 17:52:51 -0300 Subject: [PATCH 1/7] adding parametrized test --- include/faker-cxx/word.h | 37 ++-- src/modules/word.cpp | 68 ++++---- tests/modules/string_test.cpp | 26 +-- tests/modules/word_test.cpp | 314 ++++++++++++++++++++++------------ 4 files changed, 276 insertions(+), 169 deletions(-) diff --git a/include/faker-cxx/word.h b/include/faker-cxx/word.h index 3f5a00ca..5590a9e4 100644 --- a/include/faker-cxx/word.h +++ b/include/faker-cxx/word.h @@ -27,7 +27,7 @@ namespace faker::word * faker::word::sample(5) // "spell" * @endcode */ -FAKER_CXX_EXPORT std::string_view sample(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view sample(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** @@ -44,7 +44,7 @@ FAKER_CXX_EXPORT std::string_view sample(std::optional length = std::n * @endcode */ -FAKER_CXX_EXPORT std::string_view sampleLocale(unsigned length = 0,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view sampleLocale(unsigned length = 0,const Locale locale = Locale::en_US); /** * @brief Returns a string containing a number of space separated random words. @@ -59,7 +59,7 @@ FAKER_CXX_EXPORT std::string_view sampleLocale(unsigned length = 0,const faker:: * faker::word::words(5) // "before hourly patiently dribble equal" * @endcode */ -FAKER_CXX_EXPORT std::string words(unsigned numberOfWords = 1,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string words(unsigned numberOfWords = 1,const Locale locale = Locale::en_US); /** @@ -75,7 +75,7 @@ FAKER_CXX_EXPORT std::string words(unsigned numberOfWords = 1,const faker::Local * faker::word::wordsLocale(5,faker::locale::en_US) // "before hourly patiently dribble equal" * @endcode */ -FAKER_CXX_EXPORT std::string wordsLocale(unsigned numberOfWords = 1,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string wordsLocale(unsigned numberOfWords = 1,const Locale locale = Locale::en_US); /** * @brief Returns a random adjective. @@ -91,7 +91,7 @@ FAKER_CXX_EXPORT std::string wordsLocale(unsigned numberOfWords = 1,const faker: * faker::word::adjective(3) // "bad" * @endcode */ -FAKER_CXX_EXPORT std::string_view adjective(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view adjective(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** @@ -107,7 +107,7 @@ FAKER_CXX_EXPORT std::string_view adjective(std::optional length = std * faker::word::adjective(3) // "bad" * @endcode */ -FAKER_CXX_EXPORT std::string_view adjectiveLocale(unsigned length = 0,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view adjectiveLocale(unsigned length = 0,const Locale locale = Locale::en_US); /** * @brief Returns a random adverb. @@ -123,7 +123,8 @@ FAKER_CXX_EXPORT std::string_view adjectiveLocale(unsigned length = 0,const fake * faker::word::adverb(5) // "almost" * @endcode */ -FAKER_CXX_EXPORT std::string_view adverb(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view adverb(std::optional length = std::nullopt,const Locale locale = Locale::en_US); + /** @@ -140,7 +141,7 @@ FAKER_CXX_EXPORT std::string_view adverb(std::optional length = std::n * faker::word::adverbLocale(5) // "almost" * @endcode */ -FAKER_CXX_EXPORT std::string_view adverbLocale(unsigned length = 0,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view adverbLocale(unsigned length = 0,const Locale locale = Locale::en_US); /** * @brief Returns a random conjunction. @@ -156,7 +157,7 @@ FAKER_CXX_EXPORT std::string_view adverbLocale(unsigned length = 0,const faker:: * faker::word::conjunction(6) // "indeed" * @endcode */ -FAKER_CXX_EXPORT std::string_view conjunction(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view conjunction(std::optional length = std::nullopt,const Locale locale = Locale::en_US); @@ -174,7 +175,7 @@ FAKER_CXX_EXPORT std::string_view conjunction(std::optional length = s * faker::word::conjunctionLocale(6) // "indeed" * @endcode */ -FAKER_CXX_EXPORT std::string_view conjunctionLocale(unsigned int length=0, const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view conjunctionLocale(unsigned int length=0, const Locale locale = Locale::en_US); /** @@ -191,7 +192,7 @@ FAKER_CXX_EXPORT std::string_view conjunctionLocale(unsigned int length=0, const * faker::word::interjection(4) // "yuck" * @endcode */ -FAKER_CXX_EXPORT std::string_view interjection(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view interjection(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** * @brief Returns a random interjection. @@ -207,7 +208,7 @@ FAKER_CXX_EXPORT std::string_view interjection(std::optional length = * faker::word::interjectionLocale(4) // "yuck" * @endcode */ -FAKER_CXX_EXPORT std::string_view interjectionLocale(unsigned int length=0, const faker::Locale locale = Locale::en_US) ; +FAKER_CXX_EXPORT std::string_view interjectionLocale(unsigned int length=0, const Locale locale = Locale::en_US) ; /** * @brief Returns a random noun. @@ -223,7 +224,7 @@ FAKER_CXX_EXPORT std::string_view interjectionLocale(unsigned int length=0, con * faker::word::noun(8) // "distance" * @endcode */ -FAKER_CXX_EXPORT std::string_view noun(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view noun(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** @@ -240,7 +241,7 @@ FAKER_CXX_EXPORT std::string_view noun(std::optional length = std::nul * faker::word::nounLocale(8) // "distance" * @endcode */ -FAKER_CXX_EXPORT std::string_view nounLocale(unsigned int length=0, const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view nounLocale(unsigned int length=0, const Locale locale = Locale::en_US); /** * @brief Returns a random preposition. @@ -256,7 +257,7 @@ FAKER_CXX_EXPORT std::string_view nounLocale(unsigned int length=0, const faker: * faker::word::preposition(4) // "from" * @endcode */ -FAKER_CXX_EXPORT std::string_view preposition(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view preposition(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** * @brief Returns a random preposition. @@ -272,7 +273,7 @@ FAKER_CXX_EXPORT std::string_view preposition(std::optional length = s * faker::word::prepositionLocale(4) // "from" * @endcode */ -FAKER_CXX_EXPORT std::string_view prepositionLocale(unsigned int length=0, const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view prepositionLocale(unsigned int length=0, const Locale locale = Locale::en_US); /** * @brief Returns a random verb. @@ -288,7 +289,7 @@ FAKER_CXX_EXPORT std::string_view prepositionLocale(unsigned int length=0, const * faker::word::verb(9) // "stabilise" * @endcode */ -FAKER_CXX_EXPORT std::string_view verb(std::optional length = std::nullopt,const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view verb(std::optional length = std::nullopt,const Locale locale = Locale::en_US); /** @@ -305,7 +306,7 @@ FAKER_CXX_EXPORT std::string_view verb(std::optional length = std::nul * faker::word::verbLocale(9) // "stabilise" * @endcode */ -FAKER_CXX_EXPORT std::string_view verbLocale(unsigned int length=0, const faker::Locale locale = Locale::en_US); +FAKER_CXX_EXPORT std::string_view verbLocale(unsigned int length=0, const Locale locale = Locale::en_US); /** * @brief Returns random element of length diff --git a/src/modules/word.cpp b/src/modules/word.cpp index ed6243b3..08eb58a1 100644 --- a/src/modules/word.cpp +++ b/src/modules/word.cpp @@ -11,7 +11,7 @@ namespace faker::word { -std::string_view sample( std::optional length,const faker::Locale locale) +std::string_view sample( std::optional length,const Locale locale) { if(length) return sampleLocale(length.value(),locale); @@ -20,7 +20,7 @@ std::string_view sample( std::optional length,const faker::Locale } -std::string_view sampleLocale(unsigned int length, const faker::Locale locale) +std::string_view sampleLocale(unsigned int length, const Locale locale) { if(length==0){ length=100; @@ -39,7 +39,7 @@ std::string_view sampleLocale(unsigned int length, const faker::Locale locale) } } -std::string words(unsigned numberOfWords,const faker::Locale locale) +std::string words(unsigned numberOfWords,const Locale locale) { if (numberOfWords == 0) { @@ -48,16 +48,16 @@ std::string words(unsigned numberOfWords,const faker::Locale locale) return wordsLocale(numberOfWords,locale); } -std::string wordsLocale(unsigned numberOfWords,faker::Locale locale) +std::string wordsLocale(unsigned numberOfWords, const Locale locale) { auto localeExt = locale; if (numberOfWords == 0) { return ""; - } - if(_allWords_map.find(locale)==_allWords_map.end()) + } + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { - localeExt = faker::Locale::en_US; + localeExt = Locale::en_US; } std::string combined_words; @@ -102,7 +102,7 @@ std::string wordsLocale(unsigned numberOfWords,faker::Locale locale) return combined_words; } -std::string_view adjective(std::optional length,const faker::Locale locale) +std::string_view adjective(std::optional length,const Locale locale) { if(length) return adjectiveLocale(length.value(),locale); @@ -110,7 +110,7 @@ std::string_view adjective(std::optional length,const faker::Local return adjectiveLocale(0,locale); } -std::string_view adjectiveLocale(unsigned int length, const faker::Locale locale) +std::string_view adjectiveLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -118,15 +118,21 @@ std::string_view adjectiveLocale(unsigned int length, const faker::Locale locale } auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted= _adjetives_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } -std::string_view adverb(std::optional length, const faker::Locale locale) + +std::string_view adverb(const Locale locale) +{ + return adverbLocale(0,locale); +} + +std::string_view adverb(std::optional length, const Locale locale) { //return sortedSizeRandomElement(length, _adverbs_sorted); if(length) @@ -135,17 +141,17 @@ std::string_view adverb(std::optional length, const faker::Locale return adverbLocale(0,locale); } -std::string_view adverbLocale(unsigned int length, const faker::Locale locale) +std::string_view adverbLocale(unsigned int length, const Locale locale) { if(length==0) { length=100; - } - auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) - { - localeLocal = faker::Locale::en_US; } + Locale localeLocal = locale; + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) + { + localeLocal = Locale::en_US; + } auto sorted= _adverbs_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } @@ -159,7 +165,7 @@ std::string_view conjunction(std::optional length, const faker::Lo } -std::string_view conjunctionLocale(unsigned int length, const faker::Locale locale) +std::string_view conjunctionLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -168,7 +174,7 @@ std::string_view conjunctionLocale(unsigned int length, const faker::Locale loca auto localeLocal = locale; if(_allWords_map.find(locale)==_allWords_map.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted= _conjunctions_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); @@ -183,7 +189,7 @@ std::string_view interjection(std::optional length, const faker::L } -std::string_view interjectionLocale(unsigned int length, const faker::Locale locale) +std::string_view interjectionLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -192,13 +198,13 @@ std::string_view interjectionLocale(unsigned int length, const faker::Locale loc auto localeLocal = locale; if(_allWords_map.find(locale)==_allWords_map.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted= _interjections_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } -std::string_view noun(std::optional length, const faker::Locale locale) +std::string_view noun(std::optional length, const Locale locale) { if(length) @@ -208,7 +214,7 @@ std::string_view noun(std::optional length, const faker::Locale lo } -std::string_view nounLocale(unsigned int length, const faker::Locale locale) +std::string_view nounLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -217,13 +223,13 @@ std::string_view nounLocale(unsigned int length, const faker::Locale locale) auto localeLocal = locale; if(_allWords_map.find(locale)==_allWords_map.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted= _nouns_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } -std::string_view preposition(std::optional length, const faker::Locale locale) +std::string_view preposition(std::optional length, const Locale locale) { if(length) @@ -233,7 +239,7 @@ std::string_view preposition(std::optional length, const faker::Lo return prepositionLocale(0,locale); } -std::string_view prepositionLocale(unsigned int length, const faker::Locale locale) +std::string_view prepositionLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -242,13 +248,13 @@ std::string_view prepositionLocale(unsigned int length, const faker::Locale loca auto localeLocal = locale; if(_allWords_map.find(locale)==_allWords_map.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted=_prepositions_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } -std::string_view verb(std::optional length, const faker::Locale locale) +std::string_view verb(std::optional length, const Locale locale) { if(length) @@ -257,7 +263,7 @@ std::string_view verb(std::optional length, const faker::Locale lo return verbLocale(0,locale); } -std::string_view verbLocale(unsigned int length, const faker::Locale locale) +std::string_view verbLocale(unsigned int length, const Locale locale) { if(length==0) { @@ -266,7 +272,7 @@ std::string_view verbLocale(unsigned int length, const faker::Locale locale) auto localeLocal = locale; if(_allWords_map.find(locale)==_allWords_map.end()) { - localeLocal = faker::Locale::en_US; + localeLocal = Locale::en_US; } auto sorted=(_verbs_sorted_map).at(localeLocal); diff --git a/tests/modules/string_test.cpp b/tests/modules/string_test.cpp index ada3ef73..ce1b31ba 100644 --- a/tests/modules/string_test.cpp +++ b/tests/modules/string_test.cpp @@ -146,14 +146,14 @@ TEST_F(StringTest, shouldGenerateSampleString) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee1) { - const auto sampleLocaleength{20}; + const auto sampleLocalength{20}; const GuaranteeMap guarantee = {{';', {1, 3}}, {',', {3, 4}}, {'a', {2, 10}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocaleength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); - ASSERT_EQ(generatedSample.size(), sampleLocaleength); + ASSERT_EQ(generatedSample.size(), sampleLocalength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -170,14 +170,14 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee1) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee2) { - const auto sampleLocaleength{20}; + const auto sampleLocalength{20}; const GuaranteeMap guarantee = {{'4', {0, 1}}, {'5', {0, 2}}, {'a', {0, 3}}, {'@', {2, 2}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocaleength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); - ASSERT_EQ(generatedSample.size(), sampleLocaleength); + ASSERT_EQ(generatedSample.size(), sampleLocalength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -196,14 +196,14 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee2) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee3) { - const auto sampleLocaleength{20}; + const auto sampleLocalength{20}; const GuaranteeMap guarantee = {{'(', {0, 4}}, {'{', {0, 2}}, {'\\', {0, 1}}, {'/', {0, 5}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocaleength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); - ASSERT_EQ(generatedSample.size(), sampleLocaleength); + ASSERT_EQ(generatedSample.size(), sampleLocalength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -222,16 +222,16 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee3) TEST_F(StringTest, invalidGuaranteeForSample1) { - const auto sampleLocaleength{20}; + const auto sampleLocalength{20}; GuaranteeMap guarantee = {{'3', {5, 6}}, {':', {6}}, {'A', {10}}}; - ASSERT_THROW(sample(std::move(guarantee), sampleLocaleength), std::invalid_argument); + ASSERT_THROW(sample(std::move(guarantee), sampleLocalength), std::invalid_argument); } TEST_F(StringTest, invalidGuaranteeForSample2) { - const auto sampleLocaleength{20}; + const auto sampleLocalength{20}; GuaranteeMap guarantee = {{'a', {3}}, {'A', {10}}, {'~', {2, 2}}}; - ASSERT_THROW(sample(std::move(guarantee), sampleLocaleength), std::invalid_argument); + ASSERT_THROW(sample(std::move(guarantee), sampleLocalength), std::invalid_argument); } TEST_F(StringTest, shouldGenerateDefaultStringFromCharaters) diff --git a/tests/modules/word_test.cpp b/tests/modules/word_test.cpp index 9d99c1a8..068091c7 100644 --- a/tests/modules/word_test.cpp +++ b/tests/modules/word_test.cpp @@ -13,6 +13,32 @@ using namespace faker::word; using namespace faker; using namespace ::testing; +namespace{ + + const struct Idioms_Map& getIdiomsMap(Locale locale) + { + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) + { + return enUSIdioms; + } + else + { + return idiomsMapSpan.at(locale); + } + } + bool checkLocale(Locale locale) + { + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) + { + return false; + } + else + { + return true; + } + } + +} class WordTest : public Test { public: @@ -277,251 +303,298 @@ TEST_F(WordTest, returnsFirstElementWhenNoLengthMatch) ASSERT_TRUE(result == "three"); } + class WordTestLocale : public TestWithParam { public: }; -TEST_F(WordTestLocale, shouldGenerateadjectiveLocaleocale) +TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleocale) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedAdjective = adjectiveLocale( 7,locale); - ASSERT_TRUE(std::ranges::any_of(_adjetives_sorted_map.at(locale), [generatedAdjective](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } -TEST_F(WordTestLocale, shouldGenerateadjectiveLocaleocaleWithNoLocale) +TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleocaleWithNoLocale) { - + Locale locale = Locale::en_US; + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedAdjective = adjectiveLocale(); - ASSERT_TRUE(std::ranges::any_of(adjectives, [generatedAdjective](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } -TEST_F(WordTestLocale, shouldGenerateAdjectiveWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateAdjectiveWithExistingLength) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedAdjective = adjectiveLocale(5,locale); - ASSERT_TRUE(std::ranges::any_of(_adjetives_sorted_map.at(locale), [generatedAdjective](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } -TEST_F(WordTestLocale, shouldGenerateAdjectiveWithNonExistingLength) +TEST_P(WordTestLocale, shouldGenerateAdjectiveWithNonExistingLength) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedAdjective = adjectiveLocale(0,locale); - ASSERT_TRUE(std::ranges::any_of(_adjetives_sorted_map.at(locale), [generatedAdjective](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } -TEST_F(WordTestLocale, shouldGenerateAdvervsLocale) +TEST_P(WordTestLocale, shouldGenerateAdvervsLocale) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedAdverb = adverbLocale(7,locale); - ASSERT_TRUE(std::ranges::any_of(_adverbs_sorted_map.at(locale), [generatedAdverb](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adverbs, [generatedAdverb](const std::string_view& word) { return word == generatedAdverb; })); } -TEST_F(WordTestLocale, shouldGenerateadverbLocaleocale2) +TEST_P(WordTestLocale, shouldGenerateadverbLocaleocale2) { - const faker::Locale locale = Locale::en_US; - const auto generatedAdverb = adverb(); - - ASSERT_TRUE(std::ranges::any_of(_adverbs_sorted_map.at(locale), [generatedAdverb](const std::string_view& word) + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedAdverb = adverb(0,locale); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adverbs, [generatedAdverb](const std::string_view& word) { return word == generatedAdverb; })); } -TEST_F(WordTestLocale, shouldGenerateAdverbWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateAdverbWithExistingLength) { - const faker::Locale locale = Locale::es_AR; - const auto generatedAdverb = adverbLocale(5,locale); + + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedAdverb = adverb(5,locale); - ASSERT_TRUE(std::ranges::any_of(_adverbs_sorted_map.at(locale), [generatedAdverb](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adverbs, [generatedAdverb](const std::string_view& word) { return word == generatedAdverb; })); } -TEST_F(WordTestLocale, shouldGenerateAdverbWithNonExistingLength) +TEST_P(WordTestLocale, shouldGenerateAdverbWithNonExistingLength) { - const faker::Locale locale = Locale::es_AR; - const auto generatedAdverb = adverbLocale(0,locale); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedAdverb = adverb(0,locale); - ASSERT_TRUE(std::ranges::any_of(_adverbs_sorted_map.at(locale), [generatedAdverb](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adverbs, [generatedAdverb](const std::string_view& word) { return word == generatedAdverb; })); } -TEST_F(WordTestLocale, shouldGenerateConjunction) +TEST_P(WordTestLocale, shouldGenerateConjunction) { - const auto generatedConjunction = conjunction(); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedConjunction = conjunction(7,locale); - ASSERT_TRUE(std::ranges::any_of(_conjunctions_sorted_map.at(faker::Locale::en_US), [generatedConjunction](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.conjunctions, [generatedConjunction](const std::string_view& word) { return word == generatedConjunction; })); } -TEST_F(WordTestLocale, shouldGenerateConjunctionWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateConjunctionWithExistingLength) { - const faker::Locale locale = Locale::es_AR; - const auto generatedConjunction = conjunctionLocale(5,locale); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedConjunction = conjunction(5,locale); - ASSERT_TRUE(std::ranges::any_of(_conjunctions_sorted_map.at(locale),[generatedConjunction](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.conjunctions,[generatedConjunction](const std::string_view& word) { return word == generatedConjunction; })); } -TEST_F(WordTestLocale, shouldGenerateConjunctionWithLength0) +TEST_P(WordTestLocale, shouldGenerateConjunctionWithLength0) { - const faker::Locale locale = Locale::es_AR; - const auto generatedConjunction = conjunctionLocale(0,locale); - ASSERT_TRUE(std::ranges::any_of(_conjunctions_sorted_map.at(locale), [generatedConjunction](const std::string_view& word) + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedConjunction = conjunction(0,locale); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.conjunctions, [generatedConjunction](const std::string_view& word) { return word == generatedConjunction; })); } -TEST_F(WordTestLocale, shouldGenerateInterjection) +TEST_P(WordTestLocale, shouldGenerateInterjection) { - const faker::Locale locale = Locale::es_AR; - const auto generatedInterjection = interjectionLocale(0,locale); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedInterjection = interjection(0,locale); - ASSERT_TRUE(std::ranges::any_of(_interjections_sorted_map.at(locale), [generatedInterjection](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.interjections, [generatedInterjection](const std::string_view& word) { return word == generatedInterjection; })); } -TEST_F(WordTestLocale, shouldGenerateInterjectionWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateInterjectionWithExistingLength) { - const faker::Locale locale = Locale::es_AR; - const auto generatedInterjection = interjectionLocale(5,locale); - ASSERT_TRUE(std::ranges::any_of(_interjections_sorted_map.at(locale), [generatedInterjection](const std::string_view& word) + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedInterjection = interjection(5,locale); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.interjections, [generatedInterjection](const std::string_view& word) { return word == generatedInterjection; })); } -TEST_F(WordTestLocale, shouldGenerateInterjectionWithLength0) +TEST_P(WordTestLocale, shouldGenerateInterjectionWithLength0) { - const faker::Locale locale = Locale::es_AR; - const auto generatedInterjection = interjectionLocale(0,locale); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedInterjection = interjection(0,locale); - ASSERT_TRUE(std::ranges::any_of(_interjections_sorted_map.at(locale), [generatedInterjection](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.interjections, [generatedInterjection](const std::string_view& word) { return word == generatedInterjection; })); } -TEST_F(WordTestLocale, shouldGenerateNoun) +TEST_P(WordTestLocale, shouldGenerateNoun) { - const faker::Locale locale = Locale::en_US; - const auto generatedNoun = nounLocale(); - + + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedNoun = noun(0,locale); ASSERT_TRUE( - std::ranges::any_of(_nouns_sorted_map.at(locale), [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); + std::ranges::any_of(idiomsMapLocal.nouns, [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); } -TEST_F(WordTestLocale, shouldGenerateNounWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateNounWithExistingLength) { - const faker::Locale locale = Locale::en_US; - const auto generatedNoun = nounLocale(5); + + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedNoun = noun(5,locale); ASSERT_TRUE( - std::ranges::any_of(_nouns_sorted_map.at(locale), [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); + std::ranges::any_of(idiomsMapLocal.nouns, [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); } -TEST_F(WordTestLocale, shouldGenerateNOunWithLength0) +TEST_P(WordTestLocale, shouldGenerateNOunWithLength0) { - const faker::Locale locale = Locale::es_AR; + + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedNoun = nounLocale(0,locale); ASSERT_TRUE( - std::ranges::any_of(_nouns_sorted_map.at(locale), [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); + std::ranges::any_of(idiomsMapLocal.nouns, [generatedNoun](const std::string_view& word) { return word == generatedNoun; })); } -TEST_F(WordTestLocale, shouldGeneratePreposition) +TEST_P(WordTestLocale, shouldGeneratePreposition) { - const faker::Locale locale = Locale::en_US; - const auto generatedPreposition = preposition(); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedPreposition = preposition(0,locale); - ASSERT_TRUE(std::ranges::any_of(_prepositions_sorted_map.at(locale), [generatedPreposition](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.prepositions, [generatedPreposition](const std::string_view& word) { return word == generatedPreposition; })); } -TEST_F(WordTestLocale, shouldGeneratePrepositionWithExistingLength) +TEST_P(WordTestLocale, shouldGeneratePrepositionWithExistingLength) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedPreposition = prepositionLocale(5,locale); - ASSERT_TRUE(std::ranges::any_of(_prepositions_sorted_map.at(locale), [generatedPreposition](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.prepositions, [generatedPreposition](const std::string_view& word) { return word == generatedPreposition; })); } -TEST_F(WordTestLocale, shouldGeneratePrepositionWithLength0) +TEST_P(WordTestLocale, shouldGeneratePrepositionWithLength0) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedPreposition = prepositionLocale(0,locale); - ASSERT_TRUE(std::ranges::any_of(_prepositions_sorted_map.at(locale), [generatedPreposition](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.prepositions, [generatedPreposition](const std::string_view& word) { return word == generatedPreposition; })); } -TEST_F(WordTestLocale, shouldGenerateVerb) +TEST_P(WordTestLocale, shouldGenerateVerb) { - const faker::Locale locale = Locale::en_US; - const auto generatedVerb = verb(); + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); + const auto generatedVerb = verb(0,locale); - ASSERT_TRUE( - std::ranges::any_of(_verbs_sorted_map.at(locale), [generatedVerb](const std::string_view& word) { return word == generatedVerb; })); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.verbs, [generatedVerb](const std::string_view& word) + { return word == generatedVerb; })); } -TEST_F(WordTestLocale, shouldGenerateVerbWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateVerbWithExistingLength) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedVerb = verbLocale(5,locale); - ASSERT_TRUE( - std::ranges::any_of(_verbs_sorted_map.at(locale), [generatedVerb](const std::string_view& word) { return word == generatedVerb; })); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.verbs, [generatedVerb](const std::string_view& word) + { return word == generatedVerb; })); } -TEST_F(WordTestLocale, shouldGenerateVerbWithExistingLength0) +TEST_P(WordTestLocale, shouldGenerateVerbWithExistingLength0) { - const faker::Locale locale = Locale::es_AR; + Locale locale = GetParam(); + auto idiomsMapLocal=getIdiomsMap(locale); const auto generatedVerb = verbLocale(0,locale); - ASSERT_TRUE( - std::ranges::any_of(_verbs_sorted_map.at(locale), [generatedVerb](const std::string_view& word) { return word == generatedVerb; })); + ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.verbs, [generatedVerb](const std::string_view& word) + { return word == generatedVerb; })); } TEST_F(WordTestLocale, shouldGenerateSample) { + //default sample should be from en_US faker::Locale locale= faker::Locale::en_US; - const auto generatedSample = sampleLocale(0); + + const auto generatedSample = sample(); ASSERT_TRUE(std::ranges::any_of(_allWords_map.at(locale), [generatedSample](const std::string_view& word) { return word == generatedSample; })); } -TEST_F(WordTestLocale, shouldGenerateSampleWithExistingLength) +TEST_P(WordTestLocale, shouldGenerateSampleWithExistingLength) { - faker::Locale locale= faker::Locale::es_AR; + Locale locale = GetParam(); + Locale extra=locale; const auto generatedSample = sampleLocale(5,locale); + if(!checkLocale(locale)) + { + extra=Locale::en_US; + } + - ASSERT_TRUE(std::ranges::any_of(_allWords_map.at(locale), [generatedSample](const std::string_view& word) + ASSERT_TRUE(std::ranges::any_of(_allWords_map.at(extra), [generatedSample](const std::string_view& word) { return word == generatedSample; })); } -TEST_F(WordTestLocale, shouldGenerateSampleWithNonExistingLength) +TEST_P(WordTestLocale, shouldGenerateSampleWithNonExistingLength) { - faker::Locale locale= faker::Locale::es_AR; + Locale locale = GetParam(); + Locale extra=locale; const auto generatedSample = sampleLocale(0,locale); - - ASSERT_TRUE(std::ranges::any_of(_allWords_map.at(locale), [generatedSample](const std::string_view& word) + if(!checkLocale(locale)) + { + extra=Locale::en_US; + } + + ASSERT_TRUE(std::ranges::any_of(_allWords_map.at(extra), [generatedSample](const std::string_view& word) { return word == generatedSample; })); } -TEST_F(WordTestLocale, shouldGenerateWords) +TEST_P(WordTestLocale, shouldGenerateWords) { - faker::Locale locale= faker::Locale::en_US; - const auto generatedWords = wordsLocale(5); + Locale locale = GetParam(); + Locale extra=locale; + + if(!checkLocale(locale)) + { + extra=Locale::en_US; + } + const auto generatedWords = wordsLocale(5,locale); const auto separatedWords = common::split(generatedWords, " "); - ASSERT_TRUE(std::ranges::all_of(separatedWords, [locale](const std::string& separatedWord) - { return std::ranges::find(_allWords_map.at(locale), separatedWord) !=_allWords_map.at(locale).end(); })); + ASSERT_TRUE(std::ranges::all_of(separatedWords, [extra](const std::string& separatedWord) + { return std::ranges::find(_allWords_map.at(extra), separatedWord) !=_allWords_map.at(extra).end(); })); } TEST_F(WordTestLocale, shouldReturnRandomElementWhenExactLengthNotFound) @@ -546,21 +619,34 @@ TEST_F(WordTestLocale, shouldReturnRandomElementWhenExactLengthNotFound) ASSERT_TRUE(std::ranges::find(matchingAdjectives, generatedAdjective) == matchingAdjectives.end()); } -TEST_F(WordTestLocale, shouldReturnEmptyStringForZeroWords) +TEST_P(WordTestLocale, shouldReturnEmptyStringForZeroWords) { - faker::Locale locale= faker::Locale::es_AR; - const auto result = wordsLocale(0,locale); + Locale locale = GetParam(); + Locale extra=locale; + + if(!checkLocale(locale)) + { + extra=Locale::en_US; + } + const auto result = wordsLocale(0,extra); ASSERT_TRUE(result.empty()); } -TEST_F(WordTestLocale, shouldGenerateLargeNumberOfWords) +TEST_P(WordTestLocale, shouldGenerateLargeNumberOfWords) { - faker::Locale locale= faker::Locale::es_AR; + + Locale locale = GetParam(); + Locale extra=locale; + + if(!checkLocale(locale)) + { + extra=Locale::en_US; + } const unsigned int largeWordCount = 300; - const auto generatedWords = wordsLocale(largeWordCount,locale); + const auto generatedWords = wordsLocale(largeWordCount,extra); const auto separatedWords = common::split(generatedWords, " "); - auto dataset=_allWords_map.at(locale); + auto dataset=_allWords_map.at(extra); ASSERT_EQ(separatedWords.size(), largeWordCount); for (const auto& word : separatedWords) { @@ -606,4 +692,18 @@ TEST_F(WordTestLocale, shouldReturnFrenchSampleifAskedforFrenchWord) auto dataset=_allWords_map.at(locale); ASSERT_TRUE(std::ranges::any_of(dataset, [generatedSample](const std::string_view& word) { return word == generatedSample; })); -} \ No newline at end of file +} + + +TEST_F(WordTestLocale, shouldReturnFrenchSampleifAskedforFrenchWordUsingSample) +{ + faker::Locale locale= faker::Locale::fr_FR; + const auto generatedSample = sample(0,locale); + auto dataset=_allWords_map.at(locale); + ASSERT_TRUE(std::ranges::any_of(dataset, [generatedSample](const std::string_view& word) + { return word == generatedSample; })); +} + + +INSTANTIATE_TEST_SUITE_P(testWordByLocale, WordTestLocale, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); From c6f0618efc23cc43ed15e9ed88baf8c3a5584a63 Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Tue, 1 Oct 2024 17:53:24 -0300 Subject: [PATCH 2/7] fixing use fmt and GTest from System --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 68a59f87..5b4ce67a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(faker-cxx include(cmake/CompilerWarnings.cmake) include(CMakeDependentOption) -option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" ON) +option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF) option(BUILD_EXAMPLES "Build examples" OFF) option(BUILD_TESTING "Build tests" ON) option(CODE_COVERAGE "Build faker-cxx with coverage support" OFF) From d3b9a6057fd2cb28e0203566b9f09a4059741d71 Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Wed, 2 Oct 2024 08:19:16 -0300 Subject: [PATCH 3/7] spelling --- src/modules/internet.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/internet.cpp b/src/modules/internet.cpp index 0b5f2ef0..ab55c42b 100644 --- a/src/modules/internet.cpp +++ b/src/modules/internet.cpp @@ -341,11 +341,11 @@ std::string anonymousUsername(unsigned maxLength) else if (maxLength > defaultMax) maxLength = defaultMax; - const std::integral auto adjectiveLocaleength = number::integer(3, 1 + maxLength / 2); + const std::integral auto adjectiveLocaleLength = number::integer(3, 1 + maxLength / 2); - const auto nounLocaleength = maxLength - adjectiveLocaleength; + const auto nounLocaleLength = maxLength - adjectiveLocaleLength; - return common::format("{}{}", word::adjective(adjectiveLocaleength), word::noun(nounLocaleength)); + return common::format("{}{}", word::adjective(adjectiveLocaleLength), word::noun(nounLocaleLength)); } } From 07ad1ef71fdcbd9c58eb79e7b80fa53ba0c946be Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Wed, 2 Oct 2024 08:22:36 -0300 Subject: [PATCH 4/7] reverting modification in internet.cpp --- src/modules/internet.cpp | 6 +++--- src/modules/word_data.h | 12 ++++++++---- src/modules/word_store.h | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/modules/internet.cpp b/src/modules/internet.cpp index ab55c42b..7a8f4c98 100644 --- a/src/modules/internet.cpp +++ b/src/modules/internet.cpp @@ -341,11 +341,11 @@ std::string anonymousUsername(unsigned maxLength) else if (maxLength > defaultMax) maxLength = defaultMax; - const std::integral auto adjectiveLocaleLength = number::integer(3, 1 + maxLength / 2); + const std::integral auto adjectiveLength = number::integer(3, 1 + maxLength / 2); - const auto nounLocaleLength = maxLength - adjectiveLocaleLength; + const auto nounLength = maxLength - adjectiveLength; - return common::format("{}{}", word::adjective(adjectiveLocaleLength), word::noun(nounLocaleLength)); + return common::format("{}{}", word::adjective(adjectiveLength), word::noun(nounLength)); } } diff --git a/src/modules/word_data.h b/src/modules/word_data.h index 3d94bd20..7ecd6409 100644 --- a/src/modules/word_data.h +++ b/src/modules/word_data.h @@ -216,7 +216,8 @@ const auto _adjectives_sorted = []() return adjetives_sorted.at(faker::Locale::en_US); }(); -const auto _adjetives_sorted_map = [](){ +const auto _adjetives_sorted_map = []() +{ std::map> adjetives_sorted; for(auto i: idiomsMapSpan) { @@ -249,7 +250,8 @@ const auto _adverbs_sorted = []() } return adverbs_sorted.at(faker::Locale::en_US); }(); -const auto _adverbs_sorted_map = [](){ +const auto _adverbs_sorted_map = []() +{ std::map> adverbs_sorted; for(auto i: idiomsMapSpan) { @@ -282,7 +284,8 @@ const auto _conjunctions_sorted = []() return conjunctions_sorted.at(faker::Locale::en_US); }(); -const auto _conjunctions_sorted_map = [](){ +const auto _conjunctions_sorted_map = []() +{ std::map> conjunctions_sorted; for(auto i: idiomsMapSpan) { @@ -355,7 +358,8 @@ const auto _nouns_sorted = []() }(); -const auto _nouns_sorted_map = [](){ +const auto _nouns_sorted_map = []() +{ std::map> nouns_sorted; for(auto i: idiomsMapSpan) { diff --git a/src/modules/word_store.h b/src/modules/word_store.h index a590e6ec..8a665032 100644 --- a/src/modules/word_store.h +++ b/src/modules/word_store.h @@ -43531,6 +43531,7 @@ const faker::word::Idioms_Map frFRIdioms{{frFRAdjectives}, {frFRAdverbs}, {frFRNouns},{frFRPrepositions}, {frFRVerbs}}; + const std::map idiomsMapSpan({ {faker::Locale::en_US,{enUSIdioms}}, {faker::Locale::es_AR,{esARIdioms}}, From ae09226b0d1078dc08b211e0d2a26e7d34fab50e Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Wed, 2 Oct 2024 08:33:27 -0300 Subject: [PATCH 5/7] spell correction --- tests/modules/string_test.cpp | 26 +++++++++++++------------- tests/modules/word_test.cpp | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/modules/string_test.cpp b/tests/modules/string_test.cpp index ce1b31ba..89d5dcfd 100644 --- a/tests/modules/string_test.cpp +++ b/tests/modules/string_test.cpp @@ -146,14 +146,14 @@ TEST_F(StringTest, shouldGenerateSampleString) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee1) { - const auto sampleLocalength{20}; + const auto sampleLength{20}; const GuaranteeMap guarantee = {{';', {1, 3}}, {',', {3, 4}}, {'a', {2, 10}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLength); - ASSERT_EQ(generatedSample.size(), sampleLocalength); + ASSERT_EQ(generatedSample.size(), sampleLength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -170,14 +170,14 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee1) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee2) { - const auto sampleLocalength{20}; + const auto sampleLength{20}; const GuaranteeMap guarantee = {{'4', {0, 1}}, {'5', {0, 2}}, {'a', {0, 3}}, {'@', {2, 2}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLength); - ASSERT_EQ(generatedSample.size(), sampleLocalength); + ASSERT_EQ(generatedSample.size(), sampleLength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -196,14 +196,14 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee2) TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee3) { - const auto sampleLocalength{20}; + const auto sampleLength{20}; const GuaranteeMap guarantee = {{'(', {0, 4}}, {'{', {0, 2}}, {'\\', {0, 1}}, {'/', {0, 5}}}; for (int i = 0; i < runCount; ++i) { auto copyGuarantee = guarantee; - const auto generatedSample = sample(std::move(copyGuarantee), sampleLocalength); + const auto generatedSample = sample(std::move(copyGuarantee), sampleLength); - ASSERT_EQ(generatedSample.size(), sampleLocalength); + ASSERT_EQ(generatedSample.size(), sampleLength); ASSERT_TRUE(std::ranges::all_of( generatedSample, [](char sampleCharacter) { return static_cast(sampleCharacter) >= 33 && static_cast(sampleCharacter) <= 125; })); @@ -222,16 +222,16 @@ TEST_F(StringTest, shouldGenerateSampleStringWithGuarantee3) TEST_F(StringTest, invalidGuaranteeForSample1) { - const auto sampleLocalength{20}; + const auto sampleLength{20}; GuaranteeMap guarantee = {{'3', {5, 6}}, {':', {6}}, {'A', {10}}}; - ASSERT_THROW(sample(std::move(guarantee), sampleLocalength), std::invalid_argument); + ASSERT_THROW(sample(std::move(guarantee), sampleLength), std::invalid_argument); } TEST_F(StringTest, invalidGuaranteeForSample2) { - const auto sampleLocalength{20}; + const auto sampleLength{20}; GuaranteeMap guarantee = {{'a', {3}}, {'A', {10}}, {'~', {2, 2}}}; - ASSERT_THROW(sample(std::move(guarantee), sampleLocalength), std::invalid_argument); + ASSERT_THROW(sample(std::move(guarantee), sampleLength), std::invalid_argument); } TEST_F(StringTest, shouldGenerateDefaultStringFromCharaters) diff --git a/tests/modules/word_test.cpp b/tests/modules/word_test.cpp index 068091c7..c72ddd14 100644 --- a/tests/modules/word_test.cpp +++ b/tests/modules/word_test.cpp @@ -308,7 +308,7 @@ class WordTestLocale : public TestWithParam { public: }; -TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleocale) +TEST_P(WordTestLocale, shouldGenerateadjectiveLocale) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); @@ -317,7 +317,7 @@ TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleocale) ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } -TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleocaleWithNoLocale) +TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleWithNoLocale) { Locale locale = Locale::en_US; auto idiomsMapLocal=getIdiomsMap(locale); @@ -356,7 +356,7 @@ TEST_P(WordTestLocale, shouldGenerateAdvervsLocale) } -TEST_P(WordTestLocale, shouldGenerateadverbLocaleocale2) +TEST_P(WordTestLocale, shouldGenerateadverbLocale2) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); From a0782c79a3ea6e9eb62009b0fa6d9d6b5b6216e6 Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Wed, 2 Oct 2024 08:38:12 -0300 Subject: [PATCH 6/7] using correct function in test --- tests/modules/word_test.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/modules/word_test.cpp b/tests/modules/word_test.cpp index c72ddd14..bb42537a 100644 --- a/tests/modules/word_test.cpp +++ b/tests/modules/word_test.cpp @@ -313,7 +313,7 @@ TEST_P(WordTestLocale, shouldGenerateadjectiveLocale) Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedAdjective = adjectiveLocale( 7,locale); + const auto generatedAdjective = adjective( 7,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } @@ -321,7 +321,7 @@ TEST_P(WordTestLocale, shouldGenerateadjectiveLocaleWithNoLocale) { Locale locale = Locale::en_US; auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedAdjective = adjectiveLocale(); + const auto generatedAdjective = adjective(); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); } @@ -330,7 +330,7 @@ TEST_P(WordTestLocale, shouldGenerateAdjectiveWithExistingLength) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedAdjective = adjectiveLocale(5,locale); + const auto generatedAdjective = adjective(5,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); @@ -340,7 +340,7 @@ TEST_P(WordTestLocale, shouldGenerateAdjectiveWithNonExistingLength) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedAdjective = adjectiveLocale(0,locale); + const auto generatedAdjective = adjective(0,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adjetives, [generatedAdjective](const std::string_view& word) { return word == generatedAdjective; })); @@ -350,7 +350,7 @@ TEST_P(WordTestLocale, shouldGenerateAdvervsLocale) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedAdverb = adverbLocale(7,locale); + const auto generatedAdverb = adverb(7,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.adverbs, [generatedAdverb](const std::string_view& word) { return word == generatedAdverb; })); @@ -493,7 +493,7 @@ TEST_P(WordTestLocale, shouldGeneratePrepositionWithExistingLength) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedPreposition = prepositionLocale(5,locale); + const auto generatedPreposition = preposition(5,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.prepositions, [generatedPreposition](const std::string_view& word) { return word == generatedPreposition; })); @@ -503,7 +503,7 @@ TEST_P(WordTestLocale, shouldGeneratePrepositionWithLength0) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedPreposition = prepositionLocale(0,locale); + const auto generatedPreposition = preposition(0,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.prepositions, [generatedPreposition](const std::string_view& word) { return word == generatedPreposition; })); @@ -524,7 +524,7 @@ TEST_P(WordTestLocale, shouldGenerateVerbWithExistingLength) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedVerb = verbLocale(5,locale); + const auto generatedVerb = verb(5,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.verbs, [generatedVerb](const std::string_view& word) { return word == generatedVerb; })); @@ -534,7 +534,7 @@ TEST_P(WordTestLocale, shouldGenerateVerbWithExistingLength0) { Locale locale = GetParam(); auto idiomsMapLocal=getIdiomsMap(locale); - const auto generatedVerb = verbLocale(0,locale); + const auto generatedVerb = verb(0,locale); ASSERT_TRUE(std::ranges::any_of(idiomsMapLocal.verbs, [generatedVerb](const std::string_view& word) { return word == generatedVerb; })); } @@ -554,7 +554,7 @@ TEST_P(WordTestLocale, shouldGenerateSampleWithExistingLength) { Locale locale = GetParam(); Locale extra=locale; - const auto generatedSample = sampleLocale(5,locale); + const auto generatedSample = sample(5,locale); if(!checkLocale(locale)) { extra=Locale::en_US; @@ -569,7 +569,7 @@ TEST_P(WordTestLocale, shouldGenerateSampleWithNonExistingLength) { Locale locale = GetParam(); Locale extra=locale; - const auto generatedSample = sampleLocale(0,locale); + const auto generatedSample = sample(0,locale); if(!checkLocale(locale)) { extra=Locale::en_US; @@ -589,7 +589,7 @@ TEST_P(WordTestLocale, shouldGenerateWords) { extra=Locale::en_US; } - const auto generatedWords = wordsLocale(5,locale); + const auto generatedWords = words(5,locale); const auto separatedWords = common::split(generatedWords, " "); @@ -613,7 +613,7 @@ TEST_F(WordTestLocale, shouldReturnRandomElementWhenExactLengthNotFound) } } - const auto generatedAdjective = adjectiveLocale(existingLength + 1,locale); + const auto generatedAdjective = adjective(existingLength + 1,locale); ASSERT_TRUE(std::ranges::find(sorted, generatedAdjective) != sorted.end()); ASSERT_TRUE(std::ranges::find(matchingAdjectives, generatedAdjective) == matchingAdjectives.end()); @@ -628,7 +628,7 @@ TEST_P(WordTestLocale, shouldReturnEmptyStringForZeroWords) { extra=Locale::en_US; } - const auto result = wordsLocale(0,extra); + const auto result = words(0,extra); ASSERT_TRUE(result.empty()); } @@ -644,7 +644,7 @@ TEST_P(WordTestLocale, shouldGenerateLargeNumberOfWords) extra=Locale::en_US; } const unsigned int largeWordCount = 300; - const auto generatedWords = wordsLocale(largeWordCount,extra); + const auto generatedWords = words(largeWordCount,extra); const auto separatedWords = common::split(generatedWords, " "); auto dataset=_allWords_map.at(extra); ASSERT_EQ(separatedWords.size(), largeWordCount); From df3043f3cd208c4c2792e2c1bd489fefa27a7db4 Mon Sep 17 00:00:00 2001 From: Gustavo Bastian Date: Wed, 2 Oct 2024 08:42:09 -0300 Subject: [PATCH 7/7] check if the locale is defined using idiomsMapSpan structure, if not using default en_US. --- src/modules/word.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/modules/word.cpp b/src/modules/word.cpp index 08eb58a1..75d59371 100644 --- a/src/modules/word.cpp +++ b/src/modules/word.cpp @@ -172,10 +172,11 @@ std::string_view conjunctionLocale(unsigned int length, const Locale locale) length=100; } auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { localeLocal = Locale::en_US; } + auto sorted= _conjunctions_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } @@ -195,11 +196,13 @@ std::string_view interjectionLocale(unsigned int length, const Locale locale) { length=100; } - auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + auto localeLocal = locale; + + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { localeLocal = Locale::en_US; } + auto sorted= _interjections_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } @@ -221,10 +224,10 @@ std::string_view nounLocale(unsigned int length, const Locale locale) length=100; } auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { localeLocal = Locale::en_US; - } + } auto sorted= _nouns_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } @@ -246,10 +249,12 @@ std::string_view prepositionLocale(unsigned int length, const Locale locale) length=100; } auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { localeLocal = Locale::en_US; } + auto sorted=_prepositions_sorted_map.at(localeLocal); return sortedSizeRandomElement(length, sorted); } @@ -270,7 +275,8 @@ std::string_view verbLocale(unsigned int length, const Locale locale) length=100; } auto localeLocal = locale; - if(_allWords_map.find(locale)==_allWords_map.end()) + + if(idiomsMapSpan.find(locale)==idiomsMapSpan.end()) { localeLocal = Locale::en_US; }