Skip to content

Commit

Permalink
Feature/migrate airline to namespace (#691)
Browse files Browse the repository at this point in the history
* added plant Module to README

* added trees & flowers

* migrate airline to namespace and edit on CMakeLists

* removed static functions, changed style to faker::airline
  • Loading branch information
Hossamfc9 authored Jun 17, 2024
1 parent c12f945 commit df8b5d8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 89 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ project(faker-cxx LANGUAGES CXX)
include(cmake/CompilerWarnings.cmake)

option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF)
option(USE_STD_FORMAT "Use std::format when avaiable" ON)
option(USE_STD_FORMAT "Use std::format when available" ON)
option(BUILD_EXAMPLES "Build examples" OFF)
option(BUILD_TESTING "Build tests" ON)

if (USE_STD_FORMAT)
set(CMAKE_REQUIRED_FLAGS -std=c++20)
Expand Down
26 changes: 11 additions & 15 deletions include/faker-cxx/Airline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

#include <string_view>

namespace faker
namespace faker::airline
{
class Airline
{
public:
/**
/**
* @brief Get a random aircraft type
*
* @return a random aircraft type
Expand All @@ -16,7 +13,7 @@ class Airline
* Airline::aircraftType // "narrowbody"
* @endcode
*/
static std::string_view aircraftType();
std::string_view aircraftType();

struct Airplane
{
Expand All @@ -33,7 +30,7 @@ class Airline
* Airline::airplane() // {"Boeing 737-800", "738"}
* @endcode
*/
static Airplane airplane();
Airplane airplane();

struct AirlineInfo
{
Expand All @@ -50,7 +47,7 @@ class Airline
* Airline::airline() // {"Air Canada", "AC"}
* @endcode
*/
static AirlineInfo airline();
AirlineInfo airline();

struct Airport
{
Expand All @@ -67,7 +64,7 @@ class Airline
* Airline::airport() // {"Toronto Pearson International Airport", "YYZ"}
* @endcode
*/
static Airport airport();
Airport airport();

enum class AircraftType
{
Expand All @@ -87,7 +84,7 @@ class Airline
* Airline::seat(AircraftType::Narrowbody) // "1A"
* @endcode
*/
static std::string seat(AircraftType aircraftType);
std::string seat(AircraftType aircraftType);

/**
* @brief Get a random record location
Expand All @@ -99,7 +96,7 @@ class Airline
* Airline::recordLocator(true) // "ABC123"
* @endcode
*/
static std::string recordLocator(bool allowNumerics = false);
std::string recordLocator(bool allowNumerics = false);

/**
* @brief Get a random flight number from given length
Expand All @@ -116,7 +113,7 @@ class Airline
* Airline::flightNumber(false, 3) // "234"
* @endcode
*/
static std::string flightNumber(bool addLeadingZeros = false, unsigned int length = 4);
std::string flightNumber(bool addLeadingZeros = false, unsigned int length = 4);

struct Range
{
Expand All @@ -139,6 +136,5 @@ class Airline
* Airline::flightNumber(false, {1, 4}) // "234" // "12" // "1234"
* @endcode
*/
static std::string flightNumberByRange(bool addLeadingZeros = false, Range length = {1, 4});
};
}
std::string flightNumberByRange(bool addLeadingZeros = false, Range length = {1, 4});
}
18 changes: 9 additions & 9 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"

namespace faker
namespace faker::airline
{
std::string_view Airline::aircraftType()
std::string_view aircraftType()
{
return Helper::arrayElement(aircraftTypes);
}

Airline::Airplane Airline::airplane()
Airplane airplane()
{
return Helper::arrayElement(airplanes);
}

Airline::AirlineInfo Airline::airline()
AirlineInfo airline()
{
return Helper::arrayElement(airlines);
}

Airline::Airport Airline::airport()
Airport airport()
{
return Helper::arrayElement(airports);
}

std::string Airline::seat(Airline::AircraftType aircraftType)
std::string seat(AircraftType aircraftType)
{
return std::to_string(Number::integer(1, aircraftTypeMaxRows.at(aircraftType))) +
Helper::arrayElement<char>(aircraftTypeSeatLetters.at(aircraftType));
}

std::string Airline::recordLocator(bool allowNumerics)
std::string recordLocator(bool allowNumerics)
{
if (allowNumerics)
{
Expand All @@ -46,7 +46,7 @@ std::string Airline::recordLocator(bool allowNumerics)
return String::alpha(6, StringCasing::Upper);
}

std::string Airline::flightNumber(bool addLeadingZeros, unsigned int length)
std::string flightNumber(bool addLeadingZeros, unsigned int length)
{
if (addLeadingZeros)
{
Expand All @@ -56,7 +56,7 @@ std::string Airline::flightNumber(bool addLeadingZeros, unsigned int length)
return String::numeric(length, false);
}

std::string Airline::flightNumberByRange(bool addLeadingZeros, Airline::Range length)
std::string flightNumberByRange(bool addLeadingZeros, Range length)
{
if (addLeadingZeros)
{
Expand Down
24 changes: 12 additions & 12 deletions src/modules/airline/AirlineData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#include "faker-cxx/Airline.h"

namespace faker
namespace faker::airline
{
const std::array<std::string_view, 3> aircraftTypes = {"regional", "narrowbody", "widebody"};

const std::array<Airline::AirlineInfo, 125> airlines = {{
const std::array<AirlineInfo, 125> airlines = {{
{"Aegean Airlines", "A3"},
{"Aeroflot", "SU"},
{"Aerolineas Argentinas", "AR"},
Expand Down Expand Up @@ -138,7 +138,7 @@ const std::array<Airline::AirlineInfo, 125> airlines = {{
{"Wizz Air", "W6"},
}};

const std::array<Airline::Airplane, 155> airplanes{{
const std::array<Airplane, 155> airplanes{{
{"Aerospatiale/BAC Concorde", "SSC"},
{"Airbus A300", "AB3"},
{"Airbus A310", "310"},
Expand Down Expand Up @@ -296,7 +296,7 @@ const std::array<Airline::Airplane, 155> airplanes{{
{"Yakovlev Yak-42", "YK2"},
}};

const std::array<Airline::Airport, 119> airports = {{
const std::array<Airport, 119> airports = {{
{"Adelaide International Airport", "ADL"},
{"Adolfo Suarez Madrid-Barajas Airport", "MAD"},
{"Aeroparque Jorge Newbery Airport", "AEP"},
Expand Down Expand Up @@ -435,16 +435,16 @@ const std::array<Airline::Airport, 119> airports = {{
{"Zurich Airport", "ZRH"},
}};

const std::unordered_map<Airline::AircraftType, int> aircraftTypeMaxRows = {
{Airline::AircraftType::Regional, 20},
{Airline::AircraftType::Narrowbody, 40},
{Airline::AircraftType::Widebody, 60},
const std::unordered_map<AircraftType, int> aircraftTypeMaxRows = {
{AircraftType::Regional, 20},
{AircraftType::Narrowbody, 40},
{AircraftType::Widebody, 60},
};

const std::unordered_map<Airline::AircraftType, std::string_view> aircraftTypeSeatLetters = {
{Airline::AircraftType::Regional, "ABCD"},
{Airline::AircraftType::Narrowbody, "ABCDEF"},
{Airline::AircraftType::Widebody, "ABCDEFGHJK"},
const std::unordered_map<AircraftType, std::string_view> aircraftTypeSeatLetters = {
{AircraftType::Regional, "ABCD"},
{AircraftType::Narrowbody, "ABCDEF"},
{AircraftType::Widebody, "ABCDEFGHJK"},
};

}
12 changes: 6 additions & 6 deletions src/modules/airline/AirlineData.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

#include "faker-cxx/Airline.h"

namespace faker
namespace faker::airline
{
extern const std::array<std::string_view, 3> aircraftTypes;
extern const std::array<Airline::AirlineInfo, 125> airlines;
extern const std::array<Airline::Airplane, 155> airplanes;
extern const std::array<Airline::Airport, 119> airports;
extern const std::unordered_map<Airline::AircraftType, int> aircraftTypeMaxRows;
extern const std::unordered_map<Airline::AircraftType, std::string_view> aircraftTypeSeatLetters;
extern const std::array<AirlineInfo, 125> airlines;
extern const std::array<Airplane, 155> airplanes;
extern const std::array<Airport, 119> airports;
extern const std::unordered_map<AircraftType, int> aircraftTypeMaxRows;
extern const std::unordered_map<AircraftType, std::string_view> aircraftTypeSeatLetters;
}
33 changes: 9 additions & 24 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,24 @@ target_include_directories(${PROJECT_NAME} PRIVATE

if (USE_SYSTEM_DEPENDENCIES)
find_package(GTest REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest
GTest::gtest_main GTest::gmock GTest::gmock_main faker-cxx)
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main faker-cxx)
else ()
add_subdirectory("${CMAKE_SOURCE_DIR}/externals/googletest"
"${CMAKE_BINARY_DIR}/googletest")
add_subdirectory("${CMAKE_SOURCE_DIR}/externals/googletest" "${CMAKE_BINARY_DIR}/googletest")

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")
set(GTEST_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

target_link_libraries(${PROJECT_NAME} PRIVATE gtest_main gmock_main
faker-cxx)
target_link_libraries(${PROJECT_NAME} PRIVATE gtest_main gmock_main faker-cxx)
endif ()

if (HAS_STD_FORMAT)
target_include_directories(
${PROJECT_NAME}
PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_STD_FORMAT)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE
$<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(
${PROJECT_NAME}
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(${PROJECT_NAME} PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${CMAKE_CURRENT_LIST_DIR})
endif ()

add_test(
NAME ${PROJECT_NAME}
COMMAND ${PROJECT_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

target_code_coverage(${PROJECT_NAME} ALL)
Loading

0 comments on commit df8b5d8

Please sign in to comment.