Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Feature/science locale #1001

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/faker-cxx/science.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string_view>

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

namespace faker::science
Expand All @@ -23,7 +23,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
{
Expand All @@ -41,7 +41,7 @@ struct FAKER_CXX_EXPORT Unit
* 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.
Expand All @@ -53,7 +53,7 @@ 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.
Expand All @@ -64,7 +64,7 @@ FAKER_CXX_EXPORT Unit distanceUnit();
* 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.
Expand All @@ -75,7 +75,7 @@ FAKER_CXX_EXPORT Unit timeUnit();
* 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.
Expand All @@ -86,7 +86,7 @@ FAKER_CXX_EXPORT Unit massUnit();
* 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.
Expand All @@ -98,5 +98,5 @@ FAKER_CXX_EXPORT Unit tempUnit();
* current.
* @endcode
*/
FAKER_CXX_EXPORT Unit currentUnit();
FAKER_CXX_EXPORT Unit currentUnit(Locale locale = Locale::en_US);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add doxygen comments aboout new param

}
2 changes: 1 addition & 1 deletion src/modules/movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ std::string_view actress()
return helper::randomElement(actresses);
}

}
}
2 changes: 1 addition & 1 deletion src/modules/movie_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -1296,4 +1296,4 @@ const auto tvShows = std::to_array<std::string_view>({
"When They See Us",
});

}
}
62 changes: 43 additions & 19 deletions src/modules/science.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,71 @@

namespace faker::science
{
ChemicalElement chemicalElement()
namespace
{
const struct ScienceDefinition& getScienceDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSscienceDefinition;
}
}
}
ChemicalElement chemicalElement(Locale locale)
{
return helper::randomElement(chemicalElements);
const auto& scienceDefinition = getScienceDefinition(locale);

return helper::randomElement(scienceDefinition.chemicalElements);
}

Unit unit()
Unit unit(Locale locale)
{
const auto& scienceDefinition = getScienceDefinition(locale);

std::vector<Unit> 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);
}
}
33 changes: 26 additions & 7 deletions src/modules/science_data.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#pragma once

#include <array>

#include <span>
#include "faker-cxx/science.h"

namespace faker::science
{
const auto chemicalElements = std::to_array<ChemicalElement>(
struct ScienceDefinition
{
std::span<const ChemicalElement> chemicalElements;
std::span<const Unit> distanceUnits;
std::span<const Unit> massUnits;
std::span<const Unit> timeUnits;
std::span<const Unit> currentUnits;
std::span<const Unit> temperatureUnits;
};

const auto enUSchemicalElements = std::to_array<ChemicalElement>(
{{"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},
Expand Down Expand Up @@ -38,7 +48,7 @@ const auto chemicalElements = std::to_array<ChemicalElement>(
{"Nihonium", "Nh", 113}, {"Flerovium", "Fl", 114}, {"Moscovium", "Mc", 115}, {"Livermorium", "Lv", 116},
{"Tennessine", "Ts", 117}, {"Oganesson", "Og", 118}});

const auto distanceUnits = std::to_array<Unit>({
const auto enUSdistanceUnits = std::to_array<Unit>({
{"Millimeter", "mm", "Length"},
{"Centimeter", "cm", "Length"},
{"Meter", "m", "Length"},
Expand All @@ -49,7 +59,7 @@ const auto distanceUnits = std::to_array<Unit>({
{"Mile", "mi", "Length"},
});

const auto massUnits = std::to_array<Unit>({
const auto enUSmassUnits = std::to_array<Unit>({
{"Gram", "g", "Mass"},
{"Kilogram", "kg", "Mass"},
{"Milligram", "mg", "Mass"},
Expand All @@ -60,7 +70,7 @@ const auto massUnits = std::to_array<Unit>({
{"Slug", "sl", "Mass"},
});

const auto timeUnits = std::to_array<Unit>({
const auto enUStimeUnits = std::to_array<Unit>({
{"Second", "s", "Time"},
{"Minute", "min", "Time"},
{"Hour", "hr", "Time"},
Expand All @@ -70,16 +80,25 @@ const auto timeUnits = std::to_array<Unit>({
{"Year", "yr", "Time"},
});

const auto currentUnits = std::to_array<Unit>({
const auto enUScurrentUnits = std::to_array<Unit>({
{"Ampere", "A", "Electric Current"},
{"Milliampere", "mA", "Electric Current"},
{"Microampere", "μA", "Electric Current"},
});

const auto temperatureUnits = std::to_array<Unit>({
const auto enUStemperatureUnits = std::to_array<Unit>({
{"Celcius", "°C", "Temperature"},
{"Fahrenheit", "°F", "Temperature"},
{"Kelvin", "K", "Temperature"},
});

const ScienceDefinition enUSscienceDefinition = {
.chemicalElements = enUSchemicalElements,
.distanceUnits = enUSdistanceUnits,
.massUnits = enUSmassUnits,
.timeUnits = enUStimeUnits,
.currentUnits = enUScurrentUnits,
.temperatureUnits = enUStemperatureUnits,
};

}
95 changes: 69 additions & 26 deletions tests/modules/science_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale>
{
public:
static bool chemicalElementsAreEqual(const ChemicalElement& chemElement1, const ChemicalElement& chemElement2)
Expand All @@ -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<Unit> 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<Locale>& paramInfo) { return toString(paramInfo.param); });
Loading