From c2e3af9c290b204ea75f5a4dfea95fadd7526dc4 Mon Sep 17 00:00:00 2001 From: Mayank Pareek Date: Mon, 28 Oct 2024 08:58:12 -0400 Subject: [PATCH 1/3] Add locale to plant module --- include/faker-cxx/plant.h | 19 +++++++------ src/modules/plant.cpp | 60 ++++++++++++++++++++++++++++----------- src/modules/plant_data.h | 40 ++++++++++++++++++++------ 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/include/faker-cxx/plant.h b/include/faker-cxx/plant.h index f9ae132a..654d03d2 100644 --- a/include/faker-cxx/plant.h +++ b/include/faker-cxx/plant.h @@ -3,19 +3,22 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::plant { /** * @brief Returns a random species of tree. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of tree. * * @code * faker::plant::tree() // "Oak" * @endcode */ -FAKER_CXX_EXPORT std::string_view tree(); +FAKER_CXX_EXPORT std::string_view tree(Locale locale = Locale::en_US); /** * @brief Returns a random species of flower. @@ -26,7 +29,7 @@ FAKER_CXX_EXPORT std::string_view tree(); * faker::plant::flower() // "Rose" * @endcode */ -FAKER_CXX_EXPORT std::string_view flower(); +FAKER_CXX_EXPORT std::string_view flower(Locale locale = Locale::en_US); /** * @brief Returns a random species of shrub. @@ -37,7 +40,7 @@ FAKER_CXX_EXPORT std::string_view flower(); * faker::plant::shrub() // "Azalea" * @endcode */ -FAKER_CXX_EXPORT std::string_view shrub(); +FAKER_CXX_EXPORT std::string_view shrub(Locale locale = Locale::en_US); /** * @brief Returns a random species of grass. @@ -48,7 +51,7 @@ FAKER_CXX_EXPORT std::string_view shrub(); * faker::plant::grass() // "Kentucky Bluegrass" * @endcode */ -FAKER_CXX_EXPORT std::string_view grass(); +FAKER_CXX_EXPORT std::string_view grass(Locale locale = Locale::en_US); /** * @brief Returns a random species of fern. @@ -59,7 +62,7 @@ FAKER_CXX_EXPORT std::string_view grass(); * faker::plant::fern() // "Maidenhair" * @endcode */ -FAKER_CXX_EXPORT std::string_view fern(); +FAKER_CXX_EXPORT std::string_view fern(Locale locale = Locale::en_US); /** * @brief Returns a random species of succulent. @@ -70,7 +73,7 @@ FAKER_CXX_EXPORT std::string_view fern(); * faker::plant::succulent() // "Aloe Vera" * @endcode */ -FAKER_CXX_EXPORT std::string_view succulent(); +FAKER_CXX_EXPORT std::string_view succulent(Locale locale = Locale::en_US); /** * @brief Returns a random species of vine. @@ -81,7 +84,7 @@ FAKER_CXX_EXPORT std::string_view succulent(); * faker::plant::vine() // "Ivy" * @endcode */ -FAKER_CXX_EXPORT std::string_view vine(); +FAKER_CXX_EXPORT std::string_view vine(Locale locale = Locale::en_US); /** * @brief Returns a random type of plant. @@ -92,5 +95,5 @@ FAKER_CXX_EXPORT std::string_view vine(); * faker::plant::type() // "tree" * @endcode */ -FAKER_CXX_EXPORT std::string_view plantType(); +FAKER_CXX_EXPORT std::string_view plantType(Locale locale = Locale::en_US); } diff --git a/src/modules/plant.cpp b/src/modules/plant.cpp index 49963bff..f3d65d05 100644 --- a/src/modules/plant.cpp +++ b/src/modules/plant.cpp @@ -7,43 +7,71 @@ namespace faker::plant { -std::string_view tree() +namespace { - return helper::randomElement(trees); +const struct PlantDefinition& getPlantDefinition(Locale locale) +{ + switch (locale) + { + default: + return enUSPlantDefinition; + } +} } -std::string_view flower() +std::string_view tree(Locale locale) { - return helper::randomElement(flowers); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.trees); } -std::string_view shrub() +std::string_view flower(Locale locale) { - return helper::randomElement(shrubs); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.flowers); } -std::string_view grass() +std::string_view shrub(Locale locale) { - return helper::randomElement(grasses); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.shrubs); } -std::string_view fern() +std::string_view grass(Locale locale) { - return helper::randomElement(ferns); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.grasses); } -std::string_view succulent() +std::string_view fern(Locale locale) { - return helper::randomElement(succulents); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.ferns); } -std::string_view vine() +std::string_view succulent(Locale locale) { - return helper::randomElement(vines); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.succulents); +} + +std::string_view vine(Locale locale) +{ + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.vines); } -std::string_view plantType() +std::string_view plantType(Locale locale) { - return helper::randomElement(plantTypes); + const auto& plantDefinition = getPlantDefinition(locale); + + return helper::randomElement(plantDefinition.plantTypes); } } diff --git a/src/modules/plant_data.h b/src/modules/plant_data.h index f92e91f2..99b0fa8c 100644 --- a/src/modules/plant_data.h +++ b/src/modules/plant_data.h @@ -1,11 +1,24 @@ #pragma once #include +#include #include namespace faker::plant { -const auto trees = std::to_array({ +struct PlantDefinition +{ + std::span trees; + std::span flowers; + std::span shrubs; + std::span grasses; + std::span ferns; + std::span succulents; + std::span vines; + std::span plantTypes; +}; + +const auto enUSTrees = std::to_array({ "Acacia", "Almond", "Apple", "Ash", "Avocado", "Bamboo", "Banana", "Baobab", "Beech", "Birch", "Black Walnut", "Catalpa", "Cedar", "Cherry Blossom", "Chestnut", "Chestnut Oak", "Cottonwood", "Crape Myrtle", "Cypress", "Dogwood", "Eucalyptus", @@ -15,7 +28,7 @@ const auto trees = std::to_array({ "Spruce", "Sycamore", "Tamarind", "Walnut", "Willow", }); -const auto flowers = std::to_array({ +const auto enUSFlowers = std::to_array({ "Adenium Obesum", "African Daisy", "Allamanda Cathartica", @@ -110,7 +123,7 @@ const auto flowers = std::to_array({ "Zinnia", }); -const auto shrubs = std::to_array({ +const auto enUSShrubs = std::to_array({ "Aralia", "Arctostaphylos", "Aronia", "Artemisia", "Aucuba", "Berberis", "Bougainvillea", "Brugmansia", "Buddleja", "Buxus", "Calia", "Callicarpa", "Callistemon", "Calluna", "Calycanthus", "Camellia", "Caragana", "Carpenteria", @@ -149,7 +162,7 @@ const auto shrubs = std::to_array({ "Zenobia", "Ziziphus", }); -const auto grasses = std::to_array({ +const auto enUSGrasses = std::to_array({ "Bahiagrass", "Bahiagrass", "Bamboo", @@ -249,7 +262,7 @@ const auto grasses = std::to_array({ "Yellow Indiangrass", }); -const auto ferns = std::to_array({ +const auto enUSFerns = std::to_array({ "Adder's-tongue Fern", "Angiopteris evecta", "Beech Fern", @@ -336,7 +349,7 @@ const auto ferns = std::to_array({ "Zygopteridales", }); -const auto succulents = std::to_array({ +const auto enUSSucculents = std::to_array({ "Aeonium", "Aloe vera", "Ammocharis", @@ -470,7 +483,7 @@ const auto succulents = std::to_array({ "Zephyrlily", }); -const auto vines = std::to_array({ +const auto enUSVines = std::to_array({ "Akebia (Five-leaf Akebia)", "Asparagus asparagoides (Sprenger's Asparagus)", "Black-eyed Susan Vine (Thunbergia alata)", @@ -497,7 +510,7 @@ const auto vines = std::to_array({ "Wisteria", }); -const auto plantTypes = std::to_array({ +const auto enUSPlantTypes = std::to_array({ "fern", "flower", "grass", @@ -507,4 +520,15 @@ const auto plantTypes = std::to_array({ "vine", }); +const PlantDefinition enUSPlantDefinition = { + .trees = enUSTrees, + .flowers = enUSFlowers, + .shrubs = enUSShrubs, + .grasses = enUSGrasses, + .ferns = enUSFerns, + .succulents = enUSSucculents, + .vines = enUSVines, + .plantTypes = enUSPlantTypes, +}; + } From 7c01cdcc3bec72325de9e353589c5abca04c4a92 Mon Sep 17 00:00:00 2001 From: Mayank Pareek Date: Mon, 28 Oct 2024 09:42:02 -0400 Subject: [PATCH 2/3] update test for plant module --- tests/modules/plant_test.cpp | 104 +++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 28 deletions(-) diff --git a/tests/modules/plant_test.cpp b/tests/modules/plant_test.cpp index 3967839d..4cade2f6 100644 --- a/tests/modules/plant_test.cpp +++ b/tests/modules/plant_test.cpp @@ -7,73 +7,121 @@ #include "plant_data.h" using namespace ::testing; +using namespace faker; using namespace faker::plant; -class PlantTest : public Test +namespace +{ +const struct PlantDefinition& getPlantDefinition(Locale locale) +{ + switch (locale) + { + default: + return enUSPlantDefinition; + } +} +} + +class PlantTest : public TestWithParam { public: }; -TEST_F(PlantTest, shouldGenerateTree) +TEST_P(PlantTest, shouldGenerateTree) { - const auto generatedTree = tree(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); - ASSERT_TRUE( - std::ranges::any_of(trees, [generatedTree](const std::string_view& tree) { return tree == generatedTree; })); + const auto generatedTree = tree(locale); + + ASSERT_TRUE(std::ranges::any_of(plantDefinition.trees, + [generatedTree](const std::string_view& tree) { return tree == generatedTree; })); } -TEST_F(PlantTest, shouldGenerateFlower) +TEST_P(PlantTest, shouldGenerateFlower) { - const auto generatedFlower = flower(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); + + const auto generatedFlower = flower(locale); - ASSERT_TRUE(std::ranges::any_of(flowers, [generatedFlower](const std::string_view& flower) + ASSERT_TRUE(std::ranges::any_of(plantDefinition.flowers, [generatedFlower](const std::string_view& flower) { return flower == generatedFlower; })); } -TEST_F(PlantTest, shouldGenerateShrub) +TEST_P(PlantTest, shouldGenerateShrub) { - const auto generatedShrub = shrub(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); + + const auto generatedShrub = shrub(locale); - ASSERT_TRUE(std::ranges::any_of(shrubs, [generatedShrub](const std::string_view& shrub) + ASSERT_TRUE(std::ranges::any_of(plantDefinition.shrubs, [generatedShrub](const std::string_view& shrub) { return shrub == generatedShrub; })); } -TEST_F(PlantTest, shouldGenerateGrass) +TEST_P(PlantTest, shouldGenerateGrass) { - const auto generatedGrass = grass(); + const auto locale = GetParam(); - ASSERT_TRUE(std::ranges::any_of(grasses, [generatedGrass](const std::string_view& grass) + const auto& plantDefinition = getPlantDefinition(locale); + + const auto generatedGrass = grass(locale); + + ASSERT_TRUE(std::ranges::any_of(plantDefinition.grasses, [generatedGrass](const std::string_view& grass) { return grass == generatedGrass; })); } -TEST_F(PlantTest, shouldGenerateFern) +TEST_P(PlantTest, shouldGenerateFern) { - const auto generatedFern = fern(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); - ASSERT_TRUE( - std::ranges::any_of(ferns, [generatedFern](const std::string_view& fern) { return fern == generatedFern; })); + const auto generatedFern = fern(locale); + + ASSERT_TRUE(std::ranges::any_of(plantDefinition.ferns, + [generatedFern](const std::string_view& fern) { return fern == generatedFern; })); } -TEST_F(PlantTest, shouldGenerateSucculent) +TEST_P(PlantTest, shouldGenerateSucculent) { - const auto generatedSucculent = succulent(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); + + const auto generatedSucculent = succulent(locale); - ASSERT_TRUE(std::ranges::any_of(succulents, [generatedSucculent](const std::string_view& succulent) + ASSERT_TRUE(std::ranges::any_of(plantDefinition.succulents, [generatedSucculent](const std::string_view& succulent) { return succulent == generatedSucculent; })); } -TEST_F(PlantTest, shouldGenerateVine) +TEST_P(PlantTest, shouldGenerateVine) { - const auto generatedVine = vine(); + const auto locale = GetParam(); - ASSERT_TRUE( - std::ranges::any_of(vines, [generatedVine](const std::string_view& vine) { return vine == generatedVine; })); + const auto& plantDefinition = getPlantDefinition(locale); + + const auto generatedVine = vine(locale); + + ASSERT_TRUE(std::ranges::any_of(plantDefinition.vines, + [generatedVine](const std::string_view& vine) { return vine == generatedVine; })); } -TEST_F(PlantTest, shouldGenerateType) +TEST_P(PlantTest, shouldGenerateType) { - const auto generatedType = plantType(); + const auto locale = GetParam(); + + const auto& plantDefinition = getPlantDefinition(locale); - ASSERT_TRUE(std::ranges::any_of(plantTypes, [generatedType](const std::string_view& plantType) + const auto generatedType = plantType(locale); + + ASSERT_TRUE(std::ranges::any_of(plantDefinition.plantTypes, [generatedType](const std::string_view& plantType) { return plantType == generatedType; })); } + +INSTANTIATE_TEST_SUITE_P(TestPlantByLocale, PlantTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); From 1bda8a926f47c3ece2a3b67bda9888966bf22faa Mon Sep 17 00:00:00 2001 From: Mayank Kumar <103447831+mayank-Pareek@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:20:46 -0400 Subject: [PATCH 3/3] Add param doxygen tag to functions --- include/faker-cxx/plant.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/faker-cxx/plant.h b/include/faker-cxx/plant.h index 654d03d2..f6badc42 100644 --- a/include/faker-cxx/plant.h +++ b/include/faker-cxx/plant.h @@ -23,6 +23,8 @@ FAKER_CXX_EXPORT std::string_view tree(Locale locale = Locale::en_US); /** * @brief Returns a random species of flower. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of flower. * * @code @@ -34,6 +36,8 @@ FAKER_CXX_EXPORT std::string_view flower(Locale locale = Locale::en_US); /** * @brief Returns a random species of shrub. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of shrub. * * @code @@ -45,6 +49,8 @@ FAKER_CXX_EXPORT std::string_view shrub(Locale locale = Locale::en_US); /** * @brief Returns a random species of grass. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of grass. * * @code @@ -56,6 +62,8 @@ FAKER_CXX_EXPORT std::string_view grass(Locale locale = Locale::en_US); /** * @brief Returns a random species of fern. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of fern. * * @code @@ -67,6 +75,8 @@ FAKER_CXX_EXPORT std::string_view fern(Locale locale = Locale::en_US); /** * @brief Returns a random species of succulent. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of succulent. * * @code @@ -78,6 +88,8 @@ FAKER_CXX_EXPORT std::string_view succulent(Locale locale = Locale::en_US); /** * @brief Returns a random species of vine. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of vine. * * @code @@ -89,6 +101,8 @@ FAKER_CXX_EXPORT std::string_view vine(Locale locale = Locale::en_US); /** * @brief Returns a random type of plant. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Type of plant. * * @code