Skip to content

Commit

Permalink
removed static functions, changed style to faker::airline
Browse files Browse the repository at this point in the history
  • Loading branch information
Hossamfc9 committed Jun 17, 2024
1 parent 6972f48 commit 6a182e4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 61 deletions.
21 changes: 9 additions & 12 deletions include/faker-cxx/Airline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

#include <string_view>

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

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

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

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

enum class AircraftType
{
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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});
}
}
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;
}
45 changes: 23 additions & 22 deletions tests/modules/airline/AirlineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "airline/AirlineData.h"

using namespace ::testing;
using namespace faker;
using namespace faker::airline;

class AirlineTest : public Test
{
Expand All @@ -19,62 +19,62 @@ 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; }));
}

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;
}));
}

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); }));
}

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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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")
{
Expand All @@ -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);
}

0 comments on commit 6a182e4

Please sign in to comment.