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: Added locale to vehicle module #930

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 21 additions & 7 deletions include/faker-cxx/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,99 @@
#include <string_view>

#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.
*
* @code
* 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.
*
* @code
* 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.
*
* @code
* 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).
Expand Down
43 changes: 29 additions & 14 deletions src/modules/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
32 changes: 26 additions & 6 deletions src/modules/vehicle_data.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#pragma once

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

namespace faker::vehicle
{
const auto bicycle_types = std::to_array<std::string_view>({
struct VehicleDefinition
{
std::span<const std::string_view> bicycles;
std::span<const std::string_view> colors;
std::span<const std::string_view> fuelTypes;
std::span<const std::string_view> manufacturers;
std::span<const std::string_view> models;
std::span<const std::string_view> vehicles;
};

const auto enUSBicycles = std::to_array<std::string_view>({
"BMX bike",
"Cargo bike",
"City bike",
Expand All @@ -18,7 +29,7 @@ const auto bicycle_types = std::to_array<std::string_view>({
"Tandem bike",
});

const auto vehicle_colors = std::to_array<std::string_view>({
const auto enUSColors = std::to_array<std::string_view>({
"Black",
"Blue",
"Gray",
Expand All @@ -28,7 +39,7 @@ const auto vehicle_colors = std::to_array<std::string_view>({
"White",
});

const auto fuel_types = std::to_array<std::string_view>({
const auto enUSFuelTypes = std::to_array<std::string_view>({
"Biodiesel",
"Diesel",
"Electric",
Expand All @@ -41,15 +52,15 @@ const auto fuel_types = std::to_array<std::string_view>({
"Propane",
});

const auto manufacturers = std::to_array<std::string_view>({
const auto enUSManufacturers = std::to_array<std::string_view>({
"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",
"Mazda", "Mercedes-Benz", "Mini", "Mitsubishi", "Nissan", "Peugeot", "Porsche", "Ram",
"Renault", "Rolls-Royce", "Subaru", "Suzuki", "Tesla", "Toyota", "Volkswagen", "Volvo",
});

const auto models = std::to_array<std::string_view>({
const auto enUSModels = std::to_array<std::string_view>({
"Accord",
"CR-V",
"Camry",
Expand All @@ -66,7 +77,7 @@ const auto models = std::to_array<std::string_view>({
"Silverado 1500",
});

const auto vehicle_types = std::to_array<std::string_view>({
const auto enUSVehicles = std::to_array<std::string_view>({
"Boat",
"Bus",
"Car",
Expand All @@ -79,4 +90,13 @@ const auto vehicle_types = std::to_array<std::string_view>({
"Van",
});

const VehicleDefinition enUSVehicleDefinition = {
.bicycles = enUSBicycles,
.colors = enUSColors,
.fuelTypes= enUSFuelTypes,
.manufacturers = enUSManufacturers,
.models = enUSModels,
.vehicles = enUSVehicles,
};

}
Loading
Loading