Skip to content

Commit

Permalink
Feature/airline (#217)
Browse files Browse the repository at this point in the history
* Created Airline headers and source filse, compiles

* Added AirlineTests

---------

Co-authored-by: Michał Cieślar <[email protected]>
  • Loading branch information
eric-bodhi and cieslarmichal authored Nov 6, 2023
1 parent 0945261 commit 6c298fc
Show file tree
Hide file tree
Showing 9 changed files with 826 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(FAKER_SOURCES
src/modules/medicine/Medicine.cpp
src/modules/weather/Weather.cpp
src/common/WeatherHelper.cpp
src/modules/airline/Airline.cpp
)

set(FAKER_UT_SOURCES
Expand Down Expand Up @@ -82,6 +83,7 @@ set(FAKER_UT_SOURCES
src/modules/medicine/MedicineTest.cpp
src/modules/weather/WeatherTest.cpp
src/common/WeatherHelperTest.cpp
src/modules/airline/AirlineTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})
Expand Down
140 changes: 140 additions & 0 deletions include/faker-cxx/Airline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#pragma once

#include <string>
#include <vector>

namespace faker {
class Airline {
public:
enum class AircraftType
{
Regional,
Narrowbody,
Widebody,
};

/*
* @brief Get a random aircraft type
*
* @return a random aircraft type
*
* @code
* Airline::aircraftType // "narrowbody"
*/
static std::string aircraftType();

struct Airplane
{
std::string name;
std::string iataTypeCode;
};

/*
* @brief Get a random airplane
*
* @return a random airplane and its iataTypeCode
*
* @code
* Airline::airplane() // {"Boeing 737-800", "738"}
* @endcode
*/
static Airplane airplane();

struct AirlineStruct
{
std::string name;
std::string iataCode;
};

/*
* @brief Get a random airline
*
* @return a random airline and its iataCode
*
* @code
* Airline::airline() // {"Air Canada", "AC"}
* @endcode
*/
static AirlineStruct airline();

struct Airport
{
std::string name;
std::string iataCode;
};

/*
* @brief Get a random airport
*
* @return a random airport and its iataCode
*
* @code
* Airline::airport() // {"Toronto Pearson International Airport", "YYZ"}
* @endcode
*/
static Airport airport();

/*
* @brief Get a random seat by aircraft type
*
* @param aircraftType the aircraft type
*
* @return a random seat
*
* @code
* Airline::seat(AircraftType::Narrowbody) // "1A"
* @endcode
*/
static std::string seat(AircraftType aircraftType);

/*
* @brief Get a random record location
*
* @return a random record location
*
* @code
* Airline::recordLocator() // "ABCDEF"
* Airline::recordLocator(true) // "ABC123"
* @endcode
*/
static std::string recordLocator(bool allowNumerics = false);

/*
* @brief Get a random flight number from given length
*
* @param addLeadingZeros whether to add leading zeros
*
* @param length the length of the flight number
*
* @return a random flight number
*
* @code
* Airline::flightNumber() // "1234"
* Airline::flightNumber(true) // "0123"
* Airline::flightNumber(false, 3) // "234"
* @endcode
*/
static std::string flightNumber(bool addLeadingZeros = false, unsigned int length = 4);

struct Range {
unsigned int min;
unsigned int max;
};
/*
* @brief Get a random flight number from given length
*
* @param addLeadingZeros whether to add leading zeros
*
* @param length the length of the flight number
*
* @return a random flight number
*
* @code
* Airline::flightNumber() // "1234"
* Airline::flightNumber(true) // "0123"
* Airline::flightNumber(false, {1, 4}) // "234" // "12" // "1234"
* @endcode
*/
static std::string flightNumber(bool addLeadingZeros = false, Range length = {1, 4});
};
}
64 changes: 64 additions & 0 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "faker-cxx/Airline.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"

#include <string>

#include "data/AircraftTypes.h"
#include "data/Airplanes.h"
#include "data/Airlines.h"
#include "data/Airports.h"
#include "data/Seat.h"

namespace faker
{
std::string Airline::aircraftType()
{
return Helper::arrayElement<std::string>(aircraftTypes);
}

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

Airline::AirlineStruct Airline::airline()
{
return Helper::arrayElement<Airline::AirlineStruct>(airlines);
}

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

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

std::string Airline::recordLocator(bool allowNumerics)
{
if (allowNumerics) {
return String::alphanumeric(6, StringCasing::Upper);
}

return String::alpha(6, StringCasing::Upper);
}

std::string Airline::flightNumber(bool addLeadingZeros, unsigned int length) {
if (addLeadingZeros) {
return String::numeric(length, true);
}

return String::numeric(length, false);
}

std::string Airline::flightNumber(bool addLeadingZeros, Airline::Range length) {
if (addLeadingZeros) {
return String::numeric(Number::integer(length.min, length.max), true);
}

return String::numeric(Number::integer(length.min, length.max), false);
}
}
134 changes: 134 additions & 0 deletions src/modules/airline/AirlineTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "faker-cxx/Airline.h"
#include "gtest/gtest.h"

#include <algorithm>
#include <string>

#include "data/AircraftTypes.h"
#include "data/Airplanes.h"
#include "data/Airlines.h"
#include "data/Airports.h"
#include "data/Seat.h"

using namespace ::testing;
using namespace faker;

class AirlineTest : public Test
{
public:
};

TEST_F(AirlineTest, shouldGenerateAircraftType)
{
std::string generatedAircraftType = Airline::aircraftType();

ASSERT_TRUE(std::ranges::any_of(aircraftTypes, [generatedAircraftType](const std::string& aircraftType)
{ return aircraftType == generatedAircraftType; }));
}

TEST_F(AirlineTest, shouldGenerateAirline) {
Airline::AirlineStruct generatedAirline = Airline::airline();

ASSERT_TRUE(std::ranges::any_of(airlines, [generatedAirline](const Airline::AirlineStruct& airline)
{ return airline.name == generatedAirline.name && airline.iataCode == generatedAirline.iataCode; }));
}

TEST_F(AirlineTest, shouldGenerateAirplane) {
Airline::Airplane generatedAirplane = Airline::airplane();

ASSERT_TRUE(std::ranges::any_of(airplanes, [generatedAirplane](const Airline::Airplane& airplane)
{ return airplane.name == generatedAirplane.name && airplane.iataTypeCode == generatedAirplane.iataTypeCode; }));
}

TEST_F(AirlineTest, shouldGenerateAirport) {
Airline::Airport generatedAirport = Airline::airport();

ASSERT_TRUE(std::ranges::any_of(airports, [generatedAirport](const Airline::Airport& airport)
{ return airport.name == generatedAirport.name && airport.iataCode == generatedAirport.iataCode; }));
}

TEST_F(AirlineTest, shouldGenerateRecordLocator) {
std::string generatedRecordLocatorWithAlpha = Airline::recordLocator(false);

ASSERT_EQ(generatedRecordLocatorWithAlpha.length(), 6);
ASSERT_TRUE(std::ranges::all_of(generatedRecordLocatorWithAlpha, [](const char& c)
{ return std::isalpha(c); }));

std::string generatedRecordLocatorWithNumerics = Airline::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) {
std::string generatedSeatNumber = Airline::seat(Airline::AircraftType::Regional);

ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3);

int min = 1;
int max = aircraftTypeMaxRows.at(Airline::AircraftType::Regional);

bool inRange = false;
for (int i = max; i >= min; --i) {
std::string numberStr = std::to_string(i);
if (generatedSeatNumber.find(numberStr) != std::string::npos) {
inRange = true;
break;
}
}

ASSERT_TRUE(inRange);
ASSERT_TRUE(std::ranges::any_of(
aircraftTypeSeatLetters.at(Airline::AircraftType::Regional),
[generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; }
));
}

TEST_F(AirlineTest, shouldGenerateSeatNumberNarrowbody) {
std::string generatedSeatNumber = Airline::seat(Airline::AircraftType::Narrowbody);

ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3);

int min = 1;
int max = aircraftTypeMaxRows.at(Airline::AircraftType::Narrowbody);

bool inRange = false;
for (int i = max; i >= min; --i) {
std::string numberStr = std::to_string(i);
if (generatedSeatNumber.find(numberStr) != std::string::npos) {
inRange = true;
break;
}
}

ASSERT_TRUE(inRange);
ASSERT_TRUE(std::ranges::any_of(
aircraftTypeSeatLetters.at(Airline::AircraftType::Narrowbody),
[generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; }
));
}

TEST_F(AirlineTest, shouldGenerateSeatNumberWidebody) {
std::string generatedSeatNumber = Airline::seat(Airline::AircraftType::Widebody);

ASSERT_TRUE(generatedSeatNumber.length() == 2 || generatedSeatNumber.length() == 3);

int min = 1;
int max = aircraftTypeMaxRows.at(Airline::AircraftType::Widebody);

bool inRange = false;
for (int i = max; i >= min; --i) {
std::string numberStr = std::to_string(i);
if (generatedSeatNumber.find(numberStr) != std::string::npos) {
inRange = true;
break;
}
}

ASSERT_TRUE(inRange);
ASSERT_TRUE(std::ranges::any_of(
aircraftTypeSeatLetters.at(Airline::AircraftType::Widebody),
[generatedSeatNumber](char letter) { return generatedSeatNumber.back() == letter; }
));
}
8 changes: 8 additions & 0 deletions src/modules/airline/data/AircraftTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>
#include <vector>

namespace faker {
const std::vector<std::string> aircraftTypes = {"regional", "narrowbody", "widebody"};
}
Loading

0 comments on commit 6c298fc

Please sign in to comment.