diff --git a/include/faker-cxx/plant.h b/include/faker-cxx/plant.h index f9ae132a5..f6badc421 100644 --- a/include/faker-cxx/plant.h +++ b/include/faker-cxx/plant.h @@ -3,94 +3,111 @@ #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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of flower. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of shrub. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of grass. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of fern. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of succulent. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Species of vine. * * @code * 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. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Type of plant. * * @code * 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 49963bff5..f3d65d058 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 f92e91f22..99b0fa8c4 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, +}; + } diff --git a/tests/modules/plant_test.cpp b/tests/modules/plant_test.cpp index 3967839d4..4cade2f62 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); });