From 4bb709854396aab36bc53983267adf9470f2328b Mon Sep 17 00:00:00 2001 From: Sumeet Khedkar Date: Mon, 2 Dec 2024 10:05:27 -0800 Subject: [PATCH] feat: Feature/science locale (#1001) * Added module constructor to both header (defination and the declaration * Reverting the changes as they are directly pushed into main * Added locale for science module(en_US) * fixed the formatting using clang-format * Added comments to the new parameters * Fixed the issue with formatting. --- include/faker-cxx/science.h | 29 ++++++++--- src/modules/science.cpp | 63 +++++++++++++++------- src/modules/science_data.h | 32 +++++++++--- tests/modules/science_test.cpp | 95 ++++++++++++++++++++++++---------- 4 files changed, 161 insertions(+), 58 deletions(-) diff --git a/include/faker-cxx/science.h b/include/faker-cxx/science.h index 9b1596336..a044f1907 100644 --- a/include/faker-cxx/science.h +++ b/include/faker-cxx/science.h @@ -3,6 +3,7 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::science { @@ -16,6 +17,8 @@ struct FAKER_CXX_EXPORT ChemicalElement /** * @brief Returns a random chemical element from the periodic table. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Chemical element. * * @code @@ -23,7 +26,7 @@ struct FAKER_CXX_EXPORT ChemicalElement * table. * @endcode */ -FAKER_CXX_EXPORT ChemicalElement chemicalElement(); +FAKER_CXX_EXPORT ChemicalElement chemicalElement(Locale locale = Locale::en_US); struct FAKER_CXX_EXPORT Unit { @@ -35,17 +38,21 @@ struct FAKER_CXX_EXPORT Unit /** * @brief Returns a unit of measurement for either distance, mass, time, temp, current. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Unit. * * @code * faker::science::unit() // Object of Unit containing info about a random unit of measurement. * @endcode */ -FAKER_CXX_EXPORT Unit unit(); +FAKER_CXX_EXPORT Unit unit(Locale locale = Locale::en_US); /** * @brief Returns a unit of measurement for either distance. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Distance unit. * * @code @@ -53,44 +60,52 @@ FAKER_CXX_EXPORT Unit unit(); * distance. * @endcode */ -FAKER_CXX_EXPORT Unit distanceUnit(); +FAKER_CXX_EXPORT Unit distanceUnit(Locale locale = Locale::en_US); /** * @brief Returns a unit of measurement for either time. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Time unit. * * @code * faker::science::timeUnit() // Object of Unit containing info about a random unit of measurement used to measure time. * @endcode */ -FAKER_CXX_EXPORT Unit timeUnit(); +FAKER_CXX_EXPORT Unit timeUnit(Locale locale = Locale::en_US); /** * @brief Returns a unit of measurement for either mass. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Mass unit. * * @code * faker::science::massUnit() // Object of Unit containing info about a random unit of measurement used to measure mass. * @endcode */ -FAKER_CXX_EXPORT Unit massUnit(); +FAKER_CXX_EXPORT Unit massUnit(Locale locale = Locale::en_US); /** * @brief Returns a unit of measurement for either temp. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Temperture unit. * * @code * faker::science::tempUnit() // Object of Unit containing info about a random unit of measurement used to measure temp. * @endcode */ -FAKER_CXX_EXPORT Unit tempUnit(); +FAKER_CXX_EXPORT Unit tempUnit(Locale locale = Locale::en_US); /** * @brief Returns a unit of measurement for either current. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns Current unit. * * @code @@ -98,5 +113,5 @@ FAKER_CXX_EXPORT Unit tempUnit(); * current. * @endcode */ -FAKER_CXX_EXPORT Unit currentUnit(); +FAKER_CXX_EXPORT Unit currentUnit(Locale locale = Locale::en_US); } diff --git a/src/modules/science.cpp b/src/modules/science.cpp index 2ba902831..650a0838c 100644 --- a/src/modules/science.cpp +++ b/src/modules/science.cpp @@ -5,47 +5,72 @@ namespace faker::science { -ChemicalElement chemicalElement() +namespace { - return helper::randomElement(chemicalElements); +const struct ScienceDefinition& getScienceDefinition(Locale locale) +{ + switch (locale) + { + default: + return enUSscienceDefinition; + } +} } -Unit unit() +ChemicalElement chemicalElement(Locale locale) { + const auto& scienceDefinition = getScienceDefinition(locale); + + return helper::randomElement(scienceDefinition.chemicalElements); +} + +Unit unit(Locale locale) +{ + const auto& scienceDefinition = getScienceDefinition(locale); + std::vector units; - units.insert(units.end(), distanceUnits.begin(), distanceUnits.end()); - units.insert(units.end(), massUnits.begin(), massUnits.end()); - units.insert(units.end(), timeUnits.begin(), timeUnits.end()); - units.insert(units.end(), currentUnits.begin(), currentUnits.end()); - units.insert(units.end(), temperatureUnits.begin(), temperatureUnits.end()); + units.insert(units.end(), scienceDefinition.distanceUnits.begin(), scienceDefinition.distanceUnits.end()); + units.insert(units.end(), scienceDefinition.massUnits.begin(), scienceDefinition.massUnits.end()); + units.insert(units.end(), scienceDefinition.timeUnits.begin(), scienceDefinition.timeUnits.end()); + units.insert(units.end(), scienceDefinition.currentUnits.begin(), scienceDefinition.currentUnits.end()); + units.insert(units.end(), scienceDefinition.temperatureUnits.begin(), scienceDefinition.temperatureUnits.end()); return helper::randomElement(units); } -Unit distanceUnit() +Unit distanceUnit(Locale locale) { - return helper::randomElement(distanceUnits); + const auto& scienceDefinition = getScienceDefinition(locale); + + return helper::randomElement(scienceDefinition.distanceUnits); } -Unit timeUnit() +Unit timeUnit(Locale locale) { - return helper::randomElement(timeUnits); + const auto& scienceDefinition = getScienceDefinition(locale); + + return helper::randomElement(scienceDefinition.timeUnits); } -Unit massUnit() +Unit massUnit(Locale locale) { - return helper::randomElement(massUnits); + const auto& scienceDefinition = getScienceDefinition(locale); + + return helper::randomElement(scienceDefinition.massUnits); } -Unit tempUnit() +Unit tempUnit(Locale locale) { - return helper::randomElement(temperatureUnits); + const auto& scienceDefinition = getScienceDefinition(locale); + + return helper::randomElement(scienceDefinition.temperatureUnits); } -Unit currentUnit() +Unit currentUnit(Locale locale) { - return helper::randomElement(currentUnits); -} + const auto& scienceDefinition = getScienceDefinition(locale); + return helper::randomElement(scienceDefinition.currentUnits); +} } diff --git a/src/modules/science_data.h b/src/modules/science_data.h index c7c1b9ef7..98f584c4a 100644 --- a/src/modules/science_data.h +++ b/src/modules/science_data.h @@ -1,12 +1,23 @@ #pragma once #include +#include #include "faker-cxx/science.h" namespace faker::science { -const auto chemicalElements = std::to_array( +struct ScienceDefinition +{ + std::span chemicalElements; + std::span distanceUnits; + std::span massUnits; + std::span timeUnits; + std::span currentUnits; + std::span temperatureUnits; +}; + +const auto enUSchemicalElements = std::to_array( {{"Hydrogen", "H", 1}, {"Helium", "He", 2}, {"Lithium", "Li", 3}, {"Beryllium", "Be", 4}, {"Boron", "B", 5}, {"Carbon", "C", 6}, {"Nitrogen", "N", 7}, {"Oxygen", "O", 8}, {"Fluorine", "F", 9}, {"Neon", "Ne", 10}, {"Sodium", "Na", 11}, {"Magnesium", "Mg", 12}, @@ -38,7 +49,7 @@ const auto chemicalElements = std::to_array( {"Nihonium", "Nh", 113}, {"Flerovium", "Fl", 114}, {"Moscovium", "Mc", 115}, {"Livermorium", "Lv", 116}, {"Tennessine", "Ts", 117}, {"Oganesson", "Og", 118}}); -const auto distanceUnits = std::to_array({ +const auto enUSdistanceUnits = std::to_array({ {"Millimeter", "mm", "Length"}, {"Centimeter", "cm", "Length"}, {"Meter", "m", "Length"}, @@ -49,7 +60,7 @@ const auto distanceUnits = std::to_array({ {"Mile", "mi", "Length"}, }); -const auto massUnits = std::to_array({ +const auto enUSmassUnits = std::to_array({ {"Gram", "g", "Mass"}, {"Kilogram", "kg", "Mass"}, {"Milligram", "mg", "Mass"}, @@ -60,7 +71,7 @@ const auto massUnits = std::to_array({ {"Slug", "sl", "Mass"}, }); -const auto timeUnits = std::to_array({ +const auto enUStimeUnits = std::to_array({ {"Second", "s", "Time"}, {"Minute", "min", "Time"}, {"Hour", "hr", "Time"}, @@ -70,16 +81,25 @@ const auto timeUnits = std::to_array({ {"Year", "yr", "Time"}, }); -const auto currentUnits = std::to_array({ +const auto enUScurrentUnits = std::to_array({ {"Ampere", "A", "Electric Current"}, {"Milliampere", "mA", "Electric Current"}, {"Microampere", "μA", "Electric Current"}, }); -const auto temperatureUnits = std::to_array({ +const auto enUStemperatureUnits = std::to_array({ {"Celcius", "°C", "Temperature"}, {"Fahrenheit", "°F", "Temperature"}, {"Kelvin", "K", "Temperature"}, }); +const ScienceDefinition enUSscienceDefinition = { + .chemicalElements = enUSchemicalElements, + .distanceUnits = enUSdistanceUnits, + .massUnits = enUSmassUnits, + .timeUnits = enUStimeUnits, + .currentUnits = enUScurrentUnits, + .temperatureUnits = enUStemperatureUnits, +}; + } diff --git a/tests/modules/science_test.cpp b/tests/modules/science_test.cpp index 2fd1ddca4..90d86684e 100644 --- a/tests/modules/science_test.cpp +++ b/tests/modules/science_test.cpp @@ -8,9 +8,21 @@ using namespace ::testing; using namespace faker; -using namespace science; +using namespace faker::science; -class ScienceTest : public Test +namespace +{ +const struct ScienceDefinition& getScienceDefinition(Locale locale) +{ + switch (locale) + { + default: + return enUSscienceDefinition; + } +} +} + +class ScienceTest : public TestWithParam { public: static bool chemicalElementsAreEqual(const ChemicalElement& chemElement1, const ChemicalElement& chemElement2) @@ -25,65 +37,96 @@ class ScienceTest : public Test } }; -TEST_F(ScienceTest, shouldGenerateChemElement) +TEST_P(ScienceTest, shouldGenerateChemElement) { - const auto generatedChemElement = chemicalElement(); + const auto locale = GetParam(); + + const auto& scienceDefinition = getScienceDefinition(locale); + + const auto generatedChemElement = chemicalElement(locale); - ASSERT_TRUE(std::ranges::any_of(chemicalElements, [generatedChemElement](const ChemicalElement& chemElement) + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.chemicalElements, [generatedChemElement](const ChemicalElement& chemElement) { return chemicalElementsAreEqual(generatedChemElement, chemElement); })); } -TEST_F(ScienceTest, shouldGenerateAnyUnit) +TEST_P(ScienceTest, shouldGenerateAnyUnit) { + const auto locale = GetParam(); + + const auto& scienceDefinition = getScienceDefinition(locale); + std::vector units; - units.insert(units.end(), distanceUnits.begin(), distanceUnits.end()); - units.insert(units.end(), massUnits.begin(), massUnits.end()); - units.insert(units.end(), timeUnits.begin(), timeUnits.end()); - units.insert(units.end(), currentUnits.begin(), currentUnits.end()); - units.insert(units.end(), temperatureUnits.begin(), temperatureUnits.end()); + units.insert(units.end(), scienceDefinition.distanceUnits.begin(), scienceDefinition.distanceUnits.end()); + units.insert(units.end(), scienceDefinition.massUnits.begin(), scienceDefinition.massUnits.end()); + units.insert(units.end(), scienceDefinition.timeUnits.begin(), scienceDefinition.timeUnits.end()); + units.insert(units.end(), scienceDefinition.currentUnits.begin(), scienceDefinition.currentUnits.end()); + units.insert(units.end(), scienceDefinition.temperatureUnits.begin(), scienceDefinition.temperatureUnits.end()); - const auto generatedAnyUnit = unit(); + const auto generatedAnyUnit = unit(locale); ASSERT_TRUE(std::ranges::any_of(units, [generatedAnyUnit](const Unit& unit) { return unitsAreEqual(generatedAnyUnit, unit); })); } -TEST_F(ScienceTest, shouldGenerateDistanceUnit) +TEST_P(ScienceTest, shouldGenerateDistanceUnit) { - const auto generatedDistanceUnit = distanceUnit(); + const auto locale = GetParam(); - ASSERT_TRUE(std::ranges::any_of(distanceUnits, [generatedDistanceUnit](const Unit& distanceUnit) + const auto& scienceDefinition = getScienceDefinition(locale); + + const auto generatedDistanceUnit = distanceUnit(locale); + + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.distanceUnits, [generatedDistanceUnit](const Unit& distanceUnit) { return unitsAreEqual(generatedDistanceUnit, distanceUnit); })); } -TEST_F(ScienceTest, shouldGenerateMassUnit) +TEST_P(ScienceTest, shouldGenerateMassUnit) { - const auto generatedMassUnit = massUnit(); + const auto locale = GetParam(); + + const auto& scienceDefinition = getScienceDefinition(locale); - ASSERT_TRUE(std::ranges::any_of(massUnits, [generatedMassUnit](const Unit& massUnit) + const auto generatedMassUnit = massUnit(locale); + + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.massUnits, [generatedMassUnit](const Unit& massUnit) { return unitsAreEqual(generatedMassUnit, massUnit); })); } -TEST_F(ScienceTest, shouldGenerateTimeUnit) +TEST_P(ScienceTest, shouldGenerateTimeUnit) { - const auto generatedTimeUnit = timeUnit(); + const auto locale = GetParam(); + + const auto& scienceDefinition = getScienceDefinition(locale); + + const auto generatedTimeUnit = timeUnit(locale); - ASSERT_TRUE(std::ranges::any_of(timeUnits, [generatedTimeUnit](const Unit& timeUnit) + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.timeUnits, [generatedTimeUnit](const Unit& timeUnit) { return unitsAreEqual(generatedTimeUnit, timeUnit); })); } -TEST_F(ScienceTest, shouldGenerateTempUnit) +TEST_P(ScienceTest, shouldGenerateTempUnit) { - const auto generatedTempUnit = tempUnit(); + const auto locale = GetParam(); - ASSERT_TRUE(std::ranges::any_of(temperatureUnits, [generatedTempUnit](const Unit& tempUnit) + const auto& scienceDefinition = getScienceDefinition(locale); + + const auto generatedTempUnit = tempUnit(locale); + + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.temperatureUnits, [generatedTempUnit](const Unit& tempUnit) { return unitsAreEqual(generatedTempUnit, tempUnit); })); } -TEST_F(ScienceTest, shouldGenerateCurrentUnit) +TEST_P(ScienceTest, shouldGenerateCurrentUnit) { + const auto locale = GetParam(); + + const auto& scienceDefinition = getScienceDefinition(locale); + const auto generatedCurrentUnit = currentUnit(); - ASSERT_TRUE(std::ranges::any_of(currentUnits, [generatedCurrentUnit](const Unit& currentUnit) + ASSERT_TRUE(std::ranges::any_of(scienceDefinition.currentUnits, [generatedCurrentUnit](const Unit& currentUnit) { return unitsAreEqual(generatedCurrentUnit, currentUnit); })); } + +INSTANTIATE_TEST_SUITE_P(TestScienceByLocale, ScienceTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); });