diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d36a751e..fca5a305b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file * added locale to `book` module * added locale to `weather` module * added locale to `color` module +* added locale to `vehicle` module ## v3.0.0 (28.08.2024) diff --git a/include/faker-cxx/vehicle.h b/include/faker-cxx/vehicle.h index 20dd830b1..1d143a1a6 100644 --- a/include/faker-cxx/vehicle.h +++ b/include/faker-cxx/vehicle.h @@ -4,11 +4,13 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::vehicle { /** * @brief Returns a random bicycle type. + *@param locale The locale. Defaults to `Locale::en_US`. * * @returns bicycle type. * @@ -16,10 +18,12 @@ namespace faker::vehicle * faker::vehicle::bicycle() // "Electric bike" * @endcode */ -FAKER_CXX_EXPORT std::string_view bicycle(); +FAKER_CXX_EXPORT std::string_view bicycle(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle color. + * + *@param locale The locale. Defaults to `Locale::en_US`. * * @returns vehicle color. * @@ -27,10 +31,12 @@ FAKER_CXX_EXPORT std::string_view bicycle(); * faker::vehicle::color() // "Silver" * @endcode */ -FAKER_CXX_EXPORT std::string_view color(); +FAKER_CXX_EXPORT std::string_view color(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle fuel. + * + *@param locale The locale. Defaults to `Locale::en_US`. * * @returns vehicle fuel. * @@ -38,51 +44,59 @@ FAKER_CXX_EXPORT std::string_view color(); * faker::vehicle::fuel() // "Diesel" * @endcode */ -FAKER_CXX_EXPORT std::string_view fuel(); +FAKER_CXX_EXPORT std::string_view fuel(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle(car) manufacturer. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns vehicle(car) manufacturer. * * @code * faker::vehicle::manufacturer() // "Ferrari" * @endcode */ -FAKER_CXX_EXPORT std::string_view manufacturer(); +FAKER_CXX_EXPORT std::string_view manufacturer(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle(car) model. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns vehicle(car) model. * * @code * faker::vehicle::model() // "Fiesta" * @endcode */ -FAKER_CXX_EXPORT std::string_view model(); +FAKER_CXX_EXPORT std::string_view model(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle type. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns vehicle type. * * @code * faker::vehicle::type() // "Van" * @endcode */ -FAKER_CXX_EXPORT std::string_view type(); +FAKER_CXX_EXPORT std::string_view type(Locale locale = Locale::en_US); /** * @brief Returns a random vehicle(car). * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @returns vehicle composed by a manufacturer and model. * * @code * faker::vehicle::vehicleName() // "BMW Explorer" * @endcode */ -FAKER_CXX_EXPORT std::string vehicleName(); +FAKER_CXX_EXPORT std::string vehicleName(Locale locale = Locale::en_US); /** * @brief Returns a vehicle identification number (VIN). diff --git a/src/modules/vehicle.cpp b/src/modules/vehicle.cpp index c5cd81ff3..f49e6f326 100644 --- a/src/modules/vehicle.cpp +++ b/src/modules/vehicle.cpp @@ -7,43 +7,58 @@ #include "faker-cxx/helper.h" #include "faker-cxx/number.h" #include "faker-cxx/string.h" +#include "faker-cxx/types/locale.h" #include "vehicle_data.h" namespace faker::vehicle { -std::string_view bicycle() +namespace { - return helper::randomElement(bicycle_types); +const struct VehicleDefinition& getVehicleDefinition(Locale locale) +{ + return enUSVehicleDefinition ; +} +} + +std::string_view bicycle(Locale locale) +{ + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.bicycles); } -std::string_view color() +std::string_view color(Locale locale) { - return helper::randomElement(vehicle_colors); + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.colors); } -std::string_view fuel() +std::string_view fuel(Locale locale) { - return helper::randomElement(fuel_types); + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.fuelTypes); } -std::string_view manufacturer() +std::string_view manufacturer(Locale locale) { - return helper::randomElement(manufacturers); + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.manufacturers); } -std::string_view model() +std::string_view model(Locale locale) { - return helper::randomElement(models); + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.models); } -std::string_view type() +std::string_view type(Locale locale) { - return helper::randomElement(vehicle_types); + const auto& vehicleDefinition = getVehicleDefinition(locale); + return helper::randomElement(vehicleDefinition.vehicles); } -std::string vehicleName() +std::string vehicleName(Locale locale) { - return common::format("{} {}", manufacturer(), model()); + return common::format("{} {}", manufacturer(locale), model(locale)); } std::string vin() diff --git a/src/modules/vehicle_data.h b/src/modules/vehicle_data.h index 57522e223..884f2f846 100644 --- a/src/modules/vehicle_data.h +++ b/src/modules/vehicle_data.h @@ -1,11 +1,22 @@ #pragma once #include +#include #include namespace faker::vehicle { -const auto bicycle_types = std::to_array({ +struct VehicleDefinition +{ + std::span bicycles; + std::span colors; + std::span fuelTypes; + std::span manufacturers; + std::span models; + std::span vehicles; +}; + +const auto enUSBicycles = std::to_array({ "BMX bike", "Cargo bike", "City bike", @@ -18,7 +29,7 @@ const auto bicycle_types = std::to_array({ "Tandem bike", }); -const auto vehicle_colors = std::to_array({ +const auto enUSColors = std::to_array({ "Black", "Blue", "Gray", @@ -28,7 +39,7 @@ const auto vehicle_colors = std::to_array({ "White", }); -const auto fuel_types = std::to_array({ +const auto enUSFuelTypes = std::to_array({ "Biodiesel", "Diesel", "Electric", @@ -41,7 +52,7 @@ const auto fuel_types = std::to_array({ "Propane", }); -const auto manufacturers = std::to_array({ +const auto enUSManufacturers = std::to_array({ "Acura", "Alfa Romeo", "Aston Martin", "Audi", "BMW", "Bentley", "Cadillac", "Chevrolet", "Chrysler", "Dodge", "Ferrari", "Fiat", "Ford", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep", "Kia", "Lexus", "Lincoln", "Lotus", "Maserati", @@ -49,7 +60,7 @@ const auto manufacturers = std::to_array({ "Renault", "Rolls-Royce", "Subaru", "Suzuki", "Tesla", "Toyota", "Volkswagen", "Volvo", }); -const auto models = std::to_array({ +const auto enUSModels = std::to_array({ "Accord", "CR-V", "Camry", @@ -66,7 +77,7 @@ const auto models = std::to_array({ "Silverado 1500", }); -const auto vehicle_types = std::to_array({ +const auto enUSVehicles = std::to_array({ "Boat", "Bus", "Car", @@ -79,4 +90,13 @@ const auto vehicle_types = std::to_array({ "Van", }); +const VehicleDefinition enUSVehicleDefinition = { + .bicycles = enUSBicycles, + .colors = enUSColors, + .fuelTypes= enUSFuelTypes, + .manufacturers = enUSManufacturers, + .models = enUSModels, + .vehicles = enUSVehicles, +}; + } diff --git a/tests/modules/vehicle_test.cpp b/tests/modules/vehicle_test.cpp index 1664a3580..1f3db76f1 100644 --- a/tests/modules/vehicle_test.cpp +++ b/tests/modules/vehicle_test.cpp @@ -12,71 +12,100 @@ using namespace ::testing; using namespace faker; using namespace faker::vehicle; -class VehicleTest : public Test +namespace +{ +const struct VehicleDefinition& getVehicleDefinition(Locale locale) +{ + return enUSVehicleDefinition ; +} +} + +class VehicleTest : public TestWithParam { public: }; -TEST_F(VehicleTest, shouldGenerateBicycle) +TEST_P(VehicleTest, shouldGenerateBicycle) { - const auto generatedBicycle = bicycle(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); + + const auto generatedBicycle = bicycle(locale) ; - ASSERT_TRUE(std::ranges::any_of(bicycle_types, [generatedBicycle](const std::string_view& bicycle) + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.bicycles, [generatedBicycle](const std::string_view& bicycle) { return bicycle == generatedBicycle; })); } -TEST_F(VehicleTest, shouldGenerateColor) +TEST_P(VehicleTest, shouldGenerateColor) { - const auto generatedColor = color(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); - ASSERT_TRUE(std::ranges::any_of(vehicle_colors, [generatedColor](const std::string_view& color) + const auto generatedColor = color(locale) ; + + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.colors, [generatedColor](const std::string_view& color) { return color == generatedColor; })); } -TEST_F(VehicleTest, shouldGenerateFuel) +TEST_P(VehicleTest, shouldGenerateFuel) { - const auto generatedFuel = fuel(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); + + const auto generatedFuel = fuel(locale) ; - ASSERT_TRUE(std::ranges::any_of(fuel_types, + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.fuelTypes, [generatedFuel](const std::string_view& fuel) { return fuel == generatedFuel; })); } -TEST_F(VehicleTest, shouldGenerateManufacturer) +TEST_P(VehicleTest, shouldGenerateManufacturer) { - const auto generatedManufacturer = manufacturer(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); + + const auto generatedManufacturer = manufacturer(locale); - ASSERT_TRUE(std::ranges::any_of(manufacturers, [generatedManufacturer](const std::string_view& manufacturer) + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.manufacturers, [generatedManufacturer](const std::string_view& manufacturer) { return manufacturer == generatedManufacturer; })); } -TEST_F(VehicleTest, shouldGenerateModel) +TEST_P(VehicleTest, shouldGenerateModel) { - const auto generatedModel = model(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); - ASSERT_TRUE(std::ranges::any_of(models, [generatedModel](const std::string_view& model) + const auto generatedModel = model(locale); + + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.models, [generatedModel](const std::string_view& model) { return model == generatedModel; })); } -TEST_F(VehicleTest, shouldGenerateType) +TEST_P(VehicleTest, shouldGenerateType) { + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); + const auto generatedType = type(); - ASSERT_TRUE(std::ranges::any_of(vehicle_types, + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.vehicles, [generatedType](const std::string_view& type) { return type == generatedType; })); } -TEST_F(VehicleTest, shouldGenerateVehicle) +TEST_P(VehicleTest, shouldGenerateVehicle) { - const auto generatedVehicle = vehicleName(); + const auto locale = GetParam(); + const auto& vehicleDefinition = getVehicleDefinition(locale); + + const auto generatedVehicle = vehicleName(locale); - ASSERT_TRUE(std::ranges::any_of(manufacturers, [generatedVehicle](const std::string_view& manufacturer) + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.manufacturers, [generatedVehicle](const std::string_view& manufacturer) { return generatedVehicle.starts_with(manufacturer); })); - ASSERT_TRUE(std::ranges::any_of(models, [generatedVehicle](const std::string_view& model) + ASSERT_TRUE(std::ranges::any_of(vehicleDefinition.models, [generatedVehicle](const std::string_view& model) { return generatedVehicle.ends_with(model); })); } -TEST_F(VehicleTest, shouldGenerateVin) +TEST_P(VehicleTest, shouldGenerateVin) { const auto generatedVin = vin(); @@ -87,7 +116,7 @@ TEST_F(VehicleTest, shouldGenerateVin) ASSERT_TRUE(std::regex_match(generatedVin, match, vinRegex)); } -TEST_F(VehicleTest, shouldGenerateVrm) +TEST_P(VehicleTest, shouldGenerateVrm) { const auto generatedVrm = vrm(); @@ -97,3 +126,6 @@ TEST_F(VehicleTest, shouldGenerateVrm) ASSERT_TRUE(std::regex_match(generatedVrm, match, vrmRegex)); } + +INSTANTIATE_TEST_SUITE_P(TestBookByLocale, VehicleTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); }); \ No newline at end of file