Skip to content

Commit

Permalink
feat: add locale to plant module (#970)
Browse files Browse the repository at this point in the history
* Add locale to plant module

* update test for plant module

* Add param doxygen tag to functions
  • Loading branch information
mayank-Pareek authored Oct 29, 2024
1 parent e730d22 commit 8dbe13a
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 60 deletions.
33 changes: 25 additions & 8 deletions include/faker-cxx/plant.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,111 @@
#include <string_view>

#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);
}
60 changes: 44 additions & 16 deletions src/modules/plant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
40 changes: 32 additions & 8 deletions src/modules/plant_data.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
#pragma once

#include <array>
#include <span>
#include <string_view>

namespace faker::plant
{
const auto trees = std::to_array<std::string_view>({
struct PlantDefinition
{
std::span<const std::string_view> trees;
std::span<const std::string_view> flowers;
std::span<const std::string_view> shrubs;
std::span<const std::string_view> grasses;
std::span<const std::string_view> ferns;
std::span<const std::string_view> succulents;
std::span<const std::string_view> vines;
std::span<const std::string_view> plantTypes;
};

const auto enUSTrees = std::to_array<std::string_view>({
"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",
Expand All @@ -15,7 +28,7 @@ const auto trees = std::to_array<std::string_view>({
"Spruce", "Sycamore", "Tamarind", "Walnut", "Willow",
});

const auto flowers = std::to_array<std::string_view>({
const auto enUSFlowers = std::to_array<std::string_view>({
"Adenium Obesum",
"African Daisy",
"Allamanda Cathartica",
Expand Down Expand Up @@ -110,7 +123,7 @@ const auto flowers = std::to_array<std::string_view>({
"Zinnia",
});

const auto shrubs = std::to_array<std::string_view>({
const auto enUSShrubs = std::to_array<std::string_view>({
"Aralia", "Arctostaphylos", "Aronia", "Artemisia", "Aucuba", "Berberis",
"Bougainvillea", "Brugmansia", "Buddleja", "Buxus", "Calia", "Callicarpa",
"Callistemon", "Calluna", "Calycanthus", "Camellia", "Caragana", "Carpenteria",
Expand Down Expand Up @@ -149,7 +162,7 @@ const auto shrubs = std::to_array<std::string_view>({
"Zenobia", "Ziziphus",
});

const auto grasses = std::to_array<std::string_view>({
const auto enUSGrasses = std::to_array<std::string_view>({
"Bahiagrass",
"Bahiagrass",
"Bamboo",
Expand Down Expand Up @@ -249,7 +262,7 @@ const auto grasses = std::to_array<std::string_view>({
"Yellow Indiangrass",
});

const auto ferns = std::to_array<std::string_view>({
const auto enUSFerns = std::to_array<std::string_view>({
"Adder's-tongue Fern",
"Angiopteris evecta",
"Beech Fern",
Expand Down Expand Up @@ -336,7 +349,7 @@ const auto ferns = std::to_array<std::string_view>({
"Zygopteridales",
});

const auto succulents = std::to_array<std::string_view>({
const auto enUSSucculents = std::to_array<std::string_view>({
"Aeonium",
"Aloe vera",
"Ammocharis",
Expand Down Expand Up @@ -470,7 +483,7 @@ const auto succulents = std::to_array<std::string_view>({
"Zephyrlily",
});

const auto vines = std::to_array<std::string_view>({
const auto enUSVines = std::to_array<std::string_view>({
"Akebia (Five-leaf Akebia)",
"Asparagus asparagoides (Sprenger's Asparagus)",
"Black-eyed Susan Vine (Thunbergia alata)",
Expand All @@ -497,7 +510,7 @@ const auto vines = std::to_array<std::string_view>({
"Wisteria",
});

const auto plantTypes = std::to_array<std::string_view>({
const auto enUSPlantTypes = std::to_array<std::string_view>({
"fern",
"flower",
"grass",
Expand All @@ -507,4 +520,15 @@ const auto plantTypes = std::to_array<std::string_view>({
"vine",
});

const PlantDefinition enUSPlantDefinition = {
.trees = enUSTrees,
.flowers = enUSFlowers,
.shrubs = enUSShrubs,
.grasses = enUSGrasses,
.ferns = enUSFerns,
.succulents = enUSSucculents,
.vines = enUSVines,
.plantTypes = enUSPlantTypes,
};

}
Loading

0 comments on commit 8dbe13a

Please sign in to comment.