From e3dc6219c53b68408289e0588d4ef7c7f698abbb Mon Sep 17 00:00:00 2001 From: alisedighmoghadam Date: Sun, 24 Dec 2023 22:17:58 -0500 Subject: [PATCH] add structure module and JsonHelper --- .gitmodules | 3 + CMakeLists.txt | 8 +- faker-cxx | 1 + include/faker-cxx/Structure.h | 662 ++++++++++++++++++++++++ src/common/JsonHelper.cpp | 31 ++ src/common/JsonHelper.h | 18 + src/common/JsonHelperTest.cpp | 29 ++ src/modules/structure/Structure.cpp | 57 ++ src/modules/structure/StructureTest.cpp | 98 ++++ 9 files changed, 905 insertions(+), 2 deletions(-) create mode 160000 faker-cxx create mode 100644 include/faker-cxx/Structure.h create mode 100644 src/common/JsonHelper.cpp create mode 100644 src/common/JsonHelper.h create mode 100644 src/common/JsonHelperTest.cpp create mode 100644 src/modules/structure/Structure.cpp create mode 100644 src/modules/structure/StructureTest.cpp diff --git a/.gitmodules b/.gitmodules index 0222bacd1..3505468f7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "externals/fmt"] path = externals/fmt url = https://github.com/fmtlib/fmt.git +[submodule "faker-cxx"] + path = faker-cxx + url = https://github.com/cieslarmichal/faker-cxx.git diff --git a/CMakeLists.txt b/CMakeLists.txt index e00fc7976..34c6138e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,9 @@ set(FAKER_SOURCES src/modules/crypto/Crypto.cpp src/modules/computer/Computer.cpp src/modules/vehicle/Vehicle.cpp - src/modules/science/Science.cpp) + src/modules/science/Science.cpp + src/modules/structure/Structure.cpp + src/common/JsonHelper.cpp) set(FAKER_UT_SOURCES src/modules/animal/AnimalTest.cpp @@ -100,7 +102,9 @@ set(FAKER_UT_SOURCES src/modules/crypto/CryptoTest.cpp src/modules/computer/ComputerTest.cpp src/modules/vehicle/VehicleTest.cpp - src/modules/science/ScienceTest.cpp) + src/modules/science/ScienceTest.cpp + src/modules/structure/StructureTest.cpp + src/common/JsonHelperTest.cpp) add_library(${LIBRARY_NAME} ${FAKER_SOURCES}) diff --git a/faker-cxx b/faker-cxx new file mode 160000 index 000000000..d59e81306 --- /dev/null +++ b/faker-cxx @@ -0,0 +1 @@ +Subproject commit d59e81306af986ddd460cb4ca4bc23c7eba78b7b diff --git a/include/faker-cxx/Structure.h b/include/faker-cxx/Structure.h new file mode 100644 index 000000000..82d032723 --- /dev/null +++ b/include/faker-cxx/Structure.h @@ -0,0 +1,662 @@ +#pragma once +#include +#include +#include + +#include "Airline.h" +#include "Animal.h" +#include "Book.h" +#include "Color.h" +#include "Commerce.h" +#include "Company.h" +#include "Computer.h" +#include "Crypto.h" +#include "Database.h" +#include "Datatype.h" +#include "Date.h" +#include "Finance.h" +#include "Food.h" +#include "Git.h" +#include "Hacker.h" +#include "Helper.h" +#include "Image.h" +#include "Internet.h" +#include "Location.h" +#include "Lorem.h" +#include "Medicine.h" +#include "Movie.h" +#include "Music.h" +#include "Person.h" +#include "Phone.h" +#include "Sport.h" +#include "String.h" +#include "System.h" +#include "VideoGame.h" +#include "Weather.h" +#include "Word.h" + +namespace faker +{ +struct test +{ + Airline::AircraftType aircraftType; +}; + +enum class Token +{ + // Airline module + AirlineAircraftType, + AirlineAirplaneName, + AirlineAirplaneCode, + AirlineAirlineName, + AirlineAirlineCode, + AirlineAirportName, + AirlineAirportCode, + AirlineSeat, + AirlineRecordLocator, + AirlineFlightNumber, + + // Animal module + AnimalBear, + AnimalBird, + AnimalCat, + AnimalCetacean, + AnimalCow, + AnimalCrocodilia, + AnimalDog, + AnimalFish, + AnimalHorse, + AnimalInsect, + AnimalLion, + AnimalRabbit, + AnimalRodent, + AnimalSnake, + AnimalType, + + // Book module + BookTitle, + BookGenre, + BookAuthor, + BookPublisher, + BookIsbn, + + // Color module + ColorName, + ColorRGB, + ColorHEX, + ColorHSL, + ColorLCH, + ColorCMYK, + + // Commerce module + CommerceDepartment, + CommercePrice, + CommerceSku, + CommerceProductAdjective, + CommerceProductMaterial, + CommerceProductName, + CommerceProductFullName, + CommerceEAN13, + CommerceEAN8, + CommerceISBN13, + CommerceISBN10, + + // Company module + CompanyName, + CompanyType, + CompanyIndustry, + CompanyBuzzPhrase, + CompanyBuzzAdjective, + CompanyBuzzNoun, + CompanyBuzzVerb, + CompanyCatchPhrase, + CompanyCtachPhraseAdjective, + CompanyCatchPhraseDescriptor, + CompanyCatchPhraseNoun, + + // Computer module + ComputerType, + ComputerManufacture, + ComputerModel, + ComputerCPUManufacture, + ComputerCPUType, + ComputerCPUModel, + ComputerGPUManufacture, + ComputerGPUType, + ComputerGPUModel, + + // Crypto module + CryptoSHA256, + CryptoMD5, + + // Database module + DatabaseColumnName, + DatabaseColumnType, + DatabaseCollation, + DatabaseEngine, + DatabaseMongoDBObjectId, + + // Datatype module + DatatypeBoolean, + + // Date module + DatePastDate, + DatefutureDate, + DateRecentDate, + DateSoonDate, + DateBirthdateByAge, + DateBirthdateByYear, + DateWeekdayName, + DateWeekdayAbbreviatedName, + DateMontName, + DateMonthAbbreviatedName, + + // Finance module + FinanceCurrencyName, + FinanceCurrencyCode, + FinanceCurrencySymbol, + FinanceAccountType, + FinanceAmount, + FinanceIban, + FinanceBic, + FinanceAccountNumber, + FinancePin, + FinanceRoutingNumber, + FinanceCreditCardNumber, + FinanceCreditCardCvv, + FinanceBitcoinAddress, + FinanceLitecoinAddress, + FinanceEthereumAddress, + + // Food module + FoodAlcoholicBeverage, + FoodGrain, + FoodMilkProduct, + FoodFruit, + FoodMeat, + FoodSeafood, + FoodVegetable, + FoodOil, + FoodNut, + FoodSeed, + FoodSugarProduct, + FoodNonAlcoholicBeverage, + FoodDishName, + FoodFoodCategory, + + // Git module + GitBranch, + GitCommitDate, + GitCommitEntry, + GitCommitMessage, + GitCommitSha, + + // Hacker module + HackerAbbreviation, + HackerAdjective, + HackerNoun, + HackerVerb, + HackerIngverb, + HackerPhrase, + + // Image module + ImageImageURL, + ImageGitHubAvatarURL, + ImageDimensions, + + // Internet module + InternetUsername, + InternetEmail, + InternetExampleEmail, + InternetPassword, + InternetEmoji, + InternetProtocol, + InternetHttpMethod, + InternetHttpRequestHeader, + InternetHttpResponseHeader, + InternetHttpMediaType, + InternetIpv4, + InternetIpv6, + InternetMac, + InternetURL, + InternetDomainName, + InternetDomainWord, + InternetDomainSuffix, + + // Location module + LocationCountry, + LocationCountryCode, + LocationState, + LocationCity, + LocationZipCode, + LocationStreetAddress, + LocationStreet, + LocationBuildingNumber, + LocationSecondaryAddress, + LocationLatitude, + LocationLongitude, + LocationDirection, + LocationTimeZone, + + // Lorem module + LoremWord, + LoremWords, + LoremSentence, + LoremSentences, + LoremSlung, + LoremParagraph, + LoremParagraphs, + + // Medicine module + MedicineCondition, + MedicineMedicalTest, + MedicineSpecialty, + + // Movie module + MovieGenre, + MovieMovieTitle, + MovieTvShow, + MovieDirector, + MovieActor, + MovieActress, + + // Music module + MusicArtist, + MusicGenre, + MusicSongName, + + // Person module + PersonFirstName, + PersonLastName, + PersonMiddleName, + PersonFullName, + PersonPrefix, + PersonSuffix, + PersonSex, + PersonGender, + PersonJobTitle, + PersonJobDescriptor, + PersonJobArea, + PersonJobType, + PersonHoby, + PersonLanguage, + PersonNationality, + PersonWesternZodiac, + PersonChineseZodiac, + + // Phone module + PhoneNumber, + PhonePlatform, + PhoneModelName, + PhoneManufacturer, + PhoneIMEI, + + // Sport module + SportSport, + SportSoccerTeam, + SportMaleAthelete, + SportFemaleAthlete, + SportSportEvent, + + // System module + SystemFileName, + SystemFileExtension, + SystemCommonFileName, + SystemCommonFileExtension, + SystemMimeType, + SystemCommonFileType, + SystemFileType, + SystemDirectoryPath, + SystemFilePath, + SystemSemver, + SystemNetworkInterface, + SystemCron, + + // VideoGame module + VideoGameGameTitle, + VideoGameGenre, + VideoGamePlatform, + VideoGameStudioName, + + // Weather module + WeatherTemperatureMetric, + WeatherTemperatureImperial, + WeatherPressureMetric, + WeatherPressureImperial, + WeatherVisibilityMetric, + WeatherVisibilityImperial, + WeatherWindSpeedMetric, + WeatherWindSpeedImperial, + WeatherUvIndex, + WeatherHumidity, + WeatherWeatherDescription, + WeatherCloadCover, + + // Word module + WordSample, + WordWords, + WordAdjective, + WordAdverb, + WordConjuction, + WordInterjection, + WordNoun, + WordPreposition, + WordVerb, + +}; + +const std::map> moduleFunctions = { + {Token::AirlineAircraftType, []() { return Airline::aircraftType(); }}, + {Token::AirlineAirplaneName, []() { return Airline::airplane().name; }}, + {Token::AirlineAirplaneCode, []() { return Airline::airplane().iataTypeCode; }}, + {Token::AirlineAirplaneName, []() { return Airline::airplane().name; }}, + {Token::AirlineAirplaneCode, []() { return Airline::airplane().iataTypeCode; }}, + {Token::AirlineAirlineName, []() { return Airline::airline().name; }}, + {Token::AirlineAirlineCode, []() { return Airline::airline().iataCode; }}, + {Token::AirlineAirportName, []() { return Airline::airport().name; }}, + {Token::AirlineAirportCode, []() { return Airline::airport().iataCode; }}, + {Token::AirlineSeat, []() { return Airline::seat(Airline::AircraftType()); }}, + {Token::AirlineRecordLocator, []() { return Airline::recordLocator(); }}, + {Token::AirlineFlightNumber, []() { return Airline::flightNumber(false, 4); }}, + + {Token::AnimalBear, []() { return Animal::bear(); }}, + {Token::AnimalBird, []() { return Animal::bird(); }}, + {Token::AnimalCat, []() { return Animal::cat(); }}, + {Token::AnimalCetacean, []() { return Animal::cetacean(); }}, + {Token::AnimalCow, []() { return Animal::cow(); }}, + {Token::AnimalCrocodilia, []() { return Animal::crocodilia(); }}, + {Token::AnimalDog, []() { return Animal::dog(); }}, + {Token::AnimalFish, []() { return Animal::fish(); }}, + {Token::AnimalHorse, []() { return Animal::horse(); }}, + {Token::AnimalInsect, []() { return Animal::insect(); }}, + {Token::AnimalLion, []() { return Animal::lion(); }}, + {Token::AnimalRabbit, []() { return Animal::rabbit(); }}, + {Token::AnimalRodent, []() { return Animal::rodent(); }}, + {Token::AnimalSnake, []() { return Animal::snake(); }}, + {Token::AnimalType, []() { return Animal::type(); }}, + + {Token::BookTitle, []() { return Book::title(); }}, + {Token::BookGenre, []() { return Book::genre(); }}, + {Token::BookAuthor, []() { return Book::author(); }}, + {Token::BookPublisher, []() { return Book::publisher(); }}, + {Token::BookIsbn, []() { return Book::isbn(); }}, + + {Token::ColorName, []() { return Color::name(); }}, + {Token::ColorRGB, []() { return Color::rgb(); }}, + {Token::ColorHEX, []() { return Color::hex(); }}, + {Token::ColorHSL, []() { return Color::hsl(); }}, + {Token::ColorLCH, []() { return Color::lch(); }}, + {Token::ColorCMYK, []() { return Color::cmyk(); }}, + + {Token::CommerceDepartment, []() { return Commerce::department(); }}, + {Token::CommercePrice, []() { return Commerce::price(); }}, + {Token::CommerceSku, []() { return Commerce::sku(); }}, + {Token::CommerceProductAdjective, []() { return Commerce::productAdjective(); }}, + {Token::CommerceProductMaterial, []() { return Commerce::productMaterial(); }}, + {Token::CommerceProductName, []() { return Commerce::productName(); }}, + {Token::CommerceProductFullName, []() { return Commerce::productFullName(); }}, + {Token::CommerceEAN13, []() { return Commerce::EAN13(); }}, + {Token::CommerceEAN8, []() { return Commerce::EAN8(); }}, + {Token::CommerceISBN13, []() { return Commerce::ISBN13(); }}, + {Token::CommerceISBN10, []() { return Commerce::ISBN10(); }}, + + {Token::CompanyName, []() { return Company::name(); }}, + {Token::CompanyType, []() { return Company::type(); }}, + {Token::CompanyIndustry, []() { return Company::industry(); }}, + {Token::CompanyBuzzPhrase, []() { return Company::buzzPhrase(); }}, + {Token::CompanyBuzzAdjective, []() { return Company::buzzAdjective(); }}, + {Token::CompanyBuzzNoun, []() { return Company::buzzNoun(); }}, + {Token::CompanyBuzzVerb, []() { return Company::buzzVerb(); }}, + {Token::CompanyCatchPhrase, []() { return Company::catchPhrase(); }}, + {Token::CompanyCtachPhraseAdjective, []() { return Company::catchPhraseAdjective(); }}, + {Token::CompanyCatchPhraseDescriptor, []() { return Company::catchPhraseDescriptor(); }}, + {Token::CompanyCatchPhraseNoun, []() { return Company::catchPhraseNoun(); }}, + + {Token::ComputerManufacture, []() { return Computer::manufacture(); }}, + {Token::ComputerModel, []() { return Computer::model(); }}, + {Token::ComputerCPUManufacture, []() { return Computer::cpuManufacture(); }}, + {Token::ComputerCPUType, []() { return Computer::cpuType(); }}, + {Token::ComputerCPUModel, []() { return Computer::cpuModel(); }}, + {Token::ComputerGPUManufacture, []() { return Computer::gpuManufacture(); }}, + {Token::ComputerGPUType, []() { return Computer::gpuType(); }}, + {Token::ComputerGPUModel, []() { return Computer::gpuModel(); }}, + + {Token::CryptoSHA256, []() { return Crypto::sha256(); }}, + {Token::CryptoMD5, []() { return Crypto::md5(); }}, + + {Token::DatabaseColumnName, []() { return Database::columnName(); }}, + {Token::DatabaseColumnType, []() { return Database::columnType(); }}, + {Token::DatabaseCollation, []() { return Database::collation(); }}, + {Token::DatabaseEngine, []() { return Database::engine(); }}, + {Token::DatabaseMongoDBObjectId, []() { return Database::mongoDbObjectId(); }}, + + {Token::DatatypeBoolean, []() { return std::to_string(Datatype::boolean()); }}, + + {Token::DatePastDate, []() { return Date::pastDate(); }}, + {Token::DatefutureDate, []() { return Date::futureDate(); }}, + {Token::DateRecentDate, []() { return Date::recentDate(); }}, + {Token::DateSoonDate, []() { return Date::soonDate(); }}, + {Token::DateBirthdateByAge, []() { return Date::birthdateByAge(); }}, + {Token::DateBirthdateByYear, []() { return Date::birthdateByYear(); }}, + {Token::DateWeekdayName, []() { return Date::weekdayName(); }}, + {Token::DateWeekdayAbbreviatedName, []() { return Date::weekdayAbbreviatedName(); }}, + {Token::DateMontName, []() { return Date::monthName(); }}, + {Token::DateMonthAbbreviatedName, []() { return Date::monthAbbreviatedName(); }}, + + {Token::FinanceCurrencyName, []() { return Finance::currencyName(); }}, + {Token::FinanceCurrencyCode, []() { return Finance::currencyCode(); }}, + {Token::FinanceCurrencySymbol, []() { return Finance::currencySymbol(); }}, + {Token::FinanceAccountType, []() { return Finance::accountType(); }}, + {Token::FinanceAmount, []() { return Finance::amount(); }}, + {Token::FinanceIban, []() { return Finance::iban(); }}, + {Token::FinanceBic, []() { return Finance::bic(); }}, + {Token::FinanceAccountNumber, []() { return Finance::accountNumber(); }}, + {Token::FinancePin, []() { return Finance::pin(); }}, + {Token::FinanceRoutingNumber, []() { return Finance::routingNumber(); }}, + {Token::FinanceCreditCardNumber, []() { return Finance::creditCardNumber(); }}, + {Token::FinanceCreditCardCvv, []() { return Finance::creditCardCvv(); }}, + {Token::FinanceBitcoinAddress, []() { return Finance::bitcoinAddress(); }}, + {Token::FinanceLitecoinAddress, []() { return Finance::litecoinAddress(); }}, + {Token::FinanceEthereumAddress, []() { return Finance::ethereumAddress(); }}, + + {Token::FoodAlcoholicBeverage, []() { return Food::alcoholicBeverage(); }}, + {Token::FoodGrain, []() { return Food::grain(); }}, + {Token::FoodMilkProduct, []() { return Food::milkProduct(); }}, + {Token::FoodFruit, []() { return Food::fruit(); }}, + {Token::FoodMeat, []() { return Food::meat(); }}, + {Token::FoodSeafood, []() { return Food::seafood(); }}, + {Token::FoodVegetable, []() { return Food::vegetable(); }}, + {Token::FoodOil, []() { return Food::oil(); }}, + {Token::FoodNut, []() { return Food::nut(); }}, + {Token::FoodSeed, []() { return Food::seed(); }}, + {Token::FoodSugarProduct, []() { return Food::sugarProduct(); }}, + {Token::FoodNonAlcoholicBeverage, []() { return Food::nonalcoholicBeverage(); }}, + {Token::FoodDishName, []() { return Food::dishName(); }}, + {Token::FoodFoodCategory, []() { return Food::foodCategory(); }}, + + {Token::GitBranch, []() { return Git::branch(); }}, + {Token::GitCommitDate, []() { return Git::commitDate(); }}, + {Token::GitCommitEntry, []() { return Git::commitEntry(); }}, + {Token::GitCommitMessage, []() { return Git::commitMessage(); }}, + {Token::GitCommitSha, []() { return Git::commitSha(); }}, + + {Token::HackerAbbreviation, []() { return Hacker::abbreviation(); }}, + {Token::HackerAdjective, []() { return Hacker::adjective(); }}, + {Token::HackerNoun, []() { return Hacker::noun(); }}, + {Token::HackerVerb, []() { return Hacker::verb(); }}, + {Token::HackerIngverb, []() { return Hacker::ingverb(); }}, + {Token::HackerPhrase, []() { return Hacker::phrase(); }}, + + {Token::ImageImageURL, []() { return Image::imageUrl(); }}, + {Token::ImageGitHubAvatarURL, []() { return Image::githubAvatarUrl(); }}, + {Token::ImageDimensions, []() { return Image::dimensions(); }}, + + {Token::InternetUsername, []() { return Internet::username(); }}, + {Token::InternetEmail, []() { return Internet::email(); }}, + {Token::InternetExampleEmail, []() { return Internet::exampleEmail(); }}, + {Token::InternetPassword, []() { return Internet::password(); }}, + {Token::InternetEmoji, []() { return Internet::emoji(); }}, + {Token::InternetProtocol, []() { return Internet::protocol(); }}, + {Token::InternetHttpMethod, []() { return Internet::httpMethod(); }}, + {Token::InternetHttpRequestHeader, []() { return Internet::httpRequestHeader(); }}, + {Token::InternetHttpResponseHeader, []() { return Internet::httpResponseHeader(); }}, + {Token::InternetHttpMediaType, []() { return Internet::httpMediaType(); }}, + {Token::InternetIpv4, []() { return Internet::ipv4(); }}, + {Token::InternetIpv6, []() { return Internet::ipv6(); }}, + {Token::InternetMac, []() { return Internet::mac(); }}, + {Token::InternetURL, []() { return Internet::url(); }}, + {Token::InternetDomainName, []() { return Internet::domainName(); }}, + {Token::InternetDomainWord, []() { return Internet::domainWord(); }}, + {Token::InternetDomainSuffix, []() { return Internet::domainSuffix(); }}, + + {Token::LocationCountry, []() { return Location::country(); }}, + {Token::LocationCountryCode, []() { return Location::countryCode(); }}, + {Token::LocationState, []() { return Location::state(); }}, + {Token::LocationCity, []() { return Location::city(); }}, + {Token::LocationZipCode, []() { return Location::zipCode(); }}, + {Token::LocationStreetAddress, []() { return Location::streetAddress(); }}, + {Token::LocationStreet, []() { return Location::street(); }}, + {Token::LocationBuildingNumber, []() { return Location::buildingNumber(); }}, + {Token::LocationSecondaryAddress, []() { return Location::secondaryAddress(); }}, + {Token::LocationLatitude, []() { return Location::latitude(); }}, + {Token::LocationLongitude, []() { return Location::longitude(); }}, + {Token::LocationDirection, []() { return Location::direction(); }}, + {Token::LocationTimeZone, []() { return Location::timeZone(); }}, + + // Lorem module + {Token::LoremWord, []() { return Lorem::word(); }}, + {Token::LoremWords, []() { return Lorem::words(); }}, + {Token::LoremSentence, []() { return Lorem::sentence(); }}, + {Token::LoremSentences, []() { return Lorem::sentences(); }}, + {Token::LoremSlung, []() { return Lorem::slug(10); }}, + {Token::LoremParagraph, []() { return Lorem::paragraph(); }}, + {Token::LoremParagraphs, []() { return Lorem::paragraphs(); }}, + + // Medicine module + {Token::MedicineCondition, []() { return Medicine::condition(); }}, + {Token::MedicineMedicalTest, []() { return Medicine::medicalTest(); }}, + {Token::MedicineSpecialty, []() { return Medicine::specialty(); }}, + + // Movie module + {Token::MovieGenre, []() { return Movie::genre(); }}, + {Token::MovieMovieTitle, []() { return Movie::movieTitle(); }}, + {Token::MovieTvShow, []() { return Movie::tvShow(); }}, + {Token::MovieDirector, []() { return Movie::director(); }}, + {Token::MovieActor, []() { return Movie::actor(); }}, + {Token::MovieActress, []() { return Movie::actress(); }}, + + // Music module + {Token::MusicArtist, []() { return Music::artist(); }}, + {Token::MusicGenre, []() { return Music::genre(); }}, + {Token::MusicSongName, []() { return Music::songName(); }}, + + // Person module + {Token::PersonFirstName, []() { return Person::firstName(); }}, + {Token::PersonLastName, []() { return Person::lastName(); }}, + {Token::PersonMiddleName, []() { return Person::middleName(); }}, + {Token::PersonFullName, []() { return Person::fullName(); }}, + {Token::PersonPrefix, []() { return Person::prefix(); }}, + {Token::PersonSuffix, []() { return Person::suffix(); }}, + {Token::PersonSex, []() { return Person::sex(); }}, + {Token::PersonGender, []() { return Person::gender(); }}, + {Token::PersonJobTitle, []() { return Person::jobTitle(); }}, + {Token::PersonJobDescriptor, []() { return Person::jobDescriptor(); }}, + {Token::PersonJobArea, []() { return Person::jobArea(); }}, + {Token::PersonJobType, []() { return Person::jobType(); }}, + {Token::PersonHoby, []() { return Person::hobby(); }}, + {Token::PersonLanguage, []() { return Person::language(); }}, + {Token::PersonNationality, []() { return Person::nationality(); }}, + {Token::PersonWesternZodiac, []() { return Person::westernZodiac(); }}, + {Token::PersonChineseZodiac, []() { return Person::chineseZodiac(); }}, + + // Phone module + {Token::PhoneNumber, []() { return Phone::number(); }}, + {Token::PhonePlatform, []() { return Phone::platform(); }}, + {Token::PhoneModelName, []() { return Phone::modelName(); }}, + {Token::PhoneManufacturer, []() { return Phone::manufacturer(); }}, + {Token::PhoneIMEI, []() { return Phone::imei(); }}, + + // Sport module + {Token::SportSport, []() { return Sport::sport(); }}, + {Token::SportSoccerTeam, []() { return Sport::soccerTeam(); }}, + {Token::SportMaleAthelete, []() { return Sport::maleAthlete(); }}, + {Token::SportFemaleAthlete, []() { return Sport::femaleAthlete(); }}, + {Token::SportSportEvent, []() { return Sport::sportEvent(); }}, + + // System module + {Token::SystemFileName, []() { return System::fileName(); }}, + {Token::SystemFileExtension, []() { return System::fileExtension(); }}, + {Token::SystemCommonFileName, []() { return System::commonFileName(); }}, + {Token::SystemCommonFileExtension, []() { return System::commonFileExtension(); }}, + {Token::SystemMimeType, []() { return System::mimeType(); }}, + {Token::SystemCommonFileType, []() { return System::commonFileType(); }}, + {Token::SystemFileType, []() { return System::fileType(); }}, + {Token::SystemDirectoryPath, []() { return System::directoryPath(); }}, + {Token::SystemFilePath, []() { return System::filePath(); }}, + {Token::SystemSemver, []() { return System::semver(); }}, + {Token::SystemNetworkInterface, []() { return System::networkInterface(); }}, + {Token::SystemCron, []() { return System::cron(); }}, + + // VideoGame module + {Token::VideoGameGameTitle, []() { return VideoGame::gameTitle(); }}, + {Token::VideoGameGenre, []() { return VideoGame::genre(); }}, + {Token::VideoGamePlatform, []() { return VideoGame::platform(); }}, + {Token::VideoGameStudioName, []() { return VideoGame::studioName(); }}, + + // Weather module + {Token::WeatherTemperatureMetric, []() { return std::to_string(Weather::temperature().metric); }}, + + {Token::WeatherTemperatureImperial, []() { return std::to_string(Weather::temperature().imperial); }}, + {Token::WeatherPressureMetric, []() { return std::to_string(Weather::pressure().metric); }}, + {Token::WeatherPressureImperial, []() { return std::to_string(Weather::pressure().imperial); }}, + {Token::WeatherVisibilityMetric, []() { return std::to_string(Weather::visibility().metric); }}, + {Token::WeatherVisibilityImperial, []() { return std::to_string(Weather::visibility().imperial); }}, + {Token::WeatherWindSpeedMetric, []() { return std::to_string(Weather::windSpeed().metric); }}, + {Token::WeatherWindSpeedImperial, []() { return std::to_string(Weather::windSpeed().imperial); }}, + {Token::WeatherUvIndex, []() { return std::to_string(Weather::uvIndex()); }}, + {Token::WeatherHumidity, []() { return std::to_string(Weather::humidity()); }}, + {Token::WeatherWeatherDescription, []() { return Weather::weatherDescription(); }}, + {Token::WeatherCloadCover, []() { return std::to_string(Weather::cloudCover()); }}, + + // Word module + {Token::WordSample, []() { return Word::sample(); }}, + {Token::WordWords, []() { return Word::words(); }}, + {Token::WordAdjective, []() { return Word::adjective(); }}, + {Token::WordAdverb, []() { return Word::adverb(); }}, + {Token::WordConjuction, []() { return Word::conjunction(); }}, + {Token::WordInterjection, []() { return Word::interjection(); }}, + {Token::WordNoun, []() { return Word::noun(); }}, + {Token::WordPreposition, []() { return Word::preposition(); }}, + {Token::WordVerb, []() { return Word::verb(); }}, +}; + +class Sructure +{ +public: + /** + * @brief Get a json string based on input + * + * @return a josn string based on user selected tokens + * + * @code + * Sructure::json(tokens) // {"FoodName":"Cajun Shrimp Pasta","airlineType":"narrowbody","airportName":"Brisbane International Airport"} + * @endcode + */ + static std::string json(const std::map items); + /** + * @brief Get a csv string based on input + * + * @return a josn csv based on user selected tokens and rows + * + * @code + * Sructure::csv(tokens,2) // FoodName,airlineType,airportName + Lobster Roll,widebody,Soekarno-Hatta International Airport + Pierogi,widebody,Brasilia-Presidente Juscelino Kubitschek International Airport + * @endcode + */ + static std::string csv(const std::map items, const unsigned int rows); + +private: +}; + +} \ No newline at end of file diff --git a/src/common/JsonHelper.cpp b/src/common/JsonHelper.cpp new file mode 100644 index 000000000..e01ce7815 --- /dev/null +++ b/src/common/JsonHelper.cpp @@ -0,0 +1,31 @@ +#include "JsonHelper.h" + +namespace faker +{ + +std::map JsonHelper::simpleJsonParser(std::string json) +{ + std::map result; + std::istringstream stream(json); + std::string key, value, temp; + char ch; + + + while (stream >> ch && ch != '}') + { + if (ch == '"' ) + { + + std::getline(stream, key, '"'); + std::getline(stream, temp, ':'); + + stream >> ch; + std::getline(stream, value, '"'); + + result[key] = value; + } + } + + return result; +} +} \ No newline at end of file diff --git a/src/common/JsonHelper.h b/src/common/JsonHelper.h new file mode 100644 index 000000000..82ccd78a8 --- /dev/null +++ b/src/common/JsonHelper.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +namespace faker +{ + +class JsonHelper +{ +public: +// this function parse simple jason like {"key1":"value1","key2":"value2"} + static std::map simpleJsonParser(std::string json); + + +}; +} diff --git a/src/common/JsonHelperTest.cpp b/src/common/JsonHelperTest.cpp new file mode 100644 index 000000000..6bcbef34d --- /dev/null +++ b/src/common/JsonHelperTest.cpp @@ -0,0 +1,29 @@ +#include "JsonHelper.h" + +#include + +#include "gtest/gtest.h" + +using namespace ::testing; +using namespace faker; + +class JsonHelperTest : public Test +{ +public: + +}; + +TEST_F(JsonHelperTest, simpleJsonParser ) { + const auto result = JsonHelper::simpleJsonParser("{\"Test1\":\"Hello\",\"Test2\":\"World!\"}"); + + const auto key1 = result.find("Test1")->first; + const auto key2 = result.find("Test2")->first; + const auto value1 = result.find("Test1")->second; + const auto value2 = result.find("Test2")->second; + + ASSERT_EQ(key1, "Test1"); + ASSERT_EQ(key2, "Test2"); + ASSERT_EQ(value1, "Hello"); + ASSERT_EQ(value2, "World!"); + +} \ No newline at end of file diff --git a/src/modules/structure/Structure.cpp b/src/modules/structure/Structure.cpp new file mode 100644 index 000000000..da02ae50c --- /dev/null +++ b/src/modules/structure/Structure.cpp @@ -0,0 +1,57 @@ +#include "../include/faker-cxx/Structure.h" + +#include + +namespace faker +{ + +std::string faker::Sructure::json(const std::map items) +{ + std::string result = "{"; + + for (auto it = items.begin(); it != items.end(); ++it) + { + auto func = moduleFunctions.find(it->second)->second; + result.append("\"" + it->first + "\":\"" + func() + "\""); + + + if (std::next(it) != items.end()) + { + result.append(","); + } + } + + result.append("}"); + return result; +} +std::string faker::Sructure::csv(const std::map items, const unsigned int rows) +{ + std::string result = ""; + std::vector> funcs; + for (auto it = items.begin(); it != items.end(); ++it) + { + result.append(it->first); + funcs.push_back(moduleFunctions.find(it->second)->second); + if (std::next(it) != items.end()) + { + result.append(","); + } + } + result.append("\n"); + unsigned int n = funcs.size(); + for (int i = 0; i < rows; i++) + { + for (size_t j = 0; j < n; ++j) + { + result.append(funcs[j]()); + if (j != n - 1) + { + result.append(","); + } + } + result.append("\n"); + } + + return result; +} +} \ No newline at end of file diff --git a/src/modules/structure/StructureTest.cpp b/src/modules/structure/StructureTest.cpp new file mode 100644 index 000000000..6d954764e --- /dev/null +++ b/src/modules/structure/StructureTest.cpp @@ -0,0 +1,98 @@ +#include "faker-cxx/Structure.h" + +#include + +#include + +#include "gtest/gtest.h" +#include "../../common/JsonHelper.h" +#include "../../common/StringHelper.h" + +#include "../airline/data/Airports.h" +#include "../animal/data/Birds.h" +#include "../book/data/Titles.h" +#include "../movie/data/Actors.h" + + + +using namespace faker; +using namespace ::testing; + +class StructureTest : public Test +{ +public: + + +}; + +TEST_F(StructureTest, shouldGenerateJson) { + std::map testTokens; + testTokens.emplace("Airport name", Token::AirlineAirportName); + testTokens.emplace("Bird name", Token::AnimalBird); + testTokens.emplace("Book title", Token::BookTitle); + testTokens.emplace("Actor name",Token::MovieActor); + const auto generatedJson = Sructure::json(testTokens); + + const auto parsedJson = JsonHelper::simpleJsonParser(generatedJson); + + const auto key1 = parsedJson.find("Airport name")->first; + const auto key2 = parsedJson.find("Bird name")->first; + const auto key3 = parsedJson.find("Book title")->first; + const auto key4 = parsedJson.find("Actor name")->first; + + const auto value1 = parsedJson.find("Airport name")->second; + const auto value2 = parsedJson.find("Bird name")->second; + const auto value3 = parsedJson.find("Book title")->second; + const auto value4 = parsedJson.find("Actor name")->second; + + //there is no point for this but it's better to be there + ASSERT_EQ(key1, "Airport name"); + ASSERT_EQ(key2, "Bird name"); + ASSERT_EQ(key3, "Book title"); + ASSERT_EQ(key4, "Actor name"); + + ASSERT_TRUE( + std::ranges::any_of( + airports, [value1](const Airline::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;})); + +} + +TEST_F(StructureTest, shouldGenerateCSV) { + std::map testTokens; + const unsigned int noRows= 2; + testTokens.emplace("Airport name", Token::AirlineAirportName); + testTokens.emplace("Bird name", Token::AnimalBird); + testTokens.emplace("Book title", Token::BookTitle); + testTokens.emplace("Actor name", Token::MovieActor); + + const auto generatedCSV = Sructure::csv(testTokens,noRows); + + std::istringstream dataStream(generatedCSV); + std::string line; + + std::getline(dataStream, line); + std::vector keys = StringHelper::split(line, ","); + + //the order of map has been change based on ascending lexicographical order + ASSERT_EQ(keys.at(0), "Actor name"); + ASSERT_EQ(keys.at(1), "Airport name"); + ASSERT_EQ(keys.at(2), "Bird name"); + ASSERT_EQ(keys.at(3), "Book title"); + + + + while (std::getline(dataStream,line)) + { + std::vector tokens = StringHelper::split(line, ","); + ASSERT_TRUE(std::ranges::any_of(actors, [tokens](const std::string& actor) { return actor == tokens.at(0); })); + ASSERT_TRUE(std::ranges::any_of(airports, [tokens](const Airline::Airport& airport) + { return airport.name == tokens.at(1); })); + ASSERT_TRUE(std::ranges::any_of(birds, [tokens](const std::string& bird) { return bird == tokens.at(2); })); + ASSERT_TRUE(std::ranges::any_of(titles, [tokens](const std::string& title) { return title == tokens.at(3); })); + } + +} \ No newline at end of file