From f338017fef4f5b7d0ebf08cbf49aee633c61b243 Mon Sep 17 00:00:00 2001 From: xpoproci Date: Fri, 18 Oct 2024 11:00:43 +0200 Subject: [PATCH] refactor: add locale to esports module (#965) * add locale to esports module * fix test functions * add instantiation of tests with locales --- include/faker-cxx/esport.h | 21 ++++++++--- src/modules/esport.cpp | 42 ++++++++++++++++------ src/modules/esport_data.h | 28 ++++++++++++--- tests/modules/esport_test.cpp | 68 ++++++++++++++++++++++++++--------- 4 files changed, 123 insertions(+), 36 deletions(-) diff --git a/include/faker-cxx/esport.h b/include/faker-cxx/esport.h index 393302764..a51c70f4d 100644 --- a/include/faker-cxx/esport.h +++ b/include/faker-cxx/esport.h @@ -3,61 +3,72 @@ #include #include "faker-cxx/export.h" +#include "faker-cxx/types/locale.h" namespace faker::esport { /** * @brief Returns a random player name. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @return Player name. * * @code * faker::esport::player() // "Faker" * @endcode */ -FAKER_CXX_EXPORT std::string_view player(); +FAKER_CXX_EXPORT std::string_view player(Locale locale = Locale::en_US); /** * @brief Returns a random esport team. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @return Esport team. * * @code * faker::esport::team() // "Karmine Corp" * @endcode */ -FAKER_CXX_EXPORT std::string_view team(); +FAKER_CXX_EXPORT std::string_view team(Locale locale = Locale::en_US); /** * @brief Returns a random esport league. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @return Esport League. * * @code * faker::esport::league() // "LEC" * @endcode */ -FAKER_CXX_EXPORT std::string_view league(); +FAKER_CXX_EXPORT std::string_view league(Locale locale = Locale::en_US); /** * @brief Returns a random esport event. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @return Esport Event. * * @code * faker::esport::event() // "Faker" * @endcode */ -FAKER_CXX_EXPORT std::string_view event(); +FAKER_CXX_EXPORT std::string_view event(Locale locale = Locale::en_US); /** * @brief Returns a random competitive video game. * + * @param locale The locale. Defaults to `Locale::en_US`. + * * @return Competitive video game. * * @code * faker::esport::game() // "Rocket League" * @endcode */ -FAKER_CXX_EXPORT std::string_view game(); +FAKER_CXX_EXPORT std::string_view game(Locale locale = Locale::en_US); } diff --git a/src/modules/esport.cpp b/src/modules/esport.cpp index 9cb377ddb..3220e63da 100644 --- a/src/modules/esport.cpp +++ b/src/modules/esport.cpp @@ -7,29 +7,51 @@ namespace faker::esport { -std::string_view player() + +namespace +{ +const struct EsportDefinition& getEsportDefinition(Locale locale) { - return helper::randomElement(players); + switch (locale) + { + default: + return enUSEsportDefinition; + } +} } -std::string_view team() +std::string_view player(Locale locale) { - return helper::randomElement(teams); + const auto& esportDefinition = getEsportDefinition(locale); + + return helper::randomElement(esportDefinition.players); } -std::string_view league() +std::string_view team(Locale locale) { - return helper::randomElement(leagues); + const auto& esportDefinition = getEsportDefinition(locale); + + return helper::randomElement(esportDefinition.teams); } -std::string_view event() +std::string_view league(Locale locale) { - return helper::randomElement(events); + const auto& esportDefinition = getEsportDefinition(locale); + + return helper::randomElement(esportDefinition.leagues); } -std::string_view game() +std::string_view event(Locale locale) { - return helper::randomElement(games); + const auto& esportDefinition = getEsportDefinition(locale); + + return helper::randomElement(esportDefinition.events); } +std::string_view game(Locale locale) +{ + const auto& esportDefinition = getEsportDefinition(locale); + + return helper::randomElement(esportDefinition.games); +} } \ No newline at end of file diff --git a/src/modules/esport_data.h b/src/modules/esport_data.h index f7d976ddb..2f9fea170 100644 --- a/src/modules/esport_data.h +++ b/src/modules/esport_data.h @@ -1,11 +1,21 @@ #pragma once #include +#include #include namespace faker::esport { -const auto players = std::to_array( +struct EsportDefinition +{ + std::span players; + std::span teams; + std::span leagues; + std::span events; + std::span games; +}; + +const auto enUSPlayers = std::to_array( {"Adam", "Alvaro", "Brokenblade", "Canna", "Caps", "Carzzy", "Closer", "Comp", "Elyoya", "Finn", "Flakked", "Fresskowy", "Hans Sama", "Humanoid", "Hylissang", "Ice", "IgNar", "Irrelevant", "Isma", "Jackies", "Jankos", "Juhan", "Jun", "Labrov", "Larssen", "Luon", "Lyncas", @@ -20,27 +30,35 @@ const auto players = std::to_array( "Pyosik", "Quantum", "Raptor", "Rascal", "Samver", "SeTab", "ShowMaker", "Sponge", "Sylvie", "Teddy", "Viper", "Vital", "YoungJae", "Zeka", "Zeus", "kingen"}); -const auto teams = std::to_array( +const auto enUSTeams = std::to_array( {"Team Liquid", "G2 Esports", "OpTic Gaming", "Natus Vincere", "Astralis", "NRG", "7figures Esports", "HEROIC", "Faze Clan", "Fnatic", "Virtus.pro", "Ninjas in Pyjamas", "CompLexity", "TSM", "BIG", "Karmine Corp", "Cloud9", "Evil Geniuses", "Team VItality", "Mouz", "100 Thieves", "NaVi", "Team Secret", "Vitality", "Gambit Esports", "BDS", "Gentle Mates"}); -const auto leagues = std::to_array( +const auto enUSLeagues = std::to_array( {"LEC", "LPL", "LCK", "LCS NA", "LFL", "PUBG Mobile Super League", "VCT", "VCT GC", "F1 Esports Series", "Overwatch League", "Superliga", "Prime League", "TCL", "Call of Duty League", "RLCS"}); -const auto events = +const auto enUSEvents = std::to_array({"MSI", "EMEA Masters", "League of Legends World Championship", "EWC", "The International", "Fortnite World Cup", "Six Invitational", "Capcom X Cup", "VCT Masters" "RLCS Major", "IEM"}); -const auto games = std::to_array({"Valorant", "League Of Legend", "Rocket League", "Fifa", +const auto enUSGames = std::to_array({"Valorant", "League Of Legend", "Rocket League", "Fifa", "Overwatch", "CSGO", "Fortnite", "Dota 2", "Street Fighter," "Apex Legends", "TFT", "PUBG", "Call of Duty"}); + +const EsportDefinition enUSEsportDefinition = { + .players = enUSPlayers, + .teams = enUSTeams, + .leagues = enUSLeagues, + .events = enUSEvents, + .games = enUSGames +}; } \ No newline at end of file diff --git a/tests/modules/esport_test.cpp b/tests/modules/esport_test.cpp index d75d80b97..f8c662ede 100644 --- a/tests/modules/esport_test.cpp +++ b/tests/modules/esport_test.cpp @@ -7,49 +7,85 @@ #include "faker-cxx/esport.h" using namespace ::testing; +using namespace faker; using namespace faker::esport; -class EsportTest : public Test +namespace +{ +const struct EsportDefinition& getEsportDefinition(Locale locale) +{ + switch (locale) + { + default: + return enUSEsportDefinition; + } +} +} + +class EsportTest : public TestWithParam { public: }; -TEST_F(EsportTest, shouldGeneratePlayer) +TEST_P(EsportTest, shouldGeneratePlayer) { - const auto generatedPlayer = player(); + const auto locale = GetParam(); + + const auto& esportDefinition = getEsportDefinition(locale); + + const auto generatedPlayer = player(locale); - ASSERT_TRUE(std::ranges::any_of(players, [generatedPlayer](const std::string_view& player) + ASSERT_TRUE(std::ranges::any_of(esportDefinition.players, [generatedPlayer](const std::string_view& player) { return generatedPlayer == player; })); } -TEST_F(EsportTest, shouldGenerateTeam) +TEST_P(EsportTest, shouldGenerateTeam) { - const auto generatedTeam = team(); + const auto locale = GetParam(); + + const auto& esportDefinition = getEsportDefinition(locale); + + const auto generatedTeam = team(locale); ASSERT_TRUE( - std::ranges::any_of(teams, [generatedTeam](const std::string_view& team) { return generatedTeam == team; })); + std::ranges::any_of(esportDefinition.teams, [generatedTeam](const std::string_view& team) { return generatedTeam == team; })); } -TEST_F(EsportTest, shouldGenerateLeague) +TEST_P(EsportTest, shouldGenerateLeague) { - const auto generatedLeague = league(); + const auto locale = GetParam(); - ASSERT_TRUE(std::ranges::any_of(leagues, [generatedLeague](const std::string_view& league) + const auto& esportDefinition = getEsportDefinition(locale); + + const auto generatedLeague = league(locale); + + ASSERT_TRUE(std::ranges::any_of(esportDefinition.leagues, [generatedLeague](const std::string_view& league) { return generatedLeague == league; })); } -TEST_F(EsportTest, shouldGenerateEvent) +TEST_P(EsportTest, shouldGenerateEvent) { - const auto generatedEvent = event(); + const auto locale = GetParam(); + + const auto& esportDefinition = getEsportDefinition(locale); + + const auto generatedEvent = event(locale); - ASSERT_TRUE(std::ranges::any_of(events, [generatedEvent](const std::string_view& event) + ASSERT_TRUE(std::ranges::any_of(esportDefinition.events, [generatedEvent](const std::string_view& event) { return generatedEvent == event; })); } -TEST_F(EsportTest, shouldGenerateGame) +TEST_P(EsportTest, shouldGenerateGame) { - const auto generatedGame = game(); + const auto locale = GetParam(); + + const auto& esportDefinition = getEsportDefinition(locale); + + const auto generatedGame = game(locale); ASSERT_TRUE( - std::ranges::any_of(games, [generatedGame](const std::string_view& game) { return generatedGame == game; })); + std::ranges::any_of(esportDefinition.games, [generatedGame](const std::string_view& game) { return generatedGame == game; })); } + +INSTANTIATE_TEST_SUITE_P(TestEsportByLocale, EsportTest, ValuesIn(locales), + [](const TestParamInfo& paramInfo) { return toString(paramInfo.param); });