Skip to content

Commit

Permalink
feat: food-module locale implementation (#1010)
Browse files Browse the repository at this point in the history
* Added module constructor to both header (defination and the declaration

* Reverting the changes as they are directly pushed into main

* - Added localization definition to food_data.h.
- Added the struct to the food.cpp.
- Added the declaration to take en-US locale by default and updated doxygen comments for food.h.
- Updated test cases to take locale as arguments.
- used clang-format to format the files according to style guidelines
  • Loading branch information
SgtApone117 authored Dec 4, 2024
1 parent ac096d1 commit 25283fc
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 99 deletions.
57 changes: 43 additions & 14 deletions include/faker-cxx/food.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,189 @@
#include <string_view>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"

namespace faker::food
{
/**
* @brief Returns a random alcoholic beverage name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Alcoholic beverage.
*
* @code
* faker::food::alcoholicBeverage() // "champain"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view alcoholicBeverage();
FAKER_CXX_EXPORT std::string_view alcoholicBeverage(Locale locale = Locale::en_US);

/**
* @brief Returns a random grain measurement.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Grain.
*
* @code
* faker::food::grain() // "1 Lt"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view grain();
FAKER_CXX_EXPORT std::string_view grain(Locale locale = Locale::en_US);

/**
* @brief Returns a random milk product's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Milk product.
*
* @code
* faker::food::milkProduct() // "mozzarella"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view milkProduct();
FAKER_CXX_EXPORT std::string_view milkProduct(Locale locale = Locale::en_US);

/**
* @brief Returns a random fruit's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Fruit.
*
* @code
* faker::food::fruit() // "apple"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view fruit();
FAKER_CXX_EXPORT std::string_view fruit(Locale locale = Locale::en_US);

/**
* @brief Returns a random meat's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Meat.
*
* @code
* faker::food::meat() // "antrikot"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view meat();
FAKER_CXX_EXPORT std::string_view meat(Locale locale = Locale::en_US);

/**
* @brief Returns a random seafood's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Seafood.
*
* @code
* faker::food::seafood() // "lobster"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view seafood();
FAKER_CXX_EXPORT std::string_view seafood(Locale locale = Locale::en_US);

/**
* @brief Returns a random vegetable's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Vegetable.
*
* @code
* faker::food::vegetable() // "watermelon"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view vegetable();
FAKER_CXX_EXPORT std::string_view vegetable(Locale locale = Locale::en_US);

/**
* @brief Returns a random oil's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Oil.
*
* @code
* faker::food::oil() // "olive oil"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view oil();
FAKER_CXX_EXPORT std::string_view oil(Locale locale = Locale::en_US);

/**
* @brief Returns a random nut's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Nuts.
*
* @code
* faker::food::nut() // "walnut"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view nut();
FAKER_CXX_EXPORT std::string_view nut(Locale locale = Locale::en_US);

/**
* @brief Returns a random seed's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Seed.
*
* @code
* faker::food::seed() // "mozzarella"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view seed();
FAKER_CXX_EXPORT std::string_view seed(Locale locale = Locale::en_US);

/**
* @brief Returns a random sugar product's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Sugar product.
*
* @code
* faker::food::sugarProduct() // "honey harmony"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view sugarProduct();
FAKER_CXX_EXPORT std::string_view sugarProduct(Locale locale = Locale::en_US);

/**
* @brief Returns a random non-alcoholic beverage's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Non-alcoholic Beverage.
*
* @code
* faker::food::nonalcoholicBeverage() // "water"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view nonalcoholicBeverage();
FAKER_CXX_EXPORT std::string_view nonalcoholicBeverage(Locale locale = Locale::en_US);

/**
* @brief Returns a random dish's name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Dish.
*
* @code
* faker::food::dishName() // "beef wellington"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view dishName();
FAKER_CXX_EXPORT std::string_view dishName(Locale locale = Locale::en_US);

/**
* @brief Returns a random food categories' name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Food Category.
*
* @code
* faker::food::foodCategory() // "Dairy"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view foodCategory();
FAKER_CXX_EXPORT std::string_view foodCategory(Locale locale = Locale::en_US);
}
82 changes: 54 additions & 28 deletions src/modules/food.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,99 @@

namespace faker::food
{
std::string_view alcoholicBeverage()
namespace
{
return helper::randomElement(alcoholicBeverages);
const struct FoodDefinition& getFoodDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSFoodDefinition;
}
}
}

std::string_view alcoholicBeverage(Locale locale)
{
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.alcoholicBeverages);
}

std::string_view dishName()
std::string_view dishName(Locale locale)
{
return helper::randomElement(dishNames);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.dishNames);
}

std::string_view foodCategory()
std::string_view foodCategory(Locale locale)
{
return helper::randomElement(foodCategories);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.foodCategories);
}

std::string_view fruit()
std::string_view fruit(Locale locale)
{
return helper::randomElement(fruits);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.fruits);
}

std::string_view grain()
std::string_view grain(Locale locale)
{
return helper::randomElement(grains);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.grains);
}

std::string_view meat()
std::string_view meat(Locale locale)
{
return helper::randomElement(meats);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.meats);
}

std::string_view milkProduct()
std::string_view milkProduct(Locale locale)
{
return helper::randomElement(milkProducts);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.milkProducts);
}

std::string_view nonalcoholicBeverage()
std::string_view nonalcoholicBeverage(Locale locale)
{
return helper::randomElement(nonalcoholicBeverages);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.nonalcoholicBeverages);
}

std::string_view nut()
std::string_view nut(Locale locale)
{
return helper::randomElement(nuts);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.nuts);
}

std::string_view oil()
std::string_view oil(Locale locale)
{
return helper::randomElement(oils);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.oils);
}

std::string_view seafood()
std::string_view seafood(Locale locale)
{
return helper::randomElement(seafoods);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.seafoods);
}

std::string_view seed()
std::string_view seed(Locale locale)
{
return helper::randomElement(seeds);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.seeds);
}

std::string_view sugarProduct()
std::string_view sugarProduct(Locale locale)
{
return helper::randomElement(sugarProducts);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.sugarProducts);
}

std::string_view vegetable()
std::string_view vegetable(Locale locale)
{
return helper::randomElement(vegetables);
const auto& foodDefinition = getFoodDefinition(locale);
return helper::randomElement(foodDefinition.vegetables);
}
}
Loading

0 comments on commit 25283fc

Please sign in to comment.