Skip to content

Commit

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

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 authored Oct 3, 2024
1 parent 769f86b commit 80f7796
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 29 deletions.
17 changes: 13 additions & 4 deletions include/faker-cxx/video_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,59 @@
#include <string_view>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"

namespace faker::videogame
{
/**
* @brief Returns a random video game name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Video game name.
*
* @code
* faker::videogame::gameTitle() // "Rayman Arena"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view gameTitle();
FAKER_CXX_EXPORT std::string_view gameTitle(Locale locale = Locale::en_US);

/**
* @brief Returns a random video game genre.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Video game genre.
*
* @code
* faker::videogame::genre() // "Platformer"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view genre();
FAKER_CXX_EXPORT std::string_view genre(Locale locale = Locale::en_US);

/**
* @brief Returns a random video game platform.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Platform.
*
* @code
* faker::videogame::platform() // "Playstation 5"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view platform();
FAKER_CXX_EXPORT std::string_view platform(Locale locale = Locale::en_US);

/**
* @brief Returns a random video game studio name.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Studio name.
*
* @code
* faker::videogame::studioName() // "Activision Blizzard"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view studioName();
FAKER_CXX_EXPORT std::string_view studioName(Locale locale = Locale::en_US);
}
35 changes: 27 additions & 8 deletions src/modules/video_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,42 @@

namespace faker::videogame
{
std::string_view gameTitle()
namespace
{
return helper::randomElement(videoGameNames);
const struct VideoGames& getVideoGame(Locale locale)
{
switch (locale)
{
default:
return enUSVideoGames;
}
}
}
std::string_view gameTitle(Locale locale)
{
const auto& videoGame = getVideoGame(locale);

return helper::randomElement(videoGame.videoGameNames);
}

std::string_view genre()
std::string_view genre(Locale locale)
{
return helper::randomElement(videoGameGenres);
const auto& videoGame = getVideoGame(locale);

return helper::randomElement(videoGame.videoGameGenres);
}

std::string_view platform()
std::string_view platform(Locale locale)
{
return helper::randomElement(platforms);
const auto& videoGame = getVideoGame(locale);

return helper::randomElement(videoGame.platforms);
}

std::string_view studioName()
std::string_view studioName(Locale locale)
{
return helper::randomElement(studioNames);
const auto& videoGame = getVideoGame(locale);

return helper::randomElement(videoGame.studioNames);
}
}
25 changes: 21 additions & 4 deletions src/modules/video_game_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::videogame
{
const auto videoGameNames = std::to_array<std::string_view>({

struct VideoGames
{
std::span<const std::string_view> videoGameNames;
std::span<const std::string_view> videoGameGenres;
std::span<const std::string_view> platforms;
std::span<const std::string_view> studioNames;
};

const auto enUSVideoGameNames = std::to_array<std::string_view>({
"#killallzombies",
".hack//G.U. Last Recode",
"007: Agent Under Fire",
Expand Down Expand Up @@ -3160,7 +3170,7 @@ const auto videoGameNames = std::to_array<std::string_view>({
"Æon Flux",
});

const auto videoGameGenres = std::to_array<std::string_view>({
const auto enUSVideoGameGenres = std::to_array<std::string_view>({
"Action",
"Adventure",
"Battle royale",
Expand Down Expand Up @@ -3191,7 +3201,7 @@ const auto videoGameGenres = std::to_array<std::string_view>({
"Tower defense",
});

const auto platforms = std::to_array<std::string_view>({
const auto enUSPlatforms = std::to_array<std::string_view>({
"Android",
"Linux",
"Nintendo Switch",
Expand All @@ -3203,7 +3213,7 @@ const auto platforms = std::to_array<std::string_view>({
"iOS",
});

const auto studioNames = std::to_array<std::string_view>({
const auto enUSStudioNames = std::to_array<std::string_view>({
"0verflow",
"1st Playable Productions",
"2K Czech",
Expand Down Expand Up @@ -3598,4 +3608,11 @@ const auto studioNames = std::to_array<std::string_view>({
"n-Space",
});

const VideoGames enUSVideoGames = {
.videoGameNames = enUSVideoGameNames,
.videoGameGenres = enUSVideoGameGenres,
.platforms = enUSPlatforms,
.studioNames = enUSStudioNames,
};

}
58 changes: 45 additions & 13 deletions tests/modules/video_game_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,71 @@ using namespace ::testing;
using namespace faker;
using namespace faker::videogame;

class VideoGameTest : public Test
namespace
{
const struct VideoGames& getVideoGame(Locale locale)
{
switch (locale)
{
default:
return enUSVideoGames;
}
}
}

class VideoGameTest : public TestWithParam<Locale>
{
public:
};

TEST_F(VideoGameTest, shouldGenerateGameTitle)
TEST_P(VideoGameTest, shouldGenerateGameTitle)
{
const auto generatedGameTitle = gameTitle();
const auto locale = GetParam();

const auto& videoGame = getVideoGame(locale);

ASSERT_TRUE(std::ranges::any_of(videoGameNames, [generatedGameTitle](const std::string_view& gameTitle)
const auto generatedGameTitle = gameTitle(locale);

ASSERT_TRUE(std::ranges::any_of(videoGame.videoGameNames, [generatedGameTitle](const std::string_view& gameTitle)
{ return generatedGameTitle == gameTitle; }));
}

TEST_F(VideoGameTest, shouldGenerateGenre)
TEST_P(VideoGameTest, shouldGenerateGenre)
{
const auto generatedGenre = genre();
const auto locale = GetParam();

const auto& videoGame = getVideoGame(locale);

ASSERT_TRUE(std::ranges::any_of(videoGameGenres, [generatedGenre](const std::string_view& genre)
const auto generatedGenre = genre(locale);

ASSERT_TRUE(std::ranges::any_of(videoGame.videoGameGenres, [generatedGenre](const std::string_view& genre)
{ return generatedGenre == genre; }));
}

TEST_F(VideoGameTest, shouldGeneratePlatform)
TEST_P(VideoGameTest, shouldGeneratePlatform)
{
const auto generatedPlatform = platform();
const auto locale = GetParam();

const auto& videoGame = getVideoGame(locale);

ASSERT_TRUE(std::ranges::any_of(platforms, [generatedPlatform](const std::string_view& platform)
const auto generatedPlatform = platform(locale);

ASSERT_TRUE(std::ranges::any_of(videoGame.platforms, [generatedPlatform](const std::string_view& platform)
{ return generatedPlatform == platform; }));
}

TEST_F(VideoGameTest, shouldGenerateStudioName)
TEST_P(VideoGameTest, shouldGenerateStudioName)
{
const auto generatedStudioName = studioName();
const auto locale = GetParam();

const auto& videoGame = getVideoGame(locale);

ASSERT_TRUE(std::ranges::any_of(studioNames, [generatedStudioName](const std::string_view& studioName)
const auto generatedStudioName = studioName(locale);

ASSERT_TRUE(std::ranges::any_of(videoGame.studioNames, [generatedStudioName](const std::string_view& studioName)
{ return generatedStudioName == studioName; }));
}


INSTANTIATE_TEST_SUITE_P(TestVideoGameByLocale, VideoGameTest, ValuesIn(locales),
[](const TestParamInfo<Locale>& paramInfo) { return toString(paramInfo.param); });

0 comments on commit 80f7796

Please sign in to comment.