From 6a182e4513f5756612b5e99c697dd0c7a67f5065 Mon Sep 17 00:00:00 2001 From: Hossam Date: Mon, 17 Jun 2024 16:03:40 +0300 Subject: [PATCH] removed static functions, changed style to faker::airline --- include/faker-cxx/Airline.h | 21 ++++++------- src/modules/airline/Airline.cpp | 18 +++++------ src/modules/airline/AirlineData.cpp | 24 +++++++------- src/modules/airline/AirlineData.h | 12 +++---- tests/modules/airline/AirlineTest.cpp | 45 ++++++++++++++------------- 5 files changed, 59 insertions(+), 61 deletions(-) diff --git a/include/faker-cxx/Airline.h b/include/faker-cxx/Airline.h index 2519089be..12dba20f4 100644 --- a/include/faker-cxx/Airline.h +++ b/include/faker-cxx/Airline.h @@ -2,10 +2,8 @@ #include -namespace faker +namespace faker::airline { - namespace airline - { /** * @brief Get a random aircraft type * @@ -15,7 +13,7 @@ namespace faker * Airline::aircraftType // "narrowbody" * @endcode */ - static std::string_view aircraftType(); + std::string_view aircraftType(); struct Airplane { @@ -32,7 +30,7 @@ namespace faker * Airline::airplane() // {"Boeing 737-800", "738"} * @endcode */ - static Airplane airplane(); + Airplane airplane(); struct AirlineInfo { @@ -49,7 +47,7 @@ namespace faker * Airline::airline() // {"Air Canada", "AC"} * @endcode */ - static AirlineInfo airline(); + AirlineInfo airline(); struct Airport { @@ -66,7 +64,7 @@ namespace faker * Airline::airport() // {"Toronto Pearson International Airport", "YYZ"} * @endcode */ - static Airport airport(); + Airport airport(); enum class AircraftType { @@ -86,7 +84,7 @@ namespace faker * Airline::seat(AircraftType::Narrowbody) // "1A" * @endcode */ - static std::string seat(AircraftType aircraftType); + std::string seat(AircraftType aircraftType); /** * @brief Get a random record location @@ -98,7 +96,7 @@ namespace faker * 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 @@ -115,7 +113,7 @@ namespace faker * 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 { @@ -138,6 +136,5 @@ namespace faker * 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}); } -} diff --git a/src/modules/airline/Airline.cpp b/src/modules/airline/Airline.cpp index 2c1658d09..5c285ba00 100644 --- a/src/modules/airline/Airline.cpp +++ b/src/modules/airline/Airline.cpp @@ -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(aircraftTypeSeatLetters.at(aircraftType)); } -std::string airline::recordLocator(bool allowNumerics) +std::string recordLocator(bool allowNumerics) { if (allowNumerics) { @@ -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) { @@ -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) { diff --git a/src/modules/airline/AirlineData.cpp b/src/modules/airline/AirlineData.cpp index 87f861d43..cc0a90e85 100644 --- a/src/modules/airline/AirlineData.cpp +++ b/src/modules/airline/AirlineData.cpp @@ -6,11 +6,11 @@ #include "faker-cxx/Airline.h" -namespace faker +namespace faker::airline { const std::array aircraftTypes = {"regional", "narrowbody", "widebody"}; -const std::array airlines = {{ +const std::array airlines = {{ {"Aegean Airlines", "A3"}, {"Aeroflot", "SU"}, {"Aerolineas Argentinas", "AR"}, @@ -138,7 +138,7 @@ const std::array airlines = {{ {"Wizz Air", "W6"}, }}; -const std::array airplanes{{ +const std::array airplanes{{ {"Aerospatiale/BAC Concorde", "SSC"}, {"Airbus A300", "AB3"}, {"Airbus A310", "310"}, @@ -296,7 +296,7 @@ const std::array airplanes{{ {"Yakovlev Yak-42", "YK2"}, }}; -const std::array airports = {{ +const std::array airports = {{ {"Adelaide International Airport", "ADL"}, {"Adolfo Suarez Madrid-Barajas Airport", "MAD"}, {"Aeroparque Jorge Newbery Airport", "AEP"}, @@ -435,16 +435,16 @@ const std::array airports = {{ {"Zurich Airport", "ZRH"}, }}; -const std::unordered_map aircraftTypeMaxRows = { - {airline::AircraftType::Regional, 20}, - {airline::AircraftType::Narrowbody, 40}, - {airline::AircraftType::Widebody, 60}, +const std::unordered_map aircraftTypeMaxRows = { + {AircraftType::Regional, 20}, + {AircraftType::Narrowbody, 40}, + {AircraftType::Widebody, 60}, }; -const std::unordered_map aircraftTypeSeatLetters = { - {airline::AircraftType::Regional, "ABCD"}, - {airline::AircraftType::Narrowbody, "ABCDEF"}, - {airline::AircraftType::Widebody, "ABCDEFGHJK"}, +const std::unordered_map aircraftTypeSeatLetters = { + {AircraftType::Regional, "ABCD"}, + {AircraftType::Narrowbody, "ABCDEF"}, + {AircraftType::Widebody, "ABCDEFGHJK"}, }; } diff --git a/src/modules/airline/AirlineData.h b/src/modules/airline/AirlineData.h index 25573120c..8f2f9b448 100644 --- a/src/modules/airline/AirlineData.h +++ b/src/modules/airline/AirlineData.h @@ -5,12 +5,12 @@ #include "faker-cxx/Airline.h" -namespace faker +namespace faker::airline { extern const std::array aircraftTypes; -extern const std::array airlines; -extern const std::array airplanes; -extern const std::array airports; -extern const std::unordered_map aircraftTypeMaxRows; -extern const std::unordered_map aircraftTypeSeatLetters; +extern const std::array airlines; +extern const std::array airplanes; +extern const std::array airports; +extern const std::unordered_map aircraftTypeMaxRows; +extern const std::unordered_map aircraftTypeSeatLetters; } diff --git a/tests/modules/airline/AirlineTest.cpp b/tests/modules/airline/AirlineTest.cpp index 427fed171..5ab6f15de 100644 --- a/tests/modules/airline/AirlineTest.cpp +++ b/tests/modules/airline/AirlineTest.cpp @@ -10,7 +10,7 @@ #include "airline/AirlineData.h" using namespace ::testing; -using namespace faker; +using namespace faker::airline; class AirlineTest : public Test { @@ -19,7 +19,7 @@ class AirlineTest : public Test TEST_F(AirlineTest, shouldGenerateAircraftType) { - const auto generatedAircraftType = airline::aircraftType(); + const auto generatedAircraftType = aircraftType(); ASSERT_TRUE(std::ranges::any_of(aircraftTypes, [generatedAircraftType](const std::string_view& aircraftType) { return aircraftType == generatedAircraftType; })); @@ -27,19 +27,19 @@ TEST_F(AirlineTest, shouldGenerateAircraftType) TEST_F(AirlineTest, shouldGenerateAirline) { - const auto generatedAirline = airline::airline(); + const auto generatedAirline = airline(); ASSERT_TRUE(std::ranges::any_of( - airlines, [generatedAirline](const airline::AirlineInfo& airline) + airlines, [generatedAirline](const AirlineInfo& airline) { return airline.name == generatedAirline.name && airline.iataCode == generatedAirline.iataCode; })); } TEST_F(AirlineTest, shouldGenerateAirplane) { - const auto generatedAirplane = airline::airplane(); + const auto generatedAirplane = airplane(); ASSERT_TRUE(std::ranges::any_of(airplanes, - [generatedAirplane](const airline::Airplane& airplane) { + [generatedAirplane](const Airplane& airplane) { return airplane.name == generatedAirplane.name && airplane.iataTypeCode == generatedAirplane.iataTypeCode; })); @@ -47,21 +47,21 @@ TEST_F(AirlineTest, shouldGenerateAirplane) TEST_F(AirlineTest, shouldGenerateAirport) { - const auto generatedAirport = airline::airport(); + const auto generatedAirport = airport(); ASSERT_TRUE(std::ranges::any_of( - airports, [generatedAirport](const airline::Airport& airport) + airports, [generatedAirport](const Airport& airport) { return airport.name == generatedAirport.name && airport.iataCode == generatedAirport.iataCode; })); } TEST_F(AirlineTest, shouldGenerateRecordLocator) { - const auto generatedRecordLocatorWithAlpha = airline::recordLocator(false); + const auto generatedRecordLocatorWithAlpha = recordLocator(false); ASSERT_EQ(generatedRecordLocatorWithAlpha.length(), 6); ASSERT_TRUE(std::ranges::all_of(generatedRecordLocatorWithAlpha, [](const char& c) { return std::isalpha(c); })); - const auto generatedRecordLocatorWithNumerics = airline::recordLocator(true); + const auto generatedRecordLocatorWithNumerics = recordLocator(true); ASSERT_EQ(generatedRecordLocatorWithNumerics.length(), 6); ASSERT_TRUE(std::ranges::all_of(generatedRecordLocatorWithNumerics, [](const char& c) { return std::isalnum(c); })); @@ -69,12 +69,12 @@ TEST_F(AirlineTest, shouldGenerateRecordLocator) TEST_F(AirlineTest, shouldGenerateSeatNumberRegional) { - const auto generatedSeatNumber = airline::seat(airline::AircraftType::Regional); + const auto generatedSeatNumber = seat(AircraftType::Regional); ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3); int min = 1; - int max = aircraftTypeMaxRows.at(airline::AircraftType::Regional); + int max = aircraftTypeMaxRows.at(AircraftType::Regional); bool inRange = false; @@ -89,19 +89,19 @@ TEST_F(AirlineTest, shouldGenerateSeatNumberRegional) } ASSERT_TRUE(inRange); - ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(airline::AircraftType::Regional), + ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(AircraftType::Regional), [generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; })); } TEST_F(AirlineTest, shouldGenerateSeatNumberNarrowbody) { - const auto generatedSeatNumber = airline::seat(airline::AircraftType::Narrowbody); + const auto generatedSeatNumber = seat(AircraftType::Narrowbody); ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3); int min = 1; - int max = aircraftTypeMaxRows.at(airline::AircraftType::Narrowbody); + int max = aircraftTypeMaxRows.at(AircraftType::Narrowbody); bool inRange = false; @@ -118,19 +118,19 @@ TEST_F(AirlineTest, shouldGenerateSeatNumberNarrowbody) } ASSERT_TRUE(inRange); - ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(airline::AircraftType::Narrowbody), + ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(AircraftType::Narrowbody), [generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; })); } TEST_F(AirlineTest, shouldGenerateSeatNumberWidebody) { - const auto generatedSeatNumber = airline::seat(airline::AircraftType::Widebody); + const auto generatedSeatNumber = seat(AircraftType::Widebody); ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3); int min = 1; - int max = aircraftTypeMaxRows.at(airline::AircraftType::Widebody); + int max = aircraftTypeMaxRows.at(AircraftType::Widebody); bool inRange = false; @@ -147,14 +147,15 @@ TEST_F(AirlineTest, shouldGenerateSeatNumberWidebody) } ASSERT_TRUE(inRange); - ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(airline::AircraftType::Widebody), + ASSERT_TRUE(std::ranges::any_of(aircraftTypeSeatLetters.at(AircraftType::Widebody), [generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; })); } TEST_F(AirlineTest, shouldGenerateFlightNumberNoLeadingZeros) { - const auto flightNumber = airline::flightNumber(); + // called faker::airline to avoid ambiguous error + const auto flightNumber = faker::airline::flightNumber(); const auto flightNumberInt = std::stoi(flightNumber); ASSERT_TRUE(flightNumber.length() == 4); @@ -167,7 +168,7 @@ TEST_F(AirlineTest, shouldGenerateFlightNumberLeadingZeros) bool leadingZero = false; while (!leadingZero) { - const auto flightNumber = airline::flightNumber(true, 4); + const auto flightNumber = faker::airline::flightNumber(true, 4); if (flightNumber.substr(0, 1) == "0") { @@ -180,7 +181,7 @@ TEST_F(AirlineTest, shouldGenerateFlightNumberLeadingZeros) TEST_F(AirlineTest, shouldGenerateFlightNumberByRange) { - const auto flightNumber = airline::flightNumberByRange(false, {1, 6}); + const auto flightNumber = flightNumberByRange(false, {1, 6}); ASSERT_TRUE(flightNumber.length() <= 6); }