Skip to content

Commit

Permalink
Moved all Airline structs to AirlineTypes.h (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-bodhi authored Jan 20, 2024
1 parent aa35136 commit fe828d5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 68 deletions.
32 changes: 1 addition & 31 deletions include/faker-cxx/Airline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

#include <string>
#include <vector>
#include "./types/AirlineTypes.h"

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

/**
* @brief Get a random aircraft type
*
Expand All @@ -26,12 +20,6 @@ class Airline
*/
static std::string aircraftType();

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

/**
* @brief Get a random airplane
*
Expand All @@ -43,12 +31,6 @@ class Airline
*/
static Airplane airplane();

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

/**
* @brief Get a random airline
*
Expand All @@ -60,12 +42,6 @@ class Airline
*/
static AirlineStruct airline();

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

/**
* @brief Get a random airport
*
Expand Down Expand Up @@ -119,12 +95,6 @@ class Airline
*/
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
*
Expand Down
39 changes: 39 additions & 0 deletions include/faker-cxx/types/AirlineTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "../Airline.h"
#include <string>

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

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

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


struct Range
{
unsigned int min;
unsigned int max;
};

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

16 changes: 8 additions & 8 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ std::string Airline::aircraftType()
return Helper::arrayElement<std::string>(aircraftTypes);
}

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

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

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

std::string Airline::seat(Airline::AircraftType aircraftType)
std::string Airline::seat(faker::AircraftType aircraftType)
{
return std::to_string(Number::integer(1, aircraftTypeMaxRows.at(aircraftType))) +
Helper::arrayElement<char>(aircraftTypeSeatLetters.at(aircraftType));
Expand All @@ -59,7 +59,7 @@ std::string Airline::flightNumber(bool addLeadingZeros, unsigned int length)
return String::numeric(length, false);
}

std::string Airline::flightNumber(bool addLeadingZeros, Airline::Range length)
std::string Airline::flightNumber(bool addLeadingZeros, faker::Range length)
{
if (addLeadingZeros)
{
Expand Down
30 changes: 15 additions & 15 deletions src/modules/airline/AirlineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ TEST_F(AirlineTest, shouldGenerateAircraftType)

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

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

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

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

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

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

Expand All @@ -72,12 +72,12 @@ TEST_F(AirlineTest, shouldGenerateRecordLocator)

TEST_F(AirlineTest, shouldGenerateSeatNumberRegional)
{
std::string generatedSeatNumber = Airline::seat(Airline::AircraftType::Regional);
std::string generatedSeatNumber = Airline::seat(faker::AircraftType::Regional);

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

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

bool inRange = false;
for (int i = max; i >= min; --i)
Expand All @@ -91,19 +91,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(faker::AircraftType::Regional),
[generatedSeatNumber](char letter)
{ return generatedSeatNumber.back() == letter; }));
}

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

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

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

bool inRange = false;
for (int i = max; i >= min; --i)
Expand All @@ -117,19 +117,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(faker::AircraftType::Narrowbody),
[generatedSeatNumber](char letter)
{ return generatedSeatNumber.back() == letter; }));
}

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

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

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

bool inRange = false;
for (int i = max; i >= min; --i)
Expand All @@ -143,7 +143,7 @@ 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(faker::AircraftType::Widebody),
[generatedSeatNumber](char letter)
{ return generatedSeatNumber.back() == letter; }));
}
2 changes: 1 addition & 1 deletion src/modules/airline/data/Airlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace faker
{
const std::vector<Airline::AirlineStruct> airlines{
const std::vector<faker::AirlineStruct> airlines{
{.name = "Aegean Airlines", .iataCode = "A3"},
{.name = "Aeroflot", .iataCode = "SU"},
{.name = "Aerolineas Argentinas", .iataCode = "AR"},
Expand Down
2 changes: 1 addition & 1 deletion src/modules/airline/data/Airplanes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace faker
{
const std::vector<Airline::Airplane> airplanes{
const std::vector<faker::Airplane> airplanes{
{.name = "Aerospatiale/BAC Concorde", .iataTypeCode = "SSC"},
{.name = "Airbus A300", .iataTypeCode = "AB3"},
{.name = "Airbus A310", .iataTypeCode = "310"},
Expand Down
2 changes: 1 addition & 1 deletion src/modules/airline/data/Airports.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace faker
{
const std::vector<Airline::Airport> airports = {
const std::vector<faker::Airport> airports = {
{.name = "Adelaide International Airport", .iataCode = "ADL"},
{.name = "Adolfo Suarez Madrid-Barajas Airport", .iataCode = "MAD"},
{.name = "Aeroparque Jorge Newbery Airport", .iataCode = "AEP"},
Expand Down
16 changes: 8 additions & 8 deletions src/modules/airline/data/Seat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
namespace faker
{

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

const std::unordered_map<Airline::AircraftType, std::vector<char>> aircraftTypeSeatLetters = {
{Airline::AircraftType::Regional, {'A', 'B', 'C', 'D'}},
{Airline::AircraftType::Narrowbody, {'A', 'B', 'C', 'D', 'E', 'F'}},
{Airline::AircraftType::Widebody, {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'}},
const std::unordered_map<faker::AircraftType, std::vector<char>> aircraftTypeSeatLetters = {
{faker::AircraftType::Regional, {'A', 'B', 'C', 'D'}},
{faker::AircraftType::Narrowbody, {'A', 'B', 'C', 'D', 'E', 'F'}},
{faker::AircraftType::Widebody, {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'}},
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/structure/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const std::map<faker::StructureToken, std::function<std::string()>> moduleFuncti
{faker::StructureToken::AirlineAirlineCode, []() { return faker::Airline::airline().iataCode; }},
{faker::StructureToken::AirlineAirportName, []() { return faker::Airline::airport().name; }},
{faker::StructureToken::AirlineAirportCode, []() { return faker::Airline::airport().iataCode; }},
{faker::StructureToken::AirlineSeat, []() { return faker::Airline::seat(faker::Airline::AircraftType()); }},
{faker::StructureToken::AirlineSeat, []() { return faker::Airline::seat(faker::AircraftType()); }},
{faker::StructureToken::AirlineRecordLocator, []() { return faker::Airline::recordLocator(); }},
{faker::StructureToken::AirlineFlightNumber, []() { return faker::Airline::flightNumber(false, 4); }},

Expand Down
4 changes: 2 additions & 2 deletions src/modules/structure/StructureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_F(StructureTest, shouldGenerateJson)
ASSERT_EQ(key4, "Actor name");

ASSERT_TRUE(
std::ranges::any_of(airports, [value1](const Airline::Airport& airport) { return airport.name == value1; }));
std::ranges::any_of(airports, [value1](const faker::Airport& airport) { return airport.name == value1; }));
ASSERT_TRUE(std::ranges::any_of(birds, [value2](const std::string& bird) { return bird == value2; }));
ASSERT_TRUE(std::ranges::any_of(titles, [value3](const std::string& title) { return title == value3; }));
ASSERT_TRUE(std::ranges::any_of(actors, [value4](const std::string& actor) { return actor == value4; }));
Expand Down Expand Up @@ -80,7 +80,7 @@ TEST_F(StructureTest, shouldGenerateCSV)
{
ASSERT_TRUE(std::ranges::any_of(actors, [&line](const std::string& actor)
{ return line.find(actor) != std::string::npos; }));
ASSERT_TRUE(std::ranges::any_of(airports, [&line](const Airline::Airport& airport)
ASSERT_TRUE(std::ranges::any_of(airports, [&line](const faker::Airport& airport)
{ return line.find(airport.name) != std::string::npos; }));
ASSERT_TRUE(std::ranges::any_of(birds, [&line](const std::string& bird)
{ return line.find(bird) != std::string::npos; }));
Expand Down

0 comments on commit fe828d5

Please sign in to comment.