Skip to content

Commit

Permalink
refactor: add locale to esports module (#965)
Browse files Browse the repository at this point in the history
* add locale to esports module

* fix test functions

* add instantiation of tests with locales
  • Loading branch information
xpoproci authored Oct 18, 2024
1 parent e662b6b commit f338017
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 36 deletions.
21 changes: 16 additions & 5 deletions include/faker-cxx/esport.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,72 @@
#include <string_view>

#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);
}
42 changes: 32 additions & 10 deletions src/modules/esport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 23 additions & 5 deletions src/modules/esport_data.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#pragma once

#include <array>
#include <span>
#include <string_view>

namespace faker::esport
{
const auto players = std::to_array<std::string_view>(
struct EsportDefinition
{
std::span<const std::string_view> players;
std::span<const std::string_view> teams;
std::span<const std::string_view> leagues;
std::span<const std::string_view> events;
std::span<const std::string_view> games;
};

const auto enUSPlayers = std::to_array<std::string_view>(
{"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",
Expand All @@ -20,27 +30,35 @@ const auto players = std::to_array<std::string_view>(
"Pyosik", "Quantum", "Raptor", "Rascal", "Samver", "SeTab", "ShowMaker", "Sponge", "Sylvie",
"Teddy", "Viper", "Vital", "YoungJae", "Zeka", "Zeus", "kingen"});

const auto teams = std::to_array<std::string_view>(
const auto enUSTeams = std::to_array<std::string_view>(
{"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<std::string_view>(
const auto enUSLeagues = std::to_array<std::string_view>(
{"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<std::string_view>({"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<std::string_view>({"Valorant", "League Of Legend", "Rocket League", "Fifa",
const auto enUSGames = std::to_array<std::string_view>({"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
};
}
68 changes: 52 additions & 16 deletions tests/modules/esport_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale>
{
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<Locale>& paramInfo) { return toString(paramInfo.param); });

0 comments on commit f338017

Please sign in to comment.